001/** 002 * Copyright 2016 Tampere University of Technology, Pori Department 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package service.tut.pori.apilta.files; 017 018import java.io.InputStream; 019 020import org.apache.commons.lang3.StringUtils; 021import org.apache.log4j.Logger; 022 023import service.tut.pori.apilta.files.datatypes.FileDetails; 024import core.tut.pori.context.ServiceInitializer; 025 026/** 027 * Core methods for content handling 028 */ 029public final class FilesCore { 030 private static final Logger LOGGER = Logger.getLogger(FilesCore.class); 031 private static final TemporaryTokenHandler TOKEN_HANDLER = new TemporaryTokenHandler(); 032 033 /** 034 * 035 */ 036 private FilesCore(){ 037 // nothing needed 038 } 039 040 /** 041 * 042 * @param file 043 * @return details for the created file or null on failure 044 * @throws IllegalArgumentException on bad input data 045 */ 046 public static FileDetails createFile(InputStream file) throws IllegalArgumentException{ 047 if(file == null){ 048 throw new IllegalArgumentException("Invalid file."); 049 } 050 051 FileDAO fDao = ServiceInitializer.getDAOHandler().getDAO(FileDAO.class); 052 if(fDao == null){ 053 throw new RuntimeException("FileDAO was not initialized"); 054 } 055 String guid = fDao.addFile(file); 056 if(guid == null){ 057 LOGGER.debug("Failed to create new file."); 058 return null; 059 } 060 061 //TODO schedule background process for resolving mimetype & generating thumbnail (when applicable) ? 062 063 FileDetails details = new FileDetails(); 064 details.setGUID(guid); 065 return details; 066 } 067 068 /** 069 * This can be used to change the ownership (backend id) of the file, but not to change the url. If url is given, it is simply ignored. 070 * 071 * @param details 072 * @return true on success 073 * @throws IllegalArgumentException on invalid details 074 */ 075 public static boolean updateFile(FileDetails details) throws IllegalArgumentException { 076 if(!FileDetails.isValid(details)){ 077 throw new IllegalArgumentException("Invalid file details."); 078 } 079 080 return ServiceInitializer.getDAOHandler().getDAO(FileDAO.class).updateFile(details); 081 } 082 083 /** 084 * 085 * @param guid 086 * @return details for the file or null if not found 087 */ 088 public static FileDetails getFileDetails(String guid) { 089 return ServiceInitializer.getDAOHandler().getDAO(FileDAO.class).getFileDetails(guid); 090 } 091 092 /** 093 * 094 * @param guid 095 */ 096 public static void removeFile(String guid) { 097 ServiceInitializer.getDAOHandler().getDAO(FileDAO.class).removeFile(guid); 098 } 099 100 /** 101 * 102 * @param token 103 * @return details for the token or null if token is not valid 104 */ 105 public static FileDetails getFileDetailsForToken(String token) { 106 String guid = TOKEN_HANDLER.getGUID(token); 107 if(StringUtils.isBlank(guid)){ 108 LOGGER.warn("Attempted to access file details for invalid token."); 109 return null; 110 } 111 112 return getFileDetails(guid); 113 } 114 115 /** 116 * Note: this does NOT validate the GUID, it is assumed to exist (using the generated url to access non-existent resource will result in an error) 117 * 118 * @param guid 119 * @return currently valid temporary url for the given guid/user pair 120 */ 121 public static String generateTemporaryUrl(String guid) { 122 return ServiceInitializer.getPropertyHandler().getRESTBindContext()+Definitions.SERVICE_FILES+core.tut.pori.http.Definitions.SEPARATOR_URI_PATH+Definitions.METHOD_GET_FILE_DETAILS+core.tut.pori.http.Definitions.SEPARATOR_URI_METHOD_PARAMS+Definitions.PARAMETER_TEMPORARY_TOKEN+core.tut.pori.http.Definitions.SEPARATOR_URI_QUERY_PARAM_VALUE+generateTemporaryToken(guid); 123 } 124 125 /** 126 * Note: this does NOT validate the GUID, it is assumed to exist 127 * 128 * @param guid 129 * @return currently valid temporary access token for the given guid/user pair 130 */ 131 protected static String generateTemporaryToken(String guid) { 132 return TOKEN_HANDLER.getToken(guid); 133 } 134}