|
| 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.emc.rest.util.SizedInputStream; |
| 30 | +import com.sun.jersey.core.util.ReaderWriter; |
| 31 | + |
| 32 | +import javax.ws.rs.Produces; |
| 33 | +import javax.ws.rs.WebApplicationException; |
| 34 | +import javax.ws.rs.core.MediaType; |
| 35 | +import javax.ws.rs.core.MultivaluedMap; |
| 36 | +import javax.ws.rs.ext.MessageBodyWriter; |
| 37 | +import java.io.IOException; |
| 38 | +import java.io.OutputStream; |
| 39 | +import java.lang.annotation.Annotation; |
| 40 | +import java.lang.reflect.Type; |
| 41 | + |
| 42 | +@Produces({"application/octet-stream", "*/*"}) |
| 43 | +public class SizedInputStreamWriter implements MessageBodyWriter<SizedInputStream> { |
| 44 | + @Override |
| 45 | + public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { |
| 46 | + return SizedInputStream.class.isAssignableFrom(type); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public long getSize(SizedInputStream sizedInputStream, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { |
| 51 | + return sizedInputStream.getSize(); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void writeTo(SizedInputStream sizedInputStream, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { |
| 56 | + ReaderWriter.writeTo(sizedInputStream, entityStream); |
| 57 | + } |
| 58 | +} |
0 commit comments