Author: keith
Date: Thu Feb  7 15:02:40 2008
New Revision: 13412

Log:

Adding support for getting a simple XML using the system object



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
==============================================================================
--- 
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
  Thu Feb  7 15:02:40 2008
@@ -16,6 +16,8 @@
 package org.wso2.mashup.hostobjects.system;
 
 import org.apache.axiom.om.util.UUIDGenerator;
+import org.apache.axiom.om.impl.builder.StAXOMBuilder;
+import org.apache.axiom.om.OMElement;
 import org.apache.axis2.AxisFault;
 import org.apache.axis2.context.ConfigurationContext;
 import org.apache.axis2.context.MessageContext;
@@ -40,16 +42,9 @@
 import org.wso2.utils.NetworkUtils;
 import org.wso2.wsas.ServerManager;
 
-import java.io.BufferedReader;
-import java.io.File;
-import java.io.FileReader;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.Reader;
-import java.net.MalformedURLException;
-import java.net.SocketException;
-import java.net.URI;
+import javax.xml.stream.XMLStreamException;
+import java.io.*;
+import java.net.*;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.HashMap;
@@ -728,4 +723,55 @@
             throw AxisFault.makeFault(e);
         }
     }
+
+    public static Object jsFunction_getXML(Context context, Scriptable 
thisObj, Object[] arguments,
+                                                Function funObj) throws 
MashupFault {
+        if (arguments.length != 1 || arguments[0] == null || !(arguments[0] 
instanceof String)) {
+            throw new MashupFault("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;
+        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) {
+                ByteArrayOutputStream bos = new ByteArrayOutputStream();
+                byte[] buffer = new byte[1024];
+                int c;
+                while ((c = in.read(buffer)) != -1) {
+                    bos.write(buffer, 0, c);
+                }
+                String str = new String(bos.toByteArray());
+                if (str.startsWith("<?")) {
+                    str = str.substring(str.indexOf("?>")+2);
+                }
+                Object[] objects = {str};
+                httpCon.disconnect();
+                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);
+            }
+        } catch (MalformedURLException e) {
+            throw new MashupFault(e);
+        } catch (IOException e) {
+            throw new MashupFault(e);
+        } 
+    }
 }

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

Reply via email to