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.facebookjazz.reference;
017
018import java.io.IOException;
019import java.io.InputStream;
020
021import org.apache.commons.io.IOUtils;
022import org.apache.log4j.Logger;
023
024import service.tut.pori.contentanalysis.Definitions;
025import service.tut.pori.contentanalysis.PhotoTaskResponse;
026import service.tut.pori.facebookjazz.FBTaskResponse;
027import core.tut.pori.http.annotations.HTTPMethodParameter;
028import core.tut.pori.http.annotations.HTTPService;
029import core.tut.pori.http.annotations.HTTPServiceMethod;
030import core.tut.pori.http.parameters.InputStreamParameter;
031import core.tut.pori.utils.XMLFormatter;
032
033/**
034 * 
035 * Reference implementation for Server APIs.
036 * 
037 * This class defines the APIs offered by the Front-end to Analysis Back-ends.
038 * 
039 * <h1>Implementation Service path {@value service.tut.pori.facebookjazz.Definitions#SERVICE_FBJ}</h1>
040 * 
041 * @see service.tut.pori.facebookjazz.FacebookJazzService
042 *
043 */
044@HTTPService(name = service.tut.pori.facebookjazz.reference.Definitions.SERVICE_FBJ_REFERENCE_SERVER)
045public class ServerService {
046  private static final Logger LOGGER = Logger.getLogger(ServerService.class);
047  private XMLFormatter _formatter = new XMLFormatter();
048  
049  /**
050   * The request is to be sent in the body of POST method. The Content-Type header MUST be set to "text/xml". The character set MUST be UTF-8. For example, "Content-Type: text/xml; charset=UTF-8".
051   * 
052   * The method can be called multiple times by the back-ends. Each call is assumed to be an incremental update on the previous one and can contain any amount of new information. 
053   * It is also possible to update previously submitted data by using the same identifiers: photo GUID (for photos), and media object id or a combination of back-end id and object id (for media objects).
054   * 
055   * Task results may contain photo specific back-end status information, though it is not required.
056   * 
057   * Using an invalid back-end id (both for the task and the generated media objects) can result in an error, causing the entire task result to be rejected. It is not allowed for a one back-end to update/modify media objects previously submitted by another back-end, or by the user.
058   * 
059   * <h2>Example Query:</h2>
060   *
061   * POST /rest/{@value service.tut.pori.facebookjazz.reference.Definitions#SERVICE_FBJ_REFERENCE_SERVER}/{@value service.tut.pori.contentanalysis.Definitions#METHOD_TASK_FINISHED}<br>
062   * Content-Type: text/xml; charset=UTF-8<br><br>
063   *
064   * <b>[HTTP BODY STARTS]</b><br>
065   * 
066   * {@doc.restlet service="[service.tut.pori.facebookjazz.reference.Definitions#SERVICE_FBJ_REFERENCE_EXAMPLE]" method="[service.tut.pori.contentanalysis.Definitions#ELEMENT_TASK_RESULTS]" type="GET" query="" body_uri=""} <br>
067   * 
068   * <b>[HTTP BODY ENDS]</b><br>
069   *
070   * <h2>Example Result:</h2>
071   * 
072   * {@doc.restlet service="[service.tut.pori.facebookjazz.reference.Definitions#SERVICE_FBJ_REFERENCE_SERVER]" method="[service.tut.pori.contentanalysis.Definitions#METHOD_TASK_FINISHED]" type="POST" query="" body_uri="[service.tut.pori.facebookjazz.reference.Definitions#SERVICE_FBJ_REFERENCE_EXAMPLE]/[service.tut.pori.contentanalysis.Definitions#ELEMENT_TASK_RESULTS]"}
073   * 
074   * @param xml Only the result data should be in the body. See {@link service.tut.pori.facebookjazz.FBTaskResponse}
075   */
076  @HTTPServiceMethod(name = Definitions.METHOD_TASK_FINISHED, acceptedMethods={core.tut.pori.http.Definitions.METHOD_POST})
077  public void taskFinished(@HTTPMethodParameter(name = InputStreamParameter.PARAMETER_DEFAULT_NAME, bodyParameter = true) InputStreamParameter xml) {
078    try {
079      String body = IOUtils.toString(xml.getValue()); // read the body
080      LOGGER.debug(body); // print to debug
081      try(InputStream input = IOUtils.toInputStream(body)){ // convert back to stream for unmarshal
082        FBJReferenceCore.taskFinished(_formatter.toObject(input, FBTaskResponse.class, PhotoTaskResponse.class));
083      }
084    } catch (IOException ex) {
085      LOGGER.error(ex, ex);
086    }
087  }
088}