Hi Thomas

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 think JAXRS MessageContextImpl is just not setting attachments on the
message (as in message.setAttachments()), this is because Attachments that
MultipartBody deals with and those which are set in message.setAttachments
are of different types


>  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.


Yes, this is the way to get to the request/response object


> 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?
>
>
I'm presuming the CXF input stream representing an attachment is supporting
reset() - so yes, it's fine if it works :-)


> 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?
>
>
At the moment you may want to replace the existing MultipartBody instance
with a new one by initializing it with a new collection of Attachments,
getting some from the immutable collection and adding some new attachcment
to the new collection

cheers, Sergey


> Thanks in advance,
> Thomas
>
>

Reply via email to