88
99import java .io .File ;
1010import java .io .IOException ;
11+ import java .nio .file .Files ;
12+ import java .nio .file .Path ;
13+ import java .nio .file .Paths ;
1114import java .util .Date ;
1215
1316/**
@@ -32,21 +35,29 @@ final class FileProvider implements HttpRequest {
3235 @ Override
3336 public void handle (Request req , Response res ) {
3437 String path = req .getURI ().getPath ();
35- File reqFile = new File (ROOT + "\\ " + path );
38+
39+ if (path .length () <= 1 )
40+ path = "index.html" ;
41+
42+ Path reqFile = Paths .get (ROOT + "\\ " + path );
3643
3744 /*
3845 * If the file wasn't found, it will search in the target-directory for
3946 * the file by the raw-name without extension.
4047 */
41- if (OPTIONS .isFallBackSearching () && !reqFile .exists () || !reqFile .isFile ()) {
42- File dir = new File (ROOT + "\\ " + getURIPath (path ));
43- String name = getURIFilename (path );
44- reqFile = getFirst (dir , name );
48+ if (OPTIONS .isFallBackSearching () && !Files .exists (reqFile ) && !Files .isDirectory (reqFile )) {
49+ String name = reqFile .getFileName ().toString ();
50+
51+ try {
52+ reqFile = Files .walk (reqFile .getParent ()).filter (sub -> getBaseName (sub ).equals (name )).findFirst ().get ();
53+ } catch (IOException e ) {
54+ // TODO: Handle error
55+ }
4556 }
4657
47- if (reqFile != null && reqFile . exists ( )) {
58+ if (Files . exists ( reqFile ) && Files . isRegularFile ( reqFile )) {
4859
49- if (reqFile .getName ().charAt (0 ) == '.' ) {
60+ if (reqFile .getFileName (). toString ().charAt (0 ) == '.' ) {
5061 switch (OPTIONS .getDotFiles ()) {
5162 case IGNORE :
5263 res .setStatus (Status ._404 );
@@ -77,84 +88,25 @@ public void handle(Request req, Response res) {
7788 }
7889 }
7990
80- private void finish (File file , Request req , Response res ) {
91+ private void finish (Path file , Request req , Response res ) {
8192 if (OPTIONS .getHandler () != null )
8293 OPTIONS .getHandler ().handle (req , res );
8394
84-
85- // Apply header
86- if (OPTIONS .isLastModified ())
87- res .setHeader ("Last-Modified" , Utils .getGMTDate (new Date (file .lastModified ())));
88-
89- res .setHeader ("Cache-Control" , String .valueOf (OPTIONS .getMaxAge ()));
90- res .send (file );
91- }
92-
93- /**
94- * Returns the first file which has this specific name.
95- * If no file where found, null will be returned.
96- *
97- * @param dir The root directory.
98- * @param name The target name.
99- * @return The first file which will be found, null otherwise.
100- */
101- private File getFirst (File dir , String name ) {
102- File [] fs = dir .listFiles ();
103-
104- if (fs == null )
105- return null ;
106-
107- for (File f : fs ) {
108- if (f .isFile () && getBaseName (f .getName ()).equals (name )) {
109- return f ;
110- }
95+ try {
96+ // Apply header
97+ if (OPTIONS .isLastModified ())
98+ res .setHeader ("Last-Modified" , Utils .getGMTDate (new Date (Files .getLastModifiedTime (file ).toMillis ())));
99+ } catch (IOException e ) {
100+ // TODO: Handle error
111101 }
112102
113- return null ;
114- }
115-
116- private String getBaseName (String path ) {
117- int index = path .lastIndexOf ('.' );
118- return index == -1 ? path : path .substring (0 , index );
119- }
120-
121- /**
122- * Extract the filename from an URI-Path.
123- *
124- * @param path The path.
125- * @return The file name.
126- */
127- private String getURIFilename (String path ) {
128- int s = path .lastIndexOf ('/' );
129- int e = path .lastIndexOf ('.' );
130-
131- if (e == -1 && s == -1 )
132- return "index" ;
133-
134- if (e == -1 )
135- e = path .length ();
136-
137- if (s == -1 )
138- s = 0 ;
139-
140- String name = path .substring (s + 1 , e );
141-
142- return name .isEmpty () ? "index" : name ;
103+ res .setHeader ("Cache-Control" , String .valueOf (OPTIONS .getMaxAge ()));
104+ res .send (file ); // TODO: FIX
143105 }
144106
145- /**
146- * Extract the directory path from an URI-Path.
147- *
148- * @param path The path
149- * @return The path (without file name)
150- */
151- private String getURIPath (String path ) {
152- int s = path .indexOf ('/' );
153- int e = path .lastIndexOf ('/' );
154-
155- if (e == s || s == -1 || e == -1 )
156- return "" ;
157-
158- return path .substring (s , e );
107+ private String getBaseName (Path path ) {
108+ String name = path .getFileName ().toString ();
109+ int index = name .lastIndexOf ('.' );
110+ return index == -1 ? name : name .substring (0 , index );
159111 }
160112}
0 commit comments