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;
020:
021: import java.net.InetAddress;
022: import java.net.UnknownHostException;
023:
024: import org.restlet.data.Request;
025: import org.restlet.data.Response;
026:
027: /**
028: * Router of calls from Server connectors to Restlets. The attached Restlets are
029: * typically Applications.<br/><br/>
030: *
031: * A virtual host is defined along three properties:
032: * <ul>
033: * <li>request's {@link Request#getHostRef()}: the URI of the host that
034: * received the request. Note that the same IP address can correspond to
035: * multiple domain names and therefore receive request with different "hostRef"
036: * URIs.</li>
037: * <li>request's {@link Request#getResourceRef()}: the URI of the target
038: * resource of the request. If this reference is relative, then it is based on
039: * the "hostRef", otherwise it is maintained as received. This difference is
040: * useful for resources identified by URNs or for Web proxies or Web caches.</li>
041: * <li>response's {@link Response#getServerInfo()}: the information about the
042: * server connector receiving the requests such as it IP address and port
043: * number.</li>
044: * </ul>
045: * When creating a new instance, you can define Java regular expressions ({@link java.util.regex.Pattern})
046: * that must match the domain name, port, scheme for references or IP address
047: * and port number for server information. The default values match everything.
048: *
049: * @see java.util.regex.Pattern
050: * @see <a href="http://en.wikipedia.org/wiki/Virtual_hosting">Wikipedia -
051: * Virtual Hosting</a>
052: * @see <a href="http://httpd.apache.org/docs/2.2/vhosts/">Apache - Virtual
053: * Hosting</a>
054: * @author Jerome Louvel (contact@noelios.com)
055: */
056: public class VirtualHost extends Router {
057: /**
058: * Returns the IP address of a given domain name.
059: *
060: * @param domain
061: * The domain name.
062: * @return The IP address.
063: */
064: public static String getIpAddress(String domain) {
065: String result = null;
066:
067: try {
068: result = InetAddress.getByName(domain).getHostAddress();
069: } catch (UnknownHostException e) {
070: }
071:
072: return result;
073: }
074:
075: /**
076: * Returns the local host IP address.
077: *
078: * @return The local host IP address.
079: */
080: public static String getLocalHostAddress() {
081: String result = null;
082:
083: try {
084: result = InetAddress.getLocalHost().getHostAddress();
085: } catch (UnknownHostException e) {
086: }
087:
088: return result;
089: }
090:
091: /**
092: * Returns the local host name.
093: *
094: * @return The local host name.
095: */
096: public static String getLocalHostName() {
097: String result = null;
098:
099: try {
100: result = InetAddress.getLocalHost().getHostName();
101: } catch (UnknownHostException e) {
102: }
103:
104: return result;
105: }
106:
107: /** The display name. */
108: private String name;
109:
110: /** The hostRef host domain pattern to match. */
111: private String hostDomain;
112:
113: /** The hostRef host port pattern to match. */
114: private String hostPort;
115:
116: /** The hostRef scheme pattern to match. */
117: private String hostScheme;
118:
119: /** The resourceRef host domain pattern to match. */
120: private String resourceDomain;
121:
122: /** The resourceRef host port pattern to match. */
123: private String resourcePort;
124:
125: /** The resourceRef scheme pattern to match. */
126: private String resourceScheme;
127:
128: /** The listening server address pattern to match. */
129: private String serverAddress;
130:
131: /** The listening server port pattern to match. */
132: private String serverPort;
133:
134: /**
135: * Constructor. Note that usage of this constructor is not recommended as
136: * the Router won't have a proper context set. In general you will prefer to
137: * use the other constructor and pass it the parent component's context.
138: */
139: public VirtualHost() {
140: this (null);
141: }
142:
143: /**
144: * Constructor. Accepts all incoming requests by default, use the set
145: * methods to restrict the matchable patterns.
146: *
147: * @param context
148: * The context.
149: */
150: public VirtualHost(Context context) {
151: this (context, ".*", ".*", ".*", ".*", ".*", ".*", ".*", ".*");
152: }
153:
154: /**
155: * Constructor.
156: *
157: * @param context
158: * The context.
159: * @param hostDomain
160: * The hostRef host domain pattern to match.
161: * @param hostPort
162: * The hostRef host port pattern to match.
163: * @param hostScheme
164: * The hostRef scheme protocol pattern to match.
165: * @param resourceDomain
166: * The resourceRef host domain pattern to match.
167: * @param resourcePort
168: * The resourceRef host port pattern to match.
169: * @param resourceScheme
170: * The resourceRef scheme protocol pattern to match.
171: * @param serverAddress
172: * The listening server address pattern to match.
173: * @param serverPort
174: * The listening server port pattern to match.
175: */
176: public VirtualHost(Context context, String hostDomain,
177: String hostPort, String hostScheme, String resourceDomain,
178: String resourcePort, String resourceScheme,
179: String serverAddress, String serverPort) {
180: super (context);
181: this .hostDomain = hostDomain;
182: this .hostPort = hostPort;
183: this .hostScheme = hostScheme;
184:
185: this .resourceDomain = resourceDomain;
186: this .resourcePort = resourcePort;
187: this .resourceScheme = resourceScheme;
188:
189: this .serverAddress = serverAddress;
190: this .serverPort = serverPort;
191: }
192:
193: @Override
194: protected Route createRoute(String uriPattern, Restlet target) {
195: return new Route(this , uriPattern, target) {
196: @Override
197: protected void beforeHandle(Request request,
198: Response response) {
199: super .beforeHandle(request, response);
200:
201: // Set the request's root reference
202: request.setRootRef(request.getResourceRef()
203: .getBaseRef());
204: }
205: };
206: }
207:
208: /**
209: * Returns the hostRef host domain to match. Uses patterns in
210: * java.util.regex.
211: *
212: * @return The hostRef host domain to match.
213: */
214: public String getHostDomain() {
215: return this .hostDomain;
216: }
217:
218: /**
219: * Returns the hostRef host port to match. Uses patterns in java.util.regex.
220: *
221: * @return The hostRef host port to match.
222: */
223: public String getHostPort() {
224: return this .hostPort;
225: }
226:
227: /**
228: * Returns the hostRef scheme to match. Uses patterns in java.util.regex.
229: *
230: * @return The hostRef scheme to match.
231: */
232: public String getHostScheme() {
233: return this .hostScheme;
234: }
235:
236: /**
237: * Returns the display name.
238: *
239: * @return The display name.
240: */
241: public String getName() {
242: return name;
243: }
244:
245: /**
246: * Returns the resourceRef host domain to match. Uses patterns in
247: * java.util.regex.
248: *
249: * @return The resourceRef host domain to match.
250: */
251: public String getResourceDomain() {
252: return this .resourceDomain;
253: }
254:
255: /**
256: * Returns the resourceRef host port to match. Uses patterns in
257: * java.util.regex.
258: *
259: * @return The resourceRef host port to match.
260: */
261: public String getResourcePort() {
262: return this .resourcePort;
263: }
264:
265: /**
266: * Returns the resourceRef scheme to match. Uses patterns in
267: * java.util.regex.
268: *
269: * @return The resourceRef scheme to match.
270: */
271: public String getResourceScheme() {
272: return this .resourceScheme;
273: }
274:
275: /**
276: * Returns the listening server address. Uses patterns in java.util.regex.
277: *
278: * @return The listening server address.
279: */
280: public String getServerAddress() {
281: return this .serverAddress;
282: }
283:
284: /**
285: * Returns the listening server port. Uses patterns in java.util.regex.
286: *
287: * @return The listening server port.
288: */
289: public String getServerPort() {
290: return this .serverPort;
291: }
292:
293: /**
294: * Sets the hostRef host domain to match. Uses patterns in java.util.regex.
295: *
296: * @param hostDomain
297: * The hostRef host domain to match.
298: */
299: public void setHostDomain(String hostDomain) {
300: this .hostDomain = hostDomain;
301: }
302:
303: /**
304: * Sets the hostRef host port to match. Uses patterns in java.util.regex.
305: *
306: * @param hostPort
307: * The hostRef host port to match.
308: */
309: public void setHostPort(String hostPort) {
310: this .hostPort = hostPort;
311: }
312:
313: /**
314: * Sets the hostRef scheme to match. Uses patterns in java.util.regex.
315: *
316: * @param hostScheme
317: * The hostRef scheme to match.
318: */
319: public void setHostScheme(String hostScheme) {
320: this .hostScheme = hostScheme;
321: }
322:
323: /**
324: * Sets the display name.
325: *
326: * @param name
327: * The display name.
328: */
329: public void setName(String name) {
330: this .name = name;
331: }
332:
333: /**
334: * Sets the resourceRef host domain to match. Uses patterns in
335: * java.util.regex.
336: *
337: * @param resourceDomain
338: * The resourceRef host domain to match.
339: */
340: public void setResourceDomain(String resourceDomain) {
341: this .resourceDomain = resourceDomain;
342: }
343:
344: /**
345: * Sets the resourceRef host port to match. Uses patterns in
346: * java.util.regex.
347: *
348: * @param resourcePort
349: * The resourceRef host port to match.
350: */
351: public void setResourcePort(String resourcePort) {
352: this .resourcePort = resourcePort;
353: }
354:
355: /**
356: * Sets the resourceRef scheme to match. Uses patterns in java.util.regex.
357: *
358: * @param resourceScheme
359: * The resourceRef scheme to match.
360: */
361: public void setResourceScheme(String resourceScheme) {
362: this .resourceScheme = resourceScheme;
363: }
364:
365: /**
366: * Sets the listening server address. Uses patterns in java.util.regex.
367: *
368: * @param serverAddress
369: * The listening server address.
370: */
371: public void setServerAddress(String serverAddress) {
372: this .serverAddress = serverAddress;
373: }
374:
375: /**
376: * Sets the listening server port. Uses patterns in java.util.regex.
377: *
378: * @param serverPort
379: * The listening server port.
380: */
381: public void setServerPort(String serverPort) {
382: this.serverPort = serverPort;
383: }
384:
385: }
|