001/**
002 * Copyright 2014 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;
017
018import java.util.Arrays;
019
020import core.tut.pori.http.Response;
021import core.tut.pori.http.Response.Status;
022import core.tut.pori.http.annotations.HTTPAuthenticationParameter;
023import core.tut.pori.http.annotations.HTTPMethodParameter;
024import core.tut.pori.http.annotations.HTTPService;
025import core.tut.pori.http.annotations.HTTPServiceMethod;
026import core.tut.pori.http.parameters.AuthenticationParameter;
027import core.tut.pori.http.parameters.InputStreamParameter;
028import core.tut.pori.http.parameters.Limits;
029import core.tut.pori.http.parameters.LongParameter;
030import core.tut.pori.http.parameters.StringParameter;
031
032/**
033 * Service declarations for File Service.
034 * 
035 * @see service.tut.pori.fileservice.reference.ClientService
036 */
037@HTTPService(name=Definitions.SERVICE_FS)
038public class FileService {
039
040  /**
041   * @see service.tut.pori.fileservice.reference.ClientService#addFile(AuthenticationParameter, InputStreamParameter, StringParameter)
042   * 
043   * @param authenticatedUser
044   * @param file
045   * @param filename
046   * @return response
047   */
048  @HTTPServiceMethod(name=Definitions.METHOD_ADD_FILE, acceptedMethods={core.tut.pori.http.Definitions.METHOD_POST})
049  public Response addFile(
050      @HTTPAuthenticationParameter AuthenticationParameter authenticatedUser,
051      @HTTPMethodParameter(name=InputStreamParameter.PARAMETER_DEFAULT_NAME, bodyParameter=true) InputStreamParameter file,
052      @HTTPMethodParameter(name=Definitions.PARAMETER_FILE_NAME, required = false) StringParameter filename
053      )
054  {
055    File result = FileCore.addFile(authenticatedUser.getUserIdentity(), file.getValue(), filename.getValue());
056    if(result == null){
057      return new Response(Status.BAD_REQUEST);
058    }else{
059      return new Response(FileList.getFileList(Arrays.asList(result)));
060    }
061  }
062  
063  /**
064   * @see service.tut.pori.fileservice.reference.ClientService#deleteFile(AuthenticationParameter, LongParameter)
065   * 
066   * @param authenticatedUser
067   * @param fileId
068   */
069  @HTTPServiceMethod(name=Definitions.METHOD_DELETE_FILE, acceptedMethods=core.tut.pori.http.Definitions.METHOD_DELETE)
070  public void deleteFile(
071      @HTTPAuthenticationParameter AuthenticationParameter authenticatedUser,
072      @HTTPMethodParameter(name=Definitions.PARAMETER_FILE_ID) LongParameter fileId
073      )
074  {
075    FileCore.deleteFiles(authenticatedUser.getUserIdentity(), fileId.getValues());
076  }
077  
078  /**
079   * @see service.tut.pori.fileservice.reference.ClientService#listFiles(AuthenticationParameter, LongParameter, Limits)
080   * 
081   * @param authenticatedUser
082   * @param fileId
083   * @param limits 
084   * @return response
085   */
086  @HTTPServiceMethod(name=Definitions.METHOD_LIST_FILES, acceptedMethods={core.tut.pori.http.Definitions.METHOD_GET})
087  public Response listFiles(
088      @HTTPAuthenticationParameter AuthenticationParameter authenticatedUser,
089      @HTTPMethodParameter(name=Definitions.PARAMETER_FILE_ID, required=false) LongParameter fileId,
090      @HTTPMethodParameter(name=Limits.PARAMETER_DEFAULT_NAME, required=false) Limits limits
091      )
092  {
093    return new Response(FileCore.listFiles(authenticatedUser.getUserIdentity(), fileId.getValues(), limits));
094  }
095}