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.twitterjazz.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 core.tut.pori.http.annotations.HTTPMethodParameter;
026import core.tut.pori.http.annotations.HTTPService;
027import core.tut.pori.http.annotations.HTTPServiceMethod;
028import core.tut.pori.http.parameters.InputStreamParameter;
029import core.tut.pori.utils.XMLFormatter;
030
031/**
032 * 
033 * Reference implementation for Back-end APIs. This class defines the APIs implemented by analysis back-end services.
034 * As a general note, the back-end should ignore any unknown elements it cannot process.
035 * 
036 */
037@HTTPService(name = service.tut.pori.twitterjazz.reference.Definitions.SERVICE_TJ_REFERENCE_BACKEND)
038public class BackendService {
039  private static final Logger LOGGER = Logger.getLogger(BackendService.class);
040  private XMLFormatter _xmlFormatter = new XMLFormatter();
041  
042  /**
043   * 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".
044   * 
045   * <h2>Example Query:</h2>
046   *
047   * POST /rest/{@value service.tut.pori.twitterjazz.reference.Definitions#SERVICE_TJ_REFERENCE_BACKEND}/{@value service.tut.pori.contentanalysis.Definitions#METHOD_ADD_TASK}<br>
048   * Content-Type: text/xml; charset=UTF-8<br><br>
049   *
050   * <b>[HTTP BODY STARTS]</b><br>
051   * 
052   * {@doc.restlet service="[service.tut.pori.twitterjazz.reference.Definitions#SERVICE_TJ_REFERENCE_EXAMPLE]" method="[service.tut.pori.contentanalysis.Definitions#ELEMENT_TASK_DETAILS]" type="GET" query="" body_uri=""} <br>
053   * 
054   * <b>[HTTP BODY ENDS]</b><br>
055   *
056   * <h2>Example Result:</h2>
057   * 
058   * {@doc.restlet service="[service.tut.pori.twitterjazz.reference.Definitions#SERVICE_TJ_REFERENCE_BACKEND]" method="[service.tut.pori.contentanalysis.Definitions#METHOD_ADD_TASK]" type="POST" query="" body_uri="[service.tut.pori.twitterjazz.reference.Definitions#SERVICE_TJ_REFERENCE_EXAMPLE]/[service.tut.pori.contentanalysis.Definitions#ELEMENT_TASK_DETAILS]"}
059   * 
060   * @param xml The HTTP body. Only the workload data should be in the body. See {@link service.tut.pori.twitterjazz.reference.TwitterTaskDetails}
061   */
062  @HTTPServiceMethod(name = Definitions.METHOD_ADD_TASK, acceptedMethods={core.tut.pori.http.Definitions.METHOD_POST})
063  public void addTask(@HTTPMethodParameter(name = InputStreamParameter.PARAMETER_DEFAULT_NAME, bodyParameter = true) InputStreamParameter xml) {
064    try {
065      String body = IOUtils.toString(xml.getValue()); // read the body
066      LOGGER.debug(body); // print to debug
067      try(InputStream input = IOUtils.toInputStream(body)){ // convert back to stream for unmarshal
068        TJReferenceCore.addTask(_xmlFormatter.toObject(input, TwitterTaskDetails.class));
069      }
070    } catch (IOException ex) {
071      LOGGER.error(ex, ex);
072    }
073  }
074}