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.util.Arrays;
019
020import service.tut.pori.fileservice.Definitions;
021import service.tut.pori.fileservice.FileList;
022import core.tut.pori.http.Response;
023import core.tut.pori.http.annotations.HTTPAuthenticationParameter;
024import core.tut.pori.http.annotations.HTTPMethodParameter;
025import core.tut.pori.http.annotations.HTTPService;
026import core.tut.pori.http.annotations.HTTPServiceMethod;
027import core.tut.pori.http.parameters.AuthenticationParameter;
028import core.tut.pori.http.parameters.InputStreamParameter;
029import core.tut.pori.http.parameters.Limits;
030import core.tut.pori.http.parameters.LongParameter;
031import core.tut.pori.http.parameters.StringParameter;
032
033/**
034 * Reference implementation for client API methods.
035 * 
036 * This class defines the APIs available for file storage. Note that the file storage API is a "light-weight" service provided to enable analysis of arbitrary files not located on external providers. For a proper, backed up, cloud storage one should take a look at the one of the external providers supported by the front-end service (e.g. Facebook, Twitter, Picasa, FSIO), and use the synchronization methods for importing the content to the front-end service.
037 * 
038 * <h1>Implementation Service path {@value service.tut.pori.fileservice.Definitions#SERVICE_FS}</h1>
039 * 
040 * @see service.tut.pori.fileservice.FileService
041 */
042@HTTPService(name = service.tut.pori.fileservice.reference.Definitions.SERVICE_FS_REFERENCE_CLIENT)
043public class ClientService {
044  /**
045   * This method allows an authenticated user to upload a file directly using HTTP Post. Note that this method does not implement HTTP PUT, i.e. the file will not be available in a user defined URI regardless of the given filename. The uploaded file can only be accessed using the returned URL. The URL is generally a static link, and you can use the List Files method to retrieve up-to-date URLs.
046   * The File Service stores the files in binary format, and thus enforces no limits on the file type. The maximum file size depends on the features of the system to where the front-end service has been deployed - the File Service itself imposes no limits on file sizes. In general files larger than 2 GB should be avoided.
047   * Note that simply uploading a file will <i>not</i> start the analysis process. One must pass the received URL link to the Content Storage service to initialize the file analysis.
048   * 
049   * @param authenticatedUser Note: this method requires authentication, but for the reference implementation, anonymous access is granted.
050   * @param file file contents
051   * @param filename Optional filename for the uploaded data. The filename is processed simply as metadata and omitting the parameter has no effect on the file upload. 
052   * @return See {@link service.tut.pori.fileservice.FileList}
053   */
054  @HTTPServiceMethod(name=Definitions.METHOD_ADD_FILE, acceptedMethods={core.tut.pori.http.Definitions.METHOD_POST})
055  public Response addFile(
056      @HTTPAuthenticationParameter(required=false) AuthenticationParameter authenticatedUser,
057      @HTTPMethodParameter(name=InputStreamParameter.PARAMETER_DEFAULT_NAME, bodyParameter=true) InputStreamParameter file,
058      @HTTPMethodParameter(name=Definitions.PARAMETER_FILE_NAME, required = false) StringParameter filename
059      )
060  {
061    return new Response(FileList.getFileList(Arrays.asList(FileReferenceCore.addFile(authenticatedUser.getUserIdentity(), file.getValue(), filename.getValue()))));
062  }
063  
064  /**
065   * This method can be used to delete one or more files. Non-existent file IDs will be ignored.
066   * 
067   * @param authenticatedUser Note: this method requires authentication, but for the reference implementation, anonymous access is granted.
068   * @param fileId
069   */
070  @HTTPServiceMethod(name=Definitions.METHOD_DELETE_FILE, acceptedMethods=core.tut.pori.http.Definitions.METHOD_DELETE)
071  public void deleteFile(
072      @HTTPAuthenticationParameter(required=false) AuthenticationParameter authenticatedUser,
073      @HTTPMethodParameter(name=Definitions.PARAMETER_FILE_ID) LongParameter fileId
074      )
075  {
076    FileReferenceCore.deleteFile(authenticatedUser.getUserIdentity(), fileId.getValue());
077  }
078  
079  /**
080   * List all or a subset of the files user has uploaded to the system.
081   * 
082   * @param authenticatedUser Note: this method requires authentication, but for the reference implementation, anonymous access is granted.
083   * @param fileId Optional file ID filter for retrieving a subset of the uploaded files. If ID is not given, all files will be retrieved respecting the optional limits parameter.
084   * @param limits paging limits
085   * @return See {@link service.tut.pori.fileservice.FileList}
086   */
087  @HTTPServiceMethod(name=Definitions.METHOD_LIST_FILES, acceptedMethods={core.tut.pori.http.Definitions.METHOD_GET})
088  public Response listFiles(
089      @HTTPAuthenticationParameter(required=false) AuthenticationParameter authenticatedUser,
090      @HTTPMethodParameter(name=Definitions.PARAMETER_FILE_ID, required=false) LongParameter fileId,
091      @HTTPMethodParameter(name=Limits.PARAMETER_DEFAULT_NAME, required=false) Limits limits
092      )
093  {
094    return new Response(FileReferenceCore.listFiles(authenticatedUser.getUserIdentity(), fileId.getValues(), limits));
095  }
096}