1 /**
2 *
3 * Copyright 2003-2004 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
19
20
21
22
23
24 package javax.security.jacc;
25
26
27 /**
28 * @version $Rev: 159088 $ $Date: 2005-03-25 18:18:03 -0800 (Fri, 25 Mar 2005) $
29 */
30 final class HTTPMethodSpec {
31
32 private final static String[] HTTP_METHODS = {"GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS", "TRACE"};
33 private final static int[] HTTP_MASKS = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40};
34
35 final static int NA = 0x00;
36 final static int INTEGRAL = 0x01;
37 final static int CONFIDENTIAL = 0x02;
38 final static int NONE = INTEGRAL | CONFIDENTIAL;
39
40 private final int mask;
41 private final int transport;
42 private String actions;
43
44 public HTTPMethodSpec(String[] HTTPMethods) {
45 this(HTTPMethods, null);
46 }
47
48 public HTTPMethodSpec(String name, boolean parseTransportType) {
49 if (parseTransportType) {
50 if (name == null || name.length() == 0) {
51 this.transport = NONE;
52 } else {
53 String[] tokens = name.split(":", 2);
54
55 if (tokens.length == 2) {
56 if (tokens[1].equals("NONE")) {
57 this.transport = NONE;
58 } else if (tokens[1].equals("INTEGRAL")) {
59 this.transport = INTEGRAL;
60 } else if (tokens[1].equals("CONFIDENTIAL")) {
61 this.transport = CONFIDENTIAL;
62 } else {
63 throw new IllegalArgumentException("Invalid transportType: " + tokens[1]);
64 }
65 } else {
66 this.transport = NONE;
67 }
68 name = tokens[0];
69 }
70 } else {
71 this.transport = NA;
72 }
73
74 if (name == null || name.length() == 0) {
75 this.mask = 0x7F;
76 } else {
77 String[] methods = name.split(",", -1);
78 int tmpMask = 0;
79
80 for (int i = 0; i < methods.length; i++) {
81 boolean found = false;
82
83 for (int j = 0; j < HTTP_METHODS.length; j++) {
84 if (methods[i].equals(HTTP_METHODS[j])) {
85 tmpMask |= HTTP_MASKS[j];
86 found = true;
87
88 break;
89 }
90 }
91 if (!found) throw new IllegalArgumentException("Invalid HTTPMethodSpec");
92 }
93 this.mask = tmpMask;
94 }
95 }
96
97 public HTTPMethodSpec(String[] HTTPMethods, String transport) {
98 boolean parseTransportType = transport != null;
99
100 if (HTTPMethods == null || HTTPMethods.length == 0) {
101 this.mask = 0x7F;
102 } else {
103 int tmpMask = 0;
104
105 for (int i = 0; i < HTTPMethods.length; i++) {
106
107 for (int j = 0; j < HTTP_METHODS.length; j++) {
108 if (HTTPMethods[i].equals(HTTP_METHODS[j])) {
109 tmpMask |= HTTP_MASKS[j];
110
111 break;
112 }
113 }
114 if (tmpMask == 0) throw new IllegalArgumentException("Invalid HTTPMethodSpec");
115 }
116 this.mask = tmpMask;
117 }
118
119 if (parseTransportType) {
120 if (transport.length() == 0 || transport.equals("NONE")) {
121 this.transport = NONE;
122 } else if (transport.equals("INTEGRAL")) {
123 this.transport = INTEGRAL;
124 } else if (transport.equals("CONFIDENTIAL")) {
125 this.transport = CONFIDENTIAL;
126 } else {
127 throw new IllegalArgumentException("Invalid transport");
128 }
129 } else {
130 this.transport = NA;
131 }
132 }
133
134 public HTTPMethodSpec(String singleMethod, int transport) {
135 int tmpMask = 0;
136
137 for (int j = 0; j < HTTP_METHODS.length; j++) {
138 if (HTTP_METHODS[j].equals(singleMethod)) {
139 tmpMask = HTTP_MASKS[j];
140
141 break;
142 }
143 }
144 if (tmpMask == 0) throw new IllegalArgumentException("Invalid HTTPMethodSpec");
145 this.mask = tmpMask;
146 this.transport = transport;
147 }
148
149 public boolean equals(HTTPMethodSpec o) {
150 return mask == o.mask && transport == o.transport;
151 }
152
153 public String getActions() {
154 if (actions == null) {
155 boolean first = true;
156 StringBuffer buffer = new StringBuffer();
157
158 for (int i = 0; i < HTTP_MASKS.length; i++) {
159 if ((mask & HTTP_MASKS[i]) > 0) {
160 if (first) {
161 first = false;
162 } else {
163 buffer.append(",");
164 }
165 buffer.append(HTTP_METHODS[i]);
166 }
167 }
168
169 if (transport == INTEGRAL) {
170 buffer.append(":INTEGRAL");
171 } else if (transport == CONFIDENTIAL) {
172 buffer.append(":CONFIDENTIAL");
173 }
174
175 actions = buffer.toString();
176 }
177 return actions;
178 }
179
180 public int hashCode() {
181 return mask ^ transport;
182 }
183
184 public boolean implies(HTTPMethodSpec p) {
185 return ((mask & p.mask) == p.mask) && ((transport & p.transport) == p.transport);
186 }
187 }