This repository has been archived by the owner on Dec 16, 2022. It is now read-only.
/
Utils.java
394 lines (354 loc) · 13.1 KB
/
Utils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
//: "The contents of this file are subject to the Mozilla Public License
//: Version 1.1 (the "License"); you may not use this file except in
//: compliance with the License. You may obtain a copy of the License at
//: http://www.mozilla.org/MPL/
//:
//: Software distributed under the License is distributed on an "AS IS"
//: basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
//: License for the specific language governing rights and limitations
//: under the License.
//:
//: The Original Code is Guanxi (http://www.guanxi.uhi.ac.uk).
//:
//: The Initial Developer of the Original Code is Alistair Young alistair@codebrane.com
//: All Rights Reserved.
//:
package org.guanxi.common;
import java.io.*;
import java.net.URL;
import java.rmi.server.UID;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.TimeZone;
import java.util.zip.*;
import javax.servlet.http.HttpServletRequest;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.xml.security.exceptions.Base64DecodingException;
import org.apache.xml.security.utils.Base64;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlOptions;
import org.guanxi.common.definitions.Shibboleth;
import org.guanxi.xal.saml_2_0.metadata.EntitiesDescriptorDocument;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* <font size=5><b></b></font>
*
* @author Alistair Young alistair@smo.uhi.ac.uk
* @author matthew
*/
public class Utils {
/** OS dependent line engine, e.g. \n */
public static final String LINE_ENDING = System.getProperty("line.separator");
/** Doesn't provide compatibility with the compression format used by both GZIP and PKZIP */
public static final boolean RFC1951_WRAP = false;
/** Provides compatibility with the compression format used by both GZIP and PKZIP */
public static final boolean RFC1951_NO_WRAP = true;
/** Shortcut for using default compression level */
public static final int RFC1951_DEFAULT_COMPRESSION_LEVEL = Deflater.DEFAULT_COMPRESSION;
/**
* Returns the parameters and their values from an HTTP request
*
* @param request HTTP request object
* @return Hashtable of parameters and their values
*/
public static Hashtable<String, String> getRequestParameters(HttpServletRequest request) {
Hashtable<String, String> params = new Hashtable<String, String>();
Enumeration<?> e = request.getParameterNames();
String name,value;
while (e.hasMoreElements()) {
name = (String)e.nextElement();
value = request.getParameter(name);
params.put(name, value);
}
return params;
}
public static String getUniqueID() {
UID uid = new UID();
return uid.toString();
}
public static String base64(Document inDocToEncode) {
try {
DOMSource domSource = new DOMSource(inDocToEncode);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
return Base64.encode(writer.toString().getBytes());
}
catch(TransformerException te) {
return null;
}
}
public static String base64(byte[] data) {
return Base64.encode(data);
}
public static String decodeBase64(String b64Data) {
try {
return new String(Base64.decode(b64Data));
}
catch(Base64DecodingException bde) {
return null;
}
}
public static byte[] decodeBase64b(String b64Data) {
try {
return Base64.decode(b64Data);
}
catch(Base64DecodingException bde) {
return null;
}
}
/**
* Gets the text value of a particular node in the config file
*
* @param configNode The parent node of the node we're interested in
* @param option The node whose text we want
* @return Text value of the node
*/
public static String getConfigOption(Node configNode, String option) {
NodeList childNodes = null;
int childCount = 0;
NodeList nodes = configNode.getChildNodes();
if (nodes == null) return "-1";
for (int count=0; count < nodes.getLength(); count++) {
if (nodes.item(count).getLocalName() == null) continue;
// Have we found the node we're looking for?
if (nodes.item(count).getLocalName().equalsIgnoreCase(option)) {
// Get the child nodes. We're looking for the text node containing the value
childNodes = nodes.item(count).getChildNodes();
for (childCount=0; childCount < childNodes.getLength(); childCount++) {
// Got a text node?
if (childNodes.item(childCount).getNodeType() == Node.TEXT_NODE) {
// Anything in it?
if (childNodes.item(childCount).getNodeValue() != null)
// Sometimes you can get blank text nodes with Xerces
if (!childNodes.item(childCount).getNodeValue().equalsIgnoreCase("")) {
return childNodes.item(childCount).getNodeValue();
}
}
}
}
}
return "-1";
}
public static void zipDirectory(String dir, ZipOutputStream zipStream) {
try {
File dirToZip = new File(dir);
// Have a gander in the directory to see what's there
String[] dirList = dirToZip.list();
byte[] readBuffer = new byte[2156];
int bytesIn = 0;
for (int count=0; count < dirList.length; count++) {
File dirFile = new File(dirToZip, dirList[count]);
// If the next entity is a directory, call recursively
if (dirFile.isDirectory()) {
zipDirectory(dirFile.getPath(), zipStream);
continue;
}
// Get a handle to the file...
FileInputStream fis = new FileInputStream(dirFile);
// ...get an entry ready for it ...
ZipEntry zipEntry = new ZipEntry(dirFile.getPath());
// ...bung it in the ZIP stream...
zipStream.putNextEntry(zipEntry);
// ...and read the data into the ZIP stream
while ((bytesIn = fis.read(readBuffer)) != -1) {
zipStream.write(readBuffer, 0, bytesIn);
}
fis.close();
}
}
catch(Exception e) {
System.out.println(e);
}
}
/**
* Creates an ID that is compatible with XML Schema
*
* @return ID that is compatible with XML Schema
*/
public static String createNCNameID() {
// xsd:NCName is derived from xsd:Name, which can't start with a number
String id = new UID().toString();
id = id.replaceAll(":", "-");
id = "GUANXI-" + id;
return id;
}
/**
* Parses a SAML2 Metadata document
*
* @param metadataURL The url of the metadata
* @return EntitiesDescriptorDocument for the metadata
* @throws GuanxiException if an error occurs
*/
public static EntitiesDescriptorDocument parseSAML2Metadata(String metadataURL) throws GuanxiException {
try {
return EntitiesDescriptorDocument.Factory.parse(new URL(metadataURL).openStream());
}
catch(Exception e) {
throw new GuanxiException(e);
}
}
/**
* Writes a SAML2 metadata document to disk
*
* @param saml2MetadataDoc The SAML2 metadata document
* @param filenameAndPath The full path and name of the file to write
* @throws GuanxiException if an error occurs
*/
public static void writeSAML2MetadataToDisk(EntitiesDescriptorDocument saml2MetadataDoc,
String filenameAndPath) throws GuanxiException {
HashMap<String, String> namespaces = new HashMap<String, String>();
ByteArrayOutputStream buffer;
OutputStream out;
namespaces.put(Shibboleth.NS_SAML_10_PROTOCOL, Shibboleth.NS_PREFIX_SAML_10_PROTOCOL);
namespaces.put(Shibboleth.NS_SAML_10_ASSERTION, Shibboleth.NS_PREFIX_SAML_10_ASSERTION);
XmlOptions xmlOptions = new XmlOptions();
xmlOptions.setSavePrettyPrint();
xmlOptions.setSavePrettyPrintIndent(2);
xmlOptions.setUseDefaultNamespace();
xmlOptions.setSaveAggressiveNamespaces();
xmlOptions.setSaveSuggestedPrefixes(namespaces);
xmlOptions.setSaveNamespacesFirst();
buffer = new ByteArrayOutputStream();
try {
saml2MetadataDoc.save(buffer, xmlOptions); // throws IOException, but shouldnt as ByteArrayOutputStream will not
out = new FileOutputStream(filenameAndPath);
try {
out.write(buffer.toByteArray());
}
finally {
out.close();
}
}
catch (IOException ioe) {
throw new GuanxiException(ioe);
}
}
/**
* Converts a local time to UTC Zulu format:
* 2005-06-21T11:13:29Z
*
* @param obj The XmlObject that contains the local time in an attribute
* @param interval the interval in minutes to add to any other time attrbutes
*/
public static void zuluXmlObject(XmlObject obj, int interval) {
SimpleDateFormat zulu = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
zulu.setTimeZone(TimeZone.getTimeZone("GMT"));
Calendar calNow = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
Calendar calAfter = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
calAfter.add(Calendar.MINUTE, interval);
NamedNodeMap attrs = obj.getDomNode().getAttributes();
for (int cc=0; cc < attrs.getLength(); cc++) {
Node attr = attrs.item(cc);
if (attr.getNodeName().equals("NotBefore")) {
attr.setNodeValue(zulu.format(calNow.getTime()));
}
if (attr.getNodeName().equals("NotOnOrAfter")) {
attr.setNodeValue(zulu.format(calAfter.getTime()));
}
if (attr.getNodeName().equals("IssueInstant")) {
attr.setNodeValue(zulu.format(calNow.getTime()));
}
if (attr.getNodeName().equals("AuthenticationInstant")) {
attr.setNodeValue(zulu.format(calNow.getTime()));
}
if (attr.getNodeName().equals("AuthnInstant")) {
attr.setNodeValue(zulu.format(calNow.getTime()));
}
}
}
/**
* Generates local time in UTC Zulu format:
* 2005-06-21T11:13:29Z
*
* @return The time now in Zulu format
*/
public static String zuluNow() {
SimpleDateFormat zulu = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
zulu.setTimeZone(TimeZone.getTimeZone("GMT"));
Calendar calNow = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
return zulu.format(calNow.getTime());
}
/**
* This reads the input stream completely, returning the result. The input stream is
* always in a closed state after calling this, even if an error has occurred.
*
* @param in stream to read from
* @return array of bytes read from the stream
* @throws GuanxiException if an error occurs
*/
public static byte[] read(InputStream in) throws GuanxiException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int read;
try {
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
return out.toByteArray();
}
catch(IOException ioe) {
// Reading from the stream failed
return null;
}
finally {
try {
in.close();
}
catch(IOException ioe) {
// Closing the stream failed. Shouldn't stop us returning the data though
}
}
}
/**
* Reconstitutes data deflated according to RFC1951
*
* @param deflatedData the deflated data to reconstitute
* @param useWrap if true then the ZLIB header and checksum fields will not be used.
* This provides compatibility with the compression format used by both GZIP and PKZIP.
* @return String representing the reconstituted data
* @throws GuanxiException if an error occurs
*/
public static String inflate(byte[] deflatedData, boolean useWrap) throws GuanxiException {
try {
byte[] inflatedData = new byte[(10 * deflatedData.length)];
Inflater decompresser = new Inflater(useWrap);
decompresser.setInput(deflatedData, 0, deflatedData.length);
int inflatedBytesLength = decompresser.inflate(inflatedData);
decompresser.end();
return new String(inflatedData, 0, inflatedBytesLength);
}
catch(DataFormatException dfe) {
throw new GuanxiException(dfe);
}
}
/**
* Deflates data according to RFC1951
*
* @param data the data to deflate
* @param compressionLevel the compression level to use
* @param useWrap if true then the ZLIB header and checksum fields will not be used.
* This provides compatibility with the compression format used by both GZIP and PKZIP.
* @return String representing the deflated data
*/
public static String deflate(String data, int compressionLevel, boolean useWrap) {
byte[] deflatedData = new byte[data.length()];
Deflater deflater = new Deflater(compressionLevel, useWrap);
deflater.setInput(data.getBytes());
deflater.finish();
int deflatedBytesLength = deflater.deflate(deflatedData);
return new String(deflatedData, 0, deflatedBytesLength);
}
}