/*
 * Copyright 2004,2005 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.rampart.samples.policy.sample09;

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.OMXMLBuilderFactory;
import org.apache.axiom.om.OMXMLParserWrapper;
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 java.io.FileInputStream;

public class Client {

    public static void main(String[] args) throws Exception {
        
        if(args.length != 3) {
            System.out.println("Usage: $java Client endpoint_address client_repo_path policy_xml_path");
        }
        
        ConfigurationContext ctx = ConfigurationContextFactory.createConfigurationContextFromFileSystem(args[1], null);
        
        ServiceClient client = new ServiceClient(ctx, null);
        Options options = new Options();
        options.setAction("urn:echo");
        options.setTo(new EndpointReference(args[0]));
        //load and attach in/out sec policies
        String policyPath= args[2].split("/")[0];
        options.setProperty(RampartMessageData.KEY_RAMPART_IN_POLICY, loadPolicy(policyPath + "/client_in_policy.xml"));
        options.setProperty(RampartMessageData.KEY_RAMPART_OUT_POLICY, loadPolicy(policyPath + "/client_out_policy.xml"));
        client.setOptions(options);
        
        client.engageModule("addressing");
        client.engageModule("rampart");

        OMElement response = client.sendReceive(getPayload("Hello world"));
        
        System.out.println(response);
        
    }
    
    private static Policy loadPolicy(String xmlPath) throws Exception {
        OMXMLParserWrapper builder = OMXMLBuilderFactory.createOMBuilder(new FileInputStream(xmlPath));
        return PolicyEngine.getPolicy(builder.getDocumentElement());
    }
    
    private static OMElement getPayload(String value) {
        OMFactory factory = OMAbstractFactory.getOMFactory();
        OMNamespace ns = factory.createOMNamespace("http://sample09.policy.samples.rampart.apache.org","ns1");
        OMElement elem = factory.createOMElement("echo", ns);
        OMElement childElem = factory.createOMElement("param0", ns);
        childElem.setText(value);
        elem.addChild(childElem);
        
        return elem;
    }
    
}

@@ -1299,16 +1299,37 @@
 eData;%0A%0A
+import java.io.File;%0A
 import j
@@ -1352,16 +1352,16 @@
 Stream;%0A
-
 %0Apublic 
@@ -1981,22 +1981,20 @@
         
-String
+File
  policyP
@@ -2002,29 +2002,41 @@
 th= 
-args%5B2%5D.split(%22/%22)%5B0%5D
+new File(args%5B2%5D).getParentFile()
 ;%0A  
@@ -2106,32 +2106,41 @@
 ICY, loadPolicy(
+new File(
 policyPath + %22/c
@@ -2129,29 +2129,27 @@
 e(policyPath
- +
+,
  %22
-/
 client_in_po
@@ -2151,32 +2151,33 @@
 in_policy.xml%22))
+)
 ;%0A        option
@@ -2244,16 +2244,25 @@
 dPolicy(
+new File(
 policyPa
@@ -2267,10 +2267,9 @@
 Path
- +
+,
  %22/c
@@ -2291,16 +2291,17 @@
 y.xml%22))
+)
 ;%0A      
@@ -2603,22 +2603,20 @@
 dPolicy(
-String
+File
  xmlPath

Fix logic error.