Author: keith
Date: Mon Apr 28 02:43:33 2008
New Revision: 16251

Log:

Fixing WSRequest Host Object to take care of security configurations off policy


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
      Mon Apr 28 02:43:33 2008
@@ -30,16 +30,24 @@
 import org.apache.axis2.context.ConfigurationContext;
 import org.apache.axis2.context.ConfigurationContextFactory;
 import org.apache.axis2.context.MessageContext;
+import org.apache.axis2.description.AxisOperation;
 import org.apache.axis2.description.WSDL2Constants;
 import org.apache.axis2.transport.http.HTTPConstants;
 import org.apache.axis2.transport.http.HttpTransportProperties;
 import org.apache.axis2.transport.http.HttpTransportProperties.Authenticator;
 import org.apache.axis2.util.JavaUtils;
 import org.apache.axis2.util.XMLUtils;
+import org.apache.axis2.wsdl.WSDLConstants;
 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
 import org.apache.commons.httpclient.cookie.CookiePolicy;
 import org.apache.neethi.Policy;
 import org.apache.rampart.RampartMessageData;
+import org.apache.rampart.policy.RampartPolicyBuilder;
+import org.apache.rampart.policy.RampartPolicyData;
+import org.apache.ws.secpolicy.WSSPolicyException;
+import org.apache.ws.secpolicy.model.SupportingToken;
+import org.apache.ws.secpolicy.model.Token;
+import org.apache.ws.secpolicy.model.UsernameToken;
 import org.mozilla.javascript.Context;
 import org.mozilla.javascript.Function;
 import org.mozilla.javascript.NativeArray;
@@ -51,6 +59,7 @@
 import org.wso2.javascript.rhino.JavaScriptEngineConstants;
 import org.wso2.javascript.xmlimpl.XML;
 import org.wso2.mashup.MashupFault;
+import org.wso2.mashup.MashupConstants;
 import org.wso2.mashup.utils.MashupUtils;
 
 import javax.wsdl.Definition;
@@ -75,6 +84,7 @@
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.ArrayList;
 
 /**
  * <p/>
@@ -347,6 +357,7 @@
             wsRequest.reset();
         }
 
+        NativeArray optionsArray = null;
         String wsdlURL;
         QName serviceQName = null;
         String endpointName = null;
@@ -366,7 +377,21 @@
                     throw 
Context.reportRuntimeError("INVALID_SYNTAX_EXCEPTION");
                 break;
             case 3:
-                throw Context.reportRuntimeError("INVALID_SYNTAX_EXCEPTION");
+                if (arguments[0] instanceof String)
+                    wsdlURL = (String) arguments[0];
+                else
+                    throw 
Context.reportRuntimeError("INVALID_SYNTAX_EXCEPTION");
+                if (arguments[1] instanceof Boolean)
+                    wsRequest.async = ((Boolean) arguments[1]).booleanValue();
+                else
+                    throw 
Context.reportRuntimeError("INVALID_SYNTAX_EXCEPTION");
+                if (arguments[2] instanceof NativeArray) {
+                    optionsArray = (NativeArray) arguments[2];
+
+                }
+                else
+                    throw 
Context.reportRuntimeError("INVALID_SYNTAX_EXCEPTION");
+                break;
             case 4:
                 if (arguments[1] instanceof org.wso2.javascript.xmlimpl.QName) 
{
                     org.wso2.javascript.xmlimpl.QName qName =
@@ -485,11 +510,34 @@
 
                 }
             }
-            ConfigurationContext defaultConfigurationContext =
-                        
ConfigurationContextFactory.createDefaultConfigurationContext();
+            String wso2wsasHome = 
System.getProperty(MashupConstants.WSO2WSAS_HOME);
+            ConfigurationContext configurationContext =
+                        
ConfigurationContextFactory.createConfigurationContextFromFileSystem(
+                                wso2wsasHome + File.separator + 
MashupConstants.WORK_DIRECTORY +
+                                        File.separator + 
MashupConstants.REPO_DIRECTORY);
             wsRequest.sender =
-                        new ServiceClient(defaultConfigurationContext, 
definition, serviceQName,
+                        new ServiceClient(configurationContext, definition, 
serviceQName,
                                           endpointName);
+            if (optionsArray != null) {
+                Object usernameObject =
+                        optionsArray.get("username", optionsArray);
+                Options options = wsRequest.sender.getOptions();
+                if (usernameObject != null &&
+                        !(usernameObject instanceof Undefined)
+                        && !(usernameObject instanceof UniqueTag)) {
+                    String username = usernameObject.toString();
+                    options.setUserName(username);
+                }
+
+                Object passwordObject =
+                        optionsArray.get("username", optionsArray);
+                if (passwordObject != null &&
+                        !(passwordObject instanceof Undefined)
+                        && !(passwordObject instanceof UniqueTag)) {
+                    String password = passwordObject.toString();
+                    options.setPassword(password);
+                }
+            }
             wsRequest.targetNamespace = definition.getTargetNamespace();
             } catch (MalformedURLException e) {
                 throw new MashupFault(e);
@@ -624,6 +672,42 @@
                 wsRequest.readyState = 2;
                 // TODO do we need to call onreadystatechange here too
                 if (wsRequest.wsdlMode) {
+                    boolean needRampart = false;
+                    AxisOperation axisOperation =
+                            (AxisOperation) wsRequest.sender.getAxisService()
+                                    .getChild(operationName);
+                    Policy policy = axisOperation.getMessage(
+                            
WSDLConstants.MESSAGE_LABEL_IN_VALUE).getEffectivePolicy();
+                    List it = (List) policy.getAlternatives().next();
+
+                    //Process policy and build policy data
+                    RampartPolicyData policyData = 
RampartPolicyBuilder.build(it);
+                    SupportingToken supportingTokens = 
policyData.getSignedSupportingTokens();
+                    if (policyData.isTransportBinding()) {
+                        if (supportingTokens != null && 
supportingTokens.getTokens() != null &&
+                                supportingTokens.getTokens().size() > 0) {
+
+//                            log.debug("Processing signed supporting tokens");
+
+                            ArrayList tokens = supportingTokens.getTokens();
+                            for (Iterator iter = tokens.iterator(); 
iter.hasNext();) {
+
+                                Token token = (Token) iter.next();
+                                if (token instanceof UsernameToken) {
+                                    needRampart = true;
+                                } else {
+                                    throw new 
MashupFault("unsupportedSignedSupportingToken " +
+                                                               "{" + 
token.getName()
+                                                                       
.getNamespaceURI()
+                                                                       + "}" + 
token.getName()
+                                                                       
.getLocalPart());
+                                }
+                            }
+                        }
+                    }
+                    if (needRampart) {
+                        wsRequest.sender.engageModule("rampart");
+                    }
                     wsRequest.responseXML = 
wsRequest.sender.sendReceive(operationName, payloadElement);
                 } else {
                     wsRequest.responseXML = 
wsRequest.sender.sendReceive(payloadElement);
@@ -644,6 +728,10 @@
                 wsRequest.error.jsSet_code(faultCode.toString());
             wsRequest.error.jsSet_reason(e.getReason());
             throw e;
+        } catch (WSSPolicyException e) {
+            wsRequest.error = new WebServiceErrorHostObject();
+            wsRequest.error.jsSet_details(e.getMessage());
+            throw AxisFault.makeFault(e);
         }
     }
 

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

Reply via email to