View Javadoc

1   /*
2    * Licensed to the University Corporation for Advanced Internet Development, 
3    * Inc. (UCAID) under one or more contributor license agreements.  See the 
4    * NOTICE file distributed with this work for additional information regarding
5    * copyright ownership. The UCAID licenses this file to You under the Apache 
6    * License, Version 2.0 (the "License"); you may not use this file except in 
7    * compliance with the License.  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  package edu.internet2.middleware.shibboleth.common.attribute.encoding.provider;
19  
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import javax.xml.namespace.QName;
24  
25  import org.opensaml.Configuration;
26  import org.opensaml.xml.XMLObject;
27  import org.opensaml.xml.XMLObjectBuilder;
28  import org.opensaml.xml.schema.XSString;
29  
30  import edu.internet2.middleware.shibboleth.common.attribute.BaseAttribute;
31  import edu.internet2.middleware.shibboleth.common.attribute.provider.ScopedAttributeValue;
32  import edu.internet2.middleware.shibboleth.common.xmlobject.ShibbolethScopedValue;
33  
34  /**
35   * Base class for scoped attribute encoders.
36   * 
37   * @param <EncodedType> the type of object created by encoding the attribute
38   */
39  public abstract class AbstractScopedAttributeEncoder<EncodedType> extends AbstractAttributeEncoder<EncodedType> {
40  
41      /** Builder of Shibboleth scoped value XMLObjects. */
42      private final XMLObjectBuilder<ShibbolethScopedValue> shibScopeValueBuilder = Configuration.getBuilderFactory()
43              .getBuilder(ShibbolethScopedValue.TYPE_NAME);
44  
45      /** Builder of string XMLObjects. */
46      private final XMLObjectBuilder<XSString> stringValueBuilder = Configuration.getBuilderFactory().getBuilder(
47              XSString.TYPE_NAME);
48  
49      /** Type of scoping to use. */
50      private String scopeType;
51  
52      /** Delimeter used for "inline" scopeType. */
53      private String scopeDelimiter;
54  
55      /** Attribute name used for "attribute" scopeType. */
56      private String scopeAttribute;
57  
58      /**
59       * Get the scope attribute.
60       * 
61       * @return Returns the scopeAttribute.
62       */
63      public String getScopeAttribute() {
64          return scopeAttribute;
65      }
66  
67      /**
68       * Get the scope delimiter.
69       * 
70       * @return Returns the scopeDelimiter.
71       */
72      public String getScopeDelimiter() {
73          return scopeDelimiter;
74      }
75  
76      /**
77       * Get the scope type.
78       * 
79       * @return Returns the scopeType.
80       */
81      public String getScopeType() {
82          return scopeType;
83      }
84  
85      /**
86       * Set the scope attribute.
87       * 
88       * @param newScopeAttribute The scopeAttribute to set.
89       */
90      public void setScopeAttribute(String newScopeAttribute) {
91          scopeAttribute = newScopeAttribute;
92      }
93  
94      /**
95       * Set the scope delimiter.
96       * 
97       * @param newScopeDelimiter The scopeDelimiter to set.
98       */
99      public void setScopeDelimiter(String newScopeDelimiter) {
100         scopeDelimiter = newScopeDelimiter;
101     }
102 
103     /**
104      * Set the scope type.
105      * 
106      * @param newScopeType The scopeType to set.
107      */
108     public void setScopeType(String newScopeType) {
109         scopeType = newScopeType;
110     }
111 
112     /**
113      * Encodes attributes whose values are scoped.
114      * 
115      * @param objectName name of the attribute value element to create for each value
116      * @param attribute the attribute whose values will be encoded
117      * 
118      * @return the list of encoded attribute values
119      */
120     @SuppressWarnings("unchecked")
121     protected List<XMLObject> encodeAttributeValues(QName objectName, BaseAttribute<ScopedAttributeValue> attribute) {
122         ArrayList<XMLObject> encodedValues = new ArrayList<XMLObject>();
123 
124         if ("attribute".equals(getScopeType())) {
125             ShibbolethScopedValue scopedValue;
126 
127             for (ScopedAttributeValue attributeValue : attribute.getValues()) {
128                 if (attributeValue == null) {
129                     continue;
130                 }
131 
132                 scopedValue = shibScopeValueBuilder.buildObject(objectName);
133                 scopedValue.setScopeAttributeName(getScopeAttribute());
134                 scopedValue.setScope(attributeValue.getScope());
135                 scopedValue.setValue(attributeValue.getValue());
136 
137                 encodedValues.add(scopedValue);
138             }
139 
140         } else if ("inline".equals(getScopeType())) {
141             XSString scopedValue;
142 
143             for (ScopedAttributeValue attributeValue : attribute.getValues()) {
144                 if (attributeValue == null) {
145                     continue;
146                 }
147 
148                 scopedValue = stringValueBuilder.buildObject(objectName, XSString.TYPE_NAME);
149                 scopedValue.setValue(attributeValue.getValue() + getScopeDelimiter() + attributeValue.getScope());
150 
151                 encodedValues.add(scopedValue);
152             }
153 
154         }
155 
156         return encodedValues;
157     }
158 }