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.EnumSet;
022: import java.util.HashSet;
023: import java.util.List;
024: import java.util.Set;
025:
026: import org.restlet.util.Series;
027:
028: /**
029: * Generic response sent by server connectors. It is then received by client
030: * connectors. Responses are uniform across all types of connectors, protocols
031: * and components.
032: *
033: * @see org.restlet.data.Request
034: * @see org.restlet.Uniform
035: * @author Jerome Louvel (contact@noelios.com)
036: */
037: public class Response extends Message {
038: /** The set of methods allowed on the requested resource. */
039: private Set<Method> allowedMethods;
040:
041: /** The authentication request sent by an origin server to a client. */
042: private ChallengeRequest challengeRequest;
043:
044: /** The cookie settings provided by the server. */
045: private Series<CookieSetting> cookieSettings;
046:
047: /** The set of dimensions on which the response entity may vary. */
048: private Set<Dimension> dimensions;
049:
050: /** The redirection reference. */
051: private Reference redirectRef;
052:
053: /** The associated request. */
054: private Request request;
055:
056: /** The server-specific information. */
057: private ServerInfo serverInfo;
058:
059: /** The status. */
060: private Status status;
061:
062: /**
063: * Constructor.
064: *
065: * @param request
066: * The request associated to this response.
067: */
068: public Response(Request request) {
069: this .allowedMethods = null;
070: this .challengeRequest = null;
071: this .cookieSettings = null;
072: this .dimensions = null;
073: this .redirectRef = null;
074: this .request = request;
075: this .serverInfo = null;
076: this .status = Status.SUCCESS_OK;
077: }
078:
079: /**
080: * Returns the set of methods allowed on the requested resource. This
081: * property only has to be updated when a status
082: * CLIENT_ERROR_METHOD_NOT_ALLOWED is set.
083: *
084: * @return The list of allowed methods.
085: */
086: public Set<Method> getAllowedMethods() {
087: if (this .allowedMethods == null)
088: this .allowedMethods = new HashSet<Method>();
089: return this .allowedMethods;
090: }
091:
092: /**
093: * Returns the authentication request sent by an origin server to a client.
094: *
095: * @return The authentication request sent by an origin server to a client.
096: */
097: public ChallengeRequest getChallengeRequest() {
098: return this .challengeRequest;
099: }
100:
101: /**
102: * Returns the cookie settings provided by the server.
103: *
104: * @return The cookie settings provided by the server.
105: */
106: public Series<CookieSetting> getCookieSettings() {
107: if (this .cookieSettings == null)
108: this .cookieSettings = new CookieSettingSeries();
109: return this .cookieSettings;
110: }
111:
112: /**
113: * Returns the set of selecting dimensions on which the response entity may
114: * vary. If some server-side content negotiation is done, this set should be
115: * properly updated, other it can be left empty.
116: *
117: * @return The set of dimensions on which the response entity may vary.
118: */
119: public Set<Dimension> getDimensions() {
120: if (this .dimensions == null)
121: this .dimensions = EnumSet.noneOf(Dimension.class);
122: return this .dimensions;
123: }
124:
125: /**
126: * Returns the reference that the client should follow for redirections or
127: * resource creations.
128: *
129: * @return The redirection reference.
130: */
131: public Reference getRedirectRef() {
132: return this .redirectRef;
133: }
134:
135: /**
136: * Returns the associated request
137: *
138: * @return The associated request
139: */
140: public Request getRequest() {
141: return request;
142: }
143:
144: /**
145: * Returns the server-specific information.
146: *
147: * @return The server-specific information.
148: */
149: public ServerInfo getServerInfo() {
150: if (this .serverInfo == null)
151: this .serverInfo = new ServerInfo();
152: return this .serverInfo;
153: }
154:
155: /**
156: * Returns the status.
157: *
158: * @return The status.
159: */
160: public Status getStatus() {
161: return this .status;
162: }
163:
164: /**
165: * Permanently redirects the client to a target URI. The client is expected
166: * to reuse the same method for the new request.
167: *
168: * @param targetUri
169: * The target URI.
170: */
171: public void redirectPermanent(String targetUri) {
172: redirectPermanent(new Reference(targetUri));
173: }
174:
175: /**
176: * Permanently redirects the client to a target URI. The client is expected
177: * to reuse the same method for the new request.
178: *
179: * @param targetRef
180: * The target URI reference.
181: */
182: public void redirectPermanent(Reference targetRef) {
183: setRedirectRef(targetRef);
184: setStatus(Status.REDIRECTION_PERMANENT);
185: }
186:
187: /**
188: * Redirects the client to a different URI that SHOULD be retrieved using a
189: * GET method on that resource. This method exists primarily to allow the
190: * output of a POST-activated script to redirect the user agent to a
191: * selected resource. The new URI is not a substitute reference for the
192: * originally requested resource.
193: *
194: * @param targetUri
195: * The target URI.
196: */
197: public void redirectSeeOther(String targetUri) {
198: redirectSeeOther(new Reference(targetUri));
199: }
200:
201: /**
202: * Redirects the client to a different URI that SHOULD be retrieved using a
203: * GET method on that resource. This method exists primarily to allow the
204: * output of a POST-activated script to redirect the user agent to a
205: * selected resource. The new URI is not a substitute reference for the
206: * originally requested resource.
207: *
208: * @param targetRef
209: * The target reference.
210: */
211: public void redirectSeeOther(Reference targetRef) {
212: setRedirectRef(targetRef);
213: setStatus(Status.REDIRECTION_SEE_OTHER);
214: }
215:
216: /**
217: * Temporarily redirects the client to a target URI. The client is expected
218: * to reuse the same method for the new request.
219: *
220: * @param targetUri
221: * The target URI.
222: */
223: public void redirectTemporary(String targetUri) {
224: redirectTemporary(new Reference(targetUri));
225: }
226:
227: /**
228: * Temporarily redirects the client to a target URI. The client is expected
229: * to reuse the same method for the new request.
230: *
231: * @param targetRef
232: * The target reference.
233: */
234: public void redirectTemporary(Reference targetRef) {
235: setRedirectRef(targetRef);
236: setStatus(Status.REDIRECTION_TEMPORARY);
237: }
238:
239: /**
240: * Sets the authentication request sent by an origin server to a client.
241: *
242: * @param request
243: * The authentication request sent by an origin server to a
244: * client.
245: */
246: public void setChallengeRequest(ChallengeRequest request) {
247: this .challengeRequest = request;
248: }
249:
250: /**
251: * Sets the reference that the client should follow for redirections or
252: * resource creations.
253: *
254: * @param redirectRef
255: * The redirection reference.
256: */
257: public void setRedirectRef(Reference redirectRef) {
258: this .redirectRef = redirectRef;
259: }
260:
261: /**
262: * Sets the reference that the client should follow for redirections or
263: * resource creations.
264: *
265: * @param redirectUri
266: * The redirection URI.
267: */
268: public void setRedirectRef(String redirectUri) {
269: Reference baseRef = (getRequest().getResourceRef() != null) ? getRequest()
270: .getResourceRef().getBaseRef()
271: : null;
272: setRedirectRef(new Reference(baseRef, redirectUri)
273: .getTargetRef());
274: }
275:
276: /**
277: * Sets the associated request.
278: *
279: * @param request
280: * The associated request
281: */
282: public void setRequest(Request request) {
283: this .request = request;
284: }
285:
286: /**
287: * Sets the status.
288: *
289: * @param status
290: * The status to set.
291: */
292: public void setStatus(Status status) {
293: this .status = status;
294: }
295:
296: /**
297: * Sets the status.
298: *
299: * @param status
300: * The status to set.
301: * @param message
302: * The status message.
303: */
304: public void setStatus(Status status, String message) {
305: setStatus(new Status(status, message));
306: }
307:
308: /**
309: * Private cookie setting series.
310: *
311: * @author Jerome Louvel (contact@noelios.com)
312: */
313: private static class CookieSettingSeries extends
314: Series<CookieSetting> {
315: /**
316: * Constructor.
317: */
318: public CookieSettingSeries() {
319: super ();
320: }
321:
322: /**
323: * Constructor.
324: *
325: * @param delegate
326: * The delegate list.
327: */
328: public CookieSettingSeries(List<CookieSetting> delegate) {
329: super (delegate);
330: }
331:
332: @Override
333: public CookieSetting createEntry(String name, String value) {
334: return new CookieSetting(name, value);
335: }
336:
337: @Override
338: public Series<CookieSetting> createSeries(
339: List<CookieSetting> delegate) {
340: if (delegate != null)
341: return new CookieSettingSeries(delegate);
342: else
343: return new CookieSettingSeries();
344: }
345: }
346:
347: }
|