Skip to content

Instantly share code, notes, and snippets.

@jsfeng
Created August 17, 2012 14:29
Show Gist options
  • Save jsfeng/3379150 to your computer and use it in GitHub Desktop.
Save jsfeng/3379150 to your computer and use it in GitHub Desktop.
AMF Provider for REST service
package net.java.ws.providers;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import flex.messaging.io.SerializationContext;
import flex.messaging.io.amf.Amf3Input;
import flex.messaging.io.amf.Amf3Output;
/**
* A JAX-RS provider for data that is serialized/deserialized according to the AMF specification for serialization of objects.
* E.g. mime type "application/x-amf".
*/
@Provider
@Produces ("application/x-amf")
@Consumes ("application/x-amf")
public class AMFProvider implements MessageBodyReader, MessageBodyWriter {
public boolean isReadable(Class realType, Type genericType, Annotation[] annotations, MediaType mediaType) {
return true; //we'll assume everything is readable, for now.
}
public Object readFrom(Class realType, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, InputStream stream)
throws IOException, WebApplicationException {
SerializationContext context = new SerializationContext();
Amf3Input input = new Amf3Input(context);
input.setInputStream(stream);
return input.readObject();
}
public boolean isWriteable(Class realType, Type genericType, Annotation[] annotations, MediaType mediaType) {
return true; //we'll assume everything is writeable, for now.
}
public void writeTo(Object o, Class realType, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, OutputStream stream) throws IOException, WebApplicationException {
SerializationContext context = new SerializationContext();
Amf3Output output = new Amf3Output(context);
output.setOutputStream(stream);
output.writeObject(obj);
}
public long getSize(Object o, Class type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return -1; //we don't know the size
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment