Skip to content

Instantly share code, notes, and snippets.

@stoicflame
Created September 15, 2011 15:58
Show Gist options
  • Save stoicflame/1219646 to your computer and use it in GitHub Desktop.
Save stoicflame/1219646 to your computer and use it in GitHub Desktop.
implementation of a MessageBodyReader<DataHandler>
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