package com.sun.ws.rest.samples.atomserver.resources; import com.sun.syndication.feed.atom.Content; import com.sun.syndication.feed.atom.Entry; import com.sun.syndication.feed.atom.Feed; import com.sun.syndication.feed.atom.Link; import com.sun.ws.rest.api.ConsumeMime; import com.sun.ws.rest.api.Entity; import com.sun.ws.rest.api.HttpMethod; import com.sun.ws.rest.api.ProduceMime; import com.sun.ws.rest.api.SubResources; import com.sun.ws.rest.api.UriTemplate; import com.sun.ws.rest.api.core.HttpContext; import com.sun.ws.rest.api.representation.AtomEntryRepresentation; import com.sun.ws.rest.api.representation.AtomFeedRepresentation; import com.sun.ws.rest.api.representation.Representation; import com.sun.ws.rest.api.response.Created; import com.sun.ws.rest.api.response.HttpResponse; import java.net.URI; import javax.annotation.Resource; /** * * @author Paul.Sandoz@Sun.Com */ @UriTemplate("/collection/") @ProduceMime("application/atom+xml") @SubResources({EntryResource.class, EditEntryResource.class}) public class FeedResource { @Resource HttpContext context; @HttpMethod public Feed getFeed() { return FileHelper.getFeedDocument(getThisUriPath().toString()); } @HttpMethod @ConsumeMime("application/atom+xml") public HttpResponse postEntry(Entry e) { // Get the next unique name of the entry String entryId = FileStore.FS.getNextId(); URI entryUri = getUri(entryId); URI editEntryUri = getUri("edit/" + entryId); // Set the links and Id addLink(e, "self", entryUri.toString()); addLink(e, "edit", editEntryUri.toString()); e.setId(entryId); // Write out the entry document FileHelper.createEntryDocument(entryId, e); // Update the feed document with the entry Feed f = FileHelper.getFeedDocument(getThisUriPath()); FileHelper.updateFeedDocumentWithNewEntry(f, e); Representation r = new AtomEntryRepresentation(e); r.setContentLocation(entryUri); return new Created(r, entryUri); } @HttpMethod public HttpResponse postMediaEntry(Entity entry) { // Get the next unique name of the entry String entryId = FileStore.FS.getNextId(); Entry e = FileHelper.createDefaulMediaLinkEntryDocument(); URI entryUri = getUri(entryId); URI mediaUri = getUri(entryId + "/media"); URI editEntryUri = getUri("edit/" + entryId); URI editMediaUri = getUri("edit/" + entryId + "/media"); // Set the links and Id addLink(e, "self", entryUri.toString()); addLink(e, "edit", editEntryUri.toString()); addLink(e, "edit-media", editMediaUri.toString()); e.setId(entryId); // Set the content to link to the media Content c = new Content(); c.setType(entry.getMediaType().toString()); c.setSrc(mediaUri.toString()); e.getContents().add(c); // Write out the entry document FileHelper.createEntryDocument(entryId, e); FileHelper.createMediaDocument(entryId, entry.getContent()); // Update the feed document with the entry Feed f = FileHelper.getFeedDocument(getThisUriPath()); FileHelper.updateFeedDocumentWithNewEntry(f, e); return new Created(new AtomEntryRepresentation(e), entryUri); } private String getThisUriPath() { return context.getHttpRequestContext().getURI().toString(); } private URI getUri(String path) { return context.getHttpRequestContext().getURI().resolve(path); } private void addLink(Entry e, String rel, String uri) { Link l = new Link(); l.setRel(rel); l.setHref(uri); e.getOtherLinks().add(l); } }