|
| 1 | +/* |
| 2 | + * Copyright (c) 2015, EMC Corporation. |
| 3 | + * Redistribution and use in source and binary forms, with or without modification, |
| 4 | + * are permitted provided that the following conditions are met: |
| 5 | + * |
| 6 | + * + Redistributions of source code must retain the above copyright notice, |
| 7 | + * this list of conditions and the following disclaimer. |
| 8 | + * + Redistributions in binary form must reproduce the above copyright |
| 9 | + * notice, this list of conditions and the following disclaimer in the |
| 10 | + * documentation and/or other materials provided with the distribution. |
| 11 | + * + The name of EMC Corporation may not be used to endorse or promote |
| 12 | + * products derived from this software without specific prior written |
| 13 | + * permission. |
| 14 | + * |
| 15 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 17 | + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 18 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 19 | + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 20 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 21 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 22 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 23 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 24 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 25 | + * POSSIBILITY OF SUCH DAMAGE. |
| 26 | + */ |
| 27 | +package com.emc.rest.smart; |
| 28 | + |
| 29 | +import com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider; |
| 30 | +import com.sun.jersey.spi.inject.Injectable; |
| 31 | + |
| 32 | +import javax.ws.rs.Consumes; |
| 33 | +import javax.ws.rs.Produces; |
| 34 | +import javax.ws.rs.WebApplicationException; |
| 35 | +import javax.ws.rs.core.Context; |
| 36 | +import javax.ws.rs.core.MediaType; |
| 37 | +import javax.ws.rs.core.MultivaluedMap; |
| 38 | +import javax.ws.rs.ext.MessageBodyReader; |
| 39 | +import javax.ws.rs.ext.Providers; |
| 40 | +import javax.xml.bind.annotation.XmlRootElement; |
| 41 | +import javax.xml.bind.annotation.XmlType; |
| 42 | +import javax.xml.parsers.SAXParserFactory; |
| 43 | +import java.io.IOException; |
| 44 | +import java.io.InputStream; |
| 45 | +import java.lang.annotation.Annotation; |
| 46 | +import java.lang.reflect.Type; |
| 47 | + |
| 48 | +@Produces("application/octet-stream") |
| 49 | +@Consumes("application/octet-stream") |
| 50 | +public class OctetStreamXmlProvider implements MessageBodyReader<Object> { |
| 51 | + private MessageBodyReader<Object> delegate; |
| 52 | + |
| 53 | + public OctetStreamXmlProvider(@Context Injectable<SAXParserFactory> spf, @Context Providers ps) { |
| 54 | + this.delegate = new XMLRootElementProvider.General(spf, ps); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { |
| 59 | + return (type.getAnnotation(XmlRootElement.class) != null || type.getAnnotation(XmlType.class) != null) |
| 60 | + && mediaType.equals(MediaType.APPLICATION_OCTET_STREAM_TYPE); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException { |
| 65 | + return delegate.readFrom(type, genericType, annotations, mediaType, httpHeaders, entityStream); |
| 66 | + } |
| 67 | +} |
0 commit comments