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.misc;
20:
21: import org.restlet.Component;
22: import org.restlet.Restlet;
23: import org.restlet.data.Form;
24: import org.restlet.data.MediaType;
25: import org.restlet.data.Method;
26: import org.restlet.data.Parameter;
27: import org.restlet.data.Protocol;
28: import org.restlet.data.Request;
29: import org.restlet.data.Response;
30: import org.restlet.data.Status;
31:
32: /**
33: * Simple HTTP server invoked by the simple client.
34: *
35: * @author Jerome Louvel (contact@noelios.com)
36: */
37: public class SimpleServer {
38: public static void main(String[] args) {
39: try {
40: // Create a new Restlet component
41: Component component = new Component();
42:
43: // Create the HTTP server connector, then add it as a server
44: // connector to the Restlet component. Note that the component
45: // is the call restlet.
46: component.getServers().add(Protocol.HTTP, 9876);
47:
48: // Prepare and attach a test Handler
49: Restlet handler = new Restlet(component.getContext()) {
50: @Override
51: public void handle(Request request, Response response) {
52: if (request.getMethod().equals(Method.PUT)) {
53: System.out.println("Handling the call...");
54: System.out
55: .println("Trying to get the entity as a form...");
56: Form form = request.getEntityAsForm();
57:
58: System.out
59: .println("Trying to getParameters...");
60: StringBuffer sb = new StringBuffer("foo");
61: for (Parameter p : form) {
62: System.out.println(p);
63:
64: sb.append("field name = ");
65: sb.append(p.getName());
66: sb.append("value = ");
67: sb.append(p.getValue());
68: sb.append("\n");
69: System.out.println(sb.toString());
70: }
71:
72: response.setEntity(sb.toString(),
73: MediaType.TEXT_PLAIN);
74: System.out.println("Done!");
75: } else {
76: response
77: .setStatus(Status.SERVER_ERROR_NOT_IMPLEMENTED);
78: }
79: }
80: };
81:
82: component.getDefaultHost().attach("/test", handler);
83:
84: // Now, start the component
85: component.start();
86: } catch (Exception e) {
87: e.printStackTrace();
88: }
89: }
90: }
|