You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by an...@apache.org on 2007/04/30 22:46:14 UTC

svn commit: r533853 - in /incubator/tuscany/java/sca/modules/binding-ws-axis2/src/main/java/org/apache/tuscany/binding/axis2: Axis2ServiceServlet.java TuscanyListingAgent.java

Author: antelder
Date: Mon Apr 30 13:46:13 2007
New Revision: 533853

URL: http://svn.apache.org/viewvc?view=rev&rev=533853
Log:
Fix axis2 binding so that ?wsdl works with the Tuscany service names

Added:
    incubator/tuscany/java/sca/modules/binding-ws-axis2/src/main/java/org/apache/tuscany/binding/axis2/TuscanyListingAgent.java   (with props)
Modified:
    incubator/tuscany/java/sca/modules/binding-ws-axis2/src/main/java/org/apache/tuscany/binding/axis2/Axis2ServiceServlet.java

Modified: incubator/tuscany/java/sca/modules/binding-ws-axis2/src/main/java/org/apache/tuscany/binding/axis2/Axis2ServiceServlet.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/binding-ws-axis2/src/main/java/org/apache/tuscany/binding/axis2/Axis2ServiceServlet.java?view=diff&rev=533853&r1=533852&r2=533853
==============================================================================
--- incubator/tuscany/java/sca/modules/binding-ws-axis2/src/main/java/org/apache/tuscany/binding/axis2/Axis2ServiceServlet.java (original)
+++ incubator/tuscany/java/sca/modules/binding-ws-axis2/src/main/java/org/apache/tuscany/binding/axis2/Axis2ServiceServlet.java Mon Apr 30 13:46:13 2007
@@ -21,6 +21,7 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.MalformedURLException;
+import java.net.SocketException;
 import java.net.URL;
 import java.util.Collections;
 import java.util.Enumeration;
@@ -35,8 +36,12 @@
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import org.apache.axis2.AxisFault;
+import org.apache.axis2.addressing.EndpointReference;
 import org.apache.axis2.context.ConfigurationContext;
 import org.apache.axis2.transport.http.AxisServlet;
+import org.apache.axis2.transport.http.ListingAgent;
+import org.apache.axis2.transport.http.server.HttpUtils;
 import org.apache.tuscany.core.component.SimpleWorkContext;
 import org.apache.tuscany.spi.Scope;
 import org.apache.tuscany.spi.bootstrap.ComponentNames;
@@ -51,11 +56,11 @@
  */
 public class Axis2ServiceServlet extends AxisServlet {
 
-    private static final long serialVersionUID = 1L;
+    protected TuscanyListingAgent agent;
+    protected boolean inited;
 
+    private static final long serialVersionUID = 1L;
     private static final ServletConfig DUMMY_CONFIG = createDummyServletConfig();
-    
-    private boolean inited;
 
     public void init(ConfigurationContext configContext) {
         this.configContext = configContext;
@@ -64,6 +69,7 @@
         } catch (ServletException e) {
             throw new RuntimeException(e);
         }
+        agent = new TuscanyListingAgent(configContext);
     }
 
     /**
@@ -91,7 +97,7 @@
     
     @Override
     public String getServletName() {
-        return "TuscanyAxis2DummyServlet";
+        return "TuscanyAxis2Servlet";
     }
 
     /**
@@ -246,4 +252,59 @@
             e.printStackTrace();
         }
     }
+
+    /**
+     * Override the AxisServlet doGet to use the TuscanyListingAgent for ?wsdl 
+     */
+    @Override
+    protected void doGet(HttpServletRequest request,
+                         HttpServletResponse response) throws ServletException, IOException {
+
+        initContextRoot(request);
+
+        String query = request.getQueryString();
+        if ((query != null) && (query.indexOf("wsdl2") >= 0 ||
+                query.indexOf("wsdl") >= 0 || query.indexOf("xsd") >= 0 ||
+                query.indexOf("policy") >= 0)) {
+            agent.processListService(request, response);
+        } else {
+            super.doGet(request, response);
+        }
+    }
+    
+    /**
+
+    /**
+     * Override the AxisServlet method so as to not add "/services" into the url
+     */
+    @Override
+    public EndpointReference[] getEPRsForService(String serviceName, String ip) throws AxisFault {
+        //RUNNING_PORT
+        String port = (String) configContext.getProperty(ListingAgent.RUNNING_PORT);
+        if (port == null) {
+            port = "8080";
+        }
+        if (ip == null) {
+            try {
+                ip = HttpUtils.getIpAddress();
+                if (ip == null) {
+                    ip = "localhost";
+                }
+            } catch (SocketException e) {
+                throw new AxisFault(e);
+            }
+        }
+
+
+        StringBuilder eprString = new StringBuilder("http://");
+        eprString.append(ip).append(":").append(port);
+        if (!serviceName.startsWith("/")) {
+            eprString.append('/');
+        }
+        eprString.append(serviceName);
+        EndpointReference endpoint = new EndpointReference(eprString.toString());
+
+        return new EndpointReference[]{endpoint};
+    }
+    
 }

Added: incubator/tuscany/java/sca/modules/binding-ws-axis2/src/main/java/org/apache/tuscany/binding/axis2/TuscanyListingAgent.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/binding-ws-axis2/src/main/java/org/apache/tuscany/binding/axis2/TuscanyListingAgent.java?view=auto&rev=533853
==============================================================================
--- incubator/tuscany/java/sca/modules/binding-ws-axis2/src/main/java/org/apache/tuscany/binding/axis2/TuscanyListingAgent.java (added)
+++ incubator/tuscany/java/sca/modules/binding-ws-axis2/src/main/java/org/apache/tuscany/binding/axis2/TuscanyListingAgent.java Mon Apr 30 13:46:13 2007
@@ -0,0 +1,314 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.tuscany.binding.axis2;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.xml.stream.FactoryConfigurationError;
+import javax.xml.stream.XMLOutputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+
+import org.apache.axiom.attachments.utils.IOUtils;
+import org.apache.axis2.Constants;
+import org.apache.axis2.context.ConfigurationContext;
+import org.apache.axis2.deployment.DeploymentConstants;
+import org.apache.axis2.description.AxisDescription;
+import org.apache.axis2.description.AxisService;
+import org.apache.axis2.description.PolicyInclude;
+import org.apache.axis2.transport.http.ListingAgent;
+import org.apache.axis2.util.ExternalPolicySerializer;
+import org.apache.neethi.Policy;
+import org.apache.neethi.PolicyRegistry;
+import org.apache.ws.commons.schema.XmlSchema;
+
+/**
+ * A Tuscany specific Axis2 ListingAgent as the Axis2 one does not work
+ * with the Tuscany sevice names which include slash ('/') characters.
+ * Unfortunately it ends up having  to copy a fair amount of Axis2 code to do this. 
+ */
+public class TuscanyListingAgent extends ListingAgent {
+
+    private static final String LIST_SINGLE_SERVICE_JSP_NAME =
+        "listSingleService.jsp";
+    
+    public TuscanyListingAgent(ConfigurationContext aConfigContext) {
+        super(aConfigContext);
+    }
+
+    @Override
+    public void processListService(HttpServletRequest req,
+                                   HttpServletResponse res)
+            throws IOException, ServletException {
+
+
+        String filePart = req.getRequestURL().toString();
+//        String serviceName = filePart.substring(filePart.lastIndexOf("/") + 1,
+//                                                filePart.length());
+// Change the Axis2 code so as to use the complete ServletPath as the service name
+// this line is the only change to to Axis2 code
+        String serviceName = req.getServletPath();
+        if (serviceName.startsWith("/")) {
+            serviceName = serviceName.substring(1);
+        }
+        
+        HashMap services = configContext.getAxisConfiguration().getServices();
+        String query = req.getQueryString();
+        int wsdl2 = query.indexOf("wsdl2");
+        int wsdl = query.indexOf("wsdl");
+        int xsd = query.indexOf("xsd");
+        int policy = query.indexOf("policy");
+
+        if ((services != null) && !services.isEmpty()) {
+            Object serviceObj = services.get(serviceName);
+            if (serviceObj != null) {
+                boolean isHttp = "http".equals(req.getScheme());
+                if (wsdl2 >= 0) {
+                    OutputStream out = res.getOutputStream();
+                    res.setContentType("text/xml");
+                    String ip = extractHostAndPort(filePart, isHttp);
+                    ((AxisService) serviceObj)
+                            .printWSDL2(out, ip, configContext.getServiceContextPath());
+                    out.flush();
+                    out.close();
+                    return;
+                } else if (wsdl >= 0) {
+                    OutputStream out = res.getOutputStream();
+                    res.setContentType("text/xml");
+                    String ip = extractHostAndPort(filePart, isHttp);
+                    ((AxisService) serviceObj).printWSDL(out, ip, configContext.getServicePath());
+                    out.flush();
+                    out.close();
+                    return;
+                } else if (xsd >= 0) {
+                    OutputStream out = res.getOutputStream();
+                    res.setContentType("text/xml");
+                    AxisService axisService = (AxisService) serviceObj;
+                    //call the populator
+                    axisService.populateSchemaMappings();
+                    Map schemaMappingtable =
+                            axisService.getSchemaMappingTable();
+                    ArrayList schemas = axisService.getSchema();
+
+                    //a name is present - try to pump the requested schema
+                    String xsds = req.getParameter("xsd");
+                    if (!"".equals(xsds)) {
+                        XmlSchema schema =
+                                (XmlSchema) schemaMappingtable.get(xsds);
+                        if (schema != null) {
+                            //schema is there - pump it outs
+                            schema.write(new OutputStreamWriter(out, "UTF8"));
+                            out.flush();
+                            out.close();
+                        } else {
+                            InputStream in = axisService.getClassLoader()
+                                    .getResourceAsStream(DeploymentConstants.META_INF + "/" + xsds);
+                            if (in != null) {
+                                out.write(IOUtils.getStreamAsByteArray(in));
+                                out.flush();
+                                out.close();
+                            } else {
+                                res.sendError(HttpServletResponse.SC_NOT_FOUND);
+                            }
+                        }
+
+                        //multiple schemas are present and the user specified
+                        //no name - in this case we cannot possibly pump a schema
+                        //so redirect to the service root
+                    } else if (schemas.size() > 1) {
+                        res.sendRedirect("");
+                        //user specified no name and there is only one schema
+                        //so pump that out
+                    } else {
+                        XmlSchema schema = axisService.getSchema(0);
+                        if (schema != null) {
+                            schema.write(new OutputStreamWriter(out, "UTF8"));
+                            out.flush();
+                            out.close();
+                        }
+                    }
+                    return;
+                } else if (policy >= 0) {
+
+                    OutputStream out = res.getOutputStream();
+
+                    ExternalPolicySerializer serializer = new ExternalPolicySerializer();
+                    serializer.setAssertionsToFilter(configContext
+                            .getAxisConfiguration().getLocalPolicyAssertions());
+
+                    // check whether Id is set
+                    String idParam = req.getParameter("id");
+
+                    if (idParam != null) {
+                        // Id is set
+
+                        Policy targetPolicy = findPolicy(idParam, (AxisService) serviceObj);
+
+                        if (targetPolicy != null) {
+                            XMLStreamWriter writer;
+
+                            try {
+                                writer = XMLOutputFactory.newInstance()
+                                        .createXMLStreamWriter(out);
+
+                                res.setContentType("text/xml");
+                                targetPolicy.serialize(writer);
+                                writer.flush();
+
+                            } catch (XMLStreamException e) {
+                                throw new ServletException(
+                                        "Error occured when serializing the Policy",
+                                        e);
+
+                            } catch (FactoryConfigurationError e) {
+                                throw new ServletException(
+                                        "Error occured when serializing the Policy",
+                                        e);
+                            }
+
+                        } else {
+
+                            res.setContentType("text/html");
+                            String outStr = "<b>No policy found for id="
+                                            + idParam + "</b>";
+                            out.write(outStr.getBytes());
+                        }
+
+                    } else {
+
+                        PolicyInclude policyInclude = ((AxisService) serviceObj).getPolicyInclude();
+                        Policy effecPolicy = policyInclude.getEffectivePolicy();
+
+                        if (effecPolicy != null) {
+                            XMLStreamWriter writer;
+
+                            try {
+                                writer = XMLOutputFactory.newInstance()
+                                        .createXMLStreamWriter(out);
+
+                                res.setContentType("text/xml");
+                                effecPolicy.serialize(writer);
+                                writer.flush();
+
+                            } catch (XMLStreamException e) {
+                                throw new ServletException(
+                                        "Error occured when serializing the Policy",
+                                        e);
+
+                            } catch (FactoryConfigurationError e) {
+                                throw new ServletException(
+                                        "Error occured when serializing the Policy",
+                                        e);
+                            }
+                        } else {
+
+                            res.setContentType("text/html");
+                            String outStr = "<b>No effective policy for "
+                                            + serviceName + " servcie</b>";
+                            out.write(outStr.getBytes());
+                        }
+                    }
+
+                    return;
+                } else {
+                    req.getSession().setAttribute(Constants.SINGLE_SERVICE,
+                                                  serviceObj);
+                }
+            } else {
+                req.getSession().setAttribute(Constants.SINGLE_SERVICE, null);
+            }
+        }
+
+        renderView(LIST_SINGLE_SERVICE_JSP_NAME, req, res);
+    }
+
+    private String extractHostAndPort(String filePart, boolean isHttp) {
+        int ipindex = filePart.indexOf("//");
+        String ip = null;
+        if (ipindex >= 0) {
+            ip = filePart.substring(ipindex + 2, filePart.length());
+            int seperatorIndex = ip.indexOf(":");
+            int slashIndex = ip.indexOf("/");
+            String port;
+            if (seperatorIndex >= 0) {
+                port = ip.substring(seperatorIndex + 1, slashIndex);
+                ip = ip.substring(0, seperatorIndex);
+            } else {
+                ip = ip.substring(0, slashIndex);
+                port = "80";
+            }
+            if (isHttp) {
+                configContext.setProperty(RUNNING_PORT, port);
+            }
+        }
+        return ip;
+    }
+
+    private Policy findPolicy(String id, AxisDescription des) {
+
+        List policyElements = des.getPolicyInclude().getPolicyElements();
+        PolicyRegistry registry = des.getPolicyInclude().getPolicyRegistry();
+
+        Object policyComponent;
+
+        Policy policy = registry.lookup(id);
+
+        if (policy != null) {
+            return policy;
+        }
+
+        for (Iterator iterator = policyElements.iterator(); iterator.hasNext();) {
+            policyComponent = iterator.next();
+
+            if (policyComponent instanceof Policy) {
+                // policy found for the id
+
+                if (id.equals(((Policy) policyComponent).getId())) {
+                    return (Policy) policyComponent;
+                }
+            }
+        }
+
+        AxisDescription child;
+
+        for (Iterator iterator = des.getChildren(); iterator.hasNext();) {
+            child = (AxisDescription) iterator.next();
+            policy = findPolicy(id, child);
+
+            if (policy != null) {
+                return policy;
+            }
+        }
+
+        return null;
+    }
+
+}

Propchange: incubator/tuscany/java/sca/modules/binding-ws-axis2/src/main/java/org/apache/tuscany/binding/axis2/TuscanyListingAgent.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sca/modules/binding-ws-axis2/src/main/java/org/apache/tuscany/binding/axis2/TuscanyListingAgent.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date



---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org