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.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
74 Request request = vol.httpConnection.newRequest( "HEAD", uriAsFolder );
75
76 Response response = vol.dispatch( request );
77 switch ( response.getCode() ) {
78 case 200:
79
80
81 return new HttpFolder( this, segment );
82 case 404:
83
84 request = vol.httpConnection.newRequest( "HEAD", uriAsFile );
85
86 response = vol.dispatch( request );
87
88 if ( response.getCode() == 200 ) {
89
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
105 Request request = vol.httpConnection.newRequest( "HEAD", uriAsFile );
106
107 Response response = vol.dispatch( request );
108 switch ( response.getCode() ) {
109 case 200:
110
111 return new HttpFile( this, segment );
112 case 404:
113
114 request = vol.httpConnection.newRequest( "HEAD", uriAsFolder );
115
116 response = vol.dispatch( request );
117
118 if ( response.getCode() == 200 ) {
119
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
135 Request request = vol.httpConnection.newRequest( "HEAD", uriAsFolder );
136
137 Response response = vol.dispatch( request );
138 switch ( response.getCode() ) {
139 case 200:
140
141
142 return new HttpFolder( this, segment );
143 case 404:
144
145 request = vol.httpConnection.newRequest( "HEAD", uriAsFile );
146
147 response = vol.dispatch( request );
148
149 if ( response.getCode() == 200 ) {
150
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
163 Request request = vol.httpConnection.newRequest(
164 "HEAD",
165 this.getAbsolutePath()
166 );
167
168 Response response = vol.dispatch( request );
169 if ( response.getCode() == 200 ) {
170
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
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
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
215 return Collections.EMPTY_LIST;
216 }
217
218 /**
219 * @see org.millscript.commons.vfs.VFolder#make()
220 */
221 public VFolder make() {
222
223 return null;
224 }
225
226 }