01: /*
02: * Copyright 2005-2007 Noelios Consulting.
03: *
04: * The contents of this file are subject to the terms of the Common Development
05: * and Distribution License (the "License"). You may not use this file except in
06: * compliance with the License.
07: *
08: * You can obtain a copy of the license at
09: * http://www.opensource.org/licenses/cddl1.txt See the License for the specific
10: * language governing permissions and limitations under the License.
11: *
12: * When distributing Covered Code, include this CDDL HEADER in each file and
13: * include the License file at http://www.opensource.org/licenses/cddl1.txt If
14: * applicable, add the following below this CDDL HEADER, with the fields
15: * enclosed by brackets "[]" replaced with your own identifying information:
16: * Portions Copyright [yyyy] [name of copyright owner]
17: */
18:
19: package org.restlet.example.book.rest.ch7;
20:
21: import org.restlet.Context;
22: import org.restlet.data.MediaType;
23: import org.restlet.data.Reference;
24: import org.restlet.data.ReferenceList;
25: import org.restlet.data.Request;
26: import org.restlet.data.Response;
27: import org.restlet.resource.Representation;
28: import org.restlet.resource.Variant;
29:
30: /**
31: * Resource for a user's list of bookmarks.
32: *
33: * @author Jerome Louvel (contact@noelios.com)
34: */
35: public class BookmarksResource extends UserResource {
36:
37: /**
38: * Constructor.
39: *
40: * @param context
41: * The parent context.
42: * @param request
43: * The request to handle.
44: * @param response
45: * The response to return.
46: */
47: public BookmarksResource(Context context, Request request,
48: Response response) {
49: super (context, request, response);
50: getVariants().clear();
51: if (getUser() != null) {
52: getVariants().add(new Variant(MediaType.TEXT_HTML));
53: }
54: }
55:
56: @Override
57: public Representation getRepresentation(Variant variant) {
58: Representation result = null;
59:
60: if (variant.getMediaType().equals(MediaType.TEXT_HTML)) {
61: int code = checkAuthorization();
62: ReferenceList rl = new ReferenceList();
63:
64: // Copy the bookmark URIs into a reference list. Make sure that we
65: // only expose public bookmarks if the client isn't the owner.
66: for (Bookmark bookmark : getUser().getBookmarks()) {
67: if (!bookmark.isRestrict() || (code == 1)) {
68: rl.add(bookmark.getUri());
69: }
70: }
71:
72: result = rl.getWebRepresentation();
73: }
74:
75: return result;
76: }
77:
78: @Override
79: public void handleGet() {
80: // Make sure that the Uri ends with a "/" without changing the query.
81: // This is helpful when exposing the list of relative references of the
82: // bookmarks.
83: Reference ref = getRequest().getResourceRef();
84: if (!ref.getPath().endsWith("/")) {
85: ref.setPath(ref.getPath() + "/");
86: getResponse().redirectPermanent(ref);
87: } else {
88: super.handleGet();
89: }
90: }
91:
92: }
|