001: /*
002: * Copyright 2005-2007 Noelios Consulting.
003: *
004: * The contents of this file are subject to the terms of the Common Development
005: * and Distribution License (the "License"). You may not use this file except in
006: * compliance with the License.
007: *
008: * You can obtain a copy of the license at
009: * http://www.opensource.org/licenses/cddl1.txt See the License for the specific
010: * language governing permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL HEADER in each file and
013: * include the License file at http://www.opensource.org/licenses/cddl1.txt If
014: * applicable, add the following below this CDDL HEADER, with the fields
015: * enclosed by brackets "[]" replaced with your own identifying information:
016: * Portions Copyright [yyyy] [name of copyright owner]
017: */
018:
019: package org.restlet.example.book.rest.ch7;
020:
021: import java.util.Date;
022:
023: import org.restlet.Context;
024: import org.restlet.data.Form;
025: import org.restlet.data.MediaType;
026: import org.restlet.data.Request;
027: import org.restlet.data.Response;
028: import org.restlet.data.Status;
029: import org.restlet.resource.Representation;
030: import org.restlet.resource.StringRepresentation;
031: import org.restlet.resource.Variant;
032:
033: /**
034: * Resource for a user's bookmark.
035: *
036: * @author Jerome Louvel (contact@noelios.com)
037: */
038: public class BookmarkResource extends UserResource {
039:
040: private Bookmark bookmark;
041:
042: private String uri;
043:
044: /**
045: * Constructor.
046: *
047: * @param context
048: * The parent context.
049: * @param request
050: * The request to handle.
051: * @param response
052: * The response to return.
053: */
054: public BookmarkResource(Context context, Request request,
055: Response response) {
056: super (context, request, response);
057:
058: if (getUser() != null) {
059: this .uri = (String) request.getAttributes().get("URI");
060: this .bookmark = getUser().getBookmark(this .uri);
061:
062: if (this .bookmark != null) {
063: if ((checkAuthorization() != 1)
064: && this .bookmark.isRestrict()) {
065: // Intentionnally hide the bookmark existence
066: getResponse().setStatus(
067: Status.CLIENT_ERROR_NOT_FOUND);
068: }
069: } else {
070: // Bookmark not found, remove the variant added by the super
071: // class (UserResource).
072: getVariants().clear();
073: }
074: } else {
075: // User not found
076: getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
077: }
078: }
079:
080: @Override
081: public boolean allowDelete() {
082: return true;
083: }
084:
085: @Override
086: public boolean allowPut() {
087: return true;
088: }
089:
090: @Override
091: public void delete() {
092: if ((this .bookmark != null) && (checkAuthorization() == 1)) {
093: // Delete the bookmark
094: getUser().getBookmarks().remove(this .bookmark);
095: getContainer().delete(this .bookmark);
096: getContainer().set(getUser());
097: getContainer().commit();
098: getResponse().setStatus(Status.SUCCESS_OK);
099: } else {
100: // Intentionnally hide the bookmark existence
101: getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
102: }
103: }
104:
105: @Override
106: public Representation getRepresentation(Variant variant) {
107: Representation result = null;
108:
109: if (variant.getMediaType().equals(MediaType.TEXT_PLAIN)) {
110: // Creates a text representation
111: StringBuilder sb = new StringBuilder();
112: sb.append("----------------\n");
113: sb.append("Bookmark details\n");
114: sb.append("----------------\n\n");
115: sb.append("User: ").append(
116: this .bookmark.getUser().getName()).append('\n');
117: sb.append("URI: ").append(this .bookmark.getUri())
118: .append('\n');
119: sb.append("Short: ").append(
120: this .bookmark.getShortDescription()).append('\n');
121: sb.append("Long: ").append(
122: this .bookmark.getLongDescription()).append('\n');
123: sb.append("Date: ").append(this .bookmark.getDateTime())
124: .append('\n');
125: sb.append("Restrict: ").append(
126: Boolean.toString(this .bookmark.isRestrict()))
127: .append('\n');
128: result = new StringRepresentation(sb);
129: }
130:
131: return result;
132: }
133:
134: @Override
135: public void put(Representation entity) {
136: if (checkAuthorization() == 1) {
137: if (entity.getMediaType().equals(
138: MediaType.APPLICATION_WWW_FORM, true)) {
139: // Parse the entity as a web form
140: Form form = new Form(entity);
141:
142: // If the bookmark doesn't exist, create it
143: if (this .bookmark == null) {
144: this .bookmark = new Bookmark(getUser(), this .uri);
145: getUser().getBookmarks().add(this .bookmark);
146: getResponse().setStatus(Status.SUCCESS_CREATED);
147: } else {
148: getResponse().setStatus(Status.SUCCESS_NO_CONTENT);
149: }
150:
151: // Update the bookmark
152: this .bookmark.setShortDescription(form
153: .getFirstValue("bookmark[short_description]"));
154: this .bookmark.setLongDescription(form
155: .getFirstValue("bookmark[long_description]"));
156: this .bookmark.setDateTime(new Date());
157: this .bookmark.setRestrict(new Boolean(form
158: .getFirstValue("bookmark[restrict]")));
159:
160: // Commit the changes
161: getContainer().set(this .bookmark);
162: getContainer().set(getUser());
163: getContainer().commit();
164: }
165: } else {
166: // Intentionnally hide the bookmark existence
167: getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
168: }
169: }
170:
171: }
|