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