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.contentstorage.reference;
017
018import service.tut.pori.contentanalysis.CAContentCore.ServiceType;
019import service.tut.pori.contentstorage.Definitions;
020import service.tut.pori.contentstorage.MediaList;
021import core.tut.pori.http.Response;
022import core.tut.pori.http.Response.Status;
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.IntegerParameter;
029import core.tut.pori.http.parameters.StringParameter;
030
031/**
032 * Reference implementation for client API methods.
033 * 
034 * <h1>Implementation Service path {@value service.tut.pori.contentstorage.Definitions#SERVICE_CS}</h1>
035 * 
036 * @see service.tut.pori.contentstorage.ContentStorageService
037 * 
038 */
039@HTTPService(name = service.tut.pori.contentstorage.reference.Definitions.SERVICE_COS_REFERENCE_CLIENT)
040public class ClientService {
041  /**
042   * This method allows the user to initialize external account synchronization. The synchronization process is always performed as a scheduled task, and thus, may not complete immediately. For new content discovered during the synchronization process, a new analysis task will be created, and the task will be automatically added for the appropriate analysis back-ends.
043   * 
044   * @param authenticatedUser Note: this method requires authentication, but for the reference implementation, anonymous access is granted.
045   * @param backendId Back-ends used for the analysis of the generated tasks. If no back-end IDs are given, the default back-ends will be used. An attempt to give incompatible back-ends may result in failure of the entire task. When providing specific back-ends by Id one should make sure that the back-ends are capable of performing the requested tasks.
046   * @param serviceId Any combination of supported external providers designated by serviceIds. The supported id values are: 1 (Picasa), 2 (FSIO), 4 (Facebook), 6 (Twitter), 7 (Url Storage). See {@link service.tut.pori.contentanalysis.CAContentCore.ServiceType}
047   */
048  @HTTPServiceMethod(name=Definitions.METHOD_SYNCHRONIZE)
049  public void synchronize(
050      @HTTPAuthenticationParameter(required=false) AuthenticationParameter authenticatedUser,
051      @HTTPMethodParameter(name=service.tut.pori.contentanalysis.Definitions.PARAMETER_BACKEND_ID, required = false) IntegerParameter backendId,
052      @HTTPMethodParameter(name=service.tut.pori.contentanalysis.Definitions.PARAMETER_SERVICE_ID) IntegerParameter serviceId
053      )
054  {
055    ContentStorageReferenceCore.synchronize(authenticatedUser.getUserIdentity(), backendId.getValues(), ServiceType.fromIdArray(serviceId.getValues()));
056  }
057  
058  /**
059   * This method can be used to add files denoted by arbitrary URL links to the service. Photo and video analysis tasks will be scheduled for the added URLs and submitted for back-ends.
060   * 
061   * The files must be publicly accessible and of one of file types supported by the validator {@link core.tut.pori.utils.MediaUrlValidator}.
062   * 
063   * @param authenticatedUser Note: this method requires authentication, but for the reference implementation, anonymous access is granted.
064   * @param backendId Back-ends used for the analysis of the generated tasks. If no back-end IDs are given, the default back-ends will be used. An attempt to give incompatible back-ends may result in failure of the entire task. When providing specific back-ends by Id one should make sure that the back-ends are capable of performing the requested tasks.
065   * @param url Any number of URL links.
066   * @return See {@link service.tut.pori.contentstorage.MediaList}
067   */
068  @HTTPServiceMethod(name=Definitions.METHOD_ADD_URL, acceptedMethods={core.tut.pori.http.Definitions.METHOD_POST})
069  public Response addUrl(
070      @HTTPAuthenticationParameter(required=false) AuthenticationParameter authenticatedUser,
071      @HTTPMethodParameter(name=service.tut.pori.contentanalysis.Definitions.PARAMETER_BACKEND_ID, required = false) IntegerParameter backendId,
072      @HTTPMethodParameter(name=service.tut.pori.contentanalysis.Definitions.PARAMETER_URL) StringParameter url
073      )
074  {
075    MediaList media = ContentStorageReferenceCore.addUrls(authenticatedUser.getUserIdentity(), backendId.getValues(), url.getValues());
076    if(MediaList.isEmpty(media)){
077      return new Response(Status.BAD_REQUEST);
078    }else{
079      return new Response(media);
080    }
081  }
082}