Created
September 15, 2011 15:58
-
-
Save stoicflame/1219646 to your computer and use it in GitHub Desktop.
implementation of a MessageBodyReader<DataHandler>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import javax.activation.DataHandler; | |
| import javax.activation.DataSource; | |
| import javax.ws.rs.Consumes; | |
| import javax.ws.rs.WebApplicationException; | |
| import javax.ws.rs.core.MediaType; | |
| import javax.ws.rs.core.MultivaluedMap; | |
| import javax.ws.rs.ext.MessageBodyReader; | |
| import javax.ws.rs.ext.Provider; | |
| import java.io.IOException; | |
| import java.io.InputStream; | |
| import java.io.OutputStream; | |
| import java.lang.annotation.Annotation; | |
| import java.lang.reflect.Type; | |
| /** | |
| * @author Ryan Heaton | |
| */ | |
| @Provider | |
| @Consumes( MediaType.WILDCARD ) | |
| public class DataHandlerReader implements MessageBodyReader<DataHandler> { | |
| public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { | |
| return DataHandler.class.isAssignableFrom(type); | |
| } | |
| public DataHandler readFrom(Class<DataHandler> type, Type genericType, Annotation[] annotations, final MediaType mediaType, MultivaluedMap<String, String> httpHeaders, final InputStream entityStream) throws IOException, WebApplicationException { | |
| return new DataHandler(new DataSource() { | |
| public InputStream getInputStream() throws IOException { | |
| return entityStream; | |
| } | |
| public OutputStream getOutputStream() throws IOException { | |
| throw new IOException(); | |
| } | |
| public String getContentType() { | |
| return mediaType.toString(); | |
| } | |
| public String getName() { | |
| return getClass().getName(); | |
| } | |
| }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment