Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
HelloWorld |
|
| 1.5;1.5 |
1 | /* | |
2 | * Copyright (c) 2009, Swiss Federal Department of Defence Civil Protection and Sport | |
3 | * (http://www.vbs.admin.ch) | |
4 | * Copyright (c) 2009, Ergon Informatik AG (http://www.ergon.ch) | |
5 | * All rights reserved. | |
6 | * | |
7 | * Licensed under the Open Permis License which accompanies this distribution, | |
8 | * and is available at http://www.openpermis.org/BSDlicenceKent.txt | |
9 | */ | |
10 | package org.openpermis.examples.xacml; | |
11 | ||
12 | import java.io.IOException; | |
13 | import java.io.InputStreamReader; | |
14 | import java.io.Reader; | |
15 | import java.io.StringWriter; | |
16 | ||
17 | import org.openpermis.AuthorizationService; | |
18 | import org.openpermis.xacml.XacmlAuthorizationService; | |
19 | import org.openpermis.xacml.basic.BasicXacmlAuthorizationService; | |
20 | ||
21 | ||
22 | /** | |
23 | * A sample HelloWorld application that can be invoked from the command line. | |
24 | * @since 0.4.0 | |
25 | */ | |
26 | public final class HelloWorld { | |
27 | ||
28 | //---- Static | |
29 | ||
30 | /** | |
31 | * This main method makes a simple request. | |
32 | * @since 0.4.0 | |
33 | */ | |
34 | public static void main (String[] args) { | |
35 | ||
36 | 0 | final XacmlAuthorizationService xacmlAuthService = createService(); |
37 | ||
38 | // Preparing the xacml authorization request. | |
39 | 0 | final Reader request = new InputStreamReader( |
40 | HelloWorld.class.getResourceAsStream("request.xml") | |
41 | ); | |
42 | ||
43 | try { | |
44 | // Preparting the writer for the xacml respone. | |
45 | 0 | final StringWriter response = new StringWriter(); |
46 | ||
47 | // Ask the xacml authorization service for an access decision. | |
48 | 0 | xacmlAuthService.getAccessDescision(request, response); |
49 | ||
50 | ||
51 | // Prints the request and the response to the standard out. | |
52 | 0 | System.out.println( |
53 | "XACML Request:\n" + | |
54 | convertStreamToString( | |
55 | new InputStreamReader( | |
56 | HelloWorld.class.getResourceAsStream("request.xml") | |
57 | ) | |
58 | ) | |
59 | ); | |
60 | 0 | System.out.println("XACML Response:\n" + response.toString()); |
61 | ||
62 | 0 | } catch (IOException e) { |
63 | 0 | System.out.println("Could not read request or write response."); |
64 | ||
65 | 0 | } |
66 | ||
67 | 0 | } |
68 | ||
69 | /** | |
70 | * This main method creates a xacml authorization service. | |
71 | * @since 0.4.0 | |
72 | */ | |
73 | public static XacmlAuthorizationService createService () { | |
74 | // First we need a authorization service. To keep this example simple we provide a dummy | |
75 | // authorization service without a repository and a policy. | |
76 | // See the other examples for details about creating a real authorization service. | |
77 | 0 | final AuthorizationService authService = new DummyAuthorizationService(); |
78 | ||
79 | // Second we create the xacml authorization service, which is based on the authorization | |
80 | // service we've previously created. | |
81 | 0 | final XacmlAuthorizationService xacmlAuthService = |
82 | new BasicXacmlAuthorizationService(authService); | |
83 | 0 | return xacmlAuthService; |
84 | } | |
85 | ||
86 | private static final int BUF_LEN = 1000; | |
87 | ||
88 | /** | |
89 | * Converts a {@link Reader} to a {@link String}. | |
90 | * @param reader a reader | |
91 | * @return a string. | |
92 | * @throws IOException signals a reading problem. | |
93 | * @since 0.4.0 | |
94 | */ | |
95 | private static String convertStreamToString (Reader reader) throws IOException { | |
96 | 0 | StringBuilder builder = new StringBuilder(); |
97 | 0 | char[] buffer = new char[BUF_LEN]; |
98 | int len; | |
99 | 0 | while ((len = reader.read(buffer)) != -1) { |
100 | 0 | builder.append(buffer, 0, len); |
101 | } | |
102 | 0 | return builder.toString(); |
103 | } | |
104 | ||
105 | //---- Constructors | |
106 | ||
107 | /** | |
108 | * Objects of this class cannot be instantiated. | |
109 | * @since 0.4.0 | |
110 | */ | |
111 | private HelloWorld () { | |
112 | 0 | super(); |
113 | 0 | } |
114 | ||
115 | } |