Showing posts with label webservice. Show all posts
Showing posts with label webservice. Show all posts

Tuesday, 10 July 2012

Adding SOAP header in Webservice Handler


In this program in  "handleRequest" method i added a Username/password in SOAP Header.


import javax.xml.namespace.QName;
import javax.xml.rpc.handler.Handler;
import javax.xml.rpc.handler.HandlerInfo;
import javax.xml.rpc.handler.MessageContext;
import javax.xml.rpc.handler.soap.SOAPMessageContext;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;

public class WSDLServiceHandler implements Handler {
    private static final String HDR_PREFIX = "wsse";
    private static final String HDR_URI = "http://schemas.xmlsoap.org/ws/2002/07/secext";

    @Override
    public void destroy() {
    }

    @Override
    public QName[] getHeaders() {
        return null;
    }

    @Override
    public boolean handleFault(MessageContext context) {
        return true;
    }

    @Override
    public boolean handleRequest(MessageContext context) {
        try {
            SOAPMessageContext smc = (SOAPMessageContext) context;
            SOAPMessage msg = smc.getMessage();
            SOAPPart part = msg.getSOAPPart();
            SOAPEnvelope env = part.getEnvelope();
            SOAPHeader sh = msg.getSOAPHeader();
            if (sh == null) {
                sh = env.addHeader();
            }
            Name nameHdr = env.createName("Security", HDR_PREFIX, HDR_URI);
            SOAPHeaderElement security = sh.addHeaderElement(nameHdr);
            security.setMustUnderstand(false);
            security.addNamespaceDeclaration(HDR_PREFIX, HDR_URI);
            SOAPElement userNameToken = security.addChildElement("UsernameToken", HDR_PREFIX);
            userNameToken.addNamespaceDeclaration(HDR_PREFIX, HDR_URI);
            SOAPElement userName = userNameToken.addChildElement("Username",HDR_PREFIX);
            userName.addNamespaceDeclaration(HDR_PREFIX, HDR_URI);
            userName.addTextNode("USERNAME");
            SOAPElement password = userNameToken.addChildElement("Password",HDR_PREFIX);
            password.addNamespaceDeclaration(HDR_PREFIX, HDR_URI);
            password.addTextNode("UserPassword");
            String soapEnvelope = env.toString();
        } catch (SOAPException e) {
            e.printStackTrace();
        }
        return true;
    }

    @Override
    public boolean handleResponse(MessageContext context) {
        return true;
    }

    @Override
    public void init(HandlerInfo config) {
    }

}

Saturday, 7 July 2012

Web Service Security using Apache Rampart

Web Service Security Encryption with Rampart 1.4 and Axis2 1.4.1

Writing Client Based on the .JKS and .CER files

  • Download axis2-1.4.1-bin.zip (Standard Binary Distribution) from axis site, the URL is http://ws.apache.org/axis2/download/1_4/download.cgi
  • Download rampart-dist-1.4-bin.zip (Standard Binary Distribution) from Apache Rampart, the URL is http://ws.apache.org/rampart/download/1.4/download.cgi
  • Copy these 2 zip files in C:\ and extract it.
Creating Project in Eclipse AND CREATING CLIENT AND STUB
  • Create a new Project in C:\ using Eclipse with the name TestWSS. File --> New --> Java Project
  • Set your class path to JAVA_HOME
  • Open Command Prompt and Go to C:\axis2-1.4.1-bin\axis2-1.4.1\bin
  • Execute axis2.bat
  • Copy your wsdl file into C:\axis2-1.4.1-bin\axis2-1.4.1\bin (Assume the wsdl file name is meal.wsdl).
  • Execute the below command from command prompt
  • C:\axis2-1.4.1-bin\axis2-1.4.1\bin>wsdl2java -uri C:\axis2-1.4.1-bin\axis2-1.4.1\bin\meal.wsdl
  • After executing the above command you will get the stub and Handler class in the C:\axis2-1.4.1-bin\axis2-1.4.1\bin with package.
Placing Client Files in Project SRC and applying signature through Rampart 1.4
  • Copy generated file into C:\TestWSS\src folder.
  • Create a folder parallel to src with the name client-repo and copy module directory from C:\rampart-dist-1.4-bin\rampart-1.4\ to C:\TestWSS\client-repo.
  • This module folder should contain rampart-1.4.mar and addressing-1.41.mar
  • Add all .jar files present in the C:\rampart-dist-1.4-bin\rampart-1.4\lib directory needs to add to the Eclipse Project build path.
  • Add all .jar files present in the C:\axis2-1.4-bin\axis2-1.4\lib directory needs to add to the Eclipse Project build path.
  • Refresh the project in Eclipse and compile it this time you won’t get any compilation errors because we already set the required jars to the class path.
Writing Client Policy File
As you can see, the above security policy contains two main security assertions: an asymmetric binding assertion and a signed parts assertion. Asymmetric binding defines what keys to be used and a few additional properties such as which algorithms to be used in cryptographic operations, layout of the security header, etc. Signed parts assertion defines what parts of the message should be signed. In this tutorial we will be signing the SOAP body of the message.

Tuesday, 12 July 2011

Spring web services using apache cxf

HelloWorld.java
------------------------------
package demo.spring;

import javax.jws.WebService;

@WebService
public interface HelloWorld {
    String sayHi(String text);
}

-----------------------------
HelloWorldImpl.java
-----------------------------
package demo.spring;

import javax.jws.WebService;

@WebService(endpointInterface = "demo.spring.HelloWorld")
public class HelloWorldImpl implements HelloWorld {

    public String sayHi(String text) {
        System.out.println("sayHi called");
        return "Hello " + text;
    }
}
-------------------------------
beans.xml
-------------------------------
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

    <jaxws:endpoint id="helloWorld"   implementor="demo.spring.HelloWorldImpl"
        address="/HelloWorld" />

</beans>
-----------------------------------------------------------------------------------
web.xml
-----------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/beans.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>CXFServlet</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>
----------------------------------------------------------------------------------
deploy the above application as web application
and use apache cxf jar files in ur class path
then the webservice will be accisable

Monday, 11 July 2011

Spring web services using apache cxf

http://cxf.apache.org/docs/writing-a-service-with-spring.html
http://cxf.apache.org/docs/writing-a-service-with-spring.html

Wednesday, 21 July 2010

web service client

public static void main(String args){
String url="http;//localhost:9080/offer/axis/personWS?wsdl";
Service service=new service();
try{
Call cal=(Call) service.createCall();
call.setTargetEndPointAddress(url);

QName qn=new QName(url,"addPerson");
call.setOperationName(qn);
call.registerTypemapping(person.class,qn,
new BeanSerializerFactory(test.class,qn),
new BeanDeserializerFactory(test.class,qn));

// creation a person object

test vo=new test();

vo.setFirstName("FirstName");
vo.setMiddleName("Middlename");

call.invoke();




}catch(ServiceException e){
e.printStracktrace();
}catch(RemoteException e){
e.printStracktrace();
}


}