Author: keith
Date: Sat May 10 09:36:03 2008
New Revision: 16780
Log:
Fixing Mashup-799.
Added:
trunk/mashup/java/modules/core/src/org/wso2/mashup/requestprocessor/GadgetProcessor.java
Modified:
trunk/mashup/java/modules/core/conf/server.xml
Modified: trunk/mashup/java/modules/core/conf/server.xml
==============================================================================
--- trunk/mashup/java/modules/core/conf/server.xml (original)
+++ trunk/mashup/java/modules/core/conf/server.xml Sat May 10 09:36:03 2008
@@ -408,6 +408,10 @@
<Item>download</Item>
<Class>org.wso2.mashup.requestprocessor.DownloadProcessor</Class>
</Processor>
+ <Processor>
+ <Item>gadget</Item>
+ <Class>org.wso2.mashup.requestprocessor.GadgetProcessor</Class>
+ </Processor>
</HttpGetRequestProcessors>
<!--
Added:
trunk/mashup/java/modules/core/src/org/wso2/mashup/requestprocessor/GadgetProcessor.java
==============================================================================
--- (empty file)
+++
trunk/mashup/java/modules/core/src/org/wso2/mashup/requestprocessor/GadgetProcessor.java
Sat May 10 09:36:03 2008
@@ -0,0 +1,128 @@
+/*
+ * 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.requestprocessor;
+
+import org.wso2.wsas.transport.HttpGetRequestProcessor;
+import org.wso2.mashup.utils.MashupUtils;
+import org.wso2.mashup.MashupConstants;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.axis2.context.ConfigurationContext;
+import org.apache.axis2.description.AxisService;
+import org.apache.axiom.om.OMException;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.xml.transform.Source;
+import javax.xml.transform.Result;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.URIResolver;
+import javax.xml.transform.stream.StreamSource;
+import javax.xml.transform.stream.StreamResult;
+import java.io.OutputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+import java.io.ByteArrayInputStream;
+import java.io.File;
+
+public class GadgetProcessor implements HttpGetRequestProcessor {
+ private static Log log = LogFactory.getLog(GadgetProcessor.class);
+
+ /**
+ * Process the HTTP GET request
+ *
+ * @param request The HttpRequest
+ * @param response The HttpResponse
+ * @param configurationContext The system ConfigurationContext
+ * @throws Exception If some failure occurs during processing
+ */
+ public void process(HttpServletRequest request, HttpServletResponse
response,
+ ConfigurationContext configurationContext) throws
Exception {
+ try {
+ String requestURI = request.getRequestURI();
+ String contextPath = configurationContext.getServiceContextPath();
+ String serviceName = MashupUtils.getServiceName(requestURI,
contextPath);
+ AxisService axisService =
configurationContext.getAxisConfiguration()
+ .getServiceForActivation(serviceName);
+
+ OutputStream outputStream = response.getOutputStream();
+ if (axisService != null) {
+ if (!axisService.isActive()) {
+ response.setStatus(HttpServletResponse.SC_NOT_FOUND);
+ response.setContentType("text/html");
+ outputStream
+ .write(("<h4>Service " + serviceName +
+ " is inactive. Cannot generate
Gadget.</h4>")
+ .getBytes());
+ outputStream.flush();
+ return;
+ }
+
+ ByteArrayOutputStream sigOutStream =
Utils.getSigStream(axisService);
+
+ InputStream gadgetStream =
Thread.currentThread().getContextClassLoader()
+ .getResourceAsStream("gadget.xslt");
+ if (gadgetStream == null) {
+ response.setStatus(HttpServletResponse.SC_NOT_FOUND);
+ response.setContentType("text/html; charset=utf-8");
+ outputStream.write(("<h4>Cannot find the stylesheet</h4>")
+ .getBytes());
+ return;
+ }
+
+ ByteArrayInputStream sigInStream = new
ByteArrayInputStream(sigOutStream
+ .toByteArray());
+ /*
+ * XSLT transform to generate the stubs
+ */
+ Source xmlSource = new StreamSource(sigInStream);
+ Source xsltSource = new StreamSource(gadgetStream);
+ Result result = new StreamResult(response.getOutputStream());
+ TransformerFactory transformerFactory =
TransformerFactory.newInstance();
+ transformerFactory.setURIResolver(new GadgetURIResolver());
+ Transformer transformer = transformerFactory
+ .newTransformer(xsltSource);
+
+ response.setContentType("text/xml");
+ transformer.transform(xmlSource, result);
+ } else {
+ response.setContentType("text/html");
+ response.setStatus(HttpServletResponse.SC_NOT_FOUND);
+ outputStream
+ .write(("<h4>Service cannot be found. Cannot display
<em>Gadget</em>.</h4>")
+ .getBytes());
+ outputStream.flush();
+ }
+ } catch (OMException e) {
+ log.debug(e);
+ } catch (Exception e) {
+ log.error(e);
+ }
+ }
+
+ class GadgetURIResolver implements URIResolver {
+
+ GadgetURIResolver() {
+ }
+
+ public Source resolve(String href, String base) {
+ String wso2wsasHome =
System.getProperty(MashupConstants.WSO2WSAS_HOME);
+ File libFolder = new File(wso2wsasHome, "lib");
+ return new StreamSource(new File(libFolder, href));
+ }
+ }
+}
\ No newline at end of file
_______________________________________________
Mashup-dev mailing list
[email protected]
http://www.wso2.org/cgi-bin/mailman/listinfo/mashup-dev