001: /*
002: * Copyright 2005-2007 Noelios Consulting.
003: *
004: * The contents of this file are subject to the terms of the Common Development
005: * and Distribution License (the "License"). You may not use this file except in
006: * compliance with the License.
007: *
008: * You can obtain a copy of the license at
009: * http://www.opensource.org/licenses/cddl1.txt See the License for the specific
010: * language governing permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL HEADER in each file and
013: * include the License file at http://www.opensource.org/licenses/cddl1.txt If
014: * applicable, add the following below this CDDL HEADER, with the fields
015: * enclosed by brackets "[]" replaced with your own identifying information:
016: * Portions Copyright [yyyy] [name of copyright owner]
017: */
018:
019: package org.restlet.util;
020:
021: import java.util.Map;
022: import java.util.Set;
023:
024: import org.restlet.data.ChallengeRequest;
025: import org.restlet.data.CookieSetting;
026: import org.restlet.data.Dimension;
027: import org.restlet.data.Form;
028: import org.restlet.data.MediaType;
029: import org.restlet.data.Method;
030: import org.restlet.data.Reference;
031: import org.restlet.data.Request;
032: import org.restlet.data.Response;
033: import org.restlet.data.ServerInfo;
034: import org.restlet.data.Status;
035: import org.restlet.resource.DomRepresentation;
036: import org.restlet.resource.Representation;
037: import org.restlet.resource.SaxRepresentation;
038:
039: /**
040: * Request wrapper. Useful for application developer who need to enrich the
041: * request with application related properties and behavior.
042: *
043: * @see <a href="http://c2.com/cgi/wiki?DecoratorPattern">The decorator (aka
044: * wrapper) pattern</a>
045: * @author Jerome Louvel (contact@noelios.com)
046: */
047: public class WrapperResponse extends Response {
048: /** The wrapped response. */
049: private Response wrappedResponse;
050:
051: /**
052: * Constructor.
053: *
054: * @param wrappedResponse
055: * The wrapped response.
056: */
057: public WrapperResponse(Response wrappedResponse) {
058: super ((Request) null);
059: this .wrappedResponse = wrappedResponse;
060: }
061:
062: /**
063: * Returns the authentication request sent by an origin server to a client.
064: *
065: * @return The authentication request sent by an origin server to a client.
066: */
067: public ChallengeRequest getChallengeRequest() {
068: return getWrappedResponse().getChallengeRequest();
069: }
070:
071: /**
072: * Returns the cookie settings provided by the server.
073: *
074: * @return The cookie settings provided by the server.
075: */
076: public Series<CookieSetting> getCookieSettings() {
077: return getWrappedResponse().getCookieSettings();
078: }
079:
080: /**
081: * Returns the reference that the client should follow for redirections or
082: * resource creations.
083: *
084: * @return The redirection reference.
085: */
086: public Reference getRedirectRef() {
087: return getWrappedResponse().getRedirectRef();
088: }
089:
090: /**
091: * Returns the associated request
092: *
093: * @return The associated request
094: */
095: public Request getRequest() {
096: return getWrappedResponse().getRequest();
097: }
098:
099: /**
100: * Returns the server-specific information.
101: *
102: * @return The server-specific information.
103: */
104: public ServerInfo getServerInfo() {
105: return getWrappedResponse().getServerInfo();
106: }
107:
108: /**
109: * Returns the status.
110: *
111: * @return The status.
112: */
113: public Status getStatus() {
114: return getWrappedResponse().getStatus();
115: }
116:
117: /**
118: * Returns the wrapped response.
119: *
120: * @return The wrapped response.
121: */
122: protected Response getWrappedResponse() {
123: return this .wrappedResponse;
124: }
125:
126: /**
127: * Sets the authentication request sent by an origin server to a client.
128: *
129: * @param request
130: * The authentication request sent by an origin server to a
131: * client.
132: */
133: public void setChallengeRequest(ChallengeRequest request) {
134: getWrappedResponse().setChallengeRequest(request);
135: }
136:
137: /**
138: * Sets the reference that the client should follow for redirections or
139: * resource creations.
140: *
141: * @param redirectRef
142: * The redirection reference.
143: */
144: public void setRedirectRef(Reference redirectRef) {
145: getWrappedResponse().setRedirectRef(redirectRef);
146: }
147:
148: /**
149: * Sets the reference that the client should follow for redirections or
150: * resource creations.
151: *
152: * @param redirectUri
153: * The redirection URI.
154: */
155: public void setRedirectRef(String redirectUri) {
156: getWrappedResponse().setRedirectRef(redirectUri);
157: }
158:
159: /**
160: * Sets the associated request.
161: *
162: * @param request
163: * The associated request
164: */
165: public void setRequest(WrapperRequest request) {
166: getWrappedResponse().setRequest(request);
167: }
168:
169: /**
170: * Sets the status.
171: *
172: * @param status
173: * The status to set.
174: */
175: public void setStatus(Status status) {
176: getWrappedResponse().setStatus(status);
177: }
178:
179: /**
180: * Sets the status.
181: *
182: * @param status
183: * The status to set.
184: * @param message
185: * The status message.
186: */
187: public void setStatus(Status status, String message) {
188: getWrappedResponse().setStatus(status, message);
189: }
190:
191: /**
192: * Returns the set of methods allowed on the requested resource. This
193: * property only has to be updated when a status
194: * CLIENT_ERROR_METHOD_NOT_ALLOWED is set.
195: *
196: * @return The list of allowed methods.
197: */
198: public Set<Method> getAllowedMethods() {
199: return getWrappedResponse().getAllowedMethods();
200: }
201:
202: /**
203: * Returns the set of selecting dimensions on which the response entity may
204: * vary. If some server-side content negotiation is done, this set should be
205: * properly updated, other it can be left empty.
206: *
207: * @return The set of dimensions on which the response entity may vary.
208: */
209: public Set<Dimension> getDimensions() {
210: return getWrappedResponse().getDimensions();
211: }
212:
213: /**
214: * Permanently redirects the client to a target URI. The client is expected
215: * to reuse the same method for the new request.
216: *
217: * @param targetRef
218: * The target URI reference.
219: */
220: public void redirectPermanent(Reference targetRef) {
221: getWrappedResponse().redirectPermanent(targetRef);
222: }
223:
224: /**
225: * Permanently redirects the client to a target URI. The client is expected
226: * to reuse the same method for the new request.
227: *
228: * @param targetUri
229: * The target URI.
230: */
231: public void redirectPermanent(String targetUri) {
232: getWrappedResponse().redirectPermanent(targetUri);
233: }
234:
235: /**
236: * Redirects the client to a different URI that SHOULD be retrieved using a
237: * GET method on that resource. This method exists primarily to allow the
238: * output of a POST-activated script to redirect the user agent to a
239: * selected resource. The new URI is not a substitute reference for the
240: * originally requested resource.
241: *
242: * @param targetRef
243: * The target reference.
244: */
245: public void redirectSeeOther(Reference targetRef) {
246: getWrappedResponse().redirectSeeOther(targetRef);
247: }
248:
249: /**
250: * Redirects the client to a different URI that SHOULD be retrieved using a
251: * GET method on that resource. This method exists primarily to allow the
252: * output of a POST-activated script to redirect the user agent to a
253: * selected resource. The new URI is not a substitute reference for the
254: * originally requested resource.
255: *
256: * @param targetUri
257: * The target URI.
258: */
259: public void redirectSeeOther(String targetUri) {
260: getWrappedResponse().redirectSeeOther(targetUri);
261: }
262:
263: /**
264: * Temporarily redirects the client to a target URI. The client is expected
265: * to reuse the same method for the new request.
266: *
267: * @param targetRef
268: * The target reference.
269: */
270: public void redirectTemporary(Reference targetRef) {
271: getWrappedResponse().redirectTemporary(targetRef);
272: }
273:
274: /**
275: * Temporarily redirects the client to a target URI. The client is expected
276: * to reuse the same method for the new request.
277: *
278: * @param targetUri
279: * The target URI.
280: */
281: public void redirectTemporary(String targetUri) {
282: getWrappedResponse().redirectTemporary(targetUri);
283: }
284:
285: /**
286: * Sets the associated request.
287: *
288: * @param request
289: * The associated request
290: */
291: public void setRequest(Request request) {
292: getWrappedResponse().setRequest(request);
293: }
294:
295: /**
296: * Returns a modifiable attributes map that can be used by developers to
297: * save information relative to the message. This is an easier alternative
298: * to the creation of a wrapper instance around the whole message.<br/>
299: * <br/>
300: *
301: * In addition, this map is a shared space between the developer and the
302: * connectors. In this case, it is used to exchange information that is not
303: * uniform across all protocols and couldn't therefore be directly included
304: * in the API. For this purpose, all attribute names starting with
305: * "org.restlet" are reserved. Currently the following attributes are used:
306: * <table>
307: * <tr>
308: * <th>Attribute name</th>
309: * <th>Class name</th>
310: * <th>Description</th>
311: * </tr>
312: * <tr>
313: * <td>org.restlet.http.headers</td>
314: * <td>org.restlet.data.Form</td>
315: * <td>Server HTTP connectors must provide all request headers and client
316: * HTTP connectors must provide all response headers, exactly as they were
317: * received. In addition, developers can also use this attribute to specify
318: * <b>non-standard</b> headers that should be added to the request or to
319: * the response. </td>
320: * </tr>
321: * </table> Adding standard HTTP headers is forbidden because it could
322: * conflict with the connector's internal behavior, limit portability or
323: * prevent future optimizations.</td>
324: *
325: * @return The modifiable attributes map.
326: */
327: public Map<String, Object> getAttributes() {
328: return getWrappedResponse().getAttributes();
329: }
330:
331: /**
332: * Returns the entity representation.
333: *
334: * @return The entity representation.
335: */
336: public Representation getEntity() {
337: return getWrappedResponse().getEntity();
338: }
339:
340: /**
341: * Returns the entity as a DOM representation.<br/> Note that this triggers
342: * the parsing of the entity into a reusable DOM document stored in memory.<br/>
343: * This method and the related getEntity*() methods can only be invoked
344: * once.
345: *
346: * @return The entity as a DOM representation.
347: */
348: public DomRepresentation getEntityAsDom() {
349: return getWrappedResponse().getEntityAsDom();
350: }
351:
352: /**
353: * Returns the entity as a DOM representation.<br/> Note that this triggers
354: * the parsing of the entity into a reusable DOM document stored in memory.<br/>
355: * This method and the related getEntity*() methods can only be invoked
356: * once.
357: *
358: * @return The entity as a DOM representation.
359: */
360: public Form getEntityAsForm() {
361: return getWrappedResponse().getEntityAsForm();
362: }
363:
364: /**
365: * Returns the entity as a higher-level object. This object is created by
366: * the Application's converter service. If you want to use this method to
367: * facilitate the processing of request entities, you need to provide a
368: * custom implementation of the ConverterService class, overriding the
369: * toObject(Representation) method. <br/> Note that this triggers the
370: * parsing of the entity.<br/> This method and the related getEntity*()
371: * methods can only be invoked once.
372: *
373: * @return The entity as a higher-level object.
374: */
375: public Object getEntityAsObject() {
376: return getWrappedResponse().getEntityAsObject();
377: }
378:
379: /**
380: * Returns the entity as a SAX representation.<br/> Note that this kind of
381: * representation can only be parsed once. If you evaluate an XPath
382: * expression, it can also only be done once. If you need to reuse the
383: * entity multiple times, consider using the getEntityAsDom() method
384: * instead.
385: *
386: * @return The entity as a SAX representation.
387: */
388: public SaxRepresentation getEntityAsSax() {
389: return getWrappedResponse().getEntityAsSax();
390: }
391:
392: /**
393: * Indicates if a content is available and can be sent. Several conditions
394: * must be met: the content must exists and have some available data.
395: *
396: * @return True if a content is available and can be sent.
397: */
398: public boolean isEntityAvailable() {
399: return getWrappedResponse().isEntityAvailable();
400: }
401:
402: /**
403: * Sets the entity from a higher-level object. This object is converted to a
404: * representation using the Application's converter service. If you want to
405: * use this method to facilitate the setting of entities, you need to
406: * provide a custom implementation of the ConverterService class, overriding
407: * the toRepresentation(Object) method.
408: *
409: * @param object
410: * The higher-level object.
411: */
412: public void setEntity(Object object) {
413: getWrappedResponse().setEntity(object);
414: }
415:
416: /**
417: * Sets the entity representation.
418: *
419: * @param entity
420: * The entity representation.
421: */
422: public void setEntity(Representation entity) {
423: getWrappedResponse().setEntity(entity);
424: }
425:
426: /**
427: * Sets a textual entity.
428: *
429: * @param value
430: * The represented string.
431: * @param mediaType
432: * The representation's media type.
433: */
434: public void setEntity(String value, MediaType mediaType) {
435: getWrappedResponse().setEntity(value, mediaType);
436: }
437:
438: }
|