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.tutorial;
20:
21: import org.restlet.Client;
22: import org.restlet.data.ChallengeResponse;
23: import org.restlet.data.ChallengeScheme;
24: import org.restlet.data.Method;
25: import org.restlet.data.Protocol;
26: import org.restlet.data.Request;
27: import org.restlet.data.Response;
28: import org.restlet.data.Status;
29:
30: /**
31: * Authenticating to an HTTP server.
32: *
33: * @author Jerome Louvel (contact@noelios.com)
34: */
35: public class Part09b {
36: public static void main(String[] args) throws Exception {
37: // Prepare the request
38: Request request = new Request(Method.GET,
39: "http://localhost:8182/");
40:
41: // Add the client authentication to the call
42: ChallengeScheme scheme = ChallengeScheme.HTTP_BASIC;
43: ChallengeResponse authentication = new ChallengeResponse(
44: scheme, "scott", "tiger");
45: request.setChallengeResponse(authentication);
46:
47: // Ask to the HTTP client connector to handle the call
48: Client client = new Client(Protocol.HTTP);
49: Response response = client.handle(request);
50:
51: if (response.getStatus().isSuccess()) {
52: // Output the response entity on the JVM console
53: response.getEntity().write(System.out);
54: } else if (response.getStatus().equals(
55: Status.CLIENT_ERROR_UNAUTHORIZED)) {
56: // Unauthorized access
57: System.out
58: .println("Access authorized by the server, check your credentials");
59: } else {
60: // Unexpected status
61: System.out.println("An unexpected status was returned: "
62: + response.getStatus());
63: }
64: }
65:
66: }
|