55import ktt .lib .httpserver .SimpleHttpHandler ;
66
77import java .io .*;
8+ import java .nio .file .Files ;
89import java .util .*;
910
1011/**
1415 * The <code>directoryName</code> parameter determines the directory's name. Add the files at the top level by keeping this field empty. <br>
1516 * The <code>preload</code> parameter determines if the handler should read the bytes when they are added or read the file at the exchange. <br>
1617 * The <code>walk</code> parameter determines if all the inner directories should be used.
17- * The handler will not add any null files.
18+ * The handler will not add any null files and will always use the latest file added for a particular context .
1819 *
1920 * @see FileHandlerAdapter
2021 * @see SimpleHttpHandler
@@ -235,7 +236,10 @@ public final void addFile(final String context, final File file, final String fi
235236 */
236237 public final void addFile (final String context , final File file , final String fileName , final boolean preload ){
237238 try {
238- files .put (getContext (context ) + getContext (fileName ),new FileEntry (file ,preload ,adapter ));
239+ files .put (
240+ (context .isEmpty () || context .equals ("/" ) || context .equals ("\\ " ) ? "" : getContext (context )) + getContext (fileName ),
241+ new FileEntry (file ,preload ,adapter )
242+ );
239243 }catch (final FileNotFoundException ignored ){ }
240244 }
241245
@@ -625,8 +629,12 @@ public final void addDirectory(final String context, final File directory, final
625629 */
626630 public final void addDirectory (final String context , final File directory , final String directoryName , final boolean preload , final boolean walk ){
627631 try {
628- directories .put (getContext (context ) + (directoryName .isEmpty () ? "" : getContext (directoryName )), new DirectoryEntry (directory , preload , adapter , walk ));
629- }catch (final IOException ignored ){ }
632+ final String target = (context .isEmpty () || context .equals ("/" ) || context .equals ("\\ " ) ? "" : getContext (context )) + (directoryName .isEmpty () ? "" : getContext (directoryName ));
633+ directories .put (
634+ target .isEmpty () ? "/" : target ,
635+ new DirectoryEntry (directory , preload , adapter , walk )
636+ );
637+ }catch (final Exception ignored ){ }
630638 }
631639
632640//
@@ -640,10 +648,10 @@ public final void handle(final SimpleHttpExchange exchange) throws IOException{
640648 if (rel .startsWith (key ) && key .startsWith (match ))
641649 match = key ;
642650
643- if (!match .isEmpty () && files .containsKey (match )){
644- final FileEntry entry = files .get (match );
645- handle (exchange ,entry .getFile (),entry .getBytes ());
646- }else {
651+ if (!match .isEmpty () && files .containsKey (match )){ // exact match
652+ final FileEntry entry = files .get (match ); // preloaded ? use preloaded bytes : adapt bytes now
653+ handle (exchange ,entry .getFile (),entry .isPreloaded () ? entry . getBytes () : adapter . getBytes ( entry . getFile (), entry . getBytes () ));
654+ }else { // beginning match
647655 match = "" ;
648656 for (final String key : directories .keySet ())
649657 if (rel .startsWith (key ) && key .startsWith (match ))
@@ -653,15 +661,20 @@ public final void handle(final SimpleHttpExchange exchange) throws IOException{
653661 final DirectoryEntry entry = directories .get (match );
654662 final String rel2 ;
655663 try {
656- rel2 = rel .substring (match .length ()+1 );
657-
658- final File file ;
659- if ((file = entry .getFile (rel2 )) != null ){
660- handle (exchange , file , entry .getBytes (rel2 )); return ;
664+ rel2 = rel .substring (match .length ());
665+
666+ if (entry .isFilesPreloaded ()){
667+ final File file ;
668+ if ((file = entry .getFile (rel2 )) != null ){
669+ handle (exchange , file , entry .getBytes (rel2 )); // use adapted preload
670+ }
671+ }else {
672+ final File file = new File (entry .getDirectory ().getAbsolutePath () + "\\ " + rel2 );
673+ handle (exchange ,file ,adapter .getBytes (file , Files .readAllBytes (file .toPath ()))); // use adapted now
661674 }
662675 }catch (final IndexOutOfBoundsException ignored ){ }
663676 }
664- handle (exchange ,null ,null );
677+ handle (exchange ,null ,null ); // not found
665678 }
666679 }
667680
0 commit comments