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 java.io.File;
22:
23: import org.restlet.Component;
24: import org.restlet.Restlet;
25: import org.restlet.Route;
26: import org.restlet.Router;
27: import org.restlet.data.Protocol;
28: import org.restlet.util.Variable;
29:
30: import com.db4o.Db4o;
31: import com.db4o.ObjectContainer;
32: import com.db4o.config.Configuration;
33:
34: /**
35: * The main Web application.
36: *
37: * @author Jerome Louvel (contact@noelios.com)
38: */
39: public class Application extends org.restlet.Application {
40:
41: public static void main(String... args) throws Exception {
42: // Create a component with an HTTP server connector
43: Component comp = new Component();
44: comp.getServers().add(Protocol.HTTP, 3000);
45:
46: // Attach the application to the default host and start it
47: comp.getDefaultHost().attach("/v1", new Application());
48: comp.start();
49: }
50:
51: private ObjectContainer container;
52:
53: public Application() {
54: /** Open and keep the db4o object container. */
55: Configuration config = Db4o.configure();
56: config.updateDepth(2);
57: this .container = Db4o.openFile(System.getProperty("user.home")
58: + File.separator + "restbook.dbo");
59: }
60:
61: @Override
62: public Restlet createRoot() {
63: Router router = new Router(getContext());
64:
65: // Add a route for user resources
66: router.attach("/users/{username}", UserResource.class);
67:
68: // Add a route for user's bookmarks resources
69: router.attach("/users/{username}/bookmarks",
70: BookmarksResource.class);
71:
72: // Add a route for bookmark resources
73: Route uriRoute = router.attach(
74: "/users/{username}/bookmarks/{URI}",
75: BookmarkResource.class);
76: uriRoute.getTemplate().getVariables().put("URI",
77: new Variable(Variable.TYPE_URI_ALL));
78:
79: return router;
80: }
81:
82: /**
83: * Returns the database container.
84: *
85: * @return the database container.
86: */
87: public ObjectContainer getContainer() {
88: return this.container;
89: }
90:
91: }
|