Hi all!
I am trying to extract attachments passed to a RESTful service within a
subclass of AbstractPhaseInterceptor<Message>. The questions I have is how
to do this properly and whether it is possible to replace/manipulate the
attachments.
What I have so far is the following:
class RequestLoggingInterceptor extends AbstractPhaseInterceptor<Message> {
RequestLoggingInterceptor() {
super(Phase.PRE_INVOKE);
}
public void handleMessage(Message message) throws Fault {
List<?> contentList = message.getContent(List.class);
MultipartBody body=(MultipartBody) contentList.get(0);
List<Attachment> attachments = body.getAllAttachments();
for (Attachment a : attachments) {
File file = new File(...);
ServicesUtils.writeToFile(a.getDataHandler
().getInputStream(), file);
...
For retrieving the attachments I also tried to invoke getAttachments on the
message but this returns null. I am wondering whether the above code
(getContent(List.class).get(0) is the proper way of doing this. At least
this is the way the invoker eventually retrieves the body to pass to my
RESTful service. The other thing I experience is when writing the file, I
have to invoke reset on the InputStream to reset the the file read position
back to the initial mark of 0. Would this be the proper way to get hold of
the attachments?
And the final question I have, would it be possible to replace the
attachment with a different one after it has been extracted? The
collection I get from body.getAllAttachments is unmodifiable, so
body.getAllAttachments().set(...) does not work. When looking at the API
provided by Attachment, I don't see any method that would allow me to
replace the data handler. On the data handler, I can only change the
DataContentHandlerFactory - could I do anything with that? And the
getInputStream gives me back a DelegatingInputStream which has package
visibility, so cannot change the stream it is delegating to.
Does anybody have any other ideas of how to accomplish this?
Thanks in advance,
Thomas