Author: azeez
Date: Thu Sep 11 07:23:01 2008
New Revision: 21745
URL: http://wso2.org/svn/browse/wso2?view=rev&revision=21745

Log:
Handling custom XSDs with JAXWS services



Modified:
   trunk/wsas/java/modules/core/src/org/wso2/wsas/transport/util/XsdUtil.java

Modified: 
trunk/wsas/java/modules/core/src/org/wso2/wsas/transport/util/XsdUtil.java
URL: 
http://wso2.org/svn/browse/wso2/trunk/wsas/java/modules/core/src/org/wso2/wsas/transport/util/XsdUtil.java?rev=21745&r1=21744&r2=21745&view=diff
==============================================================================
--- trunk/wsas/java/modules/core/src/org/wso2/wsas/transport/util/XsdUtil.java  
(original)
+++ trunk/wsas/java/modules/core/src/org/wso2/wsas/transport/util/XsdUtil.java  
Thu Sep 11 07:23:01 2008
@@ -20,6 +20,7 @@
 import org.apache.axis2.deployment.DeploymentConstants;
 import org.apache.axis2.description.AxisService;
 import org.apache.ws.commons.schema.XmlSchema;
+import org.apache.http.HttpStatus;
 import org.wso2.utils.NetworkUtils;
 import org.wso2.utils.ServerConfiguration;
 import org.wso2.wsas.ServerConstants;
@@ -32,8 +33,10 @@
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
+import java.io.ByteArrayInputStream;
 import java.util.ArrayList;
 import java.util.Map;
+import java.util.HashMap;
 
 public class XsdUtil {
 
@@ -43,107 +46,189 @@
             throws IOException {
         OutputStream outputStream = response.getOutputStream();
         String contextRoot = request.getContextPath();
-        if (axisService != null) {
-            if (!axisService.isActive()) {
-                response.setContentType("text/html");
-                outputStream.write(("<h4>Service " +
-                                    serviceName +
-                                    " is inactive. Cannot display 
Schema.</h4>").getBytes());
-                outputStream.flush();
-                return;
-            }
+        if (axisService == null) {
+            response.setContentType("text/html");
+            response.setStatus(404);
+            outputStream.write(("<h4>Service " +
+                                serviceName +
+                                " is not found. Cannot display 
Schema.</h4>").getBytes());
+            outputStream.flush();
+            return;
+        }
 
+        if (!axisService.isActive()) {
+            response.setContentType("text/html");
+            outputStream.write(("<h4>Service " +
+                                serviceName +
+                                " is inactive. Cannot display 
Schema.</h4>").getBytes());
+            outputStream.flush();
+            return;
+        }
 
-            axisService.populateSchemaMappings();
-            Map schemaMappingtable =
-                    axisService.getSchemaMappingTable();
-            String xsds = request.getParameter("xsd");
-            if (xsds != null && xsds.trim().length() != 0) {
-                response.setContentType("text/xml");
-                XmlSchema schema =
-                        (XmlSchema) schemaMappingtable.get(xsds);
+        //cater for named xsds - check for the xsd name
+        String uri = request.getQueryString();
+        if (request.getQueryString().endsWith(".xsd")) {
+            String schemaName = uri.substring(uri.lastIndexOf("=") + 1);
+
+            HashMap services = configCtx.getAxisConfiguration().getServices();
+            AxisService service = (AxisService) services.get(serviceName);
+            if (service != null) {
+                //run the population logic just to be sure
+                service.populateSchemaMappings();
+                //write out the correct schema
+                Map schemaTable = service.getSchemaMappingTable();
+                XmlSchema schema = (XmlSchema) schemaTable.get(schemaName);
                 if (schema == null) {
-                    int dotIndex = xsds.indexOf('.');
+                    int dotIndex = schemaName.indexOf('.');
                     if (dotIndex > 0) {
-                        String schemaKey = xsds.substring(0, dotIndex);
-                        schema = (XmlSchema) schemaMappingtable.get(schemaKey);
+                        String schemaKey = schemaName.substring(0, dotIndex);
+                        schema = (XmlSchema) schemaTable.get(schemaKey);
                     }
                 }
+                //schema found - write it to the stream
                 if (schema != null) {
-                    //schema is there - pump it outs
-                    schema.write(new OutputStreamWriter(outputStream, "UTF8"));
-                    outputStream.flush();
-                    outputStream.close();
+                    response.setStatus(HttpStatus.SC_OK);
+                    response.setContentType("text/xml");
+                    schema.write(response.getOutputStream());
+                    return;
                 } else {
-                    InputStream in = axisService.getClassLoader()
-                            .getResourceAsStream(DeploymentConstants.META_INF 
+ "/" + xsds);
-                    if (in != null) {
-                        outputStream.write(IOUtils.getStreamAsByteArray(in));
-                        outputStream.flush();
-                        outputStream.close();
+                    InputStream instream = service.getClassLoader()
+                            .getResourceAsStream(DeploymentConstants.META_INF 
+ "/" + schemaName);
+
+                    if (instream != null) {
+                        response.setStatus(HttpStatus.SC_OK);
+                        response.setContentType("text/xml");
+                        OutputStream outstream = response.getOutputStream();
+                        boolean checkLength = true;
+                        int length = Integer.MAX_VALUE;
+                        int nextValue = instream.read();
+                        if (checkLength) {
+                            length--;
+                        }
+                        while (-1 != nextValue && length >= 0) {
+                            outstream.write(nextValue);
+                            nextValue = instream.read();
+                            if (checkLength) {
+                                length--;
+                            }
+                        }
+                        outstream.flush();
+                        return;
                     } else {
-                        response.sendError(HttpServletResponse.SC_NOT_FOUND);
+                        ByteArrayOutputStream baos = new 
ByteArrayOutputStream();
+                        int ret = service.printXSD(baos, schemaName);
+                        if (ret > 0) {
+                            baos.flush();
+                            instream = new 
ByteArrayInputStream(baos.toByteArray());
+                            response.setStatus(HttpStatus.SC_OK);
+                            response.setContentType("text/xml");
+                            OutputStream outstream = 
response.getOutputStream();
+                            boolean checkLength = true;
+                            int length = Integer.MAX_VALUE;
+                            int nextValue = instream.read();
+                            if (checkLength) {
+                                length--;
+                            }
+                            while (-1 != nextValue && length >= 0) {
+                                outstream.write(nextValue);
+                                nextValue = instream.read();
+                                if (checkLength) {
+                                    length--;
+                                }
+                            }
+                            outstream.flush();
+                            return;
+                        }
+                        // no schema available by that name  - send 404
+                        response.sendError(HttpStatus.SC_NOT_FOUND, "Schema 
Not Found!");
+                        return;
                     }
                 }
-                return;
             }
+        }
 
-            ArrayList schemas = axisService.getSchema();
-            if (schemas.size() == 1) {
-                response.setContentType("text/xml");
-                // Write to the output stream
-                processSchema((XmlSchema) schemas.get(0), outputStream, 
contextRoot, request);
+        axisService.populateSchemaMappings();
+        Map schemaMappingtable =
+                axisService.getSchemaMappingTable();
+        String xsds = request.getParameter("xsd");
+        if (xsds != null && xsds.trim().length() != 0) {
+            response.setContentType("text/xml");
+            XmlSchema schema =
+                    (XmlSchema) schemaMappingtable.get(xsds);
+            if (schema == null) {
+                int dotIndex = xsds.indexOf('.');
+                if (dotIndex > 0) {
+                    String schemaKey = xsds.substring(0, dotIndex);
+                    schema = (XmlSchema) schemaMappingtable.get(schemaKey);
+                }
+            }
+            if (schema != null) {
+                //schema is there - pump it outs
+                schema.write(new OutputStreamWriter(outputStream, "UTF8"));
+                outputStream.flush();
+                outputStream.close();
+            } else {
+                InputStream in = axisService.getClassLoader()
+                        .getResourceAsStream(DeploymentConstants.META_INF + 
"/" + xsds);
+                if (in != null) {
+                    outputStream.write(IOUtils.getStreamAsByteArray(in));
+                    outputStream.flush();
+                    outputStream.close();
+                } else {
+                    response.sendError(HttpServletResponse.SC_NOT_FOUND);
+                }
+            }
+            return;
+        }
 
+        ArrayList schemas = axisService.getSchema();
+        if (schemas.size() == 1) {
+            response.setContentType("text/xml");
+            // Write to the output stream
+            processSchema((XmlSchema) schemas.get(0), outputStream, 
contextRoot, request);
+
+        } else {
+            String idParam = request.getParameter("id");
+            if (idParam != null) {
+                XmlSchema schema = 
axisService.getSchema(Integer.parseInt(idParam));
+                if (schema != null) {
+                    response.setContentType("text/xml");
+                    processSchema(schema, outputStream, contextRoot, request);
+                } else {
+                    response.setContentType("text/html");
+                    outputStream.write("<h4>Schema not 
found!</h4>".getBytes());
+                }
             } else {
-                String idParam = request.getParameter("id");
-                if (idParam != null) {
-                    XmlSchema schema = 
axisService.getSchema(Integer.parseInt(idParam));
-                    if (schema != null) {
-                        response.setContentType("text/xml");
-                        processSchema(schema, outputStream, contextRoot, 
request);
-                    } else {
-                        response.setContentType("text/html");
-                        outputStream.write("<h4>Schema not 
found!</h4>".getBytes());
+                String ipAddress = "http://"; + NetworkUtils.getLocalHostname() 
+ ":" +
+                                   ServerManager.getInstance().getHttpPort();
+                String version =
+                        
ServerConfiguration.getInstance().getFirstProperty("Version");
+                outputStream.write(("<html><head>" +
+                                    "<title>WSO2 Web Services Application 
Server v" +
+                                    version +
+                                    "Management Console" +
+                                    " - " +
+                                    axisService.getName() +
+                                    " Service Schema</title>" +
+                                    "</head>" +
+                                    "<body>" +
+                                    "<b>Schemas for " +
+                                    axisService.getName() +
+                                    " service</b><br/><br/>").getBytes());
+                if (schemas.size() != 0) {
+                    for (int i = 0; i < schemas.size(); i++) {
+                        String st = "<a href=\"" + ipAddress +
+                                    
RequestProcessorUtil.getServiceContextPath(configCtx) + "/" +
+                                    axisService.getName() + "?xsd&id=" + i +
+                                    "&" + 
ServerConstants.HTTPConstants.ANNOTATION + "=true" + "\">Schema " + i +
+                                    "</a><br/>";
+                        outputStream.write(st.getBytes());
                     }
                 } else {
-                    String ipAddress = "http://"; + 
NetworkUtils.getLocalHostname() + ":" +
-                                       
ServerManager.getInstance().getHttpPort();
-                    String version =
-                            
ServerConfiguration.getInstance().getFirstProperty("Version");
-                    outputStream.write(("<html><head>" +
-                                        "<title>WSO2 Web Services Application 
Server v" +
-                                        version +
-                                        "Management Console" +
-                                        " - " +
-                                        axisService.getName() +
-                                        " Service Schema</title>" +
-                                        "</head>" +
-                                        "<body>" +
-                                        "<b>Schemas for " +
-                                        axisService.getName() +
-                                        " service</b><br/><br/>").getBytes());
-                    if (schemas.size() != 0) {
-                        for (int i = 0; i < schemas.size(); i++) {
-                            String st = "<a href=\"" + ipAddress +
-                                        
RequestProcessorUtil.getServiceContextPath(configCtx) + "/" +
-                                        axisService.getName() + "?xsd&id=" + i 
+
-                                        "&" + 
ServerConstants.HTTPConstants.ANNOTATION + "=true" + "\">Schema " + i +
-                                        "</a><br/>";
-                            outputStream.write(st.getBytes());
-                        }
-                    } else {
-                        outputStream.write("<p>No schemas 
found</p>".getBytes());
-                    }
-                    outputStream.write("</body></html>".getBytes());
+                    outputStream.write("<p>No schemas found</p>".getBytes());
                 }
+                outputStream.write("</body></html>".getBytes());
             }
-        } else {  // service is null
-            response.setContentType("text/html");
-            response.setStatus(404);
-            outputStream.write(("<h4>Service " +
-                                serviceName +
-                                " is not found. Cannot display 
Schema.</h4>").getBytes());
-            outputStream.flush();
         }
     }
 

_______________________________________________
Wsas-java-dev mailing list
[email protected]
http://mailman.wso2.org/cgi-bin/mailman/listinfo/wsas-java-dev

Reply via email to