001package co.codewizards.cloudstore.ls.core.provider; 002 003import java.io.IOException; 004import java.io.ObjectOutputStream; 005import java.io.OutputStream; 006import java.lang.annotation.Annotation; 007import java.lang.reflect.Type; 008 009import javax.ws.rs.Produces; 010import javax.ws.rs.WebApplicationException; 011import javax.ws.rs.core.MediaType; 012import javax.ws.rs.core.MultivaluedMap; 013import javax.ws.rs.ext.MessageBodyWriter; 014import javax.ws.rs.ext.Provider; 015 016import co.codewizards.cloudstore.core.io.NoCloseOutputStream; 017 018/** 019 * @author Marco หงุ่ยตระกูล-Schulze - marco at nightlabs dot de 020 */ 021@Provider 022@Produces(MediaTypeConst.APPLICATION_JAVA_NATIVE) 023public class JavaNativeMessageBodyWriter 024implements MessageBodyWriter<Object> 025{ 026// private String getLogPrefix() 027// { 028// return "(" + Integer.toHexString(System.identityHashCode(this)) + ") "; //$NON-NLS-1$ //$NON-NLS-2$ 029// } 030 031 public JavaNativeMessageBodyWriter() { 032// System.out.println(JavaNativeMessageBodyWriter.class.getName() + getLogPrefix() + ": instantiated."); //$NON-NLS-1$ 033 } 034 035 @Override 036 public long getSize(Object t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) 037 { 038 return -1; 039 } 040 041 @Override 042 public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { 043 // We return always true, because we declared our media-type already in the @Produces above and thus don't need to check it here. 044 // At least I hope we don't get consulted for media-types that were not declared in @Produces. 045 return true; 046 } 047 048 @Override 049 public void writeTo( 050 Object t, Class<?> type, Type genericType, 051 Annotation[] annotations, MediaType mediaType, 052 MultivaluedMap<String, Object> httpHeaders, 053 OutputStream entityStream 054 ) throws IOException, WebApplicationException 055 { 056 try (ObjectOutputStream oout = new ObjectOutputStream(new NoCloseOutputStream(entityStream));) { 057 oout.writeObject(t); 058 oout.flush(); 059 } 060 } 061}