001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020
021 package javax.servlet;
022
023 import java.util.ArrayList;
024 import java.util.Collection;
025 import java.util.Collections;
026 import java.util.LinkedHashSet;
027
028 import javax.servlet.annotation.HttpMethodConstraint;
029 import javax.servlet.annotation.ServletSecurity;
030
031 /**
032 * @version $Rev: 916436 $ $Date: 2010-02-25 15:18:43 -0500 (Thu, 25 Feb 2010) $
033 * @since 3.0
034 */
035 public class ServletSecurityElement extends HttpConstraintElement {
036
037 private final Collection<HttpMethodConstraintElement> httpMethodConstraints;
038
039 private final Collection<String> methodNames;
040
041 public ServletSecurityElement() {
042 httpMethodConstraints = Collections.emptySet();
043 methodNames = Collections.emptySet();
044 }
045
046 public ServletSecurityElement(HttpConstraintElement defaultHttpConstraintElement) {
047 super(defaultHttpConstraintElement.getEmptyRoleSemantic(), defaultHttpConstraintElement.getTransportGuarantee(), defaultHttpConstraintElement.getRolesAllowed());
048 httpMethodConstraints = Collections.emptySet();
049 methodNames = Collections.emptySet();
050 }
051
052 public ServletSecurityElement(Collection<HttpMethodConstraintElement> httpMethodConstraints) throws IllegalArgumentException {
053 this.httpMethodConstraints = httpMethodConstraints;
054 this.methodNames = toMethodNames(httpMethodConstraints);
055 }
056
057 public ServletSecurityElement(HttpConstraintElement httpConstraintElement, Collection<HttpMethodConstraintElement> httpMethodConstraints) throws IllegalArgumentException {
058 super(httpConstraintElement.getEmptyRoleSemantic(), httpConstraintElement.getTransportGuarantee(), httpConstraintElement.getRolesAllowed());
059 this.httpMethodConstraints = Collections.unmodifiableCollection(httpMethodConstraints);
060 this.methodNames = toMethodNames(httpMethodConstraints);
061 }
062
063 public ServletSecurityElement(ServletSecurity servletSecurity) throws IllegalArgumentException {
064 super(servletSecurity.value().value(), servletSecurity.value().transportGuarantee(), servletSecurity.value().rolesAllowed());
065 Collection<HttpMethodConstraintElement> httpMethodConstraints = new ArrayList<HttpMethodConstraintElement>();
066 for (HttpMethodConstraint constraint: servletSecurity.httpMethodConstraints()) {
067 httpMethodConstraints.add(new HttpMethodConstraintElement(constraint.value(),
068 new HttpConstraintElement(constraint.emptyRoleSemantic(),
069 constraint.transportGuarantee(),
070 constraint.rolesAllowed())));
071 }
072 this.httpMethodConstraints = Collections.unmodifiableCollection(httpMethodConstraints);
073 methodNames = toMethodNames(httpMethodConstraints);
074 }
075
076 public Collection<HttpMethodConstraintElement> getHttpMethodConstraints() {
077 return httpMethodConstraints;
078 }
079
080 public Collection<String> getMethodNames() {
081 return methodNames;
082 }
083
084 private static Collection<String> toMethodNames(Collection<HttpMethodConstraintElement> constraints) throws IllegalArgumentException {
085 Collection<String> methodNames = new LinkedHashSet<String>(constraints.size());
086 for (HttpMethodConstraintElement constraint: constraints) {
087 if (!methodNames.add(constraint.getMethodName())) {
088 throw new IllegalArgumentException("duplicate method name: " + constraint.getMethodName());
089 }
090 }
091 return Collections.unmodifiableCollection(methodNames);
092 }
093 }