Author: keith Date: Wed Jan 2 00:47:21 2008 New Revision: 11734 Log:
Adding a service to genarate Javascript stubs Added: trunk/mashup/java/modules/coreservices/JSStubGenaratorService/ trunk/mashup/java/modules/coreservices/JSStubGenaratorService/META-INF/ trunk/mashup/java/modules/coreservices/JSStubGenaratorService/META-INF/services.xml trunk/mashup/java/modules/coreservices/JSStubGenaratorService/pom.xml trunk/mashup/java/modules/coreservices/JSStubGenaratorService/src/ trunk/mashup/java/modules/coreservices/JSStubGenaratorService/src/org/ trunk/mashup/java/modules/coreservices/JSStubGenaratorService/src/org/wso2/ trunk/mashup/java/modules/coreservices/JSStubGenaratorService/src/org/wso2/mashup/ trunk/mashup/java/modules/coreservices/JSStubGenaratorService/src/org/wso2/mashup/JSStubGenarator.java Added: trunk/mashup/java/modules/coreservices/JSStubGenaratorService/META-INF/services.xml ============================================================================== --- (empty file) +++ trunk/mashup/java/modules/coreservices/JSStubGenaratorService/META-INF/services.xml Wed Jan 2 00:47:21 2008 @@ -0,0 +1,29 @@ +<!-- + ~ Copyright 2007 WSO2, Inc. (http://wso2.com) + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> + +<serviceGroup name="wso2mashup-admin-JSStubGenaratorService"> + <service name="JavaScriptStubGenaratorService"> + <Description> + This service facilitates the genaratin of JavaScript stubs + given a WSDL document or url to a WSDL. + </Description> + <parameter name="ServiceClass"> + org.wso2.mashup.JSStubGenarator + </parameter> + <parameter name="serviceType" locked="false">MashupReadOnlyService</parameter> + <schema schemaNamespace="http://service.share.mashup.wso2.org/xsd"/> + </service> +</serviceGroup> \ No newline at end of file Added: trunk/mashup/java/modules/coreservices/JSStubGenaratorService/pom.xml ============================================================================== --- (empty file) +++ trunk/mashup/java/modules/coreservices/JSStubGenaratorService/pom.xml Wed Jan 2 00:47:21 2008 @@ -0,0 +1,55 @@ +<!-- + ~ Copyright (c) 2006, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + + <parent> + <groupId>org.wso2.mashup</groupId> + <artifactId>wso2mashup-coreservices</artifactId> + <version>SNAPSHOT</version> + </parent> + + <modelVersion>4.0.0</modelVersion> + <artifactId>wso2mashup-coreservices-JSStubGenaratorService</artifactId> + <packaging>aar</packaging> + <version>SNAPSHOT</version> + <name>WSO2 Mashup Server - Core Services - JavaScript Stub Genarator Service</name> + + <build> + <sourceDirectory>src</sourceDirectory> + <testSourceDirectory>test</testSourceDirectory> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <configuration> + <source>1.4</source> + <target>1.4</target> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.axis2</groupId> + <artifactId>axis2-aar-maven-plugin</artifactId> + <version>1.3</version> + <extensions>true</extensions> + <configuration> + <servicesXmlFile>META-INF/services.xml</servicesXmlFile> + <includeDependencies>false</includeDependencies> + </configuration> + </plugin> + </plugins> + </build> +</project> Added: trunk/mashup/java/modules/coreservices/JSStubGenaratorService/src/org/wso2/mashup/JSStubGenarator.java ============================================================================== --- (empty file) +++ trunk/mashup/java/modules/coreservices/JSStubGenaratorService/src/org/wso2/mashup/JSStubGenarator.java Wed Jan 2 00:47:21 2008 @@ -0,0 +1,156 @@ +/* + * Copyright 2006,2007 WSO2, Inc. http://www.wso2.org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.wso2.mashup; + +import org.apache.axiom.om.OMElement; +import org.apache.axiom.om.OMNamespace; +import org.apache.axis2.AxisFault; +import org.apache.axis2.description.AxisService; +import org.apache.axis2.description.WSDL11ToAllAxisServicesBuilder; +import org.apache.axis2.description.WSDL11ToAxisServiceBuilder; +import org.apache.axis2.description.WSDL2Constants; +import org.apache.axis2.namespace.Constants; +import org.apache.axis2.util.XMLUtils; +import org.apache.axis2.util.XSLTTemplateProcessor; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import sun.net.www.protocol.http.HttpURLConnection; + +import javax.activation.DataHandler; +import javax.xml.stream.XMLStreamException; +import javax.xml.transform.Result; +import javax.xml.transform.Source; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.stream.StreamResult; +import javax.xml.transform.stream.StreamSource; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.net.URLConnection; + +public class JSStubGenarator { + + private static Log log = LogFactory.getLog(JSStubGenarator.class); + + public String genarateStub(String type, DataHandler wsdl) throws MashupFault { + InputStream inputStream; + try { + inputStream = wsdl.getDataSource().getInputStream(); + } catch (IOException e) { + throw new MashupFault(e); + } + return getStub(type, inputStream, null); + } + + public String genarateStubFromURL(String type, String url) throws MashupFault { + HttpURLConnection uconn; + InputStream inputStream; + try { + URL wsdlURL = new URL(url); + URLConnection connection = wsdlURL.openConnection(); + if (connection instanceof HttpURLConnection) { + uconn = (HttpURLConnection) connection; + } else { + String msg = "Unable to process given URL. " + + "Only HTTP protocol is currently supported."; + log.error(msg); + throw new AxisFault(msg); + } + uconn.setRequestMethod("GET"); + uconn.setAllowUserInteraction(false); + uconn.setDefaultUseCaches(false); + uconn.setDoInput(true); + uconn.setDoOutput(false); + uconn.setInstanceFollowRedirects(true); + uconn.setUseCaches(false); + uconn.connect(); + inputStream = uconn.getInputStream(); + } catch (IOException e) { + throw new MashupFault(e); + } + return getStub(type, inputStream, url); + } + + private String getStub(String type, InputStream inputStream, String uri) + throws MashupFault { + + ByteArrayOutputStream stubOutStream; + try { + OMElement documentElement = (OMElement) XMLUtils.toOM(inputStream); + OMNamespace documentElementNS = documentElement.getNamespace(); + InputStream wsdlStream; + if (documentElementNS != null) { + WSDL11ToAxisServiceBuilder wsdl11ToAxisServiceBuilder; + if (Constants.NS_URI_WSDL11. + equals(documentElementNS.getNamespaceURI())) { + ByteArrayOutputStream outStream = new ByteArrayOutputStream(); + documentElement.serialize(outStream); + InputStream inStream = new ByteArrayInputStream(outStream.toByteArray()); + wsdl11ToAxisServiceBuilder = new WSDL11ToAllAxisServicesBuilder(inStream); + (wsdl11ToAxisServiceBuilder) + .setDocumentBaseUri(uri); + wsdl11ToAxisServiceBuilder.setAllPorts(true); + AxisService service = wsdl11ToAxisServiceBuilder.populateService(); + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + service.printWSDL2(outputStream); + wsdlStream = new ByteArrayInputStream(outputStream.toByteArray()); + } else if (WSDL2Constants.WSDL_NAMESPACE + .equals(documentElementNS.getNamespaceURI())) { + ByteArrayOutputStream outStream = new ByteArrayOutputStream(); + documentElement.serialize(outStream); + wsdlStream = new ByteArrayInputStream(outStream.toByteArray()); + } else { + throw new MashupFault("Invalid WSDL"); + } + } else { + throw new MashupFault("Invalid WSDL"); + } + InputStream sigStream = + Thread.currentThread().getContextClassLoader().getResourceAsStream( + "wsdl2sig.xslt"); + ByteArrayOutputStream sigOutStream = new ByteArrayOutputStream(); + XSLTTemplateProcessor.parse(sigOutStream, wsdlStream, sigStream); + InputStream stubStream = Thread.currentThread().getContextClassLoader() + .getResourceAsStream("jsstub.xslt"); + ByteArrayInputStream sigInStream = new ByteArrayInputStream(sigOutStream + .toByteArray()); + /* + * XSLT transform to generate the stubs + */ + Source xmlSource = new StreamSource(sigInStream); + Source xsltSource = new StreamSource(stubStream); + stubOutStream = new ByteArrayOutputStream(); + Result result = new StreamResult(stubOutStream); + Transformer transformer = TransformerFactory.newInstance() + .newTransformer(xsltSource); + if ("e4x".equals(type)) { + transformer.setParameter("e4x", "true"); + } + transformer.transform(xmlSource, result); + } catch (XMLStreamException e) { + throw new MashupFault(e); + } catch (AxisFault axisFault) { + throw new MashupFault(axisFault); + } catch (TransformerException e) { + throw new MashupFault(e); + } + return stubOutStream.toString(); + } +} \ No newline at end of file _______________________________________________ Mashup-dev mailing list [email protected] http://www.wso2.org/cgi-bin/mailman/listinfo/mashup-dev
