001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.server.http;
031:
032: import com.caucho.util.CharBuffer;
033:
034: /**
035: * The invocation key is a <host, port, url> triple
036: */
037: public class InvocationKey {
038: private boolean _isSecure;
039:
040: private CharSequence _host;
041: private int _port;
042:
043: private byte[] _uri;
044: private int _uriLength;
045:
046: /**
047: * Create an empty invocation key.
048: */
049: public InvocationKey() {
050: }
051:
052: /**
053: * Create a new invocation key with the given initial values. The
054: * values are copied.
055: *
056: * @param host the request's host
057: * @param uri the raw byte array containing the uri
058: * @param urlLength the length of the uri in the byte array
059: * @param port the request's port
060: */
061: InvocationKey(boolean isSecure, CharSequence host, int port,
062: byte[] uri, int uriLength) {
063: _isSecure = isSecure;
064:
065: if (host != null) {
066: CharBuffer cb = new CharBuffer();
067: cb.append(host);
068: _host = cb;
069: }
070: _port = port;
071:
072: _uri = new byte[uriLength];
073: System.arraycopy(uri, 0, _uri, 0, uriLength);
074: _uriLength = uriLength;
075: }
076:
077: /**
078: * Initialize the InvocationKey with a new triple.
079: *
080: * @param host the request's host
081: * @param port the request's port
082: * @param uri the raw byte array containing the uri
083: * @param urlLength the length of the uri in the byte array
084: */
085: public void init(boolean isSecure, CharSequence host, int port,
086: byte[] uri, int uriLength) {
087: _isSecure = isSecure;
088:
089: _host = host;
090: _port = port;
091:
092: _uri = uri;
093: _uriLength = uriLength;
094: }
095:
096: public boolean isSecure() {
097: return _isSecure;
098: }
099:
100: /**
101: * Returns the InvocationKey's host.
102: */
103: public CharSequence getHost() {
104: return _host;
105: }
106:
107: /**
108: * Sets the InvocationKey's host.
109: */
110: public void setHost(CharSequence host) {
111: _host = host;
112: }
113:
114: /**
115: * Returns the InvocationKey's port
116: */
117: public int getPort() {
118: return _port;
119: }
120:
121: /**
122: * Sets the InvocationKey's port.
123: */
124: public void setPort(int port) {
125: _port = port;
126: }
127:
128: /**
129: * Returns the raw byte array containing the uri
130: */
131: public byte[] getUriBuffer() {
132: return _uri;
133: }
134:
135: /**
136: * Returns the length of the uri in the byte array.
137: */
138: public int getUriLength() {
139: return _uriLength;
140: }
141:
142: /**
143: * Returns a hash code of the key.
144: */
145: public int hashCode() {
146: int hash = _port + (_isSecure ? 65521 : 31);
147: byte[] uri = _uri;
148: int length = _uriLength;
149:
150: for (int i = length - 1; i >= 0; i--)
151: hash = 65521 * hash + uri[i];
152:
153: if (_host != null)
154: hash = 65521 * hash + _host.hashCode();
155:
156: return hash;
157: }
158:
159: /**
160: * Returns true if the key matches the test key.
161: */
162: public boolean equals(Object b) {
163: if (!(b instanceof InvocationKey))
164: return false;
165:
166: InvocationKey test = (InvocationKey) b;
167:
168: if (_isSecure != test._isSecure)
169: return false;
170:
171: if (_port != test._port)
172: return false;
173:
174: int length = _uriLength;
175:
176: if (length != test._uriLength)
177: return false;
178:
179: byte[] uriA = _uri;
180: byte[] uriB = test._uri;
181:
182: for (int i = length - 1; i >= 0; i--)
183: if (uriA[i] != uriB[i])
184: return false;
185:
186: if (_host == null)
187: return test._host == null;
188: else
189: return _host.equals(test._host);
190: }
191:
192: public Object clone() {
193: return new InvocationKey(_isSecure, _host, _port, _uri,
194: _uriLength);
195: }
196:
197: /**
198: * Returns a printable representation of the key.
199: */
200: public String toString() {
201: if (_host == null)
202: return "InvocationKey[" + new String(_uri, 0, _uriLength)
203: + "]";
204: else
205: return ("InvocationKey[host=" + _host + ",port=" + _port
206: + ",uri=" + new String(_uri, 0, _uriLength) + "]");
207: }
208: }
|