Author: keith
Date: Wed Mar 26 00:41:25 2008
New Revision: 15114

Log:

Potential fix for mashup-723. Getting proxy deyails from axis2.xml and setting 
it to the scraper object



Modified:
   
trunk/mashup/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/wsrequest/WSRequestHostImpl.java

Modified: 
trunk/mashup/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/wsrequest/WSRequestHostImpl.java
==============================================================================
--- 
trunk/mashup/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/wsrequest/WSRequestHostImpl.java
      (original)
+++ 
trunk/mashup/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/wsrequest/WSRequestHostImpl.java
      Wed Mar 26 00:41:25 2008
@@ -35,20 +35,23 @@
 import org.apache.axis2.util.JavaUtils;
 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
 import org.apache.commons.httpclient.cookie.CookiePolicy;
-import org.mozilla.javascript.Context;
-import org.mozilla.javascript.Function;
-import org.mozilla.javascript.NativeArray;
-import org.mozilla.javascript.Scriptable;
-import org.mozilla.javascript.ScriptableObject;
-import org.mozilla.javascript.Undefined;
-import org.mozilla.javascript.UniqueTag;
+import org.apache.neethi.Policy;
+import org.apache.rampart.RampartMessageData;
+import org.mozilla.javascript.*;
 import org.wso2.javascript.rhino.JavaScriptEngineConstants;
 import org.wso2.javascript.xmlimpl.XML;
+import org.wso2.mashup.MashupFault;
+import org.wso2.mashup.utils.MashupUtils;
 
 import javax.xml.namespace.QName;
 import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamReader;
+import java.io.File;
+import java.io.FileNotFoundException;
 import java.io.StringReader;
+import java.net.URL;
+import java.net.MalformedURLException;
 
 /**
  * <p/>
@@ -85,6 +88,8 @@
     //WebServiceError object in case the invocation result in an error
     WebServiceErrorHostObject error = null;
 
+    private boolean wsdlMode = false;
+
     /**
      * Constructor for the use by Rhino
      */
@@ -135,6 +140,7 @@
         // set true by default to use SOAP 1.2
         String useSOAP = "true";
         String useWSA = null;
+        String useWSS = null;
         String action = null;
         String httpLocation = null;
         String httpLocationIgnoreUncited = null;
@@ -143,6 +149,9 @@
         String httpContentEncoding = null;
         NativeArray optionsArray = null;
 
+        QName service = null;
+        String endpointName = null;
+
         WSRequestHostImpl wsRequest = checkInstance(thisObj);
         if (wsRequest.readyState > 0 && wsRequest.readyState < 4) {
             throw new Error("INVALID_STATE_EXCEPTION");
@@ -166,10 +175,25 @@
                     username = (String) arguments[3];
                 else
                     throw 
Context.reportRuntimeError("INVALID_SYNTAX_EXCEPTION");
+                if (arguments[2] instanceof Boolean)
+                    wsRequest.async = ((Boolean) arguments[2]).booleanValue();
+                else
+                    throw 
Context.reportRuntimeError("INVALID_SYNTAX_EXCEPTION");
                 break;
 
             case 4:
-                if (arguments[2] instanceof String) {
+                if (arguments[2] instanceof org.wso2.javascript.xmlimpl.QName) 
{
+                    org.wso2.javascript.xmlimpl.QName qName = 
(org.wso2.javascript.xmlimpl.QName) arguments[2];
+                    String uri = (String) qName.get("uri", qName);
+                    String localName = (String) qName.get("localName", qName);
+                    service = new QName(uri, localName);
+                    if (arguments[3] instanceof String)
+                        endpointName = (String) arguments[3];
+                    else
+                        throw 
Context.reportRuntimeError("INVALID_SYNTAX_EXCEPTION");
+                    wsRequest.wsdlMode = true;
+                }
+                else if (arguments[2] instanceof String) {
                     username = (String) arguments[2];
                     if (arguments[3] instanceof String)
                         passwd = (String) arguments[3];
@@ -216,6 +240,12 @@
                 useWSA = useWSAObject.toString();
             }
 
+            Object useWSSObject = optionsArray.get("useWSS", optionsArray);
+            if (useWSSObject != null && !(useWSSObject instanceof Undefined)
+                    && !(useWSSObject instanceof UniqueTag)) {
+                useWSS = useWSSObject.toString();
+            }
+
             Object actionObject = optionsArray.get("action", optionsArray);
             if (actionObject != null && !(actionObject instanceof Undefined)
                     && !(actionObject instanceof UniqueTag)) {
@@ -263,9 +293,25 @@
         Object object = 
cx.getThreadLocal(JavaScriptEngineConstants.AXIS2_MESSAGECONTEXT);
         if (!(object instanceof MessageContext)) {
             MessageContext messageContext = (MessageContext) object;
-            wsRequest.sender = new 
ServiceClient(messageContext.getConfigurationContext(), null);
+            if (wsRequest.wsdlMode) {
+                try {
+                    wsRequest.sender = new 
ServiceClient(messageContext.getConfigurationContext(), new URL(url), service, 
endpointName);
+                } catch (MalformedURLException e) {
+                    throw new MashupFault(e);
+                }
+            } else {
+                wsRequest.sender = new 
ServiceClient(messageContext.getConfigurationContext(), null);
+            }
         } else {
-            wsRequest.sender = new ServiceClient();
+            if (wsRequest.wsdlMode) {
+                try {
+                    wsRequest.sender = new ServiceClient(null, new URL(url), 
service, endpointName);
+                } catch (MalformedURLException e) {
+                    throw new MashupFault(e);
+                }
+            } else {
+                wsRequest.sender = new ServiceClient();
+            }
         }
         // Axis2 used to support sending of multiple messages with single
         // ServiceClient. Now it seems we need to set the following explicitly
@@ -277,7 +323,7 @@
         Options options = getOptionsObject(httpMethod, httpLocation, 
httpLocationIgnoreUncited,
                                            httpQueryParameterSeparator, 
httpInputSerialization,
                                            httpContentEncoding, url, username, 
passwd, useSOAP,
-                                           useWSA, action, optionsArray, 
wsRequest);
+                                           useWSA, useWSS, action, 
optionsArray, wsRequest);
         // Setting the cookie policy here
         options.setProperty(HTTPConstants.COOKIE_POLICY, 
CookiePolicy.IGNORE_COOKIES);
         wsRequest.sender.setOptions(options);
@@ -306,9 +352,28 @@
      * 
href="http://www.wso2.org/wiki/display/wsfajax/wsrequest_specification";>WSRequest
      * specification</a> for more details.
      */
-    public void jsFunction_send(Object payload) throws AxisFault {
+    public static void jsFunction_send(Context cx, Scriptable thisObj, 
Object[] arguments,
+                                       Function funObj) throws AxisFault {
+        WSRequestHostImpl wsRequest = (WSRequestHostImpl) thisObj;
+        Object payload = null;
+        QName operationName = null;
+        if (arguments.length == 1) {
+            payload = arguments[0];
+        } else if (arguments.length == 2) {
+            if (arguments[0] instanceof org.wso2.javascript.xmlimpl.QName) {
+                org.wso2.javascript.xmlimpl.QName qName = 
(org.wso2.javascript.xmlimpl.QName) arguments[0];
+                String uri = (String) qName.get("uri", qName);
+                String localName = (String) qName.get("localName", qName);
+                operationName = new QName(uri, localName);
+            } else {
+                throw new Error("INVALID_STATE_EXCEPTION");
+            }
+            payload = arguments[1];
+        } else {
+            throw new Error("INVALID_STATE_EXCEPTION");
+        }
         OMElement payloadElement = null;
-        if (this.readyState != 1)
+        if (wsRequest.readyState != 1)
             throw new Error("INVALID_STATE_EXCEPTION");
         if (payload instanceof String) {
             try {
@@ -345,30 +410,37 @@
         // }
 
         try {
-            if (async) { // asynchronous call to send()
-                AxisCallback callback = new 
WSRequestCallBack(Context.getCurrentContext(), this);
-                sender.sendReceiveNonBlocking(payloadElement, callback);
-                this.readyState = 2;
+            if (wsRequest.async) { // asynchronous call to send()
+                AxisCallback callback = new 
WSRequestCallBack(Context.getCurrentContext(), wsRequest);
+                if (wsRequest.wsdlMode) {
+                    wsRequest.sender.sendReceiveNonBlocking(operationName, 
payloadElement, callback);
+                } else {
+                    wsRequest.sender.sendReceiveNonBlocking(payloadElement, 
callback);
+                }
+                wsRequest.readyState = 2;
             } else { // synchronous call to send()
-                this.readyState = 2;
+                wsRequest.readyState = 2;
                 // TODO do we need to call onreadystatechange here too
-                this.responseXML = sender.sendReceive(payloadElement);
-                this.readyState = 4;
+                if (wsRequest.wsdlMode) {
+                    wsRequest.sender.sendReceive(operationName, 
payloadElement);
+                } else {
+                    wsRequest.responseXML = 
wsRequest.sender.sendReceive(payloadElement);
+                }
+                wsRequest.readyState = 4;
             }
             // Calling onreadystatechange function
-            if (this.onReadyStateChangeFunction != null) {
-                Context cx = Context.getCurrentContext();
-                this.onReadyStateChangeFunction.call(cx, this, this, new 
Object[0]);
+            if (wsRequest.onReadyStateChangeFunction != null) {
+                wsRequest.onReadyStateChangeFunction.call(cx, wsRequest, 
wsRequest, new Object[0]);
             }
         } catch (AxisFault e) {
-            error = new WebServiceErrorHostObject();
+            wsRequest.error = new WebServiceErrorHostObject();
             OMElement detail = e.getDetail();
             if (detail != null)
-                error.jsSet_details(detail.toString());
+                wsRequest.error.jsSet_details(detail.toString());
             QName faultCode = e.getFaultCode();
             if (faultCode != null)
-                error.jsSet_code(faultCode.toString());
-            error.jsSet_reason(e.getReason());
+                wsRequest.error.jsSet_code(faultCode.toString());
+            wsRequest.error.jsSet_reason(e.getReason());
             throw e;
         }
     }
@@ -487,7 +559,7 @@
                                             String httpInputSerialization,
                                             String httpContentEncodingString, 
String url,
                                             String username,
-                                            String passwd, String useSOAP, 
String useWSA,
+                                            String passwd, String useSOAP, 
String useWSA, String useWSS,
                                             String action, NativeArray 
optionsArray,
                                             WSRequestHostImpl wsRequest) 
throws AxisFault {
         Options options = new Options();
@@ -495,8 +567,23 @@
         EndpointReference targetEPR = new EndpointReference(url);
         options.setTo(targetEPR);
 
-        // handle basic authentication
-        if (username != null) { // set username if not null
+        if (useWSS != null && "true".equals(useWSS)) {
+            String wso2wsasHome = System.getProperty("wso2wsas.home");
+            try {
+                Policy policy = MashupUtils.loadPolicy(wso2wsasHome + 
File.separator +
+                        "conf" + File.separator + "rampart" + File.separator + 
"scenario1-policy.xml");
+                options.setProperty(RampartMessageData.KEY_RAMPART_POLICY, 
policy);
+            } catch (XMLStreamException e) {
+                throw new MashupFault("Cannot load policy file", e);
+            } catch (FileNotFoundException e) {
+                throw new MashupFault("Cannot load policy file", e);
+            }
+            options.setUserName(username);
+            options.setPassword(passwd);
+            wsRequest.sender.engageModule("rampart");
+        } else if (username != null) {
+            // handle basic authentication
+            // set username if not null
             Authenticator authenticator = new 
HttpTransportProperties.Authenticator();
             authenticator.setUsername(username);
             if (passwd != null) { // set password if present

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

Reply via email to