001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.servicemix.jbi.security.acl.impl;
018    
019    import java.util.HashSet;
020    import java.util.Iterator;
021    import java.util.Set;
022    import java.util.StringTokenizer;
023    
024    import javax.xml.XMLConstants;
025    import javax.xml.namespace.QName;
026    
027    import org.apache.servicemix.jbi.security.GroupPrincipal;
028    
029    /**
030     * 
031     * @author gnodet
032     * @org.apache.xbean.XBean 
033     */
034    public class AuthorizationEntry {
035        
036        /**
037         * Add the roles to the ACLs list
038         */
039        public static final String TYPE_ADD = "add";
040        /**
041         * Set the ACLs to the given roles
042         */
043        public static final String TYPE_SET = "set";
044        /**
045         * Remove the given roles from the ACLs list
046         */
047        public static final String TYPE_REM = "rem";
048    
049        private Set<GroupPrincipal> acls;
050        private QName service;
051        private String endpoint;
052        private QName operation;
053        private String type = TYPE_ADD;
054    
055        public AuthorizationEntry() {
056        }
057        
058        public AuthorizationEntry(QName service, String endpoint, QName operation, String roles) {
059            this.service = service;
060            this.endpoint = endpoint;
061            this.operation = operation;
062            this.acls = buildRoles(roles);
063        }
064        
065        public AuthorizationEntry(QName service, String endpoint, QName operation, String roles, String type) {
066            this.service = service;
067            this.endpoint = endpoint;
068            this.operation = operation;
069            this.acls = buildRoles(roles);
070            this.type = type;
071        }
072        
073        /**
074         * @return the type
075         */
076        public String getType() {
077            return type;
078        }
079    
080        /**
081         * @param type the type to set
082         */
083        public void setType(String type) {
084            this.type = type;
085        }
086    
087        /**
088         * @return the endpoint
089         */
090        public String getEndpoint() {
091            return endpoint;
092        }
093    
094        /**
095         * @param endpoint the endpoint to set
096         */
097        public void setEndpoint(String endpoint) {
098            this.endpoint = endpoint;
099        }
100    
101        /**
102         * @return the service
103         */
104        public QName getService() {
105            return service;
106        }
107    
108        /**
109         * @param service the service to set
110         */
111        public void setService(QName service) {
112            // Hack a bit to support wildcards
113            // If the attribute was service="*:*", then the namespace is not found, but the prefix is set
114            if (XMLConstants.NULL_NS_URI.equals(service.getNamespaceURI())
115                            && service.getPrefix() != null && service.getPrefix().length() > 0) {
116                service = new QName(service.getPrefix(), service.getLocalPart());
117            }
118            this.service = service;
119        }
120    
121        /**
122         * @return the operation
123         */
124        public QName getOperation() {
125            return operation;
126        }
127    
128        /**
129         * @param operation the operation to set
130         */
131        public void setOperation(QName operation) {
132            this.operation = operation;
133        }
134    
135        /**
136         * @return the acls
137         */
138        public Set<GroupPrincipal> getAcls() {
139            return acls;
140        }
141    
142        /**
143         * @param acls the acls to set
144         */
145        public void setAcls(Set<GroupPrincipal> acls) {
146            this.acls = acls;
147        }
148        
149        public void setRoles(String roles) {
150            this.acls = buildRoles(roles);
151        }
152        
153        public String getRoles() {
154            StringBuffer sb = new StringBuffer();
155            if (this.acls != null) {
156                for (Iterator<GroupPrincipal> iter = this.acls.iterator(); iter.hasNext();) {
157                    GroupPrincipal p = iter.next();
158                    sb.append(p);
159                    if (iter.hasNext()) {
160                        sb.append(",");
161                    }
162                }
163            }
164            return sb.toString();
165        }
166        
167        public String toString() {
168            return "AuthorizationEntry[service=" + service + ", endpoint=" + endpoint + ", roles=" + getRoles() + "]";
169        }
170        
171        private Set<GroupPrincipal> buildRoles(String roles) {
172            Set<GroupPrincipal> s = new HashSet<GroupPrincipal>();
173            StringTokenizer iter = new StringTokenizer(roles, ",");
174            while (iter.hasMoreTokens()) {
175                String name = iter.nextToken().trim();
176                s.add(new GroupPrincipal(name));
177            }
178            return s;
179        }
180    }