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.util.logging.Logger;
022:
023: import org.restlet.Context;
024: import org.restlet.data.Parameter;
025: import org.restlet.util.Series;
026:
027: /**
028: * Converter between high-level and low-level HTTP calls.
029: *
030: * @author Jerome Louvel (contact@noelios.com)
031: */
032: public class HttpConverter {
033: /** The context. */
034: private Context context;
035:
036: /**
037: * Constructor.
038: *
039: * @param context
040: * The context to use.
041: */
042: public HttpConverter(Context context) {
043: this .context = context;
044: }
045:
046: /**
047: * Returns the context.
048: *
049: * @return The context.
050: */
051: public Context getContext() {
052: return this .context;
053: }
054:
055: /**
056: * Returns the context's logger.
057: *
058: * @return The context's logger.
059: */
060: public Logger getLogger() {
061: return getContext().getLogger();
062: }
063:
064: /**
065: * Adds additional headers if they are non-standard headers.
066: *
067: * @param existingHeaders
068: * The headers to update.
069: * @param additionalHeaders
070: * The headers to add.
071: */
072: public void addAdditionalHeaders(Series<Parameter> existingHeaders,
073: Series<Parameter> additionalHeaders) {
074: if (additionalHeaders != null) {
075: for (Parameter param : additionalHeaders) {
076: if (param.getName().equalsIgnoreCase(
077: HttpConstants.HEADER_ACCEPT)
078: || param.getName().equalsIgnoreCase(
079: HttpConstants.HEADER_ACCEPT_CHARSET)
080: || param.getName().equalsIgnoreCase(
081: HttpConstants.HEADER_ACCEPT_ENCODING)
082: || param.getName().equalsIgnoreCase(
083: HttpConstants.HEADER_ACCEPT_LANGUAGE)
084: || param.getName().equalsIgnoreCase(
085: HttpConstants.HEADER_ALLOW)
086: || param.getName().equalsIgnoreCase(
087: HttpConstants.HEADER_AUTHORIZATION)
088: || param.getName().equalsIgnoreCase(
089: HttpConstants.HEADER_CONNECTION)
090: || param.getName().equalsIgnoreCase(
091: HttpConstants.HEADER_CONTENT_ENCODING)
092: || param.getName().equalsIgnoreCase(
093: HttpConstants.HEADER_CONTENT_LANGUAGE)
094: || param.getName().equalsIgnoreCase(
095: HttpConstants.HEADER_CONTENT_LENGTH)
096: || param.getName().equalsIgnoreCase(
097: HttpConstants.HEADER_CONTENT_LOCATION)
098: || param.getName().equalsIgnoreCase(
099: HttpConstants.HEADER_CONTENT_TYPE)
100: || param.getName().equalsIgnoreCase(
101: HttpConstants.HEADER_COOKIE)
102: || param.getName().equalsIgnoreCase(
103: HttpConstants.HEADER_DATE)
104: || param.getName().equalsIgnoreCase(
105: HttpConstants.HEADER_ETAG)
106: || param.getName().equalsIgnoreCase(
107: HttpConstants.HEADER_EXPIRES)
108: || param.getName().equalsIgnoreCase(
109: HttpConstants.HEADER_HOST)
110: || param.getName().equalsIgnoreCase(
111: HttpConstants.HEADER_IF_MATCH)
112: || param.getName().equalsIgnoreCase(
113: HttpConstants.HEADER_IF_MODIFIED_SINCE)
114: || param.getName().equalsIgnoreCase(
115: HttpConstants.HEADER_IF_NONE_MATCH)
116: || param
117: .getName()
118: .equalsIgnoreCase(
119: HttpConstants.HEADER_IF_UNMODIFIED_SINCE)
120: || param.getName().equalsIgnoreCase(
121: HttpConstants.HEADER_LAST_MODIFIED)
122: || param.getName().equalsIgnoreCase(
123: HttpConstants.HEADER_LOCATION)
124: || param.getName().equalsIgnoreCase(
125: HttpConstants.HEADER_REFERRER)
126: || param.getName().equalsIgnoreCase(
127: HttpConstants.HEADER_SERVER)
128: || param.getName().equalsIgnoreCase(
129: HttpConstants.HEADER_SET_COOKIE)
130: || param.getName().equalsIgnoreCase(
131: HttpConstants.HEADER_SET_COOKIE2)
132: || param.getName().equalsIgnoreCase(
133: HttpConstants.HEADER_USER_AGENT)
134: || param.getName().equalsIgnoreCase(
135: HttpConstants.HEADER_VARY)
136: || param.getName().equalsIgnoreCase(
137: HttpConstants.HEADER_WWW_AUTHENTICATE)) {
138: // Standard headers that can't be overriden
139: getLogger()
140: .warning(
141: "Addition of the standard header \""
142: + param.getName()
143: + "\" is not allowed. Please use the Restlet API instead.");
144: } else if (param.getName().equalsIgnoreCase(
145: HttpConstants.HEADER_AGE)
146: || param.getName().equalsIgnoreCase(
147: HttpConstants.HEADER_ACCEPT_RANGES)
148: || param.getName().equalsIgnoreCase(
149: HttpConstants.HEADER_CACHE_CONTROL)
150: || param.getName().equalsIgnoreCase(
151: HttpConstants.HEADER_CONTENT_MD5)
152: || param.getName().equalsIgnoreCase(
153: HttpConstants.HEADER_CONTENT_RANGE)
154: || param.getName().equalsIgnoreCase(
155: HttpConstants.HEADER_EXPECT)
156: || param.getName().equalsIgnoreCase(
157: HttpConstants.HEADER_FROM)
158: || param.getName().equalsIgnoreCase(
159: HttpConstants.HEADER_IF_RANGE)
160: || param.getName().equalsIgnoreCase(
161: HttpConstants.HEADER_MAX_FORWARDS)
162: || param.getName().equalsIgnoreCase(
163: HttpConstants.HEADER_PRAGMA)
164: || param
165: .getName()
166: .equalsIgnoreCase(
167: HttpConstants.HEADER_PROXY_AUTHENTICATE)
168: || param
169: .getName()
170: .equalsIgnoreCase(
171: HttpConstants.HEADER_PROXY_AUTHORIZATION)
172: || param.getName().equalsIgnoreCase(
173: HttpConstants.HEADER_RANGE)
174: || param.getName().equalsIgnoreCase(
175: HttpConstants.HEADER_RETRY_AFTER)
176: || param.getName().equalsIgnoreCase(
177: HttpConstants.HEADER_TRAILER)
178: || param.getName().equalsIgnoreCase(
179: HttpConstants.HEADER_TRANSFER_ENCODING)
180: || param
181: .getName()
182: .equalsIgnoreCase(
183: HttpConstants.HEADER_TRANSFER_EXTENSION)
184: || param.getName().equalsIgnoreCase(
185: HttpConstants.HEADER_UPGRADE)
186: || param.getName().equalsIgnoreCase(
187: HttpConstants.HEADER_VIA)
188: || param.getName().equalsIgnoreCase(
189: HttpConstants.HEADER_WARNING)) {
190: // Standard headers can't shouldn't be overriden
191: getLogger()
192: .info(
193: "Addition of the standard header \""
194: + param.getName()
195: + "\" is discouraged. Future versions of the Restlet API will directly support it.");
196: existingHeaders.add(param);
197: } else {
198: existingHeaders.add(param);
199: }
200: }
201: }
202: }
203:
204: }
|