| 1 |
package com.fasterxml.jackson.module.jaxb.deser; |
| 2 |
|
| 3 |
import java.io.IOException; |
| 4 |
import java.io.InputStream; |
| 5 |
import java.io.OutputStream; |
| 6 |
import java.io.ByteArrayInputStream; |
| 7 |
|
| 8 |
import javax.activation.DataHandler; |
| 9 |
import javax.activation.DataSource; |
| 10 |
|
| 11 |
import com.fasterxml.jackson.core.JsonParser; |
| 12 |
import com.fasterxml.jackson.core.JsonProcessingException; |
| 13 |
import com.fasterxml.jackson.databind.DeserializationContext; |
| 14 |
import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer; |
| 15 |
|
| 16 |
/** |
| 17 |
* @author Ryan Heaton |
| 18 |
*/ |
| 19 |
public class DataHandlerJsonDeserializer |
| 20 |
extends StdScalarDeserializer<DataHandler> |
| 21 |
{ |
| 22 |
private static final long serialVersionUID = 1L; |
| 23 |
|
| 24 |
public DataHandlerJsonDeserializer() { super(DataHandler.class); } |
| 25 |
|
| 26 |
@Override |
| 27 |
public DataHandler deserialize(JsonParser jp, DeserializationContext ctxt) |
| 28 |
throws IOException, JsonProcessingException |
| 29 |
{ |
| 30 |
final byte[] value = jp.getBinaryValue(); |
| 31 |
return new DataHandler(new DataSource() { |
| 32 |
@Override |
| 33 |
public InputStream getInputStream() throws IOException { |
| 34 |
return new ByteArrayInputStream(value); |
| 35 |
} |
| 36 |
|
| 37 |
@Override |
| 38 |
public OutputStream getOutputStream() throws IOException { |
| 39 |
throw new IOException(); |
| 40 |
} |
| 41 |
|
| 42 |
@Override |
| 43 |
public String getContentType() { |
| 44 |
return "application/octet-stream"; |
| 45 |
} |
| 46 |
|
| 47 |
@Override |
| 48 |
public String getName() { |
| 49 |
return "json-binary-data"; |
| 50 |
} |
| 51 |
}); |
| 52 |
} |
| 53 |
} |