View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2005 Kevin Rogers
4   //
5   // This file is part of MillScript.
6   //
7   // MillScript is free software; you can redistribute it and/or modify it under
8   // the terms of the GNU General Public License as published by the Free
9   // Software Foundation; either version 2 of the License, or (at your option)
10  // any later version.
11  //
12  // MillScript is distributed in the hope that it will be useful, but WITHOUT
13  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  // more details.
16  //
17  // You should have received a copy of the GNU General Public License along with
18  // MillScript; if not, write to the Free Software Foundation, Inc., 59 Temple
19  // Place, Suite 330, Boston, MA  02111-1307  USA
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          // The method status code
69          try {
70              return request.dispatch();
71          } catch ( IOException ex ) {
72              // Alert - an i/o problem
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  }