org.apache.stanbol.entityhub.jersey.utils
Class MessageBodyReaderUtils

java.lang.Object
  extended by org.apache.stanbol.entityhub.jersey.utils.MessageBodyReaderUtils

public final class MessageBodyReaderUtils
extends Object

Utilities for implementing MessageBodyReader.

Author:
Rupert Westenthaler

Nested Class Summary
static class MessageBodyReaderUtils.RequestData
          Simple class that holds the MediaType, Name and the content as InputStream.
 
Method Summary
static MessageBodyReaderUtils.RequestData formForm(InputStream formData, String charset, String encodingParam, List<String> contentParams)
          Returns content parsed as MediaType.APPLICATION_FORM_URLENCODED.
static List<MessageBodyReaderUtils.RequestData> fromMultipart(InputStream mimeData, javax.ws.rs.core.MediaType mediaType)
          Returns content parsed from MediaType.MULTIPART_FORM_DATA.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

formForm

public static MessageBodyReaderUtils.RequestData formForm(InputStream formData,
                                                          String charset,
                                                          String encodingParam,
                                                          List<String> contentParams)
                                                   throws IOException,
                                                          UnsupportedEncodingException,
                                                          IllegalArgumentException
Returns content parsed as MediaType.APPLICATION_FORM_URLENCODED. It assumes that the encoding and the content is defined by own parameters. For the content this method allows to parse several parameters. The first existing one is used to get the content. The parameter actually used to retrieve the content will be available via MessageBodyReaderUtils.RequestData.getName().

This Method will load the content several time into memory and should not be used for big contents. However this should be fine in cases data are parsed as MediaType.APPLICATION_FORM_URLENCODED

This Method is necessary because within MessageBodyReader one can not use the FormParam annotations because only the InputStream is parsed to the MessageBodyReader.readFrom(Class, Type, java.lang.annotation.Annotation[], MediaType, javax.ws.rs.core.MultivaluedMap, InputStream) method

To test this Method with curl use:

 curl -v -X POST --data-urlencode "{encodingParam}=application/rdf+xml" 
                 --data-urlencode "{contentParam}@{datafile}" 
                  {serviceURL}
 
Note that between {contentParam} and the datafile MUST NOT be a '='!

Parameters:
formData - the data of the form as stream
charset - the charset used for the form data
encodingParam - the parameter name used to parse the encoding
contentParams - the list of parameters used for the content. The first existing parameter is used to parse the content. Additional ones are ignored.
Returns:
The parsed content (MediaType and InputStream)
Throws:
IOException - On any exception while reading from the parsed stream.
UnsupportedEncodingException - if the parsed charset is not supported by this plattform
IllegalArgumentException - In case of a Response.Status.BAD_REQUEST

fromMultipart

public static List<MessageBodyReaderUtils.RequestData> fromMultipart(InputStream mimeData,
                                                                     javax.ws.rs.core.MediaType mediaType)
                                                              throws IOException,
                                                                     IllegalArgumentException
Returns content parsed from MediaType.MULTIPART_FORM_DATA. It iterates over all BodyParts and tries to create MessageBodyReaderUtils.RequestData instances. In case the Part.getContentType() is not present or can not be parsed, the MessageBodyReaderUtils.RequestData.getMediaType() is set to null. If Part.getInputStream() is not defined an IllegalArgumentException is thrown. The Part.getFileName() is used for MessageBodyReaderUtils.RequestData.getName(). The ordering of the returned Content instances is the same as within the MimeMultipart instance parsed from the input stream.

This Method does NOT load the data into memory, but returns directly the InputStreams as returned by the BodyParts. Therefore it is saved to be used with big attachments.

This Method is necessary because within MessageBodyReader one can not use the usual annotations as used within Resources. so this method allows to access the data directly from the parameters available from the MessageBodyReader.readFrom(Class, Type, java.lang.annotation.Annotation[], MediaType, javax.ws.rs.core.MultivaluedMap, InputStream) method

To test this Method with curl use:

 curl -v -X POST -F "content=@{dataFile};type={mimeType}" 
      {serviceURL}
 
Note that between {contentParam} and the datafile MUST NOT be a '='!

Parameters:
mimeData - the mime encoded data
mediaType - the mediaType (parsed to the ByteArrayDataSource constructor)
Returns:
the contents parsed from the BodyParts
Throws:
IOException - an any Exception while reading the stream or MessagingException exceptions other than ParseExceptions
IllegalArgumentException - If a InputStream is not available for any BodyPart or on ParseExceptions while reading the MimeData from the stream.


Copyright © 2010-2013 The Apache Software Foundation. All Rights Reserved.