Author: sumedha Date: Tue Jun 10 02:34:01 2008 New Revision: 18280 URL: http://wso2.org/svn/browse/wso2?view=rev&revision=18280
Log: Fixing REST support Modified: branches/wsas/java/2.3/data-service-solution/modules/core/src/main/java/org/wso2/ws/dataservice/DBConstants.java branches/wsas/java/2.3/data-service-solution/modules/core/src/main/java/org/wso2/ws/dataservice/DBDeployer.java Modified: branches/wsas/java/2.3/data-service-solution/modules/core/src/main/java/org/wso2/ws/dataservice/DBConstants.java URL: http://wso2.org/svn/browse/wso2/branches/wsas/java/2.3/data-service-solution/modules/core/src/main/java/org/wso2/ws/dataservice/DBConstants.java?rev=18280&r1=18279&r2=18280&view=diff ============================================================================== --- branches/wsas/java/2.3/data-service-solution/modules/core/src/main/java/org/wso2/ws/dataservice/DBConstants.java (original) +++ branches/wsas/java/2.3/data-service-solution/modules/core/src/main/java/org/wso2/ws/dataservice/DBConstants.java Tue Jun 10 02:34:01 2008 @@ -65,6 +65,7 @@ public static final String MONEY = "MONEY"; public static final String SMALLMONEY = "SMALLMONEY"; public static final String BIT = "BIT"; + public static final String BOOLEAN = "BOOLEAN"; public static final String TINYINT = "TINYINT"; public static final String SMALLINT = "SMALLINT"; public static final String INTEGER = "INTEGER"; Modified: branches/wsas/java/2.3/data-service-solution/modules/core/src/main/java/org/wso2/ws/dataservice/DBDeployer.java URL: http://wso2.org/svn/browse/wso2/branches/wsas/java/2.3/data-service-solution/modules/core/src/main/java/org/wso2/ws/dataservice/DBDeployer.java?rev=18280&r1=18279&r2=18280&view=diff ============================================================================== --- branches/wsas/java/2.3/data-service-solution/modules/core/src/main/java/org/wso2/ws/dataservice/DBDeployer.java (original) +++ branches/wsas/java/2.3/data-service-solution/modules/core/src/main/java/org/wso2/ws/dataservice/DBDeployer.java Tue Jun 10 02:34:01 2008 @@ -23,11 +23,14 @@ import java.io.StringWriter; import java.sql.SQLException; import java.util.ArrayList; +import java.util.Comparator; import java.util.Date; import java.util.HashMap; import java.util.Iterator; +import java.util.Map; import java.util.Timer; import java.util.TimerTask; +import java.util.TreeMap; import javax.sql.DataSource; import javax.xml.namespace.QName; @@ -35,6 +38,8 @@ import org.apache.axiom.om.OMElement; import org.apache.axiom.om.impl.builder.StAXOMBuilder; +import org.apache.axiom.soap.SOAP11Constants; +import org.apache.axiom.soap.SOAP12Constants; import org.apache.axis2.AxisFault; import org.apache.axis2.context.ConfigurationContext; import org.apache.axis2.deployment.Deployer; @@ -43,14 +48,23 @@ import org.apache.axis2.deployment.DeploymentErrorMsgs; import org.apache.axis2.deployment.DeploymentException; import org.apache.axis2.deployment.repository.util.DeploymentFileData; +import org.apache.axis2.description.AxisBinding; +import org.apache.axis2.description.AxisBindingMessage; +import org.apache.axis2.description.AxisBindingOperation; +import org.apache.axis2.description.AxisEndpoint; +import org.apache.axis2.description.AxisMessage; import org.apache.axis2.description.AxisOperation; import org.apache.axis2.description.AxisService; import org.apache.axis2.description.AxisServiceGroup; import org.apache.axis2.description.InOutAxisOperation; import org.apache.axis2.description.Parameter; +import org.apache.axis2.description.TransportInDescription; +import org.apache.axis2.description.WSDL2Constants; import org.apache.axis2.description.java2wsdl.Java2WSDLConstants; import org.apache.axis2.engine.AxisConfiguration; import org.apache.axis2.i18n.Messages; +import org.apache.axis2.transport.http.util.RESTUtil; +import org.apache.axis2.wsdl.WSDLConstants; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.ws.commons.schema.utils.NamespaceMap; @@ -66,6 +80,13 @@ private Config config; private static HashMap conversionType = null; private AxisService axisService = null; + Map httpLocationTable; + private final String HTTP_TRANSPORT = "http"; + private final String HTTPS_TRANSPORT = "https"; + private AxisBinding soap11Binding; + private AxisBinding soap12Binding; + private AxisBinding httpBinding; + private static HashMap getConversionTable() { if (conversionType == null) { @@ -79,6 +100,7 @@ conversionType.put(DBConstants.DataTypes.MONEY, "java.math.BigDecimal"); conversionType.put(DBConstants.DataTypes.SMALLMONEY, "java.math.BigDecimal"); conversionType.put(DBConstants.DataTypes.BIT, "boolean"); + conversionType.put(DBConstants.DataTypes.BOOLEAN, "boolean"); conversionType.put(DBConstants.DataTypes.TINYINT, "byte"); conversionType.put(DBConstants.DataTypes.SMALLINT, "short"); conversionType.put(DBConstants.DataTypes.INTEGER, "int"); @@ -96,7 +118,7 @@ } return conversionType; } - public void deploy(DeploymentFileData deploymentFileData) { + public void deploy(DeploymentFileData deploymentFileData) throws DeploymentException { StringWriter errorWriter; String serviceStatus; errorWriter = new StringWriter(); @@ -116,6 +138,7 @@ PrintWriter error_ptintWriter = new PrintWriter(errorWriter); de.printStackTrace(error_ptintWriter); serviceStatus = "Error:\n" + errorWriter.toString(); + throw de; } catch (AxisFault axisFault) { log.error(Messages.getMessage(DeploymentErrorMsgs.INVALID_SERVICE, deploymentFileData .getName(), axisFault.getMessage()), axisFault); @@ -319,6 +342,116 @@ } + //REST related processing + //Get resource definitions for queries + //If there are resource definitions + Iterator resources = configElement.getChildrenWithName(new QName("resource")); + + if(resources != null){ + httpLocationTable = new TreeMap(new Comparator() { + public int compare(Object o1, Object o2) { + return (-1 * ((Comparable) o1).compareTo(o2)); + } + }); + + String interfaceName = serviceName + WSDL2Constants.INTERFACE_PREFIX; + // Create a default SOAP 1.1 Binding + createDefaultSOAP11Binding(serviceName, interfaceName); + + // Create a default SOAP 1.2 Binding + createDefaultSOAP12Binding(serviceName, interfaceName); + + // Create a default HTTP Binding + createDefaultHTTPBinding(serviceName, interfaceName); + + // We need to get all transports from the axis2 engine and create endpoints for each of + // those in here. + createDefaultEndpoints(axisService); + + while(resources.hasNext()){ + OMElement resourceEle = (OMElement) resources.next(); + //Resource resource = (Resource)resources.get(a); + String method = resourceEle.getAttributeValue(new QName("method")); + String path = resourceEle.getAttributeValue(new QName("path")); + + //Create an axisoperation to serve resource request + String pathWithoutSlashes = path.replaceAll("/", "_"); + String operationName = "_"+method+pathWithoutSlashes; + + AxisOperation axisOperation = new InOutAxisOperation(new QName(operationName.toLowerCase())); + + axisOperation.setMessageReceiver(dbMessageReceiver); + axisOperation.setStyle(WSDLConstants.STYLE_DOC); + axisOperation.addParameter(new Parameter(DBConstants.DB_OPERATION_ELEMENT, + resourceEle)); + + //getting the query element + OMElement callQuery = resourceEle.getFirstChildWithName(new QName("call-query")); + String hrefVal = callQuery.getAttributeValue(new QName("href")); + OMElement query = (OMElement) queryMap.get(hrefVal); + CallQuery callQueryElement = new CallQuery(); + //callQueryElement.populateCallQuery(query,paramsPerCallQueryMap); + callQueryElement.populateCallQuery(query,axisService); + + //creating a parameter to hold query object + Parameter callQueryParameter = new Parameter(); + callQueryParameter.setName(DBConstants.CALL_QUERY_ELEMENT); + callQueryParameter.setValue(callQueryElement); + axisOperation.addParameter(callQueryParameter); + + + //iterate through with-params in callQuery element & append parameter ids + //to resource path + Iterator withParamsItr = callQuery.getChildrenWithName(new QName("with-param")); + while(withParamsItr.hasNext()){ + OMElement withParamEle = (OMElement)withParamsItr.next(); + String name = withParamEle.getAttributeValue(new QName("name")); + path +="/{"+name+"}"; + } + //path = path +"/{id}/{name}"; + log.info("Exposing query :" + hrefVal + " via HTTP " + + method + " for URL : " + path); + + // Create a default SOAP 1.1 Binding operation + AxisBindingOperation soap11BindingOperation = + createDefaultSOAP11BindingOperation(axisOperation, path, method); + + // Create a default SOAP 1.2 Binding operation + AxisBindingOperation soap12BindingOperation = + createDefaultSOAP12BindingOperation(axisOperation, path, method); + + // Create a default HTTP Binding operation + AxisBindingOperation httpBindingOperation = + createDefaultHTTPBindingOperation(axisOperation, path, method); + + + String httpLocationString = RESTUtil + .getConstantFromHTTPLocation(path, method); + //String httpLocationString = path; + httpLocationTable.put(httpLocationString, axisOperation); + + // Create the in and out axis messages for this operation + AxisMessage inMessage = axisOperation.getMessage(WSDLConstants.MESSAGE_LABEL_IN_VALUE); + if (inMessage != null) { + inMessage.setName(method + Java2WSDLConstants.MESSAGE_SUFFIX); + createAxisBindingMessage(soap11BindingOperation,inMessage); + createAxisBindingMessage(soap12BindingOperation,inMessage); + createAxisBindingMessage(httpBindingOperation,inMessage); + } + + AxisMessage outMessage = axisOperation.getMessage(WSDLConstants.MESSAGE_LABEL_OUT_VALUE); + if (outMessage != null) { + outMessage.setName(method + Java2WSDLConstants.RESPONSE_MESSAGE); + createAxisBindingMessage(soap11BindingOperation,outMessage); + createAxisBindingMessage(soap12BindingOperation,outMessage); + createAxisBindingMessage(httpBindingOperation,outMessage); + } + axisService.addOperation(axisOperation); + axisConfig.getPhasesInfo().setOperationPhases(axisOperation); + } + } + //(End) REST related processing + Iterator ops = configElement.getChildrenWithName(new QName("operation")); while (ops.hasNext()) { OMElement operation = (OMElement) ops.next(); @@ -386,6 +519,184 @@ } + private void createAxisBindingMessage( + AxisBindingOperation bindingOperation,AxisMessage inMessage) { + AxisBindingMessage soap11InBindingMessage = new AxisBindingMessage(); + soap11InBindingMessage.setName(inMessage.getName()); + soap11InBindingMessage.setAxisMessage(inMessage); + soap11InBindingMessage.setParent(bindingOperation); + + bindingOperation + .addChild(WSDLConstants.MESSAGE_LABEL_IN_VALUE, soap11InBindingMessage); + } + + + /** + * Creates AxisBindingOperation and populates it with HTTP properties + * @param axisOp The AxisOperation corresponding to this bindingOperation + * @param httpLocation The httpLocation annotation for this operation + * @param httpMethod The httpMethod annotation for this operation + * @return AxisBindingOperation having sdefault HTTP values + */ + private AxisBindingOperation createDefaultHTTPBindingOperation(AxisOperation axisOp, + String httpLocation, + String httpMethod) { + AxisBindingOperation httpBindingOperation = new AxisBindingOperation(); + httpBindingOperation.setAxisOperation(axisOp); + httpBindingOperation.setName(axisOp.getName()); + httpBindingOperation.setParent(httpBinding); + httpBindingOperation.setProperty(WSDL2Constants.ATTR_WHTTP_LOCATION, httpLocation); + httpBindingOperation.setProperty(WSDL2Constants.ATTR_WHTTP_METHOD, httpMethod); + httpBinding.addChild(httpBindingOperation.getName(), httpBindingOperation); + return httpBindingOperation; + } + + /** + * Creates AxisBindingOperation and populates it with SOAP 1.2 properties + * @param axisOp The AxisOperation corresponding to this bindingOperation + * @param httpLocation The httpLocation annotation for this operation + * @param inputAction The input action for this operation + * @return AxisBindingOperation having sdefault SOAP 1.2 values + */ + private AxisBindingOperation createDefaultSOAP12BindingOperation(AxisOperation axisOp, + String httpLocation, + String inputAction) { + AxisBindingOperation soap12BindingOperation = new AxisBindingOperation(); + soap12BindingOperation.setAxisOperation(axisOp); + soap12BindingOperation.setName(axisOp.getName()); + soap12BindingOperation.setParent(soap12Binding); + soap12BindingOperation.setProperty(WSDL2Constants.ATTR_WHTTP_LOCATION, httpLocation); + soap12Binding.addChild(soap12BindingOperation.getName(), soap12BindingOperation); + soap12BindingOperation.setProperty(WSDL2Constants.ATTR_WSOAP_ACTION, inputAction); + return soap12BindingOperation; + } + + /** + * Creates AxisBindingOperation and populates it with SOAP 1.1 properties + * @param axisOp The AxisOperation corresponding to this bindingOperation + * @param httpLocation The httpLocation annotation for this operation + * @param inputAction The input action for this operation + * @return AxisBindingOperation having sdefault SOAP 1.1 values + */ + private AxisBindingOperation createDefaultSOAP11BindingOperation(AxisOperation axisOp, + String httpLocation, + String inputAction) { + AxisBindingOperation soap11BindingOperation = new AxisBindingOperation(); + soap11BindingOperation.setAxisOperation(axisOp); + soap11BindingOperation.setName(axisOp.getName()); + soap11BindingOperation.setParent(soap11Binding); + soap11BindingOperation.setProperty(WSDL2Constants.ATTR_WHTTP_LOCATION, httpLocation); + soap11Binding.addChild(soap11BindingOperation.getName(), soap11BindingOperation); + soap11BindingOperation.setProperty(WSDL2Constants.ATTR_WSOAP_ACTION, inputAction); + return soap11BindingOperation; + } + /** + * Creates a AxisBinding and populates it with default SOAP 1.1 properties + * @param name The name of the service + * @param interfaceName The interface name + */ + private void createDefaultSOAP11Binding(String name, String interfaceName) { + soap11Binding = new AxisBinding(); + soap11Binding.setName(new QName(name + Java2WSDLConstants.BINDING_NAME_SUFFIX)); + soap11Binding.setType(WSDL2Constants.URI_WSDL2_SOAP); + soap11Binding + .setProperty(WSDL2Constants.ATTR_WSOAP_PROTOCOL, WSDL2Constants.HTTP_PROTOCAL); + soap11Binding.setProperty(WSDL2Constants.ATTR_WSOAP_VERSION, + SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI); + soap11Binding.setProperty(WSDL2Constants.INTERFACE_LOCAL_NAME, interfaceName); + soap11Binding.setProperty(WSDL2Constants.HTTP_LOCATION_TABLE, httpLocationTable); + } + + /** + * Creates a AxisBinding and populates it with default HTTP properties + * @param name The name of the service + * @param interfaceName The interface name + */ + private void createDefaultHTTPBinding(String name, String interfaceName) { + httpBinding = new AxisBinding(); + httpBinding.setName(new QName(name + Java2WSDLConstants.HTTP_BINDING)); + httpBinding.setType(WSDL2Constants.URI_WSDL2_HTTP); + httpBinding.setProperty(WSDL2Constants.INTERFACE_LOCAL_NAME, interfaceName); + httpBinding.setProperty(WSDL2Constants.HTTP_LOCATION_TABLE, httpLocationTable); + } + + /** + * Creates a AxisBinding and populates it with default SOAP 1.2 properties + * @param name The name of the service + * @param interfaceName The interface name + */ + private void createDefaultSOAP12Binding(String name, String interfaceName) { + soap12Binding = new AxisBinding(); + soap12Binding.setName(new QName(name + Java2WSDLConstants.SOAP12BINDING_NAME_SUFFIX)); + soap12Binding.setType(WSDL2Constants.URI_WSDL2_SOAP); + soap12Binding + .setProperty(WSDL2Constants.ATTR_WSOAP_PROTOCOL, WSDL2Constants.HTTP_PROTOCAL); + soap12Binding.setProperty(WSDL2Constants.ATTR_WSOAP_VERSION, + SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI); + soap12Binding.setProperty(WSDL2Constants.INTERFACE_LOCAL_NAME, interfaceName); + soap12Binding.setProperty(WSDL2Constants.HTTP_LOCATION_TABLE, httpLocationTable); + } + + + /** + * Creates a set of default endpoints for this service + * @param axisService The AxisService that the endpoints are created for + */ + private void createDefaultEndpoints(AxisService axisService) { + HashMap transportsIn = axisConfig.getTransportsIn(); + Iterator iterator = transportsIn.values().iterator(); + while (iterator.hasNext()) { + // Used to indicate whether a HTTPEndpoint is needed. Http endpoint is needed only + // for http and https transports + boolean needHttp = false; + + // The prefix is used to generate endpoint names + String prefix = ""; + TransportInDescription transportIn = (TransportInDescription) iterator.next(); + String transportInName = transportIn.getName(); + if (HTTP_TRANSPORT.equalsIgnoreCase(transportInName)) { + needHttp = true; + } else if (HTTPS_TRANSPORT.equalsIgnoreCase(transportInName)) { + needHttp = true; + prefix = WSDL2Constants.DEFAULT_HTTPS_PREFIX; + } else if (transportInName != null) { + prefix = transportInName.toUpperCase(); + } + + // Creates a default SOAP 1.1 endpoint + AxisEndpoint soap11Endpoint = new AxisEndpoint(); + String soap11EndpointName = + prefix + WSDL2Constants.DEFAULT_SOAP11_ENDPOINT_NAME; + soap11Endpoint.setName(soap11EndpointName); + soap11Endpoint.setBinding(soap11Binding); + soap11Endpoint.setParent(axisService); + soap11Endpoint.setTransportInDescription(transportInName); + axisService.addEndpoint(soap11EndpointName, soap11Endpoint); + + // Creates a default SOAP 1.2 endpoint + AxisEndpoint soap12Endpoint = new AxisEndpoint(); + String soap12EndpointName = + prefix + WSDL2Constants.DEFAULT_SOAP12_ENDPOINT_NAME; + soap12Endpoint.setName(soap12EndpointName); + soap12Endpoint.setBinding(soap12Binding); + soap12Endpoint.setParent(axisService); + soap12Endpoint.setTransportInDescription(transportInName); + axisService.addEndpoint(soap12EndpointName, soap12Endpoint); + + // Creates a HTTP endpoint if its http or https transport is used + if (needHttp) { + AxisEndpoint httpEndpoint = new AxisEndpoint(); + String httpEndpointName = + prefix + WSDL2Constants.DEFAULT_HTTP_ENDPOINT_NAME; + httpEndpoint.setName(httpEndpointName); + httpEndpoint.setBinding(httpBinding); + httpEndpoint.setParent(axisService); + httpEndpoint.setTransportInDescription(transportInName); + axisService.addEndpoint(httpEndpointName, httpEndpoint); + } + } + } + /* * map populated here (paramsPerCallQueryMap) will be used to get user * defined parameter name for web service operation _______________________________________________ Wsas-java-dev mailing list [email protected] http://wso2.org/cgi-bin/mailman/listinfo/wsas-java-dev
