View Javadoc

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