Skip to content

Instantly share code, notes, and snippets.

@hamnis
Created October 16, 2013 09:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hamnis/7004900 to your computer and use it in GitHub Desktop.
Save hamnis/7004900 to your computer and use it in GitHub Desktop.
package api.resource;
import javax.ws.rs.*;
import net.hamnaberg.json.*;
@Path("/events")
@Produces("application/vnd.collection+json")
@Consumes("application/vnd.collection+json")
public class EventResource{
@GET
public Collection list(@Context UriInfo info) {
Collection collection = Collection.create(info.getRequestURI(), loadItems());
return collection;
}
@POST
public Response create(@Context UriInfo info, Template template) {
return Response.created(info.getBaseUriBuilder().path("/events/{id}").build("1234")).build();
}
private Iterable<Item> loadItems() {
return java.util.Collections.emptyList();
}
}
package api.provider;
import net.hamnaberg.json.Collection;
import net.hamnaberg.json.Template;
import net.hamnaberg.json.Writable;
import net.hamnaberg.json.parser.CollectionParser;
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;
@Provider
@Produces("application/vnd.collection+json")
public class CollectionJsonReaderAndWriter implements MessageBodyReader<Template>, MessageBodyWriter<Writable> {
private CollectionParser parser = new CollectionParser();
@Override
public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
boolean compatible = mediaType.isCompatible(MediaType.valueOf("application/vnd.collection+json"));
return compatible && (Template.class.equals(type));
}
@Override
public Template readFrom(Class<Template> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
return parser.parseTemplate(entityStream);
}
@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
boolean compatible = mediaType.isCompatible(MediaType.valueOf("application/vnd.collection+json"));
return compatible && (Template.class.equals(type) || Collection.class.equals(type));
}
@Override
public long getSize(Writable writable, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return -1;
}
@Override
public void writeTo(Writable writable, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
writable.writeTo(entityStream);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment