1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.millscript.commons.vfs.protocols.http;
22
23 import org.millscript.commons.alert.alerts.IOAlert;
24 import org.millscript.commons.vfs.VFS;
25 import org.millscript.commons.vfs.VFolder;
26 import org.millscript.commons.vfs.protocols.AbstractProtocolVVolume;
27
28 import gnu.inet.http.HTTPConnection;
29 import gnu.inet.http.Request;
30 import gnu.inet.http.Response;
31
32 import java.io.IOException;
33 import java.net.URI;
34
35 /**
36 * The class implements a virtual filesystem volume for an HTTP connection.
37 */
38 public class HttpVolume extends AbstractProtocolVVolume {
39
40 /**
41 * The underlying HTTP connection we use for HTTP requests.
42 */
43 final HTTPConnection httpConnection;
44
45 /**
46 * Constructs a new HTTP volume
47 *
48 * @param v the VFS that was used to create this volume
49 * @param root
50 */
51 public HttpVolume( final VFS v, final URI root ) {
52 super( v, root );
53 if ( root.getPort() < 0 ) {
54 this.httpConnection = new HTTPConnection( root.getHost() );
55 } else {
56 this.httpConnection = new HTTPConnection( root.getHost(), root.getPort() );
57 }
58 }
59
60 /**
61 * Dispatches the specified method using this methods HTTP connection,
62 * returning the HTTP response.
63 *
64 * @param request the HTTP request to dispatch
65 * @return the Response holding the HTTP status code from the HTTP server
66 */
67 Response dispatch( final Request request ) {
68
69 try {
70 return request.dispatch();
71 } catch ( IOException ex ) {
72
73 throw new IOAlert(
74 "Cannot dispatch the HTTP request"
75 ).culprit( "message", ex.getMessage() ).setParentThrowable( ex ).mishap();
76 }
77 }
78
79 /**
80 * @see org.millscript.commons.vfs.protocols.AbstractProtocolVVolume#makeRootVFolder()
81 */
82 @Override
83 protected VFolder makeRootVFolder() {
84 return new HttpFolder( this, null, null );
85 }
86
87 }