Thursday, May 07, 2009

Wicket, Form and GET method

1 comments
If you are using Wicket and has previous web development experience, one of the good things is that, everytime you submit, Wicket takes care of everything. However, URL after form submission is something strange as it leads you to wicket URLs that is depending on the user's session. The question is: how do you make Wicket behave as a plain form that submits using GET method?

First, I set my URL strategy to make it bookmarkable:

MixedParamUrlCodingStrategy mypageURL = new MixedParamUrlCodingStrategy(
"/service/test",
MyPage.class,
new String[]{"type"}
);
mount(mypageURL);

Then, in the page code, I override the form onSubmit() method to do the following:

form.add(new SubmitLink("update") {
@Override
public void onSubmit() {
PageParameters parameters = new PageParameters();
parameters.add("period", Integer.toString(getPeriod()));
parameters.add("unit", getUnit().toString());
parameters.add("type", getType());
setResponsePage(getPage().getClass(), parameters);
}
});

So, after the user click on the submit link, it submits it similarly to a GET method and I get a nice URL like:

https://localhost:5443/application/service/test/app/?unit=HOUR&period=6

The result is that this makes URLs much more bookmarkable throughout the session.

Tuesday, May 05, 2009

Extracting SOAP attachments with Axis

2 comments
It was a little hard to find this information today, so I think it would be interesting to share it here. First, what I wanted was to get the attachment for a SOAP response. The response was empty, but the attachment had a png I needed to display in my Wicket application.

It turns out that it is very simple to extract SOAP attachments. All you have to do is to write a handler that will be called during the web service call. Any number of handlers can be set in the BindingProvider. For example, this is my code to set the handlers:


MyServicePort port = mws.getMyServicePort();
BindingProvider bp = (BindingProvider) port;
bp.getRequestContext().put(
BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
DEFAULT_WS_URL);
Binding binding = bp.getBinding();

// Add the logging handler
List handlerList = binding.getHandlerChain();
if (handlerList == null) {
handlerList = new ArrayList();
binding.setHandlerChain(handlerList);
}

handlerList.add(handler);


With this code, we only need to write our handler which will handle the message and can do whatever it wants with it - including accessing attachments.


public class SOAPAttachmentHandler
implements SOAPHandler<SOAPMessageContext> {
private Collection<Attachment> attachments;

@Override
public boolean handleFault(SOAPMessageContext context) {
context.getMessage();
return true;
}

@Override
public boolean handleMessage(SOAPMessageContext context) {
attachments = ((SOAPMessageContextImpl)context).
getWrappedMessage().getAttachments();
return true;
}

@Override
public Set<QName> getHeaders() {
return null;
}

@Override
public void close(MessageContext context) {
// blank
}

public Collection<Attachment> getAttachments() {
return attachments;
}


The class above extracts the attachments and store then in a class variable. After invoking the web service, I can access the attachments.