Friday, November 7, 2008

Creating Codec Specific Custom Documentation

In my last post i was described about how to create JAX-WS custom codec. In this post I am going to explain how to create endpoint documentation for your codec.

Your codec implements "com.sun.xml.ws.api.server.EndpointComponent", So your codec act as one endpoint, every endpoint component having its own SPI. So you can create your SPI handler.

While creating codec "getSPI" method was unexplained. Here it come one of the use of it.


public @Nullable T getSPI(@NotNull Class type) {

if (type == HttpMetadataPublisher.class) {
if (metadataPublisher == null)
metadataPublisher = new HttpMetadataPublisher(){
@Override
public boolean handleMetadataRequest(HttpAdapter arg0,
WSHTTPConnection arg1) throws IOException {
return false;
}

};
return type.cast(metadataPublisher);
}
return null;
}


When ever get request made to the point your codec "getSPI" method called. Normaly it returns HttpMetadataPublisher object. Then your publishers "handleMetadataRequest" method called by request handler. inside "handleMetadataRequest" you can write your custom documentation and return "true".

In case if you return "false" JAX-WS default end point documentation created.


Following steps help you to create simple custom documentation for endpoint.

Step 1: Create a class "XXXHttpMetadataPublisher" which extends from com.sun.xml.ws.transport.http.HttpMetadataPublisher.


Step 2: Overwrite a method "public boolean handleMetadataRequest(HttpAdapter adapter,
WSHTTPConnection connection)"

Step 3: Inside method parse a query string from "WSHTTPConnection".

String queryString = con.getQueryString()


Step 4: based on query string write your html help document into "WSHTTPConnection" outputStream. And return "true"

Now your "XXXHttpMetadataPublisher" looks like

import java.io.IOException;

import com.sun.xml.ws.transport.http.HttpAdapter;
import com.sun.xml.ws.transport.http.HttpMetadataPublisher;
import com.sun.xml.ws.transport.http.WSHTTPConnection;

public class XXXHttpMetadataPublisher extends HttpMetadataPublisher {

@Override
public boolean handleMetadataRequest(HttpAdapter adapter,
WSHTTPConnection connection) throws IOException {
String queryString = connection.getQueryString();
if (queryString.equals("hello") ){
connection.getOutput().write("I Have to write document here. Templete engines may help me here....".getBytes());
return true;
}
return false;
}

}


Step 5: Now go to "getSPI" method in your XXXCodec class, check type is "HttpMetadataPublisher", if its true then return your "XXXHttpMetadataPublisher".

your codec getSPI method looks like following

public @Nullable T getSPI(@NotNull Class type) {
if (type == HttpMetadataPublisher.class) {
if (metadataPublisher == null)
metadataPublisher = new XXXHttpMetadataPublisher();
return type.cast(metadataPublisher);
}
return null;
}


Your Done!!!


Now if you browse your end point with "hello" as query string, you get the the "hello" specific document.






No comments: