1
2
3
4
5 package net.sourceforge.jsdp;
6
7 import java.net.MalformedURLException;
8 import java.net.URL;
9 import java.util.regex.Matcher;
10 import java.util.regex.Pattern;
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 public class Key implements Field {
46
47
48 private static final long serialVersionUID = 1767674946938133759L;
49
50
51 public static final String BASE64 = "base64";
52
53
54 public static final String CLEAR = "clear";
55
56
57 public static final String PROMPT = "prompt";
58
59
60 public static final String URI = "uri";
61
62
63 private static final Pattern fieldPattern = Pattern.compile("([^:]+)(:((.+)))?");
64
65
66 private static final Pattern methodBase64Pattern = Pattern.compile("([\\w\\+/]{4})*([\\w\\+/]{2}==|[\\w\\+/]{3}=)*");
67
68
69 private static final Pattern methodClearPattern = Pattern.compile("[\\w'-\\./:?#\\$&\\*;=@\\[\\]\\^_`\\{\\}\\|\\+\\~ \\t]+");
70
71
72 protected String method;
73
74
75 protected String key;
76
77
78
79
80 protected Key() {
81
82 super();
83 }
84
85
86
87
88
89
90
91
92
93
94
95 public Key(final String method, final String key) throws SDPException {
96
97 super();
98
99 setMethod(method);
100 setKey(key);
101 }
102
103
104
105
106
107
108
109
110
111
112 public static Key parse(final String field) throws SDPParseException {
113
114 if (!field.startsWith("i=")) {
115 throw new SDPParseException("The string \"" + field + "\" isn't an key field");
116 }
117
118 Key k = null;
119 Matcher matcher = fieldPattern.matcher(field.substring(2));
120
121
122 if (matcher.matches()) {
123 try {
124 k = new Key(matcher.group(1), matcher.group(3));
125 }
126 catch (SDPException parseException) {
127 throw new SDPParseException("The string \"" + field + "\" isn't a valid key field", parseException);
128 }
129 }
130 else {
131 throw new SDPParseException("The string \"" + field + "\" isn't a valid key field");
132 }
133
134 return k;
135 }
136
137
138
139
140
141
142 public Object clone() {
143
144 Key field = new Key();
145
146 field.method = new String(method);
147 field.key = new String(key);
148
149 return field;
150 }
151
152
153
154
155
156
157 public String getKey() {
158
159 return key;
160 }
161
162
163
164
165
166
167 public String getMethod() {
168
169 return method;
170 }
171
172
173
174
175
176
177 public char getType() {
178
179 return Field.KEY_FIELD;
180 }
181
182
183
184
185
186
187
188 public boolean hasKey() {
189
190 return key != null;
191 }
192
193
194
195
196
197
198
199
200 public void setKey(String key) throws SDPException {
201
202 boolean result = false;
203
204 if (method.equals(Key.BASE64)) {
205 result = methodBase64Pattern.matcher(key).matches();
206 }
207 else if (method.equals(Key.CLEAR)) {
208 result = methodClearPattern.matcher(key).matches();
209 }
210 else if (method.equals(Key.PROMPT)) {
211 result = (key == null) || (key.length() == 0);
212
213
214
215
216
217 key = null;
218 }
219 else if (method.equals(Key.URI)) {
220 try {
221
222 new URL(key);
223 }
224 catch (MalformedURLException invalidUrl) {
225
226 result = false;
227 }
228 }
229
230
231
232
233
234 if (!result) {
235 throw new SDPException("Invalid key for method " + method);
236 }
237
238 this.key = key;
239 }
240
241
242
243
244
245
246
247
248 public void setMethod(final String method) throws SDPException {
249
250 if (method == null) {
251 throw new SDPException("The encryption method cannot be null");
252 }
253 else if (!method.equals(Key.BASE64) && !method.equals(Key.CLEAR) && !method.equals(Key.PROMPT) && !method.equals(Key.URI)) {
254 throw new SDPException("The method " + method + " is not supported by SDP");
255 }
256
257 this.method = method;
258 }
259
260
261
262
263
264
265
266
267
268
269
270 public String toString() {
271
272 StringBuffer result = new StringBuffer(getType() + "=");
273 result.append(method);
274
275 if (key != null) {
276 result.append(":" + key);
277 }
278
279 return result.toString();
280 }
281 }