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.ArrayList;
022: import java.util.List;
023:
024: import org.restlet.resource.Resource;
025: import org.restlet.resource.Variant;
026: import org.restlet.util.Engine;
027:
028: /**
029: * Client specific data related to a call.
030: *
031: * @author Jerome Louvel (contact@noelios.com)
032: */
033: public final class ClientInfo {
034: /** The IP addresses. */
035: private List<String> addresses;
036:
037: /** The agent name. */
038: private String agent;
039:
040: /** The port number. */
041: private int port;
042:
043: /** The character set preferences. */
044: private List<Preference<CharacterSet>> characterSetPrefs;
045:
046: /** The encoding preferences. */
047: private List<Preference<Encoding>> encodingPrefs;
048:
049: /** The language preferences. */
050: private List<Preference<Language>> languagePrefs;
051:
052: /** The media preferences. */
053: private List<Preference<MediaType>> mediaTypePrefs;
054:
055: /**
056: * Constructor.
057: */
058: public ClientInfo() {
059: this .addresses = null;
060: this .agent = null;
061: this .port = -1;
062: this .characterSetPrefs = null;
063: this .encodingPrefs = null;
064: this .languagePrefs = null;
065: this .mediaTypePrefs = null;
066: }
067:
068: /**
069: * Returns the character set preferences.
070: *
071: * @return The character set preferences.
072: */
073: public List<Preference<CharacterSet>> getAcceptedCharacterSets() {
074: if (this .characterSetPrefs == null)
075: this .characterSetPrefs = new ArrayList<Preference<CharacterSet>>();
076: return this .characterSetPrefs;
077: }
078:
079: /**
080: * Returns the encoding preferences.
081: *
082: * @return The encoding preferences.
083: */
084: public List<Preference<Encoding>> getAcceptedEncodings() {
085: if (this .encodingPrefs == null)
086: this .encodingPrefs = new ArrayList<Preference<Encoding>>();
087: return this .encodingPrefs;
088: }
089:
090: /**
091: * Returns the language preferences.
092: *
093: * @return The language preferences.
094: */
095: public List<Preference<Language>> getAcceptedLanguages() {
096: if (this .languagePrefs == null)
097: this .languagePrefs = new ArrayList<Preference<Language>>();
098: return this .languagePrefs;
099: }
100:
101: /**
102: * Returns the media type preferences.
103: *
104: * @return The media type preferences.
105: */
106: public List<Preference<MediaType>> getAcceptedMediaTypes() {
107: if (this .mediaTypePrefs == null)
108: this .mediaTypePrefs = new ArrayList<Preference<MediaType>>();
109: return this .mediaTypePrefs;
110: }
111:
112: /**
113: * Returns the client's IP address.
114: *
115: * @return The client's IP address.
116: */
117: public String getAddress() {
118: return (this .addresses == null) ? null : (this .addresses
119: .isEmpty() ? null : this .addresses.get(0));
120: }
121:
122: /**
123: * Returns the list of client IP addresses.<br/> The first address is the
124: * one of the immediate client component as returned by the
125: * getClientAdress() method and the last address should correspond to the
126: * origin client (frequently a user agent). This is useful when the user
127: * agent is separated from the origin server by a chain of intermediary
128: * components.
129: *
130: * @return The client IP addresses.
131: */
132: public List<String> getAddresses() {
133: if (this .addresses == null)
134: this .addresses = new ArrayList<String>();
135: return this .addresses;
136: }
137:
138: /**
139: * Returns the agent name (ex: "Noelios Restlet Engine/1.0").
140: *
141: * @return The agent name.
142: */
143: public String getAgent() {
144: return this .agent;
145: }
146:
147: /**
148: * Returns the port number which sent the call. If no port is specified, -1
149: * is returned.
150: *
151: * @return The port number which sent the call.
152: */
153: public int getPort() {
154: return this .port;
155: }
156:
157: /**
158: * Returns the best variant for a given resource according the the client
159: * preferences: accepted languages, accepted character sets, accepted media
160: * types and accepted encodings.<br/>A default language is provided in case
161: * the variants don't match the client preferences.
162: *
163: * @param variants
164: * The list of variants to compare.
165: * @param defaultLanguage
166: * The default language.
167: * @return The best variant.
168: * @see <a
169: * href="http://httpd.apache.org/docs/2.2/en/content-negotiation.html#algorithm">Apache
170: * content negotiation algorithm</a>
171: */
172: public Variant getPreferredVariant(List<Variant> variants,
173: Language defaultLanguage) {
174: return Engine.getInstance().getPreferredVariant(this , variants,
175: defaultLanguage);
176: }
177:
178: /**
179: * Returns the best variant for a given resource according the the client
180: * preferences.<br/>A default language is provided in case the resource's
181: * variants don't match the client preferences.
182: *
183: * @param resource
184: * The resource for which the best representation needs to be
185: * set.
186: * @param defaultLanguage
187: * The default language.
188: * @return The best variant.
189: * @see <a
190: * href="http://httpd.apache.org/docs/2.2/en/content-negotiation.html#algorithm">Apache
191: * content negotiation algorithm</a>
192: */
193: public Variant getPreferredVariant(Resource resource,
194: Language defaultLanguage) {
195: return getPreferredVariant(resource.getVariants(),
196: defaultLanguage);
197: }
198:
199: /**
200: * Sets the client's IP address.
201: *
202: * @param address
203: * The client's IP address.
204: */
205: public void setAddress(String address) {
206: if (getAddresses().isEmpty()) {
207: getAddresses().add(address);
208: } else {
209: getAddresses().set(0, address);
210: }
211: }
212:
213: /**
214: * Sets the agent name (ex: "Noelios Restlet Engine/1.0").
215: *
216: * @param agent
217: * The agent name.
218: */
219: public void setAgent(String agent) {
220: this .agent = agent;
221: }
222:
223: /**
224: * Sets the port number which sent the call.
225: *
226: * @param port
227: * The port number which sent the call.
228: */
229: public void setPort(int port) {
230: this.port = port;
231: }
232:
233: }
|