View Javadoc

1   /***
2    * Simple Web Spider - <http://simplewebspider.sourceforge.net/>
3    * Copyright (C) 2009  <berendona@users.sourceforge.net>
4    *
5    * This program is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  package simplespider.simplespider.bot.http.apache;
19  
20  import java.io.IOException;
21  import java.io.InputStream;
22  
23  import org.apache.commons.httpclient.Header;
24  import org.apache.commons.httpclient.HttpException;
25  import org.apache.commons.httpclient.HttpMethod;
26  import org.apache.commons.httpclient.StatusLine;
27  import org.apache.commons.httpclient.URI;
28  import org.apache.commons.httpclient.URIException;
29  import org.apache.commons.httpclient.methods.GetMethod;
30  
31  import simplespider.simplespider.bot.http.HttpClient;
32  import simplespider.simplespider.util.ValidityHelper;
33  
34  public class ApacheHttpClient implements HttpClient {
35  	private final org.apache.commons.httpclient.HttpClient	httpClient;
36  	private HttpMethod										method	= null;
37  
38  	ApacheHttpClient(final org.apache.commons.httpclient.HttpClient httpClient) {
39  		this.httpClient = httpClient;
40  	}
41  
42  	/*
43  	 * (non-Javadoc)
44  	 * @see simplespider.simplespider_core.http.HttpClient#createConnection(java.lang.String)
45  	 */
46  	public void createConnection(final String url) throws HttpException, IOException {
47  		ValidityHelper.checkNotEmpty("url", url);
48  
49  		if (this.method != null) {
50  			throw new IllegalStateException("There is an already open connection");
51  		}
52  
53  		this.method = new GetMethod(url);
54  		this.method.setFollowRedirects(true);
55  
56  		this.httpClient.executeMethod(this.method);
57  	}
58  
59  	/*
60  	 * (non-Javadoc)
61  	 * @see simplespider.simplespider_core.http.HttpClient#getStatusCode()
62  	 */
63  	public int getStatusCode() {
64  		checkForOpenConnection();
65  		return this.method.getStatusCode();
66  	}
67  
68  	/*
69  	 * (non-Javadoc)
70  	 * @see simplespider.simplespider_core.http.HttpClient#getStatusLine()
71  	 */
72  	public StatusLine getStatusLine() {
73  		checkForOpenConnection();
74  		return this.method.getStatusLine();
75  	}
76  
77  	/*
78  	 * (non-Javadoc)
79  	 * @see simplespider.simplespider_core.http.HttpClient#getStatusText()
80  	 */
81  	public String getStatusText() {
82  		checkForOpenConnection();
83  		return this.method.getStatusText();
84  	}
85  
86  	/*
87  	 * (non-Javadoc)
88  	 * @see simplespider.simplespider_core.http.HttpClient#getRedirectedUrl()
89  	 */
90  	public String getRedirectedUrl() throws URIException {
91  		checkForOpenConnection();
92  		final URI uri = this.method.getURI();
93  		final String realBaseUrl = uri.getURI();
94  		return realBaseUrl;
95  	}
96  
97  	/*
98  	 * (non-Javadoc)
99  	 * @see simplespider.simplespider_core.http.HttpClient#getResponseBodyAsStream()
100 	 */
101 	public InputStream getResponseBodyAsStream() throws IOException {
102 		checkForOpenConnection();
103 		return this.method.getResponseBodyAsStream();
104 	}
105 
106 	/*
107 	 * (non-Javadoc)
108 	 * @see simplespider.simplespider_core.http.HttpClient#releaseConnection()
109 	 */
110 	public void releaseConnection() {
111 		checkForOpenConnection();
112 		this.method.releaseConnection();
113 		this.method = null;
114 	}
115 
116 	private void checkForOpenConnection() {
117 		if (this.method == null) {
118 			throw new IllegalStateException("There is no open connection");
119 		}
120 	}
121 
122 	public String getMimeType() {
123 		checkForOpenConnection();
124 		final Header contentTypeHeader = this.method.getResponseHeader("Content-Type");
125 		if (contentTypeHeader == null) {
126 			return null;
127 		}
128 
129 		final String contentTypeValue = contentTypeHeader.getValue();
130 		if (contentTypeValue == null) {
131 			return null;
132 		}
133 
134 		// Only the first part is the mime type: e.g. "text/html; charset=UTF-8"
135 		final String[] split = contentTypeValue.split(";", -1);
136 		return split[0];
137 	}
138 
139 }