attached is the client.

Thanks!


On Fri, Apr 25, 2014 at 11:39 AM, Roshan Wijesena <[email protected]> wrote:

> can you attache your client also ..
>
>
> On Fri, Apr 25, 2014 at 11:14 AM, Supun Sethunga <[email protected]> wrote:
>
>> Hi,
>>
>> Im trying to secure a non-secured back-end service using a proxy. When i
>> create a secure proxy with policies from the ESB and execute the client, i
>> get the following exception.
>>
>>
>>
>>
>>
>>
>>
>> *Exception in thread "main" org.apache.axis2.AxisFault: Read timed out
>> at org.apache.axis2.AxisFault.makeFault(AxisFault.java:430)    at
>> org.apache.axis2.transport.http.HTTPSender.sendViaPost(HTTPSender.java:197)
>>     at
>> org.apache.axis2.transport.http.HTTPSender.send(HTTPSender.java:75)    at
>> org.apache.axis2.transport.http.CommonsHTTPTransportSender.writeMessageWithCommons(CommonsHTTPTransportSender.java:404)
>> at
>> org.apache.axis2.transport.http.CommonsHTTPTransportSender.invoke(CommonsHTTPTransportSender.java:231)
>>     at org.apache.axis2.engine.AxisEngine.send(AxisEngine.java:443)*
>>    ...
>>    ...
>>
>> For the testing purpose, when I create a simple pass through proxy and
>> execute the client, the sent message actually reaches the proxy with all
>> the security headers. (checked using ESB SOAP tracer). So I guess my client
>> works fine.
>>
>> But the SOAP tracer in ESB does not track the message sent when the
>> secure proxy (with policy applied) is used.
>>
>> Any idea on what causes this exception?
>>
>> I have attached the policy file herewith.
>>
>> Thanks,
>> Supun
>>
>> --
>> *Supun Sethunga*
>> Software Engineer
>> WSO2, Inc.
>> lean | enterprise | middleware
>> Mobile : +94 716546324
>>
>> _______________________________________________
>> Dev mailing list
>> [email protected]
>> http://wso2.org/cgi-bin/mailman/listinfo/dev
>>
>>
>
>
> --
> Best Regards,
>  Senior Software Engineer-WSO2 Inc.
> Roshan Wijesena
> Mobile: *+94752126789*
> Email: [email protected]
> *WSO2, Inc. :** wso2.com <http://wso2.com/>*
> lean.enterprise.middleware.
>



-- 
*Supun Sethunga*
Software Engineer
WSO2, Inc.
lean | enterprise | middleware
Mobile : +94 716546324
package client;

import org.apache.ws.security.WSPasswordCallback;

import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;

import java.io.IOException;

public class PWCBHandler implements CallbackHandler {

    public void handle(Callback[] callbacks) throws IOException,
            UnsupportedCallbackException { WSPasswordCallback pwcb = (WSPasswordCallback) callbacks[0];
            String id = pwcb.getIdentifier();
            int usage = pwcb.getUsage();

            if (usage == WSPasswordCallback.USERNAME_TOKEN) {

                if ("admin".equals(id)) {
//                if ("[email protected]".equals(id)) {
                    pwcb.setPassword("admin");
//                   pwcb.setPassword("admin123");
                }

            } else if (usage == WSPasswordCallback.SIGNATURE || usage == WSPasswordCallback.DECRYPT) {
                // Logic to get the private key password for signture or decryption
               /* if ("client".equals(id)) {
                    pwcb.setPassword("apache");
                }
                if ("service".equals(id)) {
                    pwcb.setPassword("apache");
                } */
                if ("wso2carbon".equals(id)) {
                    //pwcb.setPassword("wso2carbon");
                    pwcb.setPassword("wso2carbon");
                }
                if ("clientks".equals(id)) {
                    pwcb.setPassword("clientks");
                }
                if ("serviceks".equals(id)) {
                    pwcb.setPassword("serviceks");
                }
            }
    }

}
package client;

import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.Properties;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.impl.builder.StAXOMBuilder;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.ConfigurationContextFactory;
import org.apache.neethi.Policy;
import org.apache.neethi.PolicyEngine;
import org.apache.rampart.RampartMessageData;
import org.apache.rampart.policy.model.CryptoConfig;
import org.apache.rampart.policy.model.RampartConfig;

public class SecureOrderProcessClient {
	
	public static void main(String args[]) throws Exception{
		
		String clientRepo="/home/supun/workspace/SecureOrderProcessClient/clientRepo";
		String clientKey ="/home/supun/workspace/SecureOrderProcessClient/clientks.jks";
		String securityPolicy = "/home/supun/workspace/SecureOrderProcessClient/policy.xml";
		String trustStore = "/home/supun/workspace/SecureOrderProcessClient/clientks.jks";
		String endpoint ="http://Supun:8280/services/SecureProxy2?wsdl";;
	
		ConfigurationContext ctx = ConfigurationContextFactory.createConfigurationContextFromFileSystem(clientRepo, null);
		
		SecureOrderProcessServiceStub stub = new SecureOrderProcessServiceStub(ctx, endpoint);

		System.setProperty("javax.net.ssl.trustStore", trustStore);
        System.setProperty("javax.net.ssl.trustStorePassword", "clientks");
        
        //ServiceClient sc = new ServiceClient(ctx, null);
        ServiceClient sc = stub._getServiceClient();
        sc.engageModule("rampart");
        sc.engageModule("addressing");
        
        Options opts = new Options();
        opts.setTo(new EndpointReference(endpoint));
        opts.setAction("urn:viewOrder");
        opts.setAction("urn:makeOrder");
        
        opts.setProperty(RampartMessageData.KEY_RAMPART_POLICY, loadPolicy(securityPolicy,clientKey));
        
        sc.setOptions(opts);

        //viewOrder("0005", stub);
    	makeOrder(stub,sc);
	}
	
	
	public static Policy loadPolicy(String xmlPath , String clientKey) throws Exception {

        StAXOMBuilder builder = new StAXOMBuilder(xmlPath);
        Policy policy = PolicyEngine.getPolicy(builder.getDocumentElement());

        RampartConfig rc = new RampartConfig();

        rc.setUser("admin");
//        rc.setUserCertAlias("wso2carbon");
        rc.setUserCertAlias("clientks");
//        rc.setEncryptionUser("wso2carbon");
        rc.setEncryptionUser("serviceks");
        rc.setPwCbClass("client.PWCBHandler");

        CryptoConfig sigCryptoConfig = new CryptoConfig();
        sigCryptoConfig.setProvider("org.apache.ws.security.components.crypto.Merlin");

        Properties prop1 = new Properties();
        prop1.put("org.apache.ws.security.crypto.merlin.keystore.type", "JKS");
        prop1.put("org.apache.ws.security.crypto.merlin.file", clientKey);
//        prop1.put("org.apache.ws.security.crypto.merlin.keystore.password", "wso2carbon");
        prop1.put("org.apache.ws.security.crypto.merlin.keystore.password", "clientks");
        sigCryptoConfig.setProp(prop1);

        CryptoConfig encrCryptoConfig = new CryptoConfig();
        encrCryptoConfig.setProvider("org.apache.ws.security.components.crypto.Merlin");

        Properties prop2 = new Properties();
        prop2.put("org.apache.ws.security.crypto.merlin.keystore.type", "JKS");
        prop2.put("org.apache.ws.security.crypto.merlin.file", clientKey);
//        prop2.put("org.apache.ws.security.crypto.merlin.keystore.password", "wso2carbon");
        prop2.put("org.apache.ws.security.crypto.merlin.keystore.password", "clientks");
        encrCryptoConfig.setProp(prop2);

        rc.setSigCryptoConfig(sigCryptoConfig);
        rc.setEncrCryptoConfig(encrCryptoConfig);

        policy.addAssertion(rc);
        
        System.out.println(policy);
        return policy;
    }
	
		
    public static void makeOrder(SecureOrderProcessServiceStub stub,ServiceClient sc) throws RemoteException {
    		
    		//create the new order
    		HashMap<String, Integer> order = new HashMap<>();
    		
    		order.put("Large Pizza - chicken", 7);
    		order.put("Cocacola - 1.5l", 1);
    		order.put("French Fries", 3);
    		
    		OMFactory fac = OMAbstractFactory.getOMFactory();
    		OMNamespace omNs = fac.createOMNamespace("http://service";, "ns");
    		OMElement method = fac.createOMElement("makeOrder", omNs);

    		//create the element using above order, to be sent to the OrderProcessor
    		for (String key : order.keySet()) {
        		OMElement item = fac.createOMElement("item", omNs);    
        		OMElement itemId = fac.createOMElement("itemId", omNs);
        		OMElement quantity = fac.createOMElement("quantity", omNs);
        		
        		itemId.addChild(fac.createOMText(itemId, key));
        		item.addChild(itemId);    
        		
        		quantity.addChild(fac.createOMText(quantity, "" + order.get(key)));
        		item.addChild(quantity);    
        		method.addChild(item);    		
        	}
    		
    		System.out.println(method);

    		//sends the order to the sever and gets the response
    		//OMElement result = sc.sendReceive(method);
    		OMElement result = stub.makeOrder(method);
    		
    		//extract the order code from the received response to display
    		OMElement orderIdElement = result.getFirstElement().getFirstElement();
        	String orderId = orderIdElement.getText();
        	System.out.println("New Order added Successfuly. Your Order Code: "+orderId);

    	}
    	
    	
    	public static void viewOrder(String id, SecureOrderProcessServiceStub stub) throws RemoteException{
    		OMFactory fac = OMAbstractFactory.getOMFactory();
    		OMNamespace omNs = fac.createOMNamespace("orderprocessorns", "ns");
    		OMElement method = fac.createOMElement("viewOrder", omNs);
    		OMElement orderId = fac.createOMElement("orderId", omNs);
    		
    		orderId.addChild(fac.createOMText(orderId, id));
    		method.addChild(orderId);

    		//send the order id element to the server, and get the order details as the response
    		OMElement orderInfo = stub.viewOrder(method);
    		
    		//prints the received order details
    		printOrder(orderInfo,id);
    	}
    	
    	
    	public static void printOrder(OMElement element, String s) {
    		
    		/* prints the details of items in an order
    		 * 
    		 * If a invalid/non-existing order is given to print, a error message is printed	
    		 */
    		
    		element.build();

    		if (((OMElement) element.getFirstElement().getFirstElement()) != null) {
        
        		OMElement orderItem = element.getFirstElement().getFirstElement();
        		OMElement itemElement;
        		OMElement quantityElement;
        		
        		System.out.println("\n************ Oder Details ************\n");
        		System.out.println("OrderCode : "+ s);
        		System.out.println("\nItem\t\t\t\tAmount\n");
        
        		do {
            		itemElement = (OMElement) orderItem.getFirstOMChild();
            		quantityElement = (OMElement) itemElement.getNextOMSibling();
            		System.out.println(itemElement.getText() + " \t\t "
            		+ Integer.parseInt(quantityElement.getText()));
        		} 
        		while ((orderItem = (OMElement) orderItem.getNextOMSibling()) != null);
        	} 
    		else {
        		System.out.println("No such order found! Please check the order id again.");
    		}
    	}
    }
_______________________________________________
Dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/dev

Reply via email to