001/** 002 * Copyright 2015 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.fileservice.reference; 017 018import java.io.IOException; 019import java.io.InputStream; 020 021import org.apache.commons.io.IOUtils; 022import org.apache.commons.lang3.StringUtils; 023import org.apache.log4j.Logger; 024 025import service.tut.pori.fileservice.Definitions; 026import service.tut.pori.fileservice.File; 027import service.tut.pori.fileservice.FileList; 028import service.tut.pori.fileservice.FileProperties; 029import core.tut.pori.context.ServiceInitializer; 030import core.tut.pori.http.parameters.Limits; 031import core.tut.pori.users.UserIdentity; 032 033/** 034 * The reference implementations for File Service. 035 * 036 */ 037public final class FileReferenceCore { 038 private static final int BUFFER_SIZE = 8192; 039 private static final FSXMLObjectCreator CREATOR = new FSXMLObjectCreator(null); 040 private static final Logger LOGGER = Logger.getLogger(FileReferenceCore.class); 041 private static final int MAX_FILES = 100; 042 043 /** 044 * 045 */ 046 private FileReferenceCore(){ 047 // nothing needed 048 } 049 050 /** 051 * 052 * @param authenticatedUser 053 * @param fileId 054 */ 055 public static void deleteFile(UserIdentity authenticatedUser, Long fileId) { 056 LOGGER.info((authenticatedUser == null ? "No logged in user." : "Ignoring the logged in user, id: "+authenticatedUser.getUserId())); // only notify of the logged in status 057 } 058 059 /** 060 * Simulates uploading a file. The actual will is discarded and the returned data is for reference only. 061 * 062 * @param authenticatedUser 063 * @param file 064 * @param filename 065 * @return file object representing the added file 066 * @throws IllegalArgumentException on failure 067 */ 068 public static File addFile(UserIdentity authenticatedUser, InputStream file, String filename) throws IllegalArgumentException { 069 if(UserIdentity.isValid(authenticatedUser)){ 070 LOGGER.info("Logged in user, id: "+authenticatedUser.getUserId()); 071 }else{ 072 LOGGER.info("No logged in user."); 073 authenticatedUser = CREATOR.createUserIdentity(); 074 } 075 int byteCount = 0; 076 byte[] bytes = new byte[BUFFER_SIZE]; 077 int read = 0; 078 try { 079 while((read = IOUtils.read(file, bytes)) > 0){ // read fully, but discard all 080 byteCount += read; 081 } 082 } catch (IOException ex) { 083 LOGGER.error(ex, ex); 084 throw new IllegalArgumentException("I/O error occurred."); 085 } 086 File savedFile = new File(); 087 if(StringUtils.isBlank(filename)){ 088 LOGGER.debug("No filename given, bytes read: "+byteCount); 089 }else{ 090 LOGGER.debug("Filename: "+filename+", bytes read: "+byteCount); 091 savedFile.setName(filename); 092 } 093 String savedName = CREATOR.generateFilename(filename); 094 savedFile.setSavedName(savedName); 095 savedFile.setUserId(authenticatedUser); 096 savedFile.setFileId(CREATOR.generateFileId()); 097 savedFile.setUrl(ServiceInitializer.getPropertyHandler().getSystemProperties(FileProperties.class).getUriPath()+savedName); 098 return savedFile; 099 } 100 101 /** 102 * 103 * @param authenticatedUser 104 * @param fileIds 105 * @param limits 106 * @return example file list 107 */ 108 public static FileList listFiles(UserIdentity authenticatedUser, long[] fileIds, Limits limits) { 109 if(UserIdentity.isValid(authenticatedUser)){ 110 LOGGER.info("Logged in user, id: "+authenticatedUser.getUserId()); 111 }else{ 112 LOGGER.info("No logged in user."); 113 authenticatedUser = CREATOR.createUserIdentity(); 114 } 115 116 int maxFiles = limits.getMaxItems(Definitions.ELEMENT_FILELIST); 117 if(maxFiles >= Limits.DEFAULT_MAX_ITEMS){ 118 LOGGER.info("Maximum limit given, capping to "+MAX_FILES); 119 maxFiles = MAX_FILES; 120 } 121 122 return CREATOR.generateFileList(authenticatedUser, maxFiles, fileIds); 123 } 124 125 /** 126 * 127 * @param limits 128 * @return generated file list 129 */ 130 public static FileList generateFileList(Limits limits) { 131 int count = limits.getMaxItems(Definitions.ELEMENT_FILELIST); 132 if(count >= Limits.DEFAULT_MAX_ITEMS){ 133 LOGGER.debug("Max files was >= "+Limits.DEFAULT_MAX_ITEMS+". Limiting to "+MAX_FILES+"."); 134 count = MAX_FILES; 135 } 136 return CREATOR.generateFileList(null, count, null); 137 } 138 139 /** 140 * 141 * @return generated file 142 */ 143 public static File generateFile() { 144 return CREATOR.generateFile(); 145 } 146}