Author: keith
Date: Wed Jun 25 09:14:41 2008
New Revision: 18638
URL: http://wso2.org/svn/browse/wso2?view=rev&revision=18638

Log:
Fixing system.getXML so that it uses the utility function to fetch XML. This 
means that we can fetch pages over https too


Modified:
   
trunk/mashup/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/system/SystemHostObject.java

Modified: 
trunk/mashup/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/system/SystemHostObject.java
URL: 
http://wso2.org/svn/browse/wso2/trunk/mashup/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/system/SystemHostObject.java?rev=18638&r1=18637&r2=18638&view=diff
==============================================================================
--- 
trunk/mashup/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/system/SystemHostObject.java
  (original)
+++ 
trunk/mashup/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/system/SystemHostObject.java
  Wed Jun 25 09:14:41 2008
@@ -27,6 +27,9 @@
 import org.apache.axis2.engine.AxisConfiguration;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.apache.commons.httpclient.HttpMethod;
+import org.apache.commons.httpclient.HttpStatus;
 import org.mozilla.javascript.Context;
 import org.mozilla.javascript.EvaluatorException;
 import org.mozilla.javascript.Function;
@@ -54,6 +57,7 @@
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.Reader;
+import java.io.ByteArrayInputStream;
 import java.net.HttpURLConnection;
 import java.net.MalformedURLException;
 import java.net.SocketException;
@@ -744,36 +748,19 @@
                     "The getXML function should be called with a single 
parameter which is the url to fetch XML from");
         }
         String urlString = (String) arguments[0];
-        URL url = null;
+        HttpMethod method = new GetMethod();
         try {
-            url = new URL(urlString);
-
-            HttpURLConnection httpCon;
-            URLConnection urlConnection = url.openConnection();
-            if (urlConnection instanceof HttpURLConnection) {
-                httpCon = (HttpURLConnection) urlConnection;
-            } else {
-                throw new MashupFault("getXML supports the https protocol 
only.");
-            }
-            httpCon.setDoOutput(true);
-            httpCon.setDoInput(true);
-            httpCon.setUseCaches(false);
-            httpCon.setRequestMethod("GET");
-            HttpURLConnection.setFollowRedirects(true);
-            httpCon.connect();
-            InputStream in = httpCon.getInputStream();
-            int code = httpCon.getResponseCode();
-            if (code == HttpURLConnection.HTTP_OK) {
-                StAXOMBuilder staxOMBuilder = new StAXOMBuilder(in);
-                OMElement omElement = staxOMBuilder.getDocumentElement();
-                Object[] objects = { omElement };
-                return context.newObject(thisObj, "XML", objects);
-            } else {
-                httpCon.disconnect();
-                throw new MashupFault("Could not get the content of " + 
urlString +
-                        " . The HTTP Code returned was " +
-                        code);
+            URL url = new URL(urlString);
+            int statusCode = MashupUtils.executeHTTPMethod(method, url);
+            if (statusCode != HttpStatus.SC_OK) {
+                    throw new MashupFault(
+                            "An error occured while getting the resource at " 
+ url +
+                                    ". Reason :" + method.getStatusLine());
             }
+            StAXOMBuilder staxOMBuilder = new StAXOMBuilder(new 
ByteArrayInputStream(method.getResponseBody()));
+            OMElement omElement = staxOMBuilder.getDocumentElement();
+            Object[] objects = { omElement };
+            return context.newObject(thisObj, "XML", objects);
         } catch (MalformedURLException e) {
             throw new MashupFault(e);
         } catch (IOException e) {
@@ -782,6 +769,9 @@
             throw new MashupFault("Could not get the convert the content of " 
+ urlString +
                     " to XML. You may have " +
                     "to use the scraper object to get this url and tidy it");
+        } finally {
+            // Release the connection.
+            method.releaseConnection();
         }
     }
 

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

Reply via email to