Mercurial > dive4elements > framework
annotate artifact-database/src/main/java/de/intevation/artifactdatabase/StringUtils.java @ 266:c42a07be9aee
Jetty HTTP server: Do not bind context to restlet app for security reasons.
artifacts/trunk@1977 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Mon, 23 May 2011 10:04:31 +0000 |
parents | 23ec48ce717e |
children |
rev | line source |
---|---|
100
933bbc9fc11f
Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
87
diff
changeset
|
1 /* |
933bbc9fc11f
Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
87
diff
changeset
|
2 * Copyright (c) 2010 by Intevation GmbH |
933bbc9fc11f
Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
87
diff
changeset
|
3 * |
933bbc9fc11f
Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
87
diff
changeset
|
4 * This program is free software under the LGPL (>=v2.1) |
933bbc9fc11f
Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
87
diff
changeset
|
5 * Read the file LGPL.txt coming with the software for details |
933bbc9fc11f
Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
87
diff
changeset
|
6 * or visit http://www.gnu.org/licenses/ if it does not exist. |
933bbc9fc11f
Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
87
diff
changeset
|
7 */ |
933bbc9fc11f
Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
87
diff
changeset
|
8 |
79
f69e5b87f05f
Implementation to export artifacts as xml (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
9 package de.intevation.artifactdatabase; |
f69e5b87f05f
Implementation to export artifacts as xml (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
10 |
f69e5b87f05f
Implementation to export artifacts as xml (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
11 import java.io.UnsupportedEncodingException; |
f69e5b87f05f
Implementation to export artifacts as xml (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
12 |
f69e5b87f05f
Implementation to export artifacts as xml (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
13 import java.util.UUID; |
f69e5b87f05f
Implementation to export artifacts as xml (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
14 |
80
8447467cef86
Implementation to import artifacts from incoming xml documents (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
79
diff
changeset
|
15 import org.apache.commons.codec.DecoderException; |
8447467cef86
Implementation to import artifacts from incoming xml documents (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
79
diff
changeset
|
16 |
8447467cef86
Implementation to import artifacts from incoming xml documents (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
79
diff
changeset
|
17 import org.apache.commons.codec.binary.Hex; |
8447467cef86
Implementation to import artifacts from incoming xml documents (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
79
diff
changeset
|
18 |
79
f69e5b87f05f
Implementation to export artifacts as xml (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
19 import org.apache.log4j.Logger; |
f69e5b87f05f
Implementation to export artifacts as xml (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
20 |
f69e5b87f05f
Implementation to export artifacts as xml (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
21 /** |
87
0f48188a6e02
Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
22 * Commonly used string functions. |
0f48188a6e02
Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
23 * |
79
f69e5b87f05f
Implementation to export artifacts as xml (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
24 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a> |
f69e5b87f05f
Implementation to export artifacts as xml (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
25 */ |
f69e5b87f05f
Implementation to export artifacts as xml (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
26 public final class StringUtils |
f69e5b87f05f
Implementation to export artifacts as xml (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
27 { |
f69e5b87f05f
Implementation to export artifacts as xml (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
28 private static Logger logger = Logger.getLogger(StringUtils.class); |
f69e5b87f05f
Implementation to export artifacts as xml (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
29 |
87
0f48188a6e02
Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
30 /** |
0f48188a6e02
Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
31 * Generated a random UUIDv4 in form of a string. |
0f48188a6e02
Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
32 * @return the UUID |
0f48188a6e02
Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
33 */ |
79
f69e5b87f05f
Implementation to export artifacts as xml (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
34 public static final String newUUID() { |
f69e5b87f05f
Implementation to export artifacts as xml (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
35 return UUID.randomUUID().toString(); |
f69e5b87f05f
Implementation to export artifacts as xml (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
36 } |
f69e5b87f05f
Implementation to export artifacts as xml (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
37 |
87
0f48188a6e02
Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
38 /** |
0f48188a6e02
Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
39 * Checks if a given string is a valid UUID. |
0f48188a6e02
Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
40 * @param uuid The string to test. |
0f48188a6e02
Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
41 * @return true if the string is a valid UUID else false. |
0f48188a6e02
Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
42 */ |
79
f69e5b87f05f
Implementation to export artifacts as xml (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
43 public static final boolean checkUUID(String uuid) { |
f69e5b87f05f
Implementation to export artifacts as xml (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
44 try { |
f69e5b87f05f
Implementation to export artifacts as xml (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
45 UUID.fromString(uuid); |
f69e5b87f05f
Implementation to export artifacts as xml (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
46 } |
f69e5b87f05f
Implementation to export artifacts as xml (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
47 catch (IllegalArgumentException iae) { |
f69e5b87f05f
Implementation to export artifacts as xml (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
48 logger.warn(iae.getLocalizedMessage()); |
f69e5b87f05f
Implementation to export artifacts as xml (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
49 return false; |
f69e5b87f05f
Implementation to export artifacts as xml (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
50 } |
f69e5b87f05f
Implementation to export artifacts as xml (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
51 return true; |
f69e5b87f05f
Implementation to export artifacts as xml (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
52 } |
f69e5b87f05f
Implementation to export artifacts as xml (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
53 |
87
0f48188a6e02
Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
54 /** |
0f48188a6e02
Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
55 * Returns the UTF-8 byte array representation of a given string. |
0f48188a6e02
Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
56 * @param s The string to be transformed. |
0f48188a6e02
Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
57 * @return The byte array representation. |
0f48188a6e02
Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
58 */ |
79
f69e5b87f05f
Implementation to export artifacts as xml (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
59 public static final byte [] getUTF8Bytes(String s) { |
f69e5b87f05f
Implementation to export artifacts as xml (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
60 try { |
f69e5b87f05f
Implementation to export artifacts as xml (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
61 return s.getBytes("UTF-8"); |
f69e5b87f05f
Implementation to export artifacts as xml (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
62 } |
f69e5b87f05f
Implementation to export artifacts as xml (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
63 catch (UnsupportedEncodingException usee) { |
f69e5b87f05f
Implementation to export artifacts as xml (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
64 logger.error(usee.getLocalizedMessage(), usee); |
f69e5b87f05f
Implementation to export artifacts as xml (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
65 return s.getBytes(); |
f69e5b87f05f
Implementation to export artifacts as xml (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
66 } |
f69e5b87f05f
Implementation to export artifacts as xml (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
67 } |
80
8447467cef86
Implementation to import artifacts from incoming xml documents (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
79
diff
changeset
|
68 |
87
0f48188a6e02
Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
69 /** |
0f48188a6e02
Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
70 * Tries to convert a Base64 encoded string into the |
0f48188a6e02
Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
71 * corresponing byte array. |
0f48188a6e02
Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
72 * @param s The Base64 encoded string |
0f48188a6e02
Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
73 * @return The byte array representation or null if |
0f48188a6e02
Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
74 * an decoding error occurs. |
0f48188a6e02
Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
75 */ |
80
8447467cef86
Implementation to import artifacts from incoming xml documents (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
79
diff
changeset
|
76 public static final byte [] decodeHex(String s) { |
8447467cef86
Implementation to import artifacts from incoming xml documents (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
79
diff
changeset
|
77 try { |
8447467cef86
Implementation to import artifacts from incoming xml documents (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
79
diff
changeset
|
78 return Hex.decodeHex(s.toCharArray()); |
8447467cef86
Implementation to import artifacts from incoming xml documents (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
79
diff
changeset
|
79 } |
8447467cef86
Implementation to import artifacts from incoming xml documents (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
79
diff
changeset
|
80 catch (DecoderException de) { |
8447467cef86
Implementation to import artifacts from incoming xml documents (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
79
diff
changeset
|
81 return null; |
8447467cef86
Implementation to import artifacts from incoming xml documents (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
79
diff
changeset
|
82 } |
8447467cef86
Implementation to import artifacts from incoming xml documents (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
79
diff
changeset
|
83 } |
231
1ea35226a6de
Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
100
diff
changeset
|
84 |
1ea35226a6de
Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
100
diff
changeset
|
85 public static final String repeat(String s, int count, String sep) { |
1ea35226a6de
Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
100
diff
changeset
|
86 if (count <= 0) { |
1ea35226a6de
Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
100
diff
changeset
|
87 return ""; |
1ea35226a6de
Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
100
diff
changeset
|
88 } |
1ea35226a6de
Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
100
diff
changeset
|
89 StringBuilder sb = new StringBuilder(s); |
237
23ec48ce717e
Possible fix for flys/issue8
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
231
diff
changeset
|
90 for (--count; count > 0; --count) { |
231
1ea35226a6de
Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
100
diff
changeset
|
91 sb.append(sep).append(s); |
1ea35226a6de
Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
100
diff
changeset
|
92 } |
1ea35226a6de
Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
100
diff
changeset
|
93 return sb.toString(); |
1ea35226a6de
Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
100
diff
changeset
|
94 } |
1ea35226a6de
Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
100
diff
changeset
|
95 |
1ea35226a6de
Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
100
diff
changeset
|
96 public static final String repeat(char c, int count, char sep) { |
1ea35226a6de
Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
100
diff
changeset
|
97 if (count <= 0) { |
1ea35226a6de
Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
100
diff
changeset
|
98 return ""; |
1ea35226a6de
Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
100
diff
changeset
|
99 } |
1ea35226a6de
Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
100
diff
changeset
|
100 StringBuilder sb = new StringBuilder(2*count-1).append(c); |
237
23ec48ce717e
Possible fix for flys/issue8
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
231
diff
changeset
|
101 for (--count; count > 0; --count) { |
231
1ea35226a6de
Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
100
diff
changeset
|
102 sb.append(sep).append(c); |
1ea35226a6de
Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
100
diff
changeset
|
103 } |
1ea35226a6de
Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
100
diff
changeset
|
104 return sb.toString(); |
1ea35226a6de
Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
100
diff
changeset
|
105 } |
79
f69e5b87f05f
Implementation to export artifacts as xml (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
106 } |
f69e5b87f05f
Implementation to export artifacts as xml (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
107 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |