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.vfs.AbstractVFolder;
24  import org.millscript.commons.vfs.VEntry;
25  import org.millscript.commons.vfs.VFile;
26  import org.millscript.commons.vfs.VFolder;
27  import org.millscript.commons.vfs.alerts.VEntryNotFoundAlert;
28  
29  import gnu.inet.http.Request;
30  import gnu.inet.http.Response;
31  
32  import java.util.Collections;
33  import java.util.List;
34  
35  /**
36   * This class implements a virtual filesystem folder for an HTTP filesystem
37   * folder.
38   */
39  public class HttpFolder extends AbstractVFolder< HttpVolume > {
40  
41      /**
42       * Constructs a new HTTP virtual filesystem folder with the specified name,
43       * parent folder and volume.
44       *
45       * @param vol   the volume this entry is held on
46       * @param parent    the parent folder for this folder
47       * @param name  the name of the folder
48       */
49      HttpFolder( final HttpVolume vol, final VFolder parent, final String name ) {
50          super( vol, parent, name );
51      }
52  
53      /**
54       * Constructs a new HTTP virtual filesystem folder with the specified name,
55       * parent folder. This folder will belong to the same volume as the
56       * specified parent folder.
57       *
58       * @param parent    the parent folder for this folder
59       * @param name  the name of the folder
60       */
61      HttpFolder( final HttpFolder parent, final String name ) {
62          super( parent.getVolume(), parent, name );
63      }
64  
65      /**
66       * @see org.millscript.commons.vfs.VFolder#checkVEntry(java.lang.String)
67       */
68      public VEntry checkVEntry( final String segment ) {
69          final StringBuffer uri = this.appendAbsolutePath( new StringBuffer() ).append( segment );
70          final String uriAsFile = uri.toString();
71          final String uriAsFolder = uri.append( '/' ).toString();
72          final HttpVolume vol = this.getVolume();
73          // Try to get the segment as a folder
74          Request request = vol.httpConnection.newRequest( "HEAD", uriAsFolder );
75          // Execute the method and capture the resulting status code
76          Response response = vol.dispatch( request );
77          switch ( response.getCode() ) {
78              case 200:
79                  // There is an entry with the segment name and a / at the end so it
80                  // must be a folder
81                  return new HttpFolder( this, segment );
82              case 404:
83                  // Not found, so lets just check if its available as a file
84                  request = vol.httpConnection.newRequest( "HEAD", uriAsFile );
85                  // Execute the method and capture the resulting status code
86                  response = vol.dispatch( request );
87                  // Now check the status code
88                  if ( response.getCode() == 200 ) {
89                      // The segment represents a file, so...
90                      return new HttpFile( this, segment );
91                  }
92          }
93          throw VEntryNotFoundAlert.entryDoesNotExist( segment ).mishap();
94      }
95  
96      /**
97       * @see org.millscript.commons.vfs.VFolder#checkVFile(java.lang.String)
98       */
99      public HttpFile checkVFile( final String segment ) {
100         final StringBuffer uri = this.appendAbsolutePath( new StringBuffer() ).append( segment );
101         final String uriAsFile = uri.toString();
102         final String uriAsFolder = uri.append( '/' ).toString();
103         final HttpVolume vol = this.getVolume();
104         // Try to get the segment as a file
105         Request request = vol.httpConnection.newRequest( "HEAD", uriAsFile );
106         // Execute the method and capture the resulting status code
107         Response response = vol.dispatch( request );
108         switch ( response.getCode() ) {
109             case 200:
110                 // There is an entry with the segment name so it must be a file
111                 return new HttpFile( this, segment );
112             case 404:
113                 // Not found, so lets just check if its available as a folder
114                 request = vol.httpConnection.newRequest( "HEAD", uriAsFolder );
115                 // Execute the method and capture the resulting status code
116                 response = vol.dispatch( request );
117                 // Now check the status code
118                 if ( response.getCode() == 200 ) {
119                     // The segment represents a folder, so...
120                     throw VEntryNotFoundAlert.entryIsNotAFile( segment ).mishap();
121                 }
122         }
123         throw VEntryNotFoundAlert.entryDoesNotExist( segment ).mishap();
124     }
125 
126     /**
127      * @see org.millscript.commons.vfs.VFolder#checkVFolder(java.lang.String)
128      */
129     public HttpFolder checkVFolder( final String segment ) {
130         final StringBuffer uri = this.appendAbsolutePath( new StringBuffer() ).append( segment );
131         final String uriAsFile = uri.toString();
132         final String uriAsFolder = uri.append( '/' ).toString();
133         final HttpVolume vol = this.getVolume();
134         // Try to get the segment as a folder
135         Request request = vol.httpConnection.newRequest( "HEAD", uriAsFolder );
136         // Execute the method and capture the resulting status code
137         Response response = vol.dispatch( request );
138         switch ( response.getCode() ) {
139             case 200:
140                 // There is an entry with the segment name and a / at the end
141                 // so it must be a folder
142                 return new HttpFolder( this, segment );
143             case 404:
144                 // Not found, so lets just check if its available as a file
145                 request = vol.httpConnection.newRequest( "HEAD", uriAsFile );
146                 // Execute the method and capture the resulting status code
147                 response = vol.dispatch( request );
148                 // Now check the status code
149                 if ( response.getCode() == 200 ) {
150                     // The segment represents a file, so...
151                     throw VEntryNotFoundAlert.entryIsNotAFolder( segment ).mishap();
152                 }
153         }
154         throw VEntryNotFoundAlert.entryDoesNotExist( segment ).mishap();
155     }
156 
157     /**
158      * @see org.millscript.commons.vfs.VEntry#exists()
159      */
160     public boolean exists() {
161         final HttpVolume vol = this.getVolume();
162         // Try to get the segment as a folder
163         Request request = vol.httpConnection.newRequest(
164             "HEAD",
165             this.getAbsolutePath()
166         );
167         // Execute the method and capture the resulting status code
168         Response response = vol.dispatch( request );
169         if ( response.getCode() == 200 ) {
170             // Success
171             return true;
172         } else {
173             return false;
174         }
175     }
176 
177     /**
178      * @see org.millscript.commons.vfs.VFolder#getVFile(java.lang.String)
179      */
180     public HttpFile getVFile( final String segment ) {
181         return new HttpFile( this, segment );
182     }
183 
184     /**
185      * @see org.millscript.commons.vfs.VFolder#getVFolder(java.lang.String)
186      */
187     public HttpFolder getVFolder( final String segment ) {
188         return new HttpFolder( this, segment );
189     }
190 
191     /**
192      * @see org.millscript.commons.vfs.VFolder#listEntries()
193      */
194     @SuppressWarnings( "unchecked" )
195     public List< VEntry > listEntries() {
196         // We can't list anything via HTTP
197         return Collections.EMPTY_LIST;
198     }
199 
200     /**
201      * @see org.millscript.commons.vfs.VFolder#listFiles()
202      */
203     @SuppressWarnings( "unchecked" )
204     public List< VFile > listFiles() {
205         // We can't list anything via HTTP
206         return Collections.EMPTY_LIST;
207     }
208 
209     /**
210      * @see org.millscript.commons.vfs.VFolder#listFolders()
211      */
212     @SuppressWarnings( "unchecked" )
213     public List< VFolder > listFolders() {
214         // We can't list anything via HTTP
215         return Collections.EMPTY_LIST;
216     }
217 
218     /**
219      * @see org.millscript.commons.vfs.VFolder#make()
220      */
221     public VFolder make() {
222         // TODO Auto-generated method stub
223         return null;
224     }
225 
226 }