CXF
Lets look into an interesting component of the CXF framewk – The INTERCEPTORS. CXF is a soap container. You design soap services with that.
Now lets say you have created your service based on that and now you want do some preprocessing with the soap request that you receive. Reasons could be numerous. May be you want to do some authentication, may be authorization. You may want to do some common auditing, logging, which you want to keep independent of your services.
You certainly don’t want to push this common logic and components into your services logic (that contains your business logic, data access logic). The approach to this is designing a CXF Interceptor.
Let start with a piece of code:
public class RequestLogInterceptor extends AbstractSoapInterceptor {
public RequestLogInterceptor() {
super(Phase.RECEIVE);
addBefore(EndpointSelectionInterceptor.class.getName());
}
public void handleMessage(SoapMessage message) {
InputStream inputStream = message.getContent(InputStream.class);
StringBuffer buffer = new StringBuffer();
try {
BufferedReader br = new BufferedReader(new InputStreamReader
(inputStream));
String line = "";
while ((line = br.readLine()) != null) {
buffer.append("\t").append(line).append("\n");
}
// Necessary to re-insert message after it is used
// so other interceptors can work with it.
inputStream = new ByteArrayInputStream(buffer.toString()
.getBytes());
message.setContent(InputStream.class, inputStream);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(" **** Request Message: \n"
+ buffer.toString());
}
}
So Starting with Class definition, an Interceptor in CXF can be defined by extending it from AbstractSoapInterceptor or AbstractPhaseInterceptor<Message>. Extending one of these classes then requires you to define a function public void handleMessage(SoapMessage message) throws Fault {}. This is where you push your code to preprocess the soap requests. The above piece of code is sample logging interceptor that logs the request message.
Search
-
You are currently browsing the Sanchit Srivastava weblog archives.
No Responses Yet to “CXF”