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:
023: import org.restlet.data.ChallengeResponse;
024: import org.restlet.data.ClientInfo;
025: import org.restlet.data.Conditions;
026: import org.restlet.data.Cookie;
027: import org.restlet.data.Form;
028: import org.restlet.data.MediaType;
029: import org.restlet.data.Method;
030: import org.restlet.data.Protocol;
031: import org.restlet.data.Reference;
032: import org.restlet.data.Request;
033: import org.restlet.resource.DomRepresentation;
034: import org.restlet.resource.Representation;
035: import org.restlet.resource.SaxRepresentation;
036:
037: /**
038: * Request wrapper. Useful for application developer who need to enrich the
039: * request with application related properties and behavior.
040: *
041: * @see <a href="http://c2.com/cgi/wiki?DecoratorPattern">The decorator (aka
042: * wrapper) pattern</a>
043: * @author Jerome Louvel (contact@noelios.com)
044: */
045: public class WrapperRequest extends Request {
046: /** The wrapped request. */
047: private Request wrappedRequest;
048:
049: /**
050: * Constructor.
051: *
052: * @param wrappedRequest
053: * The wrapped request.
054: */
055: public WrapperRequest(Request wrappedRequest) {
056: this .wrappedRequest = wrappedRequest;
057: }
058:
059: /**
060: * Returns the authentication response sent by a client to an origin server.
061: *
062: * @return The authentication response sent by a client to an origin server.
063: */
064: public ChallengeResponse getChallengeResponse() {
065: return getWrappedRequest().getChallengeResponse();
066: }
067:
068: /**
069: * Returns the client-specific information.
070: *
071: * @return The client-specific information.
072: */
073: public ClientInfo getClientInfo() {
074: return getWrappedRequest().getClientInfo();
075: }
076:
077: /**
078: * Returns the conditions applying to this call.
079: *
080: * @return The conditions applying to this call.
081: */
082: public Conditions getConditions() {
083: return getWrappedRequest().getConditions();
084: }
085:
086: /**
087: * Returns the cookies provided by the client.
088: *
089: * @return The cookies provided by the client.
090: */
091: public Series<Cookie> getCookies() {
092: return getWrappedRequest().getCookies();
093: }
094:
095: /**
096: * Returns the method.
097: *
098: * @return The method.
099: */
100: public Method getMethod() {
101: return getWrappedRequest().getMethod();
102: }
103:
104: /**
105: * Returns the referrer reference if available.
106: *
107: * @return The referrer reference.
108: */
109: public Reference getReferrerRef() {
110: return getWrappedRequest().getReferrerRef();
111: }
112:
113: /**
114: * Returns the reference of the target resource.
115: *
116: * @return The reference of the target resource.
117: */
118: public Reference getResourceRef() {
119: return getWrappedRequest().getResourceRef();
120: }
121:
122: /**
123: * Returns the wrapped request.
124: *
125: * @return The wrapped request.
126: */
127: protected Request getWrappedRequest() {
128: return this .wrappedRequest;
129: }
130:
131: /**
132: * Indicates if the call came over a confidential channel such as an
133: * SSL-secured connection.
134: *
135: * @return True if the call came over a confidential channel.
136: */
137: public boolean isConfidential() {
138: return getWrappedRequest().isConfidential();
139: }
140:
141: /**
142: * Indicates if a content is available and can be sent. Several conditions
143: * must be met: the method must allow the sending of content, the content
144: * must exists and have some available data.
145: *
146: * @return True if a content is available and can be sent.
147: */
148: public boolean isEntityAvailable() {
149: return getWrappedRequest().isEntityAvailable();
150: }
151:
152: /**
153: * Sets the authentication response sent by a client to an origin server.
154: *
155: * @param response
156: * The authentication response sent by a client to an origin
157: * server.
158: */
159: public void setChallengeResponse(ChallengeResponse response) {
160: getWrappedRequest().setChallengeResponse(response);
161: }
162:
163: /**
164: * Indicates if the call came over a confidential channel such as an
165: * SSL-secured connection.
166: *
167: * @param confidential
168: * True if the call came over a confidential channel.
169: */
170: public void setConfidential(boolean confidential) {
171: getWrappedRequest().setConfidential(confidential);
172: }
173:
174: /**
175: * Sets the method called.
176: *
177: * @param method
178: * The method called.
179: */
180: public void setMethod(Method method) {
181: getWrappedRequest().setMethod(method);
182: }
183:
184: /**
185: * Sets the referrer reference if available.
186: *
187: * @param referrerRef
188: * The referrer reference.
189: */
190: public void setReferrerRef(Reference referrerRef) {
191: getWrappedRequest().setReferrerRef(referrerRef);
192: }
193:
194: /**
195: * Sets the referrer reference if available using an URI string.
196: *
197: * @param referrerUri
198: * The referrer URI.
199: */
200: public void setReferrerRef(String referrerUri) {
201: getWrappedRequest().setReferrerRef(referrerUri);
202: }
203:
204: /**
205: * Sets the target resource reference. If the reference is relative, it will
206: * be resolved as an absolute reference. Also, the context's base reference
207: * will be reset. Finally, the reference will be normalized to ensure a
208: * consistent handling of the call.
209: *
210: * @param resourceRef
211: * The resource reference.
212: */
213: public void setResourceRef(Reference resourceRef) {
214: getWrappedRequest().setResourceRef(resourceRef);
215: }
216:
217: /**
218: * Sets the target resource reference using an URI string. Note that the URI
219: * can be either absolute or relative to the context's base reference.
220: *
221: * @param resourceUri
222: * The resource URI.
223: */
224: public void setResourceRef(String resourceUri) {
225: getWrappedRequest().setResourceRef(resourceUri);
226: }
227:
228: /**
229: * Returns the host reference. This may be different from the resourceRef's
230: * host, for example for URNs and other URIs that don't contain host
231: * information.
232: *
233: * @return The host reference.
234: */
235: public Reference getHostRef() {
236: return getWrappedRequest().getHostRef();
237: }
238:
239: /**
240: * Returns the protocol by first returning the baseRef.schemeProtocol
241: * property if it is set, or the resourceRef.schemeProtocol property
242: * otherwise.
243: *
244: * @return The protocol or null if not available.
245: */
246: public Protocol getProtocol() {
247: return getWrappedRequest().getProtocol();
248: }
249:
250: /**
251: * Returns the application root reference.
252: *
253: * @return The application root reference.
254: */
255: public Reference getRootRef() {
256: return getWrappedRequest().getRootRef();
257: }
258:
259: /**
260: * Sets the host reference.
261: *
262: * @param hostRef
263: * The host reference.
264: */
265: public void setHostRef(Reference hostRef) {
266: getWrappedRequest().setHostRef(hostRef);
267: }
268:
269: /**
270: * Sets the host reference using an URI string.
271: *
272: * @param hostUri
273: * The host URI.
274: */
275: public void setHostRef(String hostUri) {
276: getWrappedRequest().setHostRef(hostUri);
277: }
278:
279: /**
280: * Sets the application root reference.
281: *
282: * @param rootRef
283: * The application root reference.
284: */
285: public void setRootRef(Reference rootRef) {
286: getWrappedRequest().setRootRef(rootRef);
287: }
288:
289: /**
290: * Returns a modifiable attributes map that can be used by developers to
291: * save information relative to the message. This is an easier alternative
292: * to the creation of a wrapper instance around the whole message.<br/>
293: * <br/>
294: *
295: * In addition, this map is a shared space between the developer and the
296: * connectors. In this case, it is used to exchange information that is not
297: * uniform across all protocols and couldn't therefore be directly included
298: * in the API. For this purpose, all attribute names starting with
299: * "org.restlet" are reserved. Currently the following attributes are used:
300: * <table>
301: * <tr>
302: * <th>Attribute name</th>
303: * <th>Class name</th>
304: * <th>Description</th>
305: * </tr>
306: * <tr>
307: * <td>org.restlet.http.headers</td>
308: * <td>org.restlet.data.Form</td>
309: * <td>Server HTTP connectors must provide all request headers and client
310: * HTTP connectors must provide all response headers, exactly as they were
311: * received. In addition, developers can also use this attribute to specify
312: * <b>non-standard</b> headers that should be added to the request or to
313: * the response. </td>
314: * </tr>
315: * </table> Adding standard HTTP headers is forbidden because it could
316: * conflict with the connector's internal behavior, limit portability or
317: * prevent future optimizations.</td>
318: *
319: * @return The modifiable attributes map.
320: */
321: public Map<String, Object> getAttributes() {
322: return getWrappedRequest().getAttributes();
323: }
324:
325: /**
326: * Returns the entity representation.
327: *
328: * @return The entity representation.
329: */
330: public Representation getEntity() {
331: return getWrappedRequest().getEntity();
332: }
333:
334: @Override
335: public DomRepresentation getEntityAsDom() {
336: return getWrappedRequest().getEntityAsDom();
337: }
338:
339: /**
340: * Returns the entity as a DOM representation.<br/> Note that this triggers
341: * the parsing of the entity into a reusable DOM document stored in memory.<br/>
342: * This method and the related getEntity*() methods can only be invoked
343: * once.
344: *
345: * @return The entity as a DOM representation.
346: */
347: public Form getEntityAsForm() {
348: return getWrappedRequest().getEntityAsForm();
349: }
350:
351: /**
352: * Returns the entity as a higher-level object. This object is created by
353: * the Application's converter service. If you want to use this method to
354: * facilitate the processing of request entities, you need to provide a
355: * custom implementation of the ConverterService class, overriding the
356: * toObject(Representation) method. <br/> Note that this triggers the
357: * parsing of the entity.<br/> This method and the related getEntity*()
358: * methods can only be invoked once.
359: *
360: * @return The entity as a higher-level object.
361: */
362: public Object getEntityAsObject() {
363: return getWrappedRequest().getEntityAsObject();
364: }
365:
366: /**
367: * Returns the entity as a SAX representation.<br/> Note that this kind of
368: * representation can only be parsed once. If you evaluate an XPath
369: * expression, it can also only be done once. If you need to reuse the
370: * entity multiple times, consider using the getEntityAsDom() method
371: * instead.
372: *
373: * @return The entity as a SAX representation.
374: */
375: public SaxRepresentation getEntityAsSax() {
376: return getWrappedRequest().getEntityAsSax();
377: }
378:
379: /**
380: * Sets the entity from a higher-level object. This object is converted to a
381: * representation using the Application's converter service. If you want to
382: * use this method to facilitate the setting of entities, you need to
383: * provide a custom implementation of the ConverterService class, overriding
384: * the toRepresentation(Object) method.
385: *
386: * @param object
387: * The higher-level object.
388: */
389: public void setEntity(Object object) {
390: getWrappedRequest().setEntity(object);
391: }
392:
393: /**
394: * Sets the entity representation.
395: *
396: * @param entity
397: * The entity representation.
398: */
399: public void setEntity(Representation entity) {
400: getWrappedRequest().setEntity(entity);
401: }
402:
403: /**
404: * Sets a textual entity.
405: *
406: * @param value
407: * The represented string.
408: * @param mediaType
409: * The representation's media type.
410: */
411: public void setEntity(String value, MediaType mediaType) {
412: getWrappedRequest().setEntity(value, mediaType);
413: }
414:
415: }
|