org.ow2.petals.binding.rest.config.RESTProvidesConfiguration Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of petals-bc-rest Show documentation
Show all versions of petals-bc-rest Show documentation
petals-bc-rest Binding Component description
/**
* Copyright (c) 2007-2012 EBM WebSourcing, 2012-2016 Linagora
*
* This program/library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 of the License, or (at your
* option) any later version.
*
* This program/library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program/library; If not, see http://www.gnu.org/licenses/
* for the GNU Lesser General Public License version 2.1.
*/
package org.ow2.petals.binding.rest.config;
import static org.ow2.petals.binding.rest.RESTConstants.ProvidesParameter.HTTP_BODY_TYPE;
import static org.ow2.petals.binding.rest.RESTConstants.ProvidesParameter.HTTP_METHOD;
import static org.ow2.petals.binding.rest.RESTConstants.ProvidesParameter.JSON_XML_MAPPING_CONVENTION;
import static org.ow2.petals.binding.rest.RESTConstants.ProvidesParameter.OPERATION_NAME;
import static org.ow2.petals.binding.rest.RESTConstants.ProvidesParameter.POST_QUERY;
import static org.ow2.petals.binding.rest.RESTConstants.ProvidesParameter.URI;
import static org.ow2.petals.binding.rest.RESTConstants.ProvidesParameter.WSDL;
import static org.ow2.petals.binding.rest.RESTConstants.ProvidesParameter.XPATH_PARAM;
import static org.ow2.petals.binding.rest.RESTConstants.ProvidesParameter.XPATH_PARAM_NAME;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
import javax.xml.namespace.QName;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.ow2.easywsdl.extensions.wsdl4complexwsdl.WSDL4ComplexWsdlFactory;
import org.ow2.easywsdl.wsdl.api.Description;
import org.ow2.easywsdl.wsdl.api.WSDLException;
import org.ow2.easywsdl.wsdl.api.abstractItf.AbsItfDescription.WSDLVersionConstants;
import org.ow2.petals.binding.rest.exchange.outgoing.JSONRequest;
import org.ow2.petals.binding.rest.exchange.outgoing.NoBodyRequest;
import org.ow2.petals.binding.rest.exchange.outgoing.PostQueryRequest;
import org.ow2.petals.binding.rest.exchange.outgoing.RESTRequest;
import org.ow2.petals.binding.rest.exchange.outgoing.XMLRequest;
import org.ow2.petals.component.framework.api.exception.PEtALSCDKException;
import org.ow2.petals.component.framework.jbidescriptor.generated.Provides;
import org.ow2.petals.component.framework.su.ServiceUnitDataHandler;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.sun.jersey.api.uri.UriTemplate;
public class RESTProvidesConfiguration extends RESTSUConfiguration {
private Map restRequests;
private Provides provides;
public RESTProvidesConfiguration(Logger logger, ServiceUnitDataHandler suDataHandler,
Provides provides) throws PEtALSCDKException {
super(logger, suDataHandler.getConfigurationExtensions(provides));
this.restRequests = getRESTMessages(suDataHandler, provides);
this.provides = provides;
}
public Provides getProvides() {
return this.provides;
}
public RESTRequest getRESTRequest(QName exchangeOperation) {
return this.restRequests.get(exchangeOperation);
}
private Map getRESTMessages(ServiceUnitDataHandler suDataHandler,
Provides provides) throws PEtALSCDKException {
Map operations = null;
try {
Description wsdl = getWSDLDescription(suDataHandler, provides);
if (wsdl != null) {
WSDLVersionConstants wsdlVersion = wsdl.getVersion();
if (wsdlVersion == WSDLVersionConstants.WSDL20) {
// operations = processWSDL20(wsdl);
} else {
// it is a WSDL 1.1, use the SU parameters
operations = processSUProvidesMapping(this.logger, suDataHandler, provides);
}
} else {
handleMissingMandatoryParameterError(WSDL);
}
} catch (WSDLException wsdle) {
throw new PEtALSCDKException(wsdle);
}
return operations;
}
private Description getWSDLDescription(ServiceUnitDataHandler suDataHandler,
Provides provides) throws WSDLException {
Document wsdl = suDataHandler.getEndpointDescription(provides);
try {
return WSDL4ComplexWsdlFactory.newInstance().newWSDLReader().read(wsdl);
} catch (final URISyntaxException e) {
throw new WSDLException(e);
}
}
// private Map processWSDL20(Description wsdl)
// throws PEtALSCDKException {
// Map operations = new HashMap();
//
// List services = wsdl.getServices();
// for (Service service : services) {
//
// List endpoints = service.getEndpoints();
// for (Endpoint endpoint : endpoints) {
//
// String address = endpoint.getAddress();
//
// Binding binding = endpoint.getBinding();
// BindingConstants bindingType = binding.getTypeOfBinding();
// if (bindingType == BindingConstants.HTTP_BINDING4WSDL20) {
// List bindingOperations =
// binding.getBindingOperations();
// for (BindingOperation bindingOperation : bindingOperations) {
// Operation operation = bindingOperation.getOperation();
// QName operationName = operation.getQName();
// String httpLocation = bindingOperation.getHttpLocation();
// String httpMethod = bindingOperation.getHttpMethod();
//
// Templates uriXSL = createURIXSL(address, httpLocation);
//
// RESTRequest restRequest = new RESTRequest(uriXSL, httpMethod);
//
// Templates httpBodyXSL = createBodyXSL();
// if(httpBodyXSL != null) {
// restRequest.setHTTPBodyXSL(httpBodyXSL);
// }
//
// operations.put(operationName, restRequest);
// }
// }
// }
// }
//
// return operations;
// }
//
// private Templates createURIXSL(String address, String httpLocation)
// throws PEtALSCDKException {
// // TODO Support of WSDL 2.0
// throw new NotImplementedException();
// }
//
// private Templates createBodyXSL() {
// // TODO Support of WSDL 2.0
// throw new NotImplementedException();
// }
private static final Map processSUProvidesMapping(Logger logger,
ServiceUnitDataHandler suDataHandler, Provides provides) throws PEtALSCDKException {
Map operations = new HashMap();
NodeList operationNodes = getSUOperationMappingNodes(provides.getAny());
int operationNb = operationNodes.getLength();
for (int i = 0; i < operationNb; i++) {
Node operationNode = operationNodes.item(i);
Element operationElt = (Element) operationNode;
QName jbiOperation = getAttributeAsQName(operationElt, OPERATION_NAME);
String httpMethod = getSubElementTextContent(operationElt, HTTP_METHOD, true);
String uriStr = getSubElementTextContent(operationElt, URI, true);
UriTemplate uri = new UriTemplate(uriStr);
String httpBodyTypeStr = getSubElementTextContent(operationElt, HTTP_BODY_TYPE, true);
HTTPBodyType httpBodyType;
try {
httpBodyType = HTTPBodyType.valueOf(HTTPBodyType.class, httpBodyTypeStr);
} catch (final IllegalArgumentException e) {
throw new PEtALSCDKException(httpBodyTypeStr + " is not a valid HTTP Body type.");
}
try {
Map xPathParamExprs = getXPathParams(operationElt);
String jsonXMLMappingConventionStr = getSubElementTextContent(operationElt,
JSON_XML_MAPPING_CONVENTION, false);
JSONXMLMappingConvention jsonXMLMappingConvention;
if (jsonXMLMappingConventionStr != null) {
jsonXMLMappingConvention = JSONXMLMappingConvention.valueOf(
JSONXMLMappingConvention.class, jsonXMLMappingConventionStr);
} else {
jsonXMLMappingConvention = JSONXMLMappingConvention.MAPPED_CONVENTION;
}
RESTRequest restRequest;
switch (httpBodyType) {
case NO_BODY:
restRequest = new NoBodyRequest(logger, httpMethod, uri, xPathParamExprs,
jsonXMLMappingConvention);
break;
case XML:
restRequest = new XMLRequest(logger, httpMethod, uri, xPathParamExprs,
jsonXMLMappingConvention);
break;
case JSON:
restRequest = new JSONRequest(logger, httpMethod, uri, xPathParamExprs,
jsonXMLMappingConvention, jsonXMLMappingConvention);
break;
case POST_QUERY_STRING:
String postQueryStr = getSubElementTextContent(operationElt, POST_QUERY,
true);
UriTemplate postQuery = new UriTemplate(postQueryStr);
restRequest = new PostQueryRequest(logger, httpMethod, uri, postQuery, xPathParamExprs,
jsonXMLMappingConvention);
break;
default:
throw new PEtALSCDKException(httpBodyType + " is not an implemented HTTP Body type.");
}
operations.put(jbiOperation, restRequest);
} catch (XPathExpressionException xpee) {
throw new PEtALSCDKException(xpee);
}
}
return operations;
}
private static Map getXPathParams(Element operationElt)
throws XPathExpressionException {
Map xPathParams = new HashMap();
String namespaceURI = operationElt.getNamespaceURI();
NodeList xPathParamElements = operationElt
.getElementsByTagNameNS(namespaceURI, XPATH_PARAM);
int xPathParamElementsNb = xPathParamElements.getLength();
XPathFactory factory = XPathFactory.newInstance();
for (int i = 0; i < xPathParamElementsNb; i++) {
Node xPathParamNode = xPathParamElements.item(i);
Element xPathParamElt = (Element) xPathParamNode;
String paramName = xPathParamElt.getAttribute(XPATH_PARAM_NAME);
String paramValue = xPathParamElt.getTextContent();
XPath xPath = factory.newXPath();
XPathExpression xPathExpression = xPath.compile(paramValue);
xPathParams.put(paramName, xPathExpression);
}
return xPathParams;
}
// private ArrayList getParamXPathExprs(String path) throws
// PEtALSCDKException {
// ArrayList paramXPathExprs = new
// ArrayList();
//
// try {
// XPathFactory xPathFactory = XPathFactory.newInstance();
// XPath xPath = xPathFactory.newXPath();
//
// UriTemplate pathTemplate = new UriTemplate(path);
// List templateParams = pathTemplate.getTemplateVariables();
// for (String templateParam : templateParams) {
// paramXPathExprs.add(xPath.compile("./" + templateParam));
// }
// } catch (XPathExpressionException xpathee) {
// throw new PEtALSCDKException(xpathee);
// } catch (PatternSyntaxException pse) {
// throw new PEtALSCDKException(pse);
// } catch (IllegalArgumentException iae) {
// throw new PEtALSCDKException(iae);
// }
//
// return paramXPathExprs;
// }
}