Lab 3: Querying & Implementing SOAP Web Services

Presenter Notes

Schedule of This lab

2:10 — 2:40
Intro & Exercise 3.1
2:40 — 3:30
Exercise 3.2: Querying Amazon
3:30 — 3:40
Coffee break
3:40 —
Part 2: Developping SOAP services

Presenter Notes

Part 1: Querying SOAP services

Goals:

  • Consuming simple Web Services given a WSDL definition
  • Adding custom headers to fullfil service contracts

Presenter Notes

Generating Java stubs

wsimport -d <class-directory> \
         -s <java-directory> \
         -p <package> \
         http://www.xmlme.com/WSDailyXml.asmx?WSDL

Presenter Notes

Using the stub

Given a Service class generated by wsimport:

Service myService = new Service();
System.out.println(
    myService
        .getEndpoint()
        .method("arg1", "arg2"));

Presenter Notes

Intercepting messages

The setHandlerResolver registers a handler that can intercept and modify SOAP messages.

Service myService = new Service();
XmlFact.setHandlerResolver(myHandlerResolver);

Presenter Notes

An Example Handler Resolver

public class SampleResolver implements HandlerResolver {

    public List<Handler> getHandlerChain(PortInfo portInfo) {
       List<Handler> handlerChain = new ArrayList<Handler>();
       handlerChain.add(new myHandler());
       return handlerChain;
    }
}

Presenter Notes

An Example Handler

class MyHandler implements SOAPHandler<SOAPMessageContext> {
    public void close(MessageContext messagecontext) {
    }

    public Set<QName> getHeaders() {
        // Find the header blocks that we can process
        return null;
    }

    public boolean handleFault(SOAPMessageContext messagecontext) {
        System.out.println("fault");
        return true; // continue processing
    }

Presenter Notes

An Example Handler

    public boolean handleMessage(SOAPMessageContext messagecontext) {
        Boolean outbound = (Boolean)messagecontext.get(
            MessageContext.MESSAGE_OUTBOUND_PROPERTY);

        if (outbound) {
            System.out.println("Message to service");
        }
        else {
            System.out.println("Message from service");
        }
        return true; // continue processing
    }
} //class

Presenter Notes

Exercises

  • 3.1 Generate a stub for the Daily XML Facts web service, and see how the WSDL relates
  • 3.2 Amazon's service requires identification headers. Add them with a handler.

Presenter Notes

Authorization

Amazon requires an application key:

  • Download the key on the labs website.
  • Copy paste it in the skeleton code.

This key will be disabled after the lab. You can create one for yourself after that.

Presenter Notes

Part 2: Implementing SOAP services

Goals:

  • Introducing the JAX-WS annotation
  • Understanding SOAP-based Web Service design.

Presenter Notes

Framework

We will use Apache CXF.

This framework implements both JAX-WS (SOAP) and JAX-RS (REST).

Messages are serialized to XML through JAXB.

Services can be created Java-first or WSDL-first. We will use the Java-first way.

Presenter Notes

JAX-WS service

@WebService
public interface MyCalc {
    public abstract int add(int a, int b);
    public abstract int substract(int a, int b);
}

Presenter Notes

JAX-WS Service Annotations

@WebService(targetNamespace="http://...",
            serviceName="HelloWorld")
public interface MyCalc {
    public abstract int add(int a, int b);
    public abstract int substract(int a, int b);
}

Presenter Notes

JAX-WS Service Annotations (2)

Style specification:

@WebService(targetNamespace="http://...",
            serviceName="HelloWorld")
@SOAPBinding(style=Style.RPC,
             use=Use.ENCODED)
public interface MyCalc {
    public abstract int add(int a, int b);
    public abstract int substract(int a, int b);
}

Presenter Notes

JAX-WS Method Annotations

public interface MyCalc {
    public abstract int add(int a, int b);
    public abstract int substract(int a, int b);

    @WebMethod(exclude=true)
    public abstract void ninja(); //Won't show up!
}

Presenter Notes

JAX-WS Method Annotations (2)

public interface MyCalc {
    @WebMethod(operationName="plus")
    public abstract int add(int a, int b);

    @WebMethod(operationName="minus")
    public abstract int substract(int a, int b);
}

Presenter Notes

JAX-WS Method Annotations (3)

public interface MyCalc {
    @RequestWrapper(localname="additionOperands",
        targetNamespace="http://example.org/calc/")
    @WebResult(name="sum")
    public abstract int add(
        @WebParam(name="operand1") int a,
        @WebParam(name="operand2") int b);
    public abstract int substract(int a, int b);
}

Presenter Notes

JAX-WS Data Serialization

public class Video {
    @XmlElement(required=true) //JAXB annotations
    String title;

    int length;

    ArrayList<String> Comments;
}

public interface SmileyVideo {
    public abstract ArrayList<String> findVideos(
        String keywords);
}

Presenter Notes

Creating a client

ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
factory.setServiceClass(SmileyVideo.class);
factory.setAddress("http://smileyvideo.com");
Service client = (SmileyVideo) factory.create();

ArrayList<Video> vids = client.findVideos("Bad Apple");

//etc.

Presenter Notes

Exercises

  • 3.3 Add the necessary annotations, and glue code to get the Random service working.
  • 3.4 Design and implement a SOAP Contacts Manager.

Presenter Notes