1414 * See the License for the specific language governing permissions and
1515 * limitations under the License.
1616 */
17+
1718package org .apache .commons .vfs2 .provider .http4 ;
1819
1920import java .io .IOException ;
3334import org .apache .http .Header ;
3435import org .apache .http .HttpResponse ;
3536import org .apache .http .HttpStatus ;
37+ import org .apache .http .client .ClientProtocolException ;
3638import org .apache .http .client .HttpClient ;
3739import org .apache .http .client .methods .HttpGet ;
3840import org .apache .http .client .methods .HttpHead ;
@@ -58,8 +60,7 @@ public class Http4FileObject<FS extends Http4FileSystem> extends AbstractFileObj
5860 private final String urlCharset ;
5961
6062 /**
61- * Internal URI mapped to this {@code FileObject}.
62- * For example, the internal URI of {@code http4://example.com/a.txt} is {@code http://example.com/a.txt}.
63+ * Internal URI mapped to this {@code FileObject}. For example, the internal URI of {@code http4://example.com/a.txt} is {@code http://example.com/a.txt}.
6364 */
6465 private final URI internalURI ;
6566
@@ -71,25 +72,23 @@ public class Http4FileObject<FS extends Http4FileSystem> extends AbstractFileObj
7172 /**
7273 * Constructs {@code Http4FileObject}.
7374 *
74- * @param name file name
75+ * @param name file name
7576 * @param fileSystem file system
7677 * @throws FileSystemException if any error occurs
7778 */
78- protected Http4FileObject (final AbstractFileName name , final FS fileSystem )
79- throws FileSystemException {
79+ protected Http4FileObject (final AbstractFileName name , final FS fileSystem ) throws FileSystemException {
8080 this (name , fileSystem , Http4FileSystemConfigBuilder .getInstance ());
8181 }
8282
8383 /**
8484 * Constructs {@code Http4FileObject}.
8585 *
86- * @param name file name
86+ * @param name file name
8787 * @param fileSystem file system
88- * @param builder {@code Http4FileSystemConfigBuilder} object
88+ * @param builder {@code Http4FileSystemConfigBuilder} object
8989 * @throws FileSystemException if any error occurs
9090 */
91- protected Http4FileObject (final AbstractFileName name , final FS fileSystem ,
92- final Http4FileSystemConfigBuilder builder ) throws FileSystemException {
91+ protected Http4FileObject (final AbstractFileName name , final FS fileSystem , final Http4FileSystemConfigBuilder builder ) throws FileSystemException {
9392 super (name , fileSystem );
9493 final FileSystemOptions fileSystemOptions = fileSystem .getFileSystemOptions ();
9594 urlCharset = builder .getUrlCharset (fileSystemOptions );
@@ -107,14 +106,11 @@ protected long doGetContentSize() throws Exception {
107106 if (lastHeadResponse == null ) {
108107 return 0L ;
109108 }
110-
111109 final Header header = lastHeadResponse .getFirstHeader (HTTP .CONTENT_LEN );
112-
113110 if (header == null ) {
114111 // Assume 0 content-length
115112 return 0 ;
116113 }
117-
118114 return Long .parseLong (header .getValue ());
119115 }
120116
@@ -123,26 +119,20 @@ protected InputStream doGetInputStream(final int bufferSize) throws Exception {
123119 final HttpGet getRequest = new HttpGet (getInternalURI ());
124120 final HttpResponse httpResponse = executeHttpUriRequest (getRequest );
125121 final int status = httpResponse .getStatusLine ().getStatusCode ();
126-
127122 if (status == HttpStatus .SC_NOT_FOUND ) {
128123 throw new FileNotFoundException (getName ());
129124 }
130-
131125 if (status != HttpStatus .SC_OK ) {
132126 throw new FileSystemException ("vfs.provider.http/get.error" , getName (), Integer .valueOf (status ));
133127 }
134-
135128 return new MonitoredHttpResponseContentInputStream (httpResponse , bufferSize );
136129 }
137130
138131 @ Override
139132 protected long doGetLastModifiedTime () throws Exception {
140133 FileSystemException .requireNonNull (lastHeadResponse , "vfs.provider.http/last-modified.error" , getName ());
141-
142134 final Header header = lastHeadResponse .getFirstHeader ("Last-Modified" );
143-
144135 FileSystemException .requireNonNull (header , "vfs.provider.http/last-modified.error" , getName ());
145-
146136 return DateUtils .parseDate (header .getValue ()).getTime ();
147137 }
148138
@@ -155,9 +145,7 @@ protected RandomAccessContent doGetRandomAccessContent(final RandomAccessMode mo
155145 protected FileType doGetType () throws Exception {
156146 lastHeadResponse = executeHttpUriRequest (new HttpHead (getInternalURI ()));
157147 final int status = lastHeadResponse .getStatusLine ().getStatusCode ();
158-
159- if (status == HttpStatus .SC_OK
160- || status == HttpStatus .SC_METHOD_NOT_ALLOWED /* method is not allowed, but resource exist */ ) {
148+ if (status == HttpStatus .SC_OK || status == HttpStatus .SC_METHOD_NOT_ALLOWED /* method is not allowed, but resource exist */ ) {
161149 return FileType .FILE ;
162150 }
163151 if (status == HttpStatus .SC_NOT_FOUND || status == HttpStatus .SC_GONE ) {
@@ -177,16 +165,18 @@ protected String[] doListChildren() throws Exception {
177165 }
178166
179167 /**
180- * Execute the request using the given {@code httpRequest} and return a {@code HttpResponse} from the execution.
168+ * Executes the request using the given {@code httpRequest} and return a {@code HttpResponse} from the execution.
181169 *
182- * @param httpRequest {@code HttpUriRequest} object
183- * @return {@code HttpResponse} from the execution
184- * @throws IOException if IO error occurs
170+ * @param httpRequest {@code HttpUriRequest} object.
171+ * @return {@code HttpResponse} from the execution.
172+ * @throws IOException in case of a problem or the connection was aborted.
173+ * @throws ClientProtocolException in case of an HTTP protocol error.
185174 * @since 2.5.0
186175 */
187176 protected HttpResponse executeHttpUriRequest (final HttpUriRequest httpRequest ) throws IOException {
188- final HttpClient httpClient = getAbstractFileSystem ().getHttpClient ();
189- final HttpClientContext httpClientContext = getAbstractFileSystem ().getHttpClientContext ();
177+ final FS abstractFileSystem = getAbstractFileSystem ();
178+ final HttpClient httpClient = abstractFileSystem .getHttpClient ();
179+ final HttpClientContext httpClientContext = abstractFileSystem .getHttpClientContext ();
190180 return httpClient .execute (httpRequest , httpClientContext );
191181 }
192182
@@ -198,7 +188,7 @@ protected FileContentInfoFactory getFileContentInfoFactory() {
198188 /**
199189 * Gets the internal {@code URI} object mapped to this file object.
200190 *
201- * @return the internal {@code URI} object mapped to this file object
191+ * @return the internal {@code URI} object mapped to this file object.
202192 */
203193 protected URI getInternalURI () {
204194 return internalURI ;
@@ -207,23 +197,23 @@ protected URI getInternalURI() {
207197 /**
208198 * Gets the last executed HEAD {@code HttpResponse} object.
209199 *
210- * @return the last executed HEAD {@code HttpResponse} object
211- * @throws IOException if IO error occurs
200+ * @return the last executed HEAD {@code HttpResponse} object.
201+ * @throws IOException in case of a problem or the connection was aborted.
202+ * @throws ClientProtocolException in case of an HTTP protocol error.
212203 */
213204 HttpResponse getLastHeadResponse () throws IOException {
214205 if (lastHeadResponse != null ) {
215206 return lastHeadResponse ;
216207 }
217-
218208 return executeHttpUriRequest (new HttpHead (getInternalURI ()));
219209 }
220210
221211 /**
222212 * Gets URL charset string.
213+ *
223214 * @return URL charset string
224215 */
225216 protected String getUrlCharset () {
226217 return urlCharset ;
227218 }
228-
229219}
0 commit comments