use the new session manager interface
[java-idp.git] / tests / edu / internet2 / middleware / shibboleth / aa / attrresolv / AttributesFile.java
1 /*
2  * Copyright [2005] [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 /*
18  * Contributed by SungGard SCT.
19  */
20
21 package edu.internet2.middleware.shibboleth.aa.attrresolv;
22
23 import java.io.FileReader;
24 import java.io.IOException;
25 import java.io.LineNumberReader;
26 import java.util.HashMap;
27 import java.util.Map;
28
29 import javax.naming.directory.Attributes;
30 import javax.naming.directory.BasicAttribute;
31 import javax.naming.directory.BasicAttributes;
32
33 import org.apache.log4j.Logger;
34 import org.opensaml.SAMLException;
35
36 import edu.internet2.middleware.shibboleth.aa.AAAttribute;
37
38 /**
39  * The AttributesFile reads attributes specified in a file as name-value pairs separated by an 'equals' sign (=)
40  * Multiple values of an attribute may be specified using multiple pairs with the same attribute name. Multi-valued
41  * attributes can be read as ordered or unordered.
42  * 
43  * @author <a href="mailto:vgoenka@sungardsct.com">Vishal Goenka </a>
44  */
45
46 public class AttributesFile {
47
48         private static Logger log = Logger.getLogger(AttributesFile.class.getName());
49         private String datafile;
50         private FileReader freader = null;
51         private LineNumberReader linereader = null;
52
53         public AttributesFile(String datafile) {
54
55                 this.datafile = datafile;
56         }
57
58         private void open() throws IOException {
59
60                 freader = new FileReader(datafile);
61                 linereader = new LineNumberReader(freader);
62         }
63
64         private void close() {
65
66                 try {
67                         if (freader != null) freader.close();
68                 } catch (Exception e) {
69                         log.warn("Unexpected error when closing file: " + datafile + " -- " + e.getMessage());
70                 }
71         }
72
73         private AVPair readAV() throws IOException {
74
75                 AVPair av = null;
76                 do {
77                         String line = linereader.readLine();
78                         if (line == null) break;
79
80                         line = line.trim();
81                         // Ignore comments and empty lines
82                         if ((line.length() == 0) || (line.charAt(0) == '#')) continue;
83
84                         int index = line.indexOf("=");
85                         if (index == -1)
86                                 throw new IOException("'=' not specified in " + datafile + ":" + linereader.getLineNumber());
87                         String attrib = line.substring(0, index).trim();
88                         String value = line.substring(index + 1).trim();
89                         if ((attrib == null) || (attrib.length() == 0))
90                                 throw new IOException("Empty attribute name in " + datafile + ":" + linereader.getLineNumber());
91
92                         if (value == null) value = "";
93
94                         av = new AVPair(attrib, value);
95                 } while (av == null);
96                 return av;
97         }
98
99         public synchronized Attributes readAttributes(boolean ordered) throws IOException {
100
101                 open();
102                 try {
103                         BasicAttributes attributes = new BasicAttributes();
104                         AVPair av = readAV();
105                         while (av != null) {
106                                 BasicAttribute ba = (BasicAttribute) attributes.get(av.name);
107                                 if (ba == null) {
108                                         ba = new BasicAttribute(av.name, ordered);
109                                         attributes.put(ba);
110                                 }
111                                 ba.add(av.value);
112                                 av = readAV();
113                         }
114                         return attributes;
115                 } finally {
116                         close();
117                 }
118         }
119
120         public synchronized Map<String, AAAttribute> getResolverAttributes(boolean returnValues) throws IOException,
121                         SAMLException {
122
123                 open();
124                 try {
125                         Map<String, AAAttribute> attributes = new HashMap<String, AAAttribute>();
126                         AVPair av = readAV();
127                         while (av != null) {
128                                 AAAttribute attr = (AAAttribute) attributes.get(av.name);
129                                 if (attr == null) {
130                                         // The intern() is to work-around the bug in AAAttribute.equals() where the name of the
131                                         // attribute is compared
132                                         // using "==" rather than "equals" ...
133                                         attr = new AAAttribute(av.name.intern());
134                                         attributes.put(attr.getName(), attr);
135                                 }
136                                 if (returnValues) {
137                                         attr.addValue(av.value);
138                                 }
139                                 av = readAV();
140                         }
141                         return attributes;
142                 } finally {
143                         close();
144                 }
145         }
146
147         private class AVPair {
148
149                 String name;
150                 String value;
151
152                 public AVPair(String a, String v) {
153
154                         name = a;
155                         value = v;
156                 }
157         }
158
159 }