package com.sun.ws.rest.samples.atomserver.resources; 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.UriParam; import com.sun.ws.rest.api.UriTemplate; import com.sun.ws.rest.api.core.HttpContext; import java.util.Date; import java.util.List; import javax.annotation.Resource; /** * * @author Paul.Sandoz@Sun.Com */ @UriTemplate("edit/{entry}") @ConsumeMime("application/atom+xml") @ProduceMime("application/atom+xml") public class EditEntryResource extends EntryResource { @Resource HttpContext context; @HttpMethod public void putEntry(@UriParam("entry") String entryId, Entry e) { String entryFilePath = FileHelper.getEntryPath(entryId); FileHelper.checkExistence(entryFilePath); String entryUri = getUri(entryId); String editEntryUri = getUri("edit/" + entryId); updateLink(e, "self", entryUri); updateLink(e, "edit", editEntryUri); if (FileHelper.hasMedia(entryId)) { String mediaUri = getUri(entryId + "/media"); String editMediaUri = getUri("edit/" + entryId + "/media"); updateLink(e, "edit-media", editMediaUri); } e.setId(entryId); // Write out the entry document FileHelper.createEntryDocument(entryId, e); // Update the feed document with the entry Feed f = FileHelper.getFeedDocument(); FileHelper.updateFeedDocumentWithExistingEntry(f, e); } private void updateLink(Entry e, String rel, String uri) { List links = e.getOtherLinks(); Link l = null; for (Object o : links) { l = (Link)o; if (l.getRel().equals(rel)) { links.remove(l); break; } } l = new Link(); l.setRel(rel); l.setHref(uri); links.add(l); } private String getUri(String path) { return context.getHttpRequestContext().getBaseURI() + "collection/" + path; } @HttpMethod public void deleteEntry(@UriParam("entry") String entryId) { String path = FileHelper.getPath(entryId); FileHelper.checkExistence(path); String entryPath = FileHelper.getEntryPath(entryId); String mediaPath = FileHelper.getMediaPath(entryId); FileStore.FS.deleteFile(mediaPath); FileStore.FS.deleteFile(entryPath); FileStore.FS.deleteFile(path); Feed f = FileHelper.getFeedDocument(); FileHelper.updateFeedDocumentRemovingEntry(f, entryId); } @HttpMethod @UriTemplate("media") @ConsumeMime("*/*") public void putMedia(@UriParam("entry") String entryId, Entity update) { String mediaPath = FileHelper.getMediaPath(entryId); FileHelper.checkExistence(mediaPath); Feed f = FileHelper.getFeedDocument(); Entry e = FileHelper.findEntry(entryId, f); // Update the entry e.setUpdated(new Date()); // Write out the feed document FileHelper.updateFeedDocument(f); // Write out the entry document FileHelper.createEntryDocument(entryId, e); // Write out the media FileHelper.createMediaDocument(entryId, update.getContent()); } }