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