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.data;
020:
021: import java.util.List;
022:
023: import org.restlet.resource.Representation;
024: import org.restlet.util.Series;
025:
026: /**
027: * Generic request sent by client connectors. It is then received by server
028: * connectors and processed by Restlets. This request can also be processed by a
029: * chain of Restlets, on both client and server sides. Requests are uniform across
030: * all types of connectors, protocols and components.
031: *
032: * @see org.restlet.data.Response
033: * @see org.restlet.Uniform
034: * @author Jerome Louvel (contact@noelios.com)
035: */
036: public class Request extends Message {
037: /** The authentication response sent by a client to an origin server. */
038: private ChallengeResponse challengeResponse;
039:
040: /** The client-specific information. */
041: private ClientInfo clientInfo;
042:
043: /** The condition data. */
044: private Conditions conditions;
045:
046: /** Indicates if the call came over a confidential channel. */
047: private boolean confidential;
048:
049: /** The cookies provided by the client. */
050: private Series<Cookie> cookies;
051:
052: /** The host reference. */
053: private Reference hostRef;
054:
055: /** The method. */
056: private Method method;
057:
058: /** The referrer reference. */
059: private Reference referrerRef;
060:
061: /** The resource reference. */
062: private Reference resourceRef;
063:
064: /** The application root reference. */
065: private Reference rootRef;
066:
067: /**
068: * Constructor.
069: */
070: public Request() {
071: this .confidential = false;
072: }
073:
074: /**
075: * Constructor.
076: *
077: * @param method
078: * The call's method.
079: * @param resourceRef
080: * The resource reference.
081: */
082: public Request(Method method, Reference resourceRef) {
083: this (method, resourceRef, null);
084: }
085:
086: /**
087: * Constructor.
088: *
089: * @param method
090: * The call's method.
091: * @param resourceRef
092: * The resource reference.
093: * @param entity
094: * The entity.
095: */
096: public Request(Method method, Reference resourceRef,
097: Representation entity) {
098: super (entity);
099: setMethod(method);
100: setResourceRef(resourceRef);
101: }
102:
103: /**
104: * Constructor.
105: *
106: * @param method
107: * The call's method.
108: * @param resourceUri
109: * The resource URI.
110: */
111: public Request(Method method, String resourceUri) {
112: this (method, new Reference(resourceUri));
113: }
114:
115: /**
116: * Constructor.
117: *
118: * @param method
119: * The call's method.
120: * @param resourceUri
121: * The resource URI.
122: * @param entity
123: * The entity.
124: */
125: public Request(Method method, String resourceUri,
126: Representation entity) {
127: this (method, new Reference(resourceUri), entity);
128: }
129:
130: /**
131: * Returns the authentication response sent by a client to an origin server.
132: *
133: * @return The authentication response sent by a client to an origin server.
134: */
135: public ChallengeResponse getChallengeResponse() {
136: return this .challengeResponse;
137: }
138:
139: /**
140: * Returns the client-specific information.
141: *
142: * @return The client-specific information.
143: */
144: public ClientInfo getClientInfo() {
145: if (this .clientInfo == null)
146: this .clientInfo = new ClientInfo();
147: return this .clientInfo;
148: }
149:
150: /**
151: * Returns the conditions applying to this call.
152: *
153: * @return The conditions applying to this call.
154: */
155: public Conditions getConditions() {
156: if (this .conditions == null)
157: this .conditions = new Conditions();
158: return this .conditions;
159: }
160:
161: /**
162: * Returns the cookies provided by the client.
163: *
164: * @return The cookies provided by the client.
165: */
166: public Series<Cookie> getCookies() {
167: if (this .cookies == null)
168: this .cookies = new CookieSeries();
169: return this .cookies;
170: }
171:
172: /**
173: * Returns the host reference. This may be different from the resourceRef's
174: * host, for example for URNs and other URIs that don't contain host
175: * information.
176: *
177: * @return The host reference.
178: */
179: public Reference getHostRef() {
180: return this .hostRef;
181: }
182:
183: /**
184: * Returns the method.
185: *
186: * @return The method.
187: */
188: public Method getMethod() {
189: return this .method;
190: }
191:
192: /**
193: * Returns the protocol by first returning the baseRef.schemeProtocol
194: * property if it is set, or the resourceRef.schemeProtocol property
195: * otherwise.
196: *
197: * @return The protocol or null if not available.
198: */
199: public Protocol getProtocol() {
200: Protocol result = (getResourceRef().getBaseRef() != null) ? getResourceRef()
201: .getBaseRef().getSchemeProtocol()
202: : null;
203:
204: if (result == null) {
205: // Attempt to guess the protocol to use
206: // from the target reference scheme
207: result = (getResourceRef() != null) ? getResourceRef()
208: .getSchemeProtocol() : null;
209: }
210:
211: return result;
212: }
213:
214: /**
215: * Returns the referrer reference if available.
216: *
217: * @return The referrer reference.
218: */
219: public Reference getReferrerRef() {
220: return this .referrerRef;
221: }
222:
223: /**
224: * Returns the reference of the target resource.
225: *
226: * @return The reference of the target resource.
227: */
228: public Reference getResourceRef() {
229: return this .resourceRef;
230: }
231:
232: /**
233: * Returns the application root reference.
234: *
235: * @return The application root reference.
236: */
237: public Reference getRootRef() {
238: return this .rootRef;
239: }
240:
241: /**
242: * Indicates if the call came over a confidential channel such as an
243: * SSL-secured connection.
244: *
245: * @return True if the call came over a confidential channel.
246: */
247: public boolean isConfidential() {
248: return this .confidential;
249: }
250:
251: /**
252: * Indicates if a content is available and can be sent. Several conditions
253: * must be met: the method must allow the sending of content, the content
254: * must exists and have some available data.
255: *
256: * @return True if a content is available and can be sent.
257: */
258: @Override
259: public boolean isEntityAvailable() {
260: if (getMethod().equals(Method.GET)
261: || getMethod().equals(Method.HEAD)
262: || getMethod().equals(Method.DELETE)) {
263: return false;
264: } else {
265: return super .isEntityAvailable();
266: }
267: }
268:
269: /**
270: * Sets the authentication response sent by a client to an origin server.
271: *
272: * @param response
273: * The authentication response sent by a client to an origin
274: * server.
275: */
276: public void setChallengeResponse(ChallengeResponse response) {
277: this .challengeResponse = response;
278: }
279:
280: /**
281: * Indicates if the call came over a confidential channel such as an
282: * SSL-secured connection.
283: *
284: * @param confidential
285: * True if the call came over a confidential channel.
286: */
287: public void setConfidential(boolean confidential) {
288: this .confidential = confidential;
289: }
290:
291: /**
292: * Sets the host reference.
293: *
294: * @param hostRef
295: * The host reference.
296: */
297: public void setHostRef(Reference hostRef) {
298: this .hostRef = hostRef;
299: }
300:
301: /**
302: * Sets the host reference using an URI string.
303: *
304: * @param hostUri
305: * The host URI.
306: */
307: public void setHostRef(String hostUri) {
308: setHostRef(new Reference(hostUri));
309: }
310:
311: /**
312: * Sets the method called.
313: *
314: * @param method
315: * The method called.
316: */
317: public void setMethod(Method method) {
318: this .method = method;
319: }
320:
321: /**
322: * Sets the referrer reference if available.
323: *
324: * @param referrerRef
325: * The referrer reference.
326: */
327: public void setReferrerRef(Reference referrerRef) {
328: this .referrerRef = referrerRef;
329:
330: // A referrer reference must not include a fragment.
331: if ((this .referrerRef != null)
332: && (this .referrerRef.getFragment() != null))
333: this .referrerRef.setFragment(null);
334: }
335:
336: /**
337: * Sets the referrer reference if available using an URI string.
338: *
339: * @param referrerUri
340: * The referrer URI.
341: */
342: public void setReferrerRef(String referrerUri) {
343: setReferrerRef(new Reference(referrerUri));
344: }
345:
346: /**
347: * Sets the target resource reference. If the reference is relative, it will
348: * be resolved as an absolute reference. Also, the context's base reference
349: * will be reset. Finally, the reference will be normalized to ensure a
350: * consistent handling of the call.
351: *
352: * @param resourceRef
353: * The resource reference.
354: */
355: public void setResourceRef(Reference resourceRef) {
356: this .resourceRef = resourceRef;
357: }
358:
359: /**
360: * Sets the target resource reference using an URI string. Note that the URI
361: * can be either absolute or relative to the context's base reference.
362: *
363: * @param resourceUri
364: * The resource URI.
365: */
366: public void setResourceRef(String resourceUri) {
367: if (getResourceRef() != null) {
368: // Allow usage of URIs relative to the current base reference
369: setResourceRef(new Reference(getResourceRef().getBaseRef(),
370: resourceUri));
371: } else {
372: setResourceRef(new Reference(resourceUri));
373: }
374: }
375:
376: /**
377: * Sets the application root reference.
378: *
379: * @param rootRef
380: * The application root reference.
381: */
382: public void setRootRef(Reference rootRef) {
383: this .rootRef = rootRef;
384: }
385:
386: /**
387: * Private cookie series.
388: *
389: * @author Jerome Louvel (contact@noelios.com)
390: */
391: private static class CookieSeries extends Series<Cookie> {
392: /**
393: * Constructor.
394: */
395: public CookieSeries() {
396: super ();
397: }
398:
399: /**
400: * Constructor.
401: *
402: * @param delegate
403: * The delegate list.
404: */
405: public CookieSeries(List<Cookie> delegate) {
406: super (delegate);
407: }
408:
409: @Override
410: public Cookie createEntry(String name, String value) {
411: return new Cookie(name, value);
412: }
413:
414: @Override
415: public Series<Cookie> createSeries(List<Cookie> delegate) {
416: if (delegate != null)
417: return new CookieSeries(delegate);
418: else
419: return new CookieSeries();
420: }
421: }
422:
423: }
|