001: /*
002: * Copyright 2006 by Lars Torunski
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: *
016: */
017: package com.torunski.crawler.parser.httpclient;
018:
019: import org.apache.commons.httpclient.Credentials;
020: import org.apache.commons.httpclient.HttpClient;
021: import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
022: import org.apache.commons.httpclient.ProxyHost;
023: import org.apache.commons.httpclient.auth.AuthScope;
024:
025: /**
026: * Contains abstract methods for the DownloadHelper and SimpleHttpClientParser.
027: *
028: * @author Lars Torunski
029: */
030: public abstract class AbstractHttpClient {
031:
032: /** the http client */
033: protected HttpClient client;
034:
035: /**
036: * Creates an instance of AbstractHttpClient.
037: * @param multiThreaded true for creating a multi threaded connection manager else only a single connection is allowed
038: */
039: protected AbstractHttpClient(boolean multiThreaded) {
040: if (multiThreaded) {
041: // Creates one instance of HttpClient with a MultiThreadedHttpConnectionManager.
042: client = new HttpClient(
043: new MultiThreadedHttpConnectionManager());
044: } else {
045: // Creates one instance of HttpClient with a single connection manager.
046: client = new HttpClient();
047: }
048: }
049:
050: /**
051: * Set the proxy settings.
052: * @param proxyHost The proxy host
053: * @param proxyPort The proxy port
054: */
055: public void setProxy(String proxyHost, int proxyPort) {
056: client.getHostConfiguration().setProxyHost(
057: new ProxyHost(proxyHost, proxyPort));
058: }
059:
060: /**
061: * Returns the proxyHost.
062: * @return the proxy host, or <code>null</code> if not set
063: */
064: public String getProxyHost() {
065: return client.getHostConfiguration().getProxyHost();
066: }
067:
068: /**
069: * Returns the proxyPort.
070: * @return the proxy port, or <code>-1</code> if not set
071: */
072: public int getProxyPort() {
073: return client.getHostConfiguration().getProxyPort();
074: }
075:
076: /**
077: * Sets the proxy credentials for the given authentication realm.
078: * Any previous credentials for the given realm will be overwritten.
079: *
080: * @param authscope the authentication scope
081: * @param credentials the authentication credentials for the given realm.
082: *
083: * @see org.apache.commons.httpclient.HttpState#setProxyCredentials(AuthScope, Credentials)
084: */
085: public void setProxyCredentials(AuthScope authscope,
086: Credentials credentials) {
087: client.getState().setProxyCredentials(authscope, credentials);
088: }
089:
090: /**
091: * Get the proxy credentials for the given authentication scope.
092: *
093: * @param authscope the {@link AuthScope authentication scope}
094: * @return the credentials
095: *
096: * @see org.apache.commons.httpclient.HttpState#getProxyCredentials(AuthScope)
097: */
098: public Credentials getProxyCredentials(AuthScope authscope) {
099: return client.getState().getProxyCredentials(authscope);
100: }
101:
102: /**
103: * Sets the credentials for the given authentication scope.
104: * Any previous credentials for the given scope will be overwritten.
105: *
106: * @param authscope the authentication scope
107: * @param credentials the authentication credentials for the given scope.
108: *
109: * @see org.apache.commons.httpclient.HttpState#setCredentials(AuthScope, Credentials)
110: */
111: public void setCredentials(AuthScope authscope,
112: Credentials credentials) {
113: client.getState().setCredentials(authscope, credentials);
114: }
115:
116: /**
117: * Get the credentials for the given authentication scope.
118: *
119: * @param authscope the authentication scope
120: * @return the credentials
121: *
122: * @see org.apache.commons.httpclient.HttpState#getCredentials(AuthScope)
123: */
124: public Credentials getCredentials(AuthScope authscope) {
125: return client.getState().getCredentials(authscope);
126: }
127:
128: }
|