All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.xins.server.StandardCallingConvention Maven / Gradle / Ivy

There is a newer version: 3.0
Show newest version
/*
 * $Id: StandardCallingConvention.java,v 1.72 2011/02/19 08:48:28 agoubard Exp $
 *
 * See the COPYRIGHT file for redistribution and use restrictions.
 */
package org.xins.server;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.xins.common.text.TextUtils;
import org.xins.common.xml.ElementFormatter;

/**
 * Standard calling convention. The technical name for this calling convention
 * is _xins-std.
 *
 * @version $Revision: 1.72 $ $Date: 2011/02/19 08:48:28 $
 * @author Anthony Goubard
 * @author Ernst de Haan
 */
public class StandardCallingConvention extends CallingConvention {

   /**
    * The response encoding format.
    */
   protected static final String RESPONSE_ENCODING = "UTF-8";

   /**
    * The content type of the HTTP response.
    */
   protected static final String RESPONSE_CONTENT_TYPE = "text/xml; charset=" + RESPONSE_ENCODING;

   /**
    * Checks if the specified request can be handled by this calling
    * convention.
    *
    * 

This method will not throw any exception. * * @param httpRequest * the HTTP request to investigate, cannot be null. * * @return * true if this calling convention is possibly * able to handle this request, or false if it * definitely not able to handle this request. */ protected boolean matches(HttpServletRequest httpRequest) { // If no _function parameter is specified, then there is no match return ! TextUtils.isEmpty(httpRequest.getParameter("_function")); } /** * Converts an HTTP request to a XINS request (implementation method). This * method should only be called from class CallingConvention. Only * then it is guaranteed that the httpRequest argument is not * null. * * @param httpRequest * the HTTP request, will not be null. * * @return * the XINS request object, never null. * * @throws InvalidRequestException * if the request is considerd to be invalid. * * @throws FunctionNotSpecifiedException * if the request does not indicate the name of the function to execute. */ protected FunctionRequest convertRequestImpl(HttpServletRequest httpRequest) throws InvalidRequestException, FunctionNotSpecifiedException { // Parse the parameters in the HTTP request Map params = gatherParams(httpRequest); // Remove all invalid parameters cleanUpParameters(params); // Determine function name String functionName = httpRequest.getParameter("_function"); if (TextUtils.isEmpty(functionName)) { throw new FunctionNotSpecifiedException(); } // Get data section String dataSectionValue = httpRequest.getParameter("_data"); Element dataElement = null; if (dataSectionValue != null && dataSectionValue.length() > 0) { // Parse the data section try { dataElement = ElementFormatter.parse(dataSectionValue); // Parsing error } catch (SAXException exception) { String detail = "Cannot parse the data section."; throw new InvalidRequestException(detail, exception); } } // Define the HTTP method in the backpack Map backpack = new HashMap(); backpack.put("_httpMethod", httpRequest.getMethod()); // Construct and return the request object return new FunctionRequest(functionName, params, dataElement, backpack); } /** * Converts a XINS result to an HTTP response (implementation method). * * @param xinsResult * the XINS result object that should be converted to an HTTP response, * will not be null. * * @param httpResponse * the HTTP response object to configure, will not be null. * * @param httpRequest * the HTTP request, will not be null. * * @throws IOException * if calling any of the methods in httpResponse causes an * I/O error. */ protected void convertResultImpl(FunctionResult xinsResult, HttpServletResponse httpResponse, Map backpack) throws IOException { // Set the status code and the content type httpResponse.setStatus(HttpServletResponse.SC_OK); httpResponse.setContentType(RESPONSE_CONTENT_TYPE); // Determine the method String method = (String) backpack.get("_httpMethod"); // Handle HEAD requests if ("HEAD".equals(method)) { StringWriter out = new StringWriter(); CallResultOutputter.output(out, xinsResult); httpResponse.setContentLength(out.getBuffer().length()); // Handle non-HEAD requests } else { Writer out = httpResponse.getWriter(); CallResultOutputter.output(out, xinsResult); out.close(); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy