cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bimargul...@apache.org
Subject svn commit: r598320 - in /incubator/cxf/trunk/rt: core/src/main/java/org/apache/cxf/transport/http/ javascript/src/main/java/org/apache/cxf/javascript/ javascript/src/test/java/org/apache/cxf/javascript/
Date Mon, 26 Nov 2007 15:53:13 GMT
Author: bimargulies
Date: Mon Nov 26 07:53:12 2007
New Revision: 598320

URL: http://svn.apache.org/viewvc?rev=598320&view=rev
Log:
 Cleanup of the query handler for Javascript, and adding support to return the common
Javascript utilities.

Modified:
    incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/transport/http/UrlUtilities.java
    incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/transport/http/WSDLQueryHandler.java
    incubator/cxf/trunk/rt/javascript/src/main/java/org/apache/cxf/javascript/JavascriptQueryHandler.java
    incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/QueryHandlerTest.java

Modified: incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/transport/http/UrlUtilities.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/transport/http/UrlUtilities.java?rev=598320&r1=598319&r2=598320&view=diff
==============================================================================
--- incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/transport/http/UrlUtilities.java
(original)
+++ incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/transport/http/UrlUtilities.java
Mon Nov 26 07:53:12 2007
@@ -19,8 +19,7 @@
 
 package org.apache.cxf.transport.http;
 
-import java.net.MalformedURLException;
-import java.net.URL;
+import java.net.URI;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.StringTokenizer;
@@ -32,7 +31,13 @@
     
     private UrlUtilities() {
     }
-    
+
+    /**
+     * Create a map from String to String that represents the contents of the query
+     * portion of a URL. For each x=y, x is the key and y is the value.
+     * @param s the query part of the URI.
+     * @return the map.
+     */
     public static Map<String, String> parseQueryString(String s) {
         Map<String, String> ht = new HashMap<String, String>();
         StringTokenizer st = new StringTokenizer(s, "&");
@@ -49,19 +54,18 @@
         return ht;
     }
     
-    
-    
-    public static String getStem(String baseURI) throws MalformedURLException {
-        URL url = null;
-        url = new URL(baseURI);
-        if (url != null) {
-            baseURI = url.getPath();
-            int idx = baseURI.lastIndexOf('/');
-            if (idx != -1) {
-                baseURI = baseURI.substring(0, idx);
-            }
-        }        
+    /**
+     * Return everything in the path up to the last slash in a URI.
+     * @param baseURI
+     * @return the trailing 
+     */
+    public static String getStem(String baseURI) {
+        URI uri = URI.create(baseURI);
+        baseURI = uri.getPath();
+        int idx = baseURI.lastIndexOf('/');
+        if (idx != -1) {
+            baseURI = baseURI.substring(0, idx);
+        }
         return baseURI;
     }
-
 }

Modified: incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/transport/http/WSDLQueryHandler.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/transport/http/WSDLQueryHandler.java?rev=598320&r1=598319&r2=598320&view=diff
==============================================================================
--- incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/transport/http/WSDLQueryHandler.java
(original)
+++ incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/transport/http/WSDLQueryHandler.java
Mon Nov 26 07:53:12 2007
@@ -27,7 +27,6 @@
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
-import java.util.logging.Level;
 import java.util.logging.Logger;
 
 import javax.wsdl.Definition;
@@ -93,13 +92,8 @@
                     return endpointInfo.getAddress().contains(ctx);
                 } else {
                     // contextMatchStrategy will be "stem"
-                    try {
-                        return endpointInfo.getAddress().
-                            contains(UrlUtilities.getStem(baseUri.substring(0, idx)));
-                    } catch (MalformedURLException mue) {
-                        LOG.log(Level.WARNING, "URL creation failed: ", mue);
-                        return false;
-                    }
+                    return endpointInfo.getAddress().
+                                contains(UrlUtilities.getStem(baseUri.substring(0, idx)));
                 }
             }
         }

Modified: incubator/cxf/trunk/rt/javascript/src/main/java/org/apache/cxf/javascript/JavascriptQueryHandler.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/javascript/src/main/java/org/apache/cxf/javascript/JavascriptQueryHandler.java?rev=598320&r1=598319&r2=598320&view=diff
==============================================================================
--- incubator/cxf/trunk/rt/javascript/src/main/java/org/apache/cxf/javascript/JavascriptQueryHandler.java
(original)
+++ incubator/cxf/trunk/rt/javascript/src/main/java/org/apache/cxf/javascript/JavascriptQueryHandler.java
Mon Nov 26 07:53:12 2007
@@ -20,13 +20,13 @@
 package org.apache.cxf.javascript;
 
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
-import java.net.MalformedURLException;
+import java.net.URI;
 import java.nio.charset.Charset;
 import java.util.Collection;
 import java.util.Map;
-import java.util.logging.Level;
 import java.util.logging.Logger;
 
 import org.apache.cxf.Bus;
@@ -41,8 +41,11 @@
 import org.apache.cxf.transports.http.StemMatchingQueryHandler;
 
 public class JavascriptQueryHandler implements StemMatchingQueryHandler {
+    private static final String JS_UTILS_PATH = "/org/apache/cxf/javascript/cxf-utils.js";
     private static final Logger LOG = LogUtils.getL7dLogger(JavascriptQueryHandler.class);
     private static final Charset UTF8 = Charset.forName("utf-8");
+    private static final String UTILS_QUERY_KEY = "jsutils";
+    private static final String CODE_QUERY_KEY = "js";
     private Bus bus;
 
     public JavascriptQueryHandler(Bus b) {
@@ -51,56 +54,81 @@
     }
 
     public String getResponseContentType(String fullQueryString, String ctx) {
-        if (fullQueryString.toLowerCase().contains("?js")) {
+        URI uri = URI.create(fullQueryString);
+        Map<String, String> map = UrlUtilities.parseQueryString(uri.getQuery());
+        if (map.containsKey(UTILS_QUERY_KEY) || map.containsKey(CODE_QUERY_KEY)) {
             return "application/javascript;charset=UTF-8";
         }
         return null;
     }
 
-    public boolean isRecognizedQuery(String baseUri, String ctx, EndpointInfo endpointInfo,

+    public boolean isRecognizedQuery(String baseUri, String ctx, EndpointInfo endpointInfo,
                                      boolean contextMatchExact) {
-        if (baseUri != null 
-            && (baseUri.contains("?") 
-            && (baseUri.toLowerCase().contains("js")))) {
-            int idx = baseUri.indexOf("?");
-            Map<String, String> map = UrlUtilities.parseQueryString(baseUri.substring(idx
+ 1));
-            if (map.containsKey("js")) {
-                try {
-                    return endpointInfo.getAddress()
-                        .contains(UrlUtilities.getStem(baseUri.substring(0, idx)));
-                } catch (MalformedURLException mue) {
-                    LOG.log(Level.WARNING, "URL creation failed: ", mue);
-                    return false;
-                }
-            }
+        if (baseUri == null) {
+            return false;
+        }
+        URI uri = URI.create(baseUri);
+        Map<String, String> map = UrlUtilities.parseQueryString(uri.getQuery());
+        if (map.containsKey(UTILS_QUERY_KEY)) {
+            // the utils are independent of the endpoint.
+            return true;
+        }
+        if (map.containsKey(CODE_QUERY_KEY)) {
+            return endpointInfo.getAddress().contains(UrlUtilities.getStem(uri.getSchemeSpecificPart()));
         }
         return false;
     }
+    
+    private void writeUtilsToResponseStream(OutputStream outputStream) {
+        InputStream utils = getClass().getResourceAsStream(JS_UTILS_PATH);
+        if (utils == null) {
+            throw new RuntimeException("Unable to get stream for " + JS_UTILS_PATH);
+        }
+        // it's amazing that this still has to be coded up.
+        byte buffer[] = new byte[1024];
+        int count;
+        try {
+            while ((count = utils.read(buffer, 0, 1024)) > 0) {
+                outputStream.write(buffer, 0, count);
+            }
+        } catch (IOException e) {
+            throw new RuntimeException("Failed to write javascript utils to HTTP response.",
e);
+        }
+    }
 
     public void writeResponse(String fullQueryString, String ctx, EndpointInfo endpoint,
OutputStream os) {
+        URI uri = URI.create(fullQueryString);
+        String query = uri.getQuery();
+        Map<String, String> map = UrlUtilities.parseQueryString(query);
         OutputStreamWriter writer = new OutputStreamWriter(os, UTF8);
-        ServiceInfo serviceInfo = endpoint.getService();
-        Collection<SchemaInfo> schemata = serviceInfo.getSchemas();
-        BasicNameManager nameManager = new BasicNameManager(serviceInfo);
-        NamespacePrefixAccumulator prefixManager = new NamespacePrefixAccumulator(serviceInfo
-            .getXmlSchemaCollection());
-        try {
-            for (SchemaInfo schema : schemata) {
-                SchemaJavascriptBuilder builder = new SchemaJavascriptBuilder(serviceInfo
-                    .getXmlSchemaCollection(), prefixManager, nameManager, schema);
-                String allThatJavascript = builder.generateCodeForSchema(schema);
-                writer.append(allThatJavascript);
-            }
+        if (map.containsKey(UTILS_QUERY_KEY)) {
+            writeUtilsToResponseStream(os);
+        } else if (map.containsKey(CODE_QUERY_KEY)) {
+            ServiceInfo serviceInfo = endpoint.getService();
+            Collection<SchemaInfo> schemata = serviceInfo.getSchemas();
+            BasicNameManager nameManager = new BasicNameManager(serviceInfo);
+            NamespacePrefixAccumulator prefixManager = new NamespacePrefixAccumulator(serviceInfo
+                .getXmlSchemaCollection());
+            try {
+                for (SchemaInfo schema : schemata) {
+                    SchemaJavascriptBuilder builder = new SchemaJavascriptBuilder(serviceInfo
+                        .getXmlSchemaCollection(), prefixManager, nameManager, schema);
+                    String allThatJavascript = builder.generateCodeForSchema(schema);
+                    writer.append(allThatJavascript);
+                }
 
-            ServiceJavascriptBuilder serviceBuilder = new ServiceJavascriptBuilder(serviceInfo,
prefixManager,
-                                                                                   nameManager);
-            serviceBuilder.walk();
-            String serviceJavascript = serviceBuilder.getCode();
-            writer.append(serviceJavascript);
-        } catch (IOException e) {
-            throw new UncheckedException(e);
+                ServiceJavascriptBuilder serviceBuilder = new ServiceJavascriptBuilder(serviceInfo,
+                                                                                       prefixManager,
+                                                                                       nameManager);
+                serviceBuilder.walk();
+                String serviceJavascript = serviceBuilder.getCode();
+                writer.append(serviceJavascript);
+            } catch (IOException e) {
+                throw new UncheckedException(e);
+            }
+        } else {
+            throw new RuntimeException("Invalid query " + fullQueryString);
         }
-        
     }
 
     public boolean isRecognizedQuery(String fullQueryString, String ctx, EndpointInfo endpoint)
{

Modified: incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/QueryHandlerTest.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/QueryHandlerTest.java?rev=598320&r1=598319&r2=598320&view=diff
==============================================================================
--- incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/QueryHandlerTest.java
(original)
+++ incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/QueryHandlerTest.java
Mon Nov 26 07:53:12 2007
@@ -23,6 +23,7 @@
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.net.URL;
+import java.net.URLConnection;
 import java.nio.charset.Charset;
 import java.util.logging.Logger;
 
@@ -93,7 +94,9 @@
     public void dlbQueryTest() throws Exception {
         LOG.finest("logged to avoid warning on LOG");
         URL endpointURL = new URL(dlbEndpoint.getAddress()  + "?js");
-        InputStream jsStream = endpointURL.openStream();
+        URLConnection connection = endpointURL.openConnection();
+        assertEquals("application/javascript;charset=UTF-8", connection.getContentType());
+        InputStream jsStream = connection.getInputStream();
         InputStreamReader isr = new InputStreamReader(jsStream, UTF8);
         BufferedReader in = new BufferedReader(isr);
         String line = in.readLine();
@@ -108,5 +111,28 @@
             line = in.readLine();
         }
         assertNotSame(0, js.length());
+    }
+    
+    @Test
+    public void utilsTest() throws Exception {
+        URL endpointURL = new URL(dlbEndpoint.getAddress()  + "?jsutils");
+        URLConnection connection = endpointURL.openConnection();
+        assertEquals("application/javascript;charset=UTF-8", connection.getContentType());
+        InputStream jsStream = connection.getInputStream(); 
+        InputStreamReader isr = new InputStreamReader(jsStream, UTF8);
+        BufferedReader in = new BufferedReader(isr);
+        String line = in.readLine();
+        StringBuilder js = new StringBuilder();
+        while (line != null) {
+            String[] tok = line.split("\\s");
+
+            for (int x = 0; x < tok.length; x++) {
+                String token = tok[x];
+                js.append("  " + token);
+            }
+            line = in.readLine();
+        }
+        String jsString = js.toString();
+        assertTrue(jsString.contains("CxfApacheOrgUtil"));
     }
 }



Mime
View raw message