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.config.attribute.resolver.attributeDefinition;
18  
19  import java.io.FileInputStream;
20  import java.io.IOException;
21  import java.util.List;
22  
23  import org.opensaml.xml.util.DatatypeHelper;
24  
25  import edu.internet2.middleware.shibboleth.common.attribute.encoding.AttributeEncoder;
26  import edu.internet2.middleware.shibboleth.common.attribute.resolver.provider.attributeDefinition.ScriptedAttributeDefinition;
27  
28  /**
29   * Scripted attribute factory.
30   */
31  public class ScriptedAttribtueDefinitionFactoryBean extends BaseAttributeDefinitionFactoryBean {
32  
33      /** The scripting language used. */
34      private String scriptLanguage;
35  
36      /** The file to read the script from. */
37      private String scriptFile;
38  
39      /** The script. */
40      private String script;
41  
42      /**
43       * Gets the scripting language being used.
44       * 
45       * @return scripting language being used
46       */
47      public String getLanguage() {
48          return scriptLanguage;
49      }
50  
51      /**
52       * Sets the scripting language being used.
53       * 
54       * @param language scripting language being used
55       */
56      public void setLanguage(String language) {
57          scriptLanguage = DatatypeHelper.safeTrimOrNullString(language);
58      }
59  
60      /**
61       * Gets the script.
62       * 
63       * @return the script
64       */
65      public String getScript() {
66          return script;
67      }
68  
69      /**
70       * Sets the script.
71       * 
72       * @param newScript the script
73       */
74      public void setScript(String newScript) {
75          script = DatatypeHelper.safeTrimOrNullString(newScript);
76      }
77  
78      /**
79       * Gets the file to read the script from.
80       * 
81       * @return file to read the script from
82       */
83      public String getScriptFile() {
84          return scriptFile;
85      }
86  
87      /**
88       * Sets the file to read the script from.
89       * 
90       * @param file file to read the script from
91       */
92      public void setScriptFile(String file) {
93          scriptFile = DatatypeHelper.safeTrimOrNullString(file);
94      }
95  
96      /** {@inheritDoc} */
97      public Class getObjectType() {
98          return ScriptedAttributeDefinition.class;
99      }
100 
101     /** {@inheritDoc} */
102     protected Object createInstance() throws Exception {
103         ScriptedAttributeDefinition definition = new ScriptedAttributeDefinition(scriptLanguage);
104         populateAttributeDefinition(definition);
105 
106         try {
107             if (getScript() == null) {
108                 FileInputStream ins = new FileInputStream(scriptFile);
109                 byte[] scriptBytes = new byte[ins.available()];
110                 ins.read(scriptBytes);
111                 script = new String(script);
112             }
113         } catch (IOException e) {
114             throw e;
115         }
116         definition.setScript(script);
117 
118         definition.initialize();
119 
120         return definition;
121     }
122 }