forked from restlet/restlet-framework-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessage.java
More file actions
492 lines (448 loc) · 15.4 KB
/
Message.java
File metadata and controls
492 lines (448 loc) · 15.4 KB
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/**
* Copyright 2005-2013 Restlet S.A.S.
*
* The contents of this file are subject to the terms of one of the following
* open source licenses: Apache 2.0 or LGPL 3.0 or LGPL 2.1 or CDDL 1.0 or EPL
* 1.0 (the "Licenses"). You can select the license that you prefer but you may
* not use this file except in compliance with one of these Licenses.
*
* You can obtain a copy of the Apache 2.0 license at
* http://www.opensource.org/licenses/apache-2.0
*
* You can obtain a copy of the LGPL 3.0 license at
* http://www.opensource.org/licenses/lgpl-3.0
*
* You can obtain a copy of the LGPL 2.1 license at
* http://www.opensource.org/licenses/lgpl-2.1
*
* You can obtain a copy of the CDDL 1.0 license at
* http://www.opensource.org/licenses/cddl1
*
* You can obtain a copy of the EPL 1.0 license at
* http://www.opensource.org/licenses/eclipse-1.0
*
* See the Licenses for the specific language governing permissions and
* limitations under the Licenses.
*
* Alternatively, you can obtain a royalty free commercial license with less
* limitations, transferable or non-transferable, directly at
* http://www.restlet.com/products/restlet-framework
*
* Restlet is a registered trademark of Restlet S.A.S.
*/
package org.restlet;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CopyOnWriteArrayList;
import org.restlet.data.CacheDirective;
import org.restlet.data.MediaType;
import org.restlet.data.RecipientInfo;
import org.restlet.data.Warning;
import org.restlet.representation.BufferingRepresentation;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.ClientResource;
/**
* Generic message exchanged between components.
*
* @author Jerome Louvel
*/
public abstract class Message {
/** The modifiable attributes map. */
private volatile ConcurrentMap<String, Object> attributes;
/** The caching directives. */
private volatile List<CacheDirective> cacheDirectives;
/** The date and time at which the message was originated. */
private volatile Date date;
/** The payload of the message. */
private volatile Representation entity;
// [ifndef gwt] member
/** The optional cached text. */
private volatile String entityText;
/** Callback invoked when an error occurs when sending the message. */
private volatile Uniform onError;
/** Callback invoked after sending the message. */
private volatile Uniform onSent;
/** The intermediary recipients info. */
private volatile List<RecipientInfo> recipientsInfo;
/** The additional warnings information. */
private volatile List<Warning> warnings;
/**
* Constructor.
*/
public Message() {
this((Representation) null);
}
/**
* Constructor.
*
* @param entity
* The payload of the message.
*/
public Message(Representation entity) {
this.attributes = null;
this.cacheDirectives = null;
this.date = null;
this.entity = entity;
// [ifndef gwt] instruction
this.entityText = null;
this.onSent = null;
this.recipientsInfo = null;
this.warnings = null;
}
// [ifndef gwt] method
/**
* If the entity is transient or its size unknown in advance but available,
* then the entity is wrapped with a {@link BufferingRepresentation}.<br>
* <br>
* Be careful as this method could create potentially very large byte
* buffers in memory that could impact your application performance.
*
* @see BufferingRepresentation
* @see ClientResource#setRequestEntityBuffering(boolean)
* @see ClientResource#setResponseEntityBuffering(boolean)
*/
public void bufferEntity() {
if ((getEntity() != null)
&& (getEntity().isTransient() || (getEntity().getSize() == Representation.UNKNOWN_SIZE))
&& getEntity().isAvailable()) {
setEntity(new org.restlet.representation.BufferingRepresentation(
getEntity()));
}
}
/**
* Asks the underlying connector to immediately flush the network buffers.
*
* @throws IOException
*/
public void flushBuffers() throws IOException {
}
/**
* Returns the modifiable map of attributes that can be used by developers
* to save information relative to the message. Creates a new instance if no
* one has been set. This is an easier alternative to the creation of a
* wrapper instance around the whole message.<br>
* <br>
*
* In addition, this map is a shared space between the developer and the
* connectors. In this case, it is used to exchange information that is not
* uniform across all protocols and couldn't therefore be directly included
* in the API. For this purpose, all attribute names starting with
* "org.restlet" are reserved. Currently the following attributes are used:
* <table>
* <tr>
* <th>Attribute name</th>
* <th>Class name</th>
* <th>Description</th>
* </tr>
* <tr>
* <td>org.restlet.http.headers</td>
* <td>org.restlet.util.Series<org.restlet.engine.header.Header></td>
* <td>Server HTTP connectors must provide all request headers and client
* HTTP connectors must provide all response headers, exactly as they were
* received. In addition, developers can also use this attribute to specify
* <b>non-standard</b> headers that should be added to the request or to the
* response.</td>
* </tr>
* <tr>
* <td>org.restlet.https.clientCertificates</td>
* <td>List<java.security.cert.Certificate></td>
* <td>For requests received via a secure connector, indicates the ordered
* list of client certificates, if they are available and accessible.</td>
* </tr>
* </table>
* <br>
* Most of the standard HTTP headers are directly supported via the Restlet
* API. Thus, adding such HTTP headers is forbidden because it could
* conflict with the connector's internal behavior, limit portability or
* prevent future optimizations. The other standard HTTP headers (that are
* not supported) can be added as attributes via the
* "org.restlet.http.headers" key.<br>
*
* @return The modifiable attributes map.
*/
public ConcurrentMap<String, Object> getAttributes() {
// Lazy initialization with double-check.
ConcurrentMap<String, Object> r = this.attributes;
if (r == null) {
synchronized (this) {
r = this.attributes;
if (r == null) {
this.attributes = r = new ConcurrentHashMap<String, Object>();
}
}
}
return this.attributes;
}
/**
* Returns the cache directives.<br>
* <br>
* Note that when used with HTTP connectors, this property maps to the
* "Cache-Control" header.
*
* @return The cache directives.
*/
public List<CacheDirective> getCacheDirectives() {
// Lazy initialization with double-check.
List<CacheDirective> r = this.cacheDirectives;
if (r == null) {
synchronized (this) {
r = this.cacheDirectives;
if (r == null) {
this.cacheDirectives = r = new CopyOnWriteArrayList<CacheDirective>();
}
}
}
return r;
}
/**
* Returns the date and time at which the message was originated.
*
* @return The date and time at which the message was originated.
*/
public Date getDate() {
return date;
}
/**
* Returns the entity representation.
*
* @return The entity representation.
*/
public Representation getEntity() {
return this.entity;
}
// [ifndef gwt] method
/**
* Returns the entity as text. This method can be called several times and
* will always return the same text. Note that if the entity is large this
* method can result in important memory consumption.
*
* @return The entity as text.
*/
public String getEntityAsText() {
if (this.entityText == null) {
try {
this.entityText = (getEntity() == null) ? null : getEntity()
.getText();
} catch (java.io.IOException e) {
Context.getCurrentLogger().log(java.util.logging.Level.FINE,
"Unable to get the entity text.", e);
}
}
return this.entityText;
}
/**
* Returns the callback invoked when an error occurs when sending the
* message.
*
* @return The callback invoked when an error occurs when sending the
* message.
*/
public Uniform getOnError() {
return onError;
}
/**
* Returns the callback invoked after sending the message.
*
* @return The callback invoked after sending the message.
*/
public Uniform getOnSent() {
return onSent;
}
/**
* Returns the intermediary recipient information.<br>
* <br>
* Note that when used with HTTP connectors, this property maps to the "Via"
* headers.
*
* @return The intermediary recipient information.
*/
public List<RecipientInfo> getRecipientsInfo() {
// Lazy initialization with double-check.
List<RecipientInfo> r = this.recipientsInfo;
if (r == null) {
synchronized (this) {
r = this.recipientsInfo;
if (r == null) {
this.recipientsInfo = r = new CopyOnWriteArrayList<RecipientInfo>();
}
}
}
return r;
}
/**
* Returns the additional warnings information.<br>
* <br>
* Note that when used with HTTP connectors, this property maps to the
* "Warning" headers.
*
* @return The additional warnings information.
*/
public List<Warning> getWarnings() {
// Lazy initialization with double-check.
List<Warning> r = this.warnings;
if (r == null) {
synchronized (this) {
r = this.warnings;
if (r == null) {
this.warnings = r = new CopyOnWriteArrayList<Warning>();
}
}
}
return r;
}
/**
* Indicates if the message was or will be exchanged confidentially, for
* example via a SSL-secured connection.
*
* @return True if the message is confidential.
*/
public abstract boolean isConfidential();
/**
* Indicates if a content is available and can be sent or received. Several
* conditions must be met: the content must exists and have some available
* data.
*
* @return True if a content is available and can be sent.
*/
public boolean isEntityAvailable() {
// The declaration of the "result" variable is a workaround for the GWT
// platform. Please keep it!
boolean result = (getEntity() != null) && getEntity().isAvailable();
return result;
}
/**
* Releases the message's entity if present.
*
* @see org.restlet.representation.Representation#release()
*/
public void release() {
if (getEntity() != null) {
getEntity().release();
}
}
/**
* Sets the modifiable map of attributes. This method clears the current map
* and puts all entries in the parameter map.
*
* @param attributes
* A map of attributes
*/
public void setAttributes(Map<String, Object> attributes) {
synchronized (getAttributes()) {
if (attributes != getAttributes()) {
getAttributes().clear();
if (attributes != null) {
getAttributes().putAll(attributes);
}
}
}
}
/**
* Sets the cache directives. Note that when used with HTTP connectors, this
* property maps to the "Cache-Control" header. This method clears the
* current list and adds all entries in the parameter list.
*
* @param cacheDirectives
* The cache directives.
*/
public void setCacheDirectives(List<CacheDirective> cacheDirectives) {
synchronized (getCacheDirectives()) {
if (cacheDirectives != getCacheDirectives()) {
getCacheDirectives().clear();
if (cacheDirectives != null) {
getCacheDirectives().addAll(cacheDirectives);
}
}
}
}
/**
* Sets the date and time at which the message was originated.
*
* @param date
* The date and time at which the message was originated.
*/
public void setDate(Date date) {
this.date = date;
}
/**
* Sets the entity representation.
*
* @param entity
* The entity representation.
*/
public void setEntity(Representation entity) {
this.entity = entity;
}
/**
* Sets a textual entity.
*
* @param value
* The represented string.
* @param mediaType
* The representation's media type.
*/
public void setEntity(String value, MediaType mediaType) {
setEntity(new StringRepresentation(value, mediaType));
}
/**
* Sets the callback invoked when an error occurs when sending the message.
*
* @param onError
* The callback invoked when an error occurs when sending the
* message.
*/
public void setOnError(Uniform onError) {
this.onError = onError;
}
/**
* Sets the callback invoked after sending the message.
*
* @param onSentCallback
* The callback invoked after sending the message.
*/
public void setOnSent(Uniform onSentCallback) {
this.onSent = onSentCallback;
}
/**
* Sets the modifiable list of intermediary recipients. Note that when used
* with HTTP connectors, this property maps to the "Via" headers. This
* method clears the current list and adds all entries in the parameter
* list.
*
* @param recipientsInfo
* A list of intermediary recipients.
*/
public void setRecipientsInfo(List<RecipientInfo> recipientsInfo) {
synchronized (getRecipientsInfo()) {
if (recipientsInfo != getRecipientsInfo()) {
getRecipientsInfo().clear();
if (recipientsInfo != null) {
getRecipientsInfo().addAll(recipientsInfo);
}
}
}
}
/**
* Sets the additional warnings information. Note that when used with HTTP
* connectors, this property maps to the "Warning" headers. This method
* clears the current list and adds all entries in the parameter list.
*
* @param warnings
* The warnings.
*/
public void setWarnings(List<Warning> warnings) {
synchronized (getWarnings()) {
if (warnings != getWarnings()) {
getWarnings().clear();
if (warnings != null) {
getWarnings().addAll(warnings);
}
}
}
}
}