| 1 | |
package com.buckosoft.PicMan.service.support; |
| 2 | |
|
| 3 | |
import java.io.IOException; |
| 4 | |
import java.io.InputStream; |
| 5 | |
import java.io.InputStreamReader; |
| 6 | |
import java.io.OutputStream; |
| 7 | |
import java.io.OutputStreamWriter; |
| 8 | |
import java.lang.annotation.Annotation; |
| 9 | |
import java.lang.reflect.Type; |
| 10 | |
|
| 11 | |
import javax.ws.rs.WebApplicationException; |
| 12 | |
import javax.ws.rs.core.MediaType; |
| 13 | |
import javax.ws.rs.core.MultivaluedMap; |
| 14 | |
import javax.ws.rs.ext.MessageBodyReader; |
| 15 | |
import javax.ws.rs.ext.MessageBodyWriter; |
| 16 | |
|
| 17 | |
import com.thoughtworks.xstream.XStream; |
| 18 | |
import com.thoughtworks.xstream.io.xml.CompactWriter; |
| 19 | |
import com.thoughtworks.xstream.io.xml.StaxDriver; |
| 20 | |
|
| 21 | 0 | public class XMLProvider implements MessageBodyReader<Object>, MessageBodyWriter<Object> { |
| 22 | |
|
| 23 | 0 | private static final XStream xstream = new XStream(new StaxDriver()); |
| 24 | |
|
| 25 | |
private static final String DEFAULT_ENCODING = "utf-8"; |
| 26 | |
|
| 27 | |
@Override |
| 28 | |
public boolean isWriteable(Class<?> type, Type genericType, |
| 29 | |
Annotation[] annotations, MediaType mediaType) { |
| 30 | 0 | return true; |
| 31 | |
} |
| 32 | |
|
| 33 | |
@Override |
| 34 | |
public boolean isReadable(Class<?> type, Type genericType, |
| 35 | |
Annotation[] annotations, MediaType mediaType) { |
| 36 | 0 | return true; |
| 37 | |
} |
| 38 | |
|
| 39 | |
protected static String getCharsetAsString(MediaType m) { |
| 40 | 0 | if (m == null) { |
| 41 | 0 | return DEFAULT_ENCODING; |
| 42 | |
} |
| 43 | 0 | String result = m.getParameters().get("charset"); |
| 44 | 0 | return (result == null) ? DEFAULT_ENCODING : result; |
| 45 | |
} |
| 46 | |
|
| 47 | |
protected XStream getXStream(Class<?> type) { |
| 48 | 0 | return xstream; |
| 49 | |
} |
| 50 | |
|
| 51 | |
@Override |
| 52 | |
public long getSize(Object t, Class<?> type, Type genericType, |
| 53 | |
Annotation[] annotations, MediaType mediaType) { |
| 54 | 0 | return -1; |
| 55 | |
} |
| 56 | |
|
| 57 | |
@Override |
| 58 | |
public void writeTo(Object t, Class<?> type, Type genericType, |
| 59 | |
Annotation[] annotations, MediaType mediaType, |
| 60 | |
MultivaluedMap<String, Object> httpHeaders, |
| 61 | |
OutputStream entityStream) throws IOException, |
| 62 | |
WebApplicationException { |
| 63 | 0 | String encoding = getCharsetAsString(mediaType); |
| 64 | 0 | XStream xStream = getXStream(t.getClass()); |
| 65 | 0 | xStream.marshal(t, new CompactWriter(new OutputStreamWriter(entityStream, encoding))); |
| 66 | 0 | } |
| 67 | |
|
| 68 | |
@Override |
| 69 | |
public Object readFrom(Class<Object> type, Type genericType, |
| 70 | |
Annotation[] annotations, MediaType mediaType, |
| 71 | |
MultivaluedMap<String, String> httpHeaders, InputStream entityStream) |
| 72 | |
throws IOException, WebApplicationException { |
| 73 | 0 | String encoding = getCharsetAsString(mediaType); |
| 74 | 0 | XStream xStream = getXStream(type); |
| 75 | 0 | return xStream.fromXML(new InputStreamReader(entityStream, encoding)); |
| 76 | |
|
| 77 | |
} |
| 78 | |
|
| 79 | |
} |