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 com.noelios.restlet.http;
020:
021: import java.io.IOException;
022: import java.util.Date;
023: import java.util.logging.Logger;
024:
025: import org.restlet.Application;
026: import org.restlet.data.Form;
027: import org.restlet.data.Parameter;
028: import org.restlet.data.Protocol;
029: import org.restlet.data.Request;
030: import org.restlet.service.ConnectorService;
031: import org.restlet.util.DateUtils;
032: import org.restlet.util.Series;
033:
034: /**
035: * Low-level call for the HTTP connectors.
036: *
037: * @author Jerome Louvel (contact@noelios.com)
038: */
039: public class HttpCall {
040:
041: /** The request headers. */
042: private final Series<Parameter> requestHeaders;
043:
044: /** The response headers. */
045: private final Series<Parameter> responseHeaders;
046:
047: /** The logger to use. */
048: private Logger logger;
049:
050: /** The hostRef domain. */
051: private String hostDomain;
052:
053: /** The hostRef port. */
054: private int hostPort;
055:
056: /** Indicates if the call is confidential. */
057: private boolean confidential;
058:
059: /** The client IP address. */
060: private String clientAddress;
061:
062: /** The client port. */
063: private int clientPort;
064:
065: /** The method. */
066: private String method;
067:
068: /** The exact protocol. */
069: private Protocol protocol;
070:
071: /** The reason phrase. */
072: private String reasonPhrase;
073:
074: /** The request URI. */
075: private String requestUri;
076:
077: /** The server IP address. */
078: private String serverAddress;
079:
080: /** The server port. */
081: private int serverPort;
082:
083: /** The status code. */
084: private int statusCode;
085:
086: /** The protocol version. */
087: private String version;
088:
089: /**
090: * Constructor.
091: */
092: public HttpCall() {
093: this .hostDomain = null;
094: this .hostPort = -1;
095: this .clientAddress = null;
096: this .clientPort = -1;
097: this .confidential = false;
098: this .method = null;
099: this .protocol = null;
100: this .reasonPhrase = "";
101: this .requestHeaders = new Form();
102: this .requestUri = null;
103: this .responseHeaders = new Form();
104: this .serverAddress = null;
105: this .serverPort = -1;
106: this .statusCode = 200;
107: this .version = null;
108: }
109:
110: /**
111: * Formats a date as a header string.
112: *
113: * @param date
114: * The date to format.
115: * @param cookie
116: * Indicates if the date should be in the cookie format.
117: * @return The formatted date.
118: */
119: public String formatDate(Date date, boolean cookie) {
120: if (cookie) {
121: return DateUtils.format(date, DateUtils.FORMAT_RFC_1036
122: .get(0));
123: } else {
124: return DateUtils.format(date, DateUtils.FORMAT_RFC_1123
125: .get(0));
126: }
127: }
128:
129: /**
130: * Returns the client address.<br/> Corresponds to the IP address of the
131: * requesting client.
132: *
133: * @return The client address.
134: */
135: public String getClientAddress() {
136: return this .clientAddress;
137: }
138:
139: /**
140: * Returns the client port.<br/> Corresponds to the TCP/IP port of the
141: * requesting client.
142: *
143: * @return The client port.
144: */
145: public int getClientPort() {
146: return this .clientPort;
147: }
148:
149: /**
150: * Returns the connector service associated to a request.
151: *
152: * @param request
153: * The request to lookup.
154: * @return The connector service associated to a request.
155: */
156: public ConnectorService getConnectorService(Request request) {
157: ConnectorService result = null;
158: Application application = (Application) request.getAttributes()
159: .get(Application.KEY);
160:
161: if (application != null) {
162: result = application.getConnectorService();
163: } else {
164: result = new ConnectorService();
165: }
166:
167: return result;
168: }
169:
170: /**
171: * Returns the host domain.
172: *
173: * @return The host domain.
174: */
175: public String getHostDomain() {
176: return this .hostDomain;
177: }
178:
179: /**
180: * Returns the host port.
181: *
182: * @return The host port.
183: */
184: public int getHostPort() {
185: return this .hostPort;
186: }
187:
188: /**
189: * Returns the logger to use.
190: *
191: * @return The logger to use.
192: */
193: public Logger getLogger() {
194: return this .logger;
195: }
196:
197: /**
198: * Returns the request method.
199: *
200: * @return The request method.
201: */
202: public String getMethod() {
203: return this .method;
204: }
205:
206: /**
207: * Returns the exact protocol (HTTP or HTTPS).
208: *
209: * @return The exact protocol (HTTP or HTTPS).
210: */
211: public Protocol getProtocol() {
212: if (this .protocol == null)
213: this .protocol = isConfidential() ? Protocol.HTTPS
214: : Protocol.HTTP;
215: return this .protocol;
216: }
217:
218: /**
219: * Returns the reason phrase.
220: *
221: * @return The reason phrase.
222: */
223: public String getReasonPhrase() {
224: return this .reasonPhrase;
225: }
226:
227: /**
228: * Returns the modifiable list of request headers.
229: *
230: * @return The modifiable list of request headers.
231: */
232: public Series<Parameter> getRequestHeaders() {
233: return this .requestHeaders;
234: }
235:
236: /**
237: * Returns the URI on the request line (most like a relative reference, but
238: * not necessarily).
239: *
240: * @return The URI on the request line.
241: */
242: public String getRequestUri() {
243: return this .requestUri;
244: }
245:
246: /**
247: * Returns the modifiable list of server headers.
248: *
249: * @return The modifiable list of server headers.
250: */
251: public Series<Parameter> getResponseHeaders() {
252: return this .responseHeaders;
253: }
254:
255: /**
256: * Returns the response address.<br/> Corresponds to the IP address of the
257: * responding server.
258: *
259: * @return The response address.
260: */
261: public String getServerAddress() {
262: return this .serverAddress;
263: }
264:
265: /**
266: * Returns the server port.
267: *
268: * @return The server port.
269: */
270: public int getServerPort() {
271: return this .serverPort;
272: }
273:
274: /**
275: * Returns the status code.
276: *
277: * @return The status code.
278: * @throws IOException
279: */
280: public int getStatusCode() throws IOException {
281: return this .statusCode;
282: }
283:
284: /**
285: * Returns the protocol version used.
286: *
287: * @return The protocol version used.
288: */
289: public String getVersion() {
290: return this .version;
291: }
292:
293: /**
294: * Indicates if the confidentiality of the call is ensured (ex: via SSL).
295: *
296: * @return True if the confidentiality of the call is ensured (ex: via SSL).
297: */
298: public boolean isConfidential() {
299: return this .confidential;
300: }
301:
302: /**
303: * Parses a date string.
304: *
305: * @param date
306: * The date string to parse.
307: * @param cookie
308: * Indicates if the date is in the cookie format.
309: * @return The parsed date.
310: */
311: public Date parseDate(String date, boolean cookie) {
312: if (cookie) {
313: return DateUtils.parse(date, DateUtils.FORMAT_RFC_1036);
314: } else {
315: return DateUtils.parse(date, DateUtils.FORMAT_RFC_1123);
316: }
317: }
318:
319: /**
320: * Sets the client address.
321: *
322: * @param clientAddress
323: * The client address.
324: */
325: protected void setClientAddress(String clientAddress) {
326: this .clientAddress = clientAddress;
327: }
328:
329: /**
330: * Sets the client port.
331: *
332: * @param clientPort
333: * The client port.
334: */
335: protected void setClientPort(int clientPort) {
336: this .clientPort = clientPort;
337: }
338:
339: /**
340: * Indicates if the confidentiality of the call is ensured (ex: via SSL).
341: *
342: * @param confidential
343: * True if the confidentiality of the call is ensured (ex: via
344: * SSL).
345: */
346: protected void setConfidential(boolean confidential) {
347: this .confidential = confidential;
348: }
349:
350: /**
351: * Sets the host domain name.
352: *
353: * @param hostDomain
354: * The baseRef domain name.
355: */
356: public void setHostDomain(String hostDomain) {
357: this .hostDomain = hostDomain;
358: }
359:
360: /**
361: * Sets the host port.
362: *
363: * @param hostPort
364: * The host port.
365: */
366: public void setHostPort(int hostPort) {
367: this .hostPort = hostPort;
368: }
369:
370: /**
371: * Sets the logger to use.
372: *
373: * @param logger
374: * The logger to use.
375: */
376: public void setLogger(Logger logger) {
377: this .logger = logger;
378: }
379:
380: /**
381: * Sets the request method.
382: *
383: * @param method
384: * The request method.
385: */
386: protected void setMethod(String method) {
387: this .method = method;
388: }
389:
390: /**
391: * Sets the exact protocol used (HTTP or HTTPS).
392: *
393: * @param protocol
394: * The protocol.
395: */
396: public void setProtocol(Protocol protocol) {
397: this .protocol = protocol;
398: }
399:
400: /**
401: * Sets the reason phrase.
402: *
403: * @param reasonPhrase
404: * The reason phrase.
405: */
406: public void setReasonPhrase(String reasonPhrase) {
407: this .reasonPhrase = reasonPhrase;
408: }
409:
410: /**
411: * Sets the full request URI.
412: *
413: * @param requestUri
414: * The full request URI.
415: */
416: protected void setRequestUri(String requestUri) {
417: if ((requestUri == null) || (requestUri.equals(""))) {
418: requestUri = "/";
419: }
420:
421: this .requestUri = requestUri;
422: }
423:
424: /**
425: * Sets the response address.<br/> Corresponds to the IP address of the
426: * responding server.
427: *
428: * @param responseAddress
429: * The response address.
430: */
431: public void setServerAddress(String responseAddress) {
432: this .serverAddress = responseAddress;
433: }
434:
435: /**
436: * Sets the server port.
437: *
438: * @param serverPort
439: * The server port.
440: */
441: public void setServerPort(int serverPort) {
442: this .serverPort = serverPort;
443: }
444:
445: /**
446: * Sets the status code.
447: *
448: * @param code
449: * The status code.
450: */
451: public void setStatusCode(int code) {
452: this .statusCode = code;
453: }
454:
455: /**
456: * Sets the protocol version used.
457: *
458: * @param version
459: * The protocol version used.
460: */
461: public void setVersion(String version) {
462: this.version = version;
463: }
464:
465: }
|