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.Restlet;
22: import org.restlet.Server;
23: import org.restlet.data.Form;
24: import org.restlet.data.MediaType;
25: import org.restlet.data.Parameter;
26: import org.restlet.data.Protocol;
27: import org.restlet.data.Request;
28: import org.restlet.data.Response;
29: import org.restlet.util.Series;
30:
31: /**
32: * Display the HTTP accept header sent by the Web browsers.
33: *
34: * @author Jerome Louvel (contact@noelios.com)
35: */
36: public class HeadersTest {
37: public static void main(String[] args) throws Exception {
38: Restlet restlet = new Restlet() {
39: @SuppressWarnings("unchecked")
40: @Override
41: public void handle(Request request, Response response) {
42: // ------------------------------
43: // Getting an HTTP request header
44: // ------------------------------
45: Series<Parameter> headers = (Series<Parameter>) request
46: .getAttributes()
47: .get("org.restlet.http.headers");
48:
49: // The headers list contains all received HTTP headers, in raw
50: // format.
51: // Below, we simply display the standard "Accept" HTTP header.
52: response.setEntity("Accept header: "
53: + headers.getFirstValue("accept", true),
54: MediaType.TEXT_PLAIN);
55:
56: // -----------------------
57: // Adding response headers
58: // -----------------------
59: headers = new Form();
60:
61: // Non-standard headers are allowed
62: headers.add("X-Test", "Test value");
63:
64: // Standard HTTP headers are forbidden. If you happen to add one
65: // like the "Location"
66: // header below, it will be ignored and a warning message will
67: // be displayed in the logs.
68: headers.add("Location", "http://www.restlet.org");
69:
70: // Setting the additional headers into the shared call's
71: // attribute
72: response.getAttributes().put(
73: "org.restlet.http.headers", headers);
74: }
75: };
76:
77: // Create the HTTP server and listen on port 8182
78: Server server = new Server(Protocol.HTTP, 8182, restlet);
79: server.start();
80: }
81:
82: }
|