2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package javax.servlet;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.HashMap;
22 import java.util.HashSet;
23 import java.util.List;
26 import javax.servlet.annotation.HttpMethodConstraint;
27 import javax.servlet.annotation.ServletSecurity;
32 * TODO SERVLET3 - Add comments
34 public class ServletSecurityElement extends HttpConstraintElement {
36 private final Map<String,HttpMethodConstraintElement> methodConstraints =
40 * Use default HttpConstraint.
42 public ServletSecurityElement() {
47 * Use specified HttpConstraintElement.
48 * @param httpConstraintElement
50 public ServletSecurityElement(HttpConstraintElement httpConstraintElement) {
51 this (httpConstraintElement, null);
55 * Use specific constraints for specified methods and default
56 * HttpConstraintElement for all other methods.
57 * @param httpMethodConstraints
58 * @throws IllegalArgumentException if a method name is specified more than
61 public ServletSecurityElement(
62 Collection<HttpMethodConstraintElement> httpMethodConstraints) {
64 addHttpMethodConstraints(httpMethodConstraints);
69 * Use specified HttpConstraintElement as default and specific constraints
70 * for specified methods.
71 * @param httpConstraintElement
72 * @param httpMethodConstraints
73 * @throws IllegalArgumentException if a method name is specified more than
75 public ServletSecurityElement(HttpConstraintElement httpConstraintElement,
76 Collection<HttpMethodConstraintElement> httpMethodConstraints) {
77 super(httpConstraintElement.getEmptyRoleSemantic(),
78 httpConstraintElement.getTransportGuarantee(),
79 httpConstraintElement.getRolesAllowed());
80 addHttpMethodConstraints(httpMethodConstraints);
84 * Create from an annotation.
86 * @throws IllegalArgumentException if a method name is specified more than
88 public ServletSecurityElement(ServletSecurity annotation) {
89 this(new HttpConstraintElement(annotation.value().value(),
90 annotation.value().transportGuarantee(),
91 annotation.value().rolesAllowed()));
93 List<HttpMethodConstraintElement> l = new ArrayList<>();
94 HttpMethodConstraint[] constraints = annotation.httpMethodConstraints();
95 if (constraints != null) {
96 for (int i = 0; i < constraints.length; i++) {
97 HttpMethodConstraintElement e =
98 new HttpMethodConstraintElement(constraints[i].value(),
99 new HttpConstraintElement(
100 constraints[i].emptyRoleSemantic(),
101 constraints[i].transportGuarantee(),
102 constraints[i].rolesAllowed()));
106 addHttpMethodConstraints(l);
109 public Collection<HttpMethodConstraintElement> getHttpMethodConstraints() {
110 Collection<HttpMethodConstraintElement> result = new HashSet<>();
111 result.addAll(methodConstraints.values());
115 public Collection<String> getMethodNames() {
116 Collection<String> result = new HashSet<>();
117 result.addAll(methodConstraints.keySet());
121 private void addHttpMethodConstraints(
122 Collection<HttpMethodConstraintElement> httpMethodConstraints) {
123 if (httpMethodConstraints == null) {
126 for (HttpMethodConstraintElement constraint : httpMethodConstraints) {
127 String method = constraint.getMethodName();
128 if (methodConstraints.containsKey(method)) {
129 throw new IllegalArgumentException(
130 "Duplicate method name: " + method);
132 methodConstraints.put(method, constraint);