Author: keith
Date: Tue Jun 24 23:31:35 2008
New Revision: 18613
URL: http://wso2.org/svn/browse/wso2?view=rev&revision=18613

Log:
Moving the code of executing the GET request to a utility class so that others 
can use it too


Modified:
   trunk/mashup/java/modules/core/src/org/wso2/mashup/utils/MashupUtils.java
   
trunk/mashup/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/feed/FeedReader.java

Modified: 
trunk/mashup/java/modules/core/src/org/wso2/mashup/utils/MashupUtils.java
URL: 
http://wso2.org/svn/browse/wso2/trunk/mashup/java/modules/core/src/org/wso2/mashup/utils/MashupUtils.java?rev=18613&r1=18612&r2=18613&view=diff
==============================================================================
--- trunk/mashup/java/modules/core/src/org/wso2/mashup/utils/MashupUtils.java   
(original)
+++ trunk/mashup/java/modules/core/src/org/wso2/mashup/utils/MashupUtils.java   
Tue Jun 24 23:31:35 2008
@@ -24,6 +24,7 @@
 import org.apache.axis2.client.ServiceClient;
 import org.apache.axis2.context.ConfigurationContext;
 import org.apache.axis2.context.ConfigurationContextFactory;
+import org.apache.axis2.context.MessageContext;
 import org.apache.axis2.deployment.DeploymentEngine;
 import org.apache.axis2.deployment.DeploymentException;
 import org.apache.axis2.deployment.DescriptionBuilder;
@@ -39,6 +40,16 @@
 import org.apache.axis2.util.JavaUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HostConfiguration;
+import org.apache.commons.httpclient.HttpStatus;
+import org.apache.commons.httpclient.HttpException;
+import org.apache.commons.httpclient.HttpMethod;
+import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
+import org.apache.commons.httpclient.protocol.Protocol;
+import org.apache.commons.httpclient.cookie.CookiePolicy;
 import org.apache.neethi.Policy;
 import org.apache.neethi.PolicyEngine;
 import org.apache.rampart.RampartMessageData;
@@ -1288,4 +1299,60 @@
         is.close();
         return bytes;
     }
+
+    /**
+     * Given a HTTPMethod and a target this utility method fetches the 
resource for you. It will
+     * take care of setting the user keyStore and stuff when the url is https.
+     * @param method The HTTP Method instance
+     * @param targetURL - The target URL to invoke
+     * @return The statusCode as an int
+     * @throws IOException Thrown in case any exception occurrs
+     */
+    public static int executeHTTPMethod(HttpMethod method, URL targetURL) 
throws IOException {
+
+        MultiThreadedHttpConnectionManager connectionManager =
+                new MultiThreadedHttpConnectionManager();
+        HttpClient httpClient = new HttpClient(connectionManager);
+        // We should not use method.setURI and set the complete URI here.
+        // If we do so commons-httpclient will not use our custom socket 
factory.
+        // Hence we set the path and query separatly
+        method.setPath(targetURL.getPath());
+        method.setQueryString(targetURL.getQuery());
+        method.setRequestHeader(HTTPConstants.HEADER_HOST, 
targetURL.getHost());
+        method.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
+        HostConfiguration config = new HostConfiguration();
+
+        int port = targetURL.getPort();
+        String targetProtocol = targetURL.getProtocol();
+        if (ServerConstants.HTTPS_TRANSPORT.equals(targetProtocol)) {
+            // If the feed is on https then we have to make sure we set the 
users keystore
+            // details into httpClient when making the call so that all 
sslHandshake stuff
+            // will go amoothly
+            MessageContext currentMessageContext =
+                    MessageContext.getCurrentMessageContext();
+
+            // In order to get the users keystore details we need his 
username. We stick the
+            // username as a parameter into the service at deployment time
+            String username =
+                    (String) 
currentMessageContext.getAxisService().getParameterValue(
+                            MashupConstants.MASHUP_AUTHOR_NAME);
+            ProtocolSocketFactory psf = new CustomProtocolSocketFactory(
+                    MashupUtils.getUserKeystoreResource(username));
+
+            // Check weather the url has a port stated explicitly. If its not 
present
+            // default to 443
+            if (port == -1) {
+                port = 443;
+            }
+            Protocol protocol = new Protocol("custom-https", psf, port);
+            config.setHost(targetURL.getHost(), port, protocol);
+        } else {
+            if (port == -1) {
+                port = 80;
+            }
+            // Execute the method.
+            config.setHost(targetURL.getHost(), port, targetProtocol);
+        }
+        return httpClient.executeMethod(config, method);
+    }
 }
\ No newline at end of file

Modified: 
trunk/mashup/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/feed/FeedReader.java
URL: 
http://wso2.org/svn/browse/wso2/trunk/mashup/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/feed/FeedReader.java?rev=18613&r1=18612&r2=18613&view=diff
==============================================================================
--- 
trunk/mashup/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/feed/FeedReader.java
  (original)
+++ 
trunk/mashup/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/feed/FeedReader.java
  Tue Jun 24 23:31:35 2008
@@ -20,26 +20,16 @@
 import com.sun.syndication.io.FeedException;
 import com.sun.syndication.io.SyndFeedInput;
 import com.sun.syndication.io.XmlReader;
-import org.apache.axis2.transport.http.HTTPConstants;
-import org.apache.axis2.context.MessageContext;
-import org.apache.commons.httpclient.HttpClient;
-import org.apache.commons.httpclient.HttpStatus;
 import org.apache.commons.httpclient.HttpException;
-import org.apache.commons.httpclient.HostConfiguration;
-import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
-import org.apache.commons.httpclient.cookie.CookiePolicy;
-import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
-import org.apache.commons.httpclient.protocol.Protocol;
+import org.apache.commons.httpclient.HttpMethod;
+import org.apache.commons.httpclient.HttpStatus;
 import org.apache.commons.httpclient.methods.GetMethod;
 import org.mozilla.javascript.Context;
 import org.mozilla.javascript.Function;
 import org.mozilla.javascript.Scriptable;
 import org.mozilla.javascript.ScriptableObject;
-import org.wso2.mashup.utils.CustomProtocolSocketFactory;
-import org.wso2.mashup.utils.MashupUtils;
-import org.wso2.mashup.MashupConstants;
 import org.wso2.mashup.MashupFault;
-import org.wso2.wsas.ServerConstants;
+import org.wso2.mashup.utils.MashupUtils;
 
 import javax.net.ssl.SSLHandshakeException;
 import java.io.IOException;
@@ -80,80 +70,25 @@
      */
     public static Feed jsFunction_get(Context cx, Scriptable thisObj, Object[] 
arguments,
                                       Function funObj) throws IOException {
-        Feed retFeed = null;
-
+        Feed retFeed;
         FeedReader feedReaderObject = (FeedReader) thisObj;
-
+        GetMethod method = new GetMethod();
         if (arguments[0] instanceof String) {
-            int statusCode;
             String feedUrl = (String) arguments[0];
-
-            // Create a method instance.
-            GetMethod method = new GetMethod();
-
-            MultiThreadedHttpConnectionManager connectionManager =
-                new MultiThreadedHttpConnectionManager();
-            HttpClient httpClient = new HttpClient(connectionManager);
-
+            URL targetURL = new URL(feedUrl);
             try {
-                URL targetURL = new URL(feedUrl);
-
-                // We should not use method.setURI and set the complete URI 
here.
-                // If we do so commons-httpclient will not use our custom 
socket factory.
-                // Hence we set the path and query separatly
-                method.setPath(targetURL.getPath());
-                method.setQueryString(targetURL.getQuery());
-                method.setRequestHeader(HTTPConstants.HEADER_HOST, 
targetURL.getHost());
-                
method.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);   
-                HostConfiguration config = new HostConfiguration();
-
-                int port = targetURL.getPort();
-                String targetProtocol = targetURL.getProtocol();
-                if (ServerConstants.HTTPS_TRANSPORT.equals(targetProtocol)) {
-
-                    // If the feed is on https then we have to make sure we 
set the users keystore
-                    // details into httpClient when making the call so that 
all sslHandshake stuff
-                    // will go amoothly
-                    MessageContext currentMessageContext =
-                            MessageContext.getCurrentMessageContext();
-
-                    // In order to get the users keystore details we need his 
username. We stick the
-                    // username as a parameter into the service at deployment 
time
-                    String username =
-                            (String) 
currentMessageContext.getAxisService().getParameterValue(
-                                    MashupConstants.MASHUP_AUTHOR_NAME);
-                    ProtocolSocketFactory psf = new 
CustomProtocolSocketFactory(
-                            MashupUtils.getUserKeystoreResource(username));
-
-                    // Check weather the url has a port stated explicitly. If 
its not present
-                    // default to 443
-                    if (port == -1) {
-                        port = 443;
-                    }
-                    Protocol protocol = new Protocol("custom-https", psf, 
port);
-                    config.setHost(targetURL.getHost(), port, protocol);
-                } else {
-                    if (port == -1) {
-                        port = 80;
-                    }
-                    // Execute the method.
-                    config.setHost(targetURL.getHost(), port, targetProtocol);
-                }
-                statusCode = httpClient.executeMethod(config, method);
-
+                int statusCode = MashupUtils.executeHTTPMethod(method, 
targetURL);
                 if (statusCode != HttpStatus.SC_OK) {
-                    throw new MashupFault("An error occured while getting the 
feed:" +
-                            method.getStatusLine());
+                    throw new MashupFault(
+                            "An error occured while getting the feed at " + 
targetURL +
+                                    ". Reason :" +
+                                    method.getStatusLine());
                 }
-
-                // Read the response body as a stream.
                 InputStream response = method.getResponseBodyAsStream();
-
                 SyndFeedInput input = new SyndFeedInput();
                 SyndFeed feed = input.build(new XmlReader(response));
                 retFeed = (Feed) cx.newObject(feedReaderObject, "Feed", new 
Object[0]);
                 retFeed.setFeed(feed);
-
             } catch (SSLHandshakeException e) {
                 if 
(e.getMessage().indexOf("sun.security.validator.ValidatorException: PKIX path 
building failed") > -1) {
                     throw new MashupFault("An error occured while getting the 
feed since a " +
@@ -168,15 +103,13 @@
                 throw new MashupFault("Fatal protocol violation: " + 
e.getMessage(), e);
             } catch (IOException e) {
                 throw new MashupFault("Fatal transport error: " + 
e.getMessage(), e);
-            } finally {
+            }  finally {
                 // Release the connection.
                 method.releaseConnection();
             }
         } else {
             throw new MashupFault("The parameter must be a string representing 
the Feeds URL");
         }
-
         return retFeed;
-
     }
 }

_______________________________________________
Mashup-dev mailing list
[email protected]
http://www.wso2.org/cgi-bin/mailman/listinfo/mashup-dev

Reply via email to