001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.harmony.luni.internal.net.www.protocol.http;
018:
019: import java.net.InetSocketAddress;
020: import java.net.Proxy;
021: import java.net.SocketAddress;
022: import java.net.URI;
023:
024: import org.apache.harmony.luni.util.Msg;
025:
026: /**
027: * An <code>HttpConfiguration</code> contains all the details needed to create an http connection
028: * and to compare whether or not two connections are the same. An HttpConfiguration
029: * will either consist of a <code>Proxy<code> or a port number (<code>int</code>)
030: * and host name (<code>String</code>) or all three, depending on whether or not a
031: * <code>Proxy</code> is used and the type of <code>Proxy</code> it is.
032: *
033: * <code>HttpConfiguration</code> is used as a key by <code>HttpConnectionManager</code>
034: * to retrieve <code>HttpConnection</code>s from its connection pool.
035: */
036: public class HttpConfiguration {
037:
038: private Proxy proxy;
039: private int hostPort;
040: private String hostName;
041: private URI uri;
042:
043: public HttpConfiguration(URI uri) {
044: this .uri = uri;
045: this .hostName = uri.getHost();
046: this .hostPort = uri.getPort();
047: if (hostPort == -1) {
048: if (uri.getScheme().equals("https")) { //$NON-NLS-1$
049: hostPort = 443;
050: } else {
051: hostPort = 80;
052: }
053: }
054: }
055:
056: public HttpConfiguration(URI uri, Proxy proxy) {
057: this .uri = uri;
058: this .proxy = proxy;
059: if (proxy.type() == Proxy.Type.HTTP) {
060: SocketAddress proxyAddr = proxy.address();
061: if (!(proxyAddr instanceof InetSocketAddress)) {
062: throw new IllegalArgumentException(Msg.getString(
063: "K0316", proxyAddr.getClass())); //$NON-NLS-1$
064: }
065: InetSocketAddress iProxyAddr = (InetSocketAddress) proxyAddr;
066: this .hostName = iProxyAddr.getHostName();
067: this .hostPort = iProxyAddr.getPort();
068: } else {
069: // using SOCKS proxy
070: this .hostName = uri.getHost();
071: this .hostPort = uri.getPort();
072: if (hostPort == -1) {
073: if (uri.getScheme().equals("https")) { //$NON-NLS-1$
074: hostPort = 443;
075: } else {
076: hostPort = 80;
077: }
078: }
079: }
080: this .uri = uri;
081: SocketAddress proxyAddr = proxy.address();
082: if (!(proxyAddr instanceof InetSocketAddress)) {
083: throw new IllegalArgumentException(Msg.getString(
084: "K0316", proxyAddr.getClass())); //$NON-NLS-1$
085: }
086: InetSocketAddress iProxyAddr = (InetSocketAddress) proxyAddr;
087: this .hostName = iProxyAddr.getHostName();
088: this .hostPort = iProxyAddr.getPort();
089: }
090:
091: /**
092: * Returns true if this configuration uses a Proxy
093: */
094: public boolean usesProxy() {
095: return proxy != null;
096: }
097:
098: /**
099: * Returns the Proxy for this configuration, or null if a proxy
100: * is not used
101: */
102: public Proxy getProxy() {
103: return proxy;
104: }
105:
106: /**
107: * Returns the host name for this configuration, or null if an http Proxy is used
108: */
109: public String getHostName() {
110: return hostName;
111: }
112:
113: /**
114: * Returns the port for this configuration, or 0 if an http Proxy is used
115: */
116: public int getHostPort() {
117: return hostPort;
118: }
119:
120: @Override
121: public boolean equals(Object arg0) {
122: if (!(arg0 instanceof HttpConfiguration)) {
123: return false;
124: } else {
125: HttpConfiguration config = (HttpConfiguration) arg0;
126: if (config.proxy != null && proxy != null) {
127: return config.proxy.equals(proxy)
128: && uri.equals(config.uri);
129: }
130: return uri.equals(config.uri);
131: }
132: }
133:
134: @Override
135: public int hashCode() {
136: return uri.hashCode();
137: }
138:
139: }
|