Archive

Archive for the ‘Java’ Category

Use subversion with both eclipse and cli

If you use both eclipse client and cli client, you need to choose the eclipse svn connector version according to your command line client otherwise ,in the acse that your cli client is older than your eclipse svn connector, you can have an error like this : “svn: This client is too old to work with working copy” or if your svn connector is older than your subversion cli client some option could not be present like share project by using the project svn setting´╗┐.

If you use both eclipse svn connector and subversion cli client, you need to choose the svn connector version according to your command line client otherwise you can have an error like this :
or if your svn connector is older than your subversion cli client some option could not be present like share project by using the project svn setting
Advertisements
Categories: Java, Linux, Tips

Write xml file with indentation

November 12, 2009 Leave a comment

We are going to see how can transform an org.w3c.dom.Document object in an xml file.

public void writeXmlFile(Document doc, String filename)
			throws TransformerFactoryConfigurationError, TransformerException {
		Source source = new DOMSource(doc);
		// prepare output
		File xmlFile = new File( filename);
		Result result = new StreamResult(xmlFile);

		// write file
		Transformer transform = TransformerFactory.newInstance()
				.newTransformer();
                //indent the xml content
		transform.setOutputProperty(OutputKeys.INDENT, "yes");
		transform.transform(source, result);
	}

The indentation is done with this line by setting to yes the property “OutputKeys.INDENT”

//indent the xml content
transform.setOutputProperty(OutputKeys.INDENT, "yes");
Categories: Java

Create axis2 client with ws-security

September 30, 2009 Leave a comment

You need to download the rampart module.

public class HelloworldClient {

    /**
     * Absolute value of the axis2 configuration path This path must have a
     * directory named "modules" which contains rampart
     */
    private String axis2ConfPath;

    /**
     *
     * @param args
     */
    public static void main(String[] args) {
        String clientAxis2Path = null;
        String user = null;
        if (args == null | args.length == 0) {
            System.err.println("Missing arguments");
            System.exit(0);
        }
        clientAxis2Path = args[0];
        user = args[1];

        String action = "http://petals.ow2.org/helloworld/sayHello";

        HelloworldClient client = new HelloworldClient(clientAxis2Path);
        SOAPEnvelope result;

        System.out.println("UnSecure WS call...");
        result = client.callSayHello(false, user, action);
        if (result == null) {
            System.out.println("\n" + "SUCCESS !!!");
        } else {
            System.out.println("ERROR : An error was expected");
        }

        System.out.println("Secure WS call...");
        result = client.callSayHello(true, user, action);

        if (result != null) {
            System.out.println("RESPONSE " + "\n" + result.toString());
            System.out.println("SUCCESS !!!");
        } else {
            System.out.println("NO RESULT RETURNED");
        }
    }

    /**
     * Creates a new instance of {@link HelloworldClient}
     *
     */
    public HelloworldClient(String path) {
        this.axis2ConfPath = path;
    }

    /**
     * Call the sayHello operation
     *
     * @param secure
     *            call in secure mode or not
     * @return
     */
    public SOAPEnvelope callSayHello(boolean secure, String user, String action) {
        SOAPEnvelope responseEnvelope = null;
        EndpointReference targetEPR = new EndpointReference(
                "http://127.0.0.1:8084/petals/services/UserPasswordSecuredService");

        String sayHelloStr = "Helloworld secured service";
        try {

            ConfigurationContext ctx = ConfigurationContextFactory
                    .createConfigurationContextFromFileSystem(this.axis2ConfPath, null);

            ServiceClient client = new ServiceClient(ctx, null);

            Options options = new Options();

            options.setTo(targetEPR);
            options.setAction(action);
            client.setOptions(options);

            client.engageModule("rampart");

            MessageContext msgContext = new MessageContext();
            SOAPEnvelope envelope = this.createWssSoapEnveloppe(sayHelloStr, secure,user,"linuxRocks");
            msgContext.setEnvelope(envelope);

            OperationClient opClient = client.createClient(ServiceClient.ANON_OUT_IN_OP);
            opClient.addMessageContext(msgContext);
            opClient.execute(true);

            MessageContext responseMsgCtx = opClient
                    .getMessageContext(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
            responseEnvelope = responseMsgCtx.getEnvelope();

        } catch (Exception e) {
            System.err.println(" ERROR : " + e.getMessage());
        }
        return responseEnvelope;
    }

    /**
     * Build the message payload
     *
     * @param data
     * @return
     */
    private OMElement getSayHelloOMElement(String data) {
        OMFactory fac = OMAbstractFactory.getOMFactory();
        OMNamespace omNs = fac.createOMNamespace("http://petals.ow2.org/helloworld", "hel");
        OMElement method = fac.createOMElement("sayHello", omNs);
        method.addChild(fac.createOMText(method, data));

        return method;
    }

    private SOAPEnvelope createWssSoapEnveloppe(String data, Boolean isSecure,String user,String password)
            throws ParserConfigurationException {
        SOAPEnvelope envelope = OMAbstractFactory.getSOAP11Factory().getDefaultEnvelope();

        if (isSecure) {
            envelope = this.addSecureSoapHeader(envelope,user,password);
        }
        envelope.getBody().addChild(this.getSayHelloOMElement(data));
        return envelope;
    }

    /**
     * Add a secure soap header in the given soap envelope
     *
     * @throws SOAPException
     * @throws ParserConfigurationException
     *
     */
    private SOAPEnvelope addSecureSoapHeader(SOAPEnvelope envelope,String user,String password)
            throws ParserConfigurationException {

        Document doc = null;
        doc = SAAJUtil.getDocumentFromSOAPEnvelope(envelope);

        WSSecHeader header = new WSSecHeader();
        header.setMustUnderstand(true);
        header.insertSecurityHeader(doc);

        WSSecUsernameToken usernameToken = new WSSecUsernameToken();
        usernameToken.addNonce();
        usernameToken.addCreated();
        usernameToken.setPasswordType(WSConstants.PASSWORD_TEXT);
        usernameToken.setUserInfo(user, password);
        usernameToken.prepare(doc);
        usernameToken.build(doc, header);

        WSSecTimestamp timeStamp = new WSSecTimestamp();
        timeStamp.setTimeToLive(100);
        timeStamp.prepare(doc);
        timeStamp.build(doc, header);

        envelope = SAAJUtil.getSOAPEnvelopeFromDOOMDocument(doc);

        return envelope;
    }
}
Categories: Java