Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package io.undertow.server.handlers.resource;
  
  import java.io.File;
  import java.net.URL;
 import java.util.Date;
 import java.util.List;
 

Author(s):
Stuart Douglas
 
 public class URLResource implements Resource {
 
     private final URL url;
     private final URLConnection connection;
     private final String path;
 
     public URLResource(final URL urlfinal URLConnection connectionString path) {
         this. = url;
         this. = connection;
         this. = path;
     }
 
     @Override
     public String getPath() {
         return ;
     }
 
     @Override
     public Date getLastModified() {
         return new Date(.getLastModified());
     }
 
     @Override
     public String getLastModifiedString() {
         return DateUtils.toDateString(getLastModified());
     }
 
     @Override
     public ETag getETag() {
         return null;
     }
 
     @Override
     public String getName() {
         String path = .getPath();
         if (path.endsWith("/")) {
             path = path.substring(0, path.length() - 1);
         }
         int sepIndex = path.lastIndexOf("/");
         if (sepIndex != -1) {
             path = path.substring(sepIndex + 1);
         }
         return path;
     }
 
     @Override
     public boolean isDirectory() {
         File file = getFile();
         if(file != null) {
             return file.isDirectory();
         } else if(.getPath().endsWith("/")) {
             return true;
         }
         return false;
     }
 
     @Override
     public List<Resourcelist() {
         List<Resourceresult = new LinkedList<Resource>();
         File file = getFile();
         try {
             if (file != null) {
                 for (File f : file.listFiles()) {
                     result.add(new URLResource(f.toURI().toURL(), f.getPath()));
                 }
             }
         } catch (MalformedURLException e) {
             throw new RuntimeException(e);
         }
         return result;
     }
 
     @Override
    public String getContentType(final MimeMappings mimeMappings) {
        final String fileName = getName();
        int index = fileName.lastIndexOf('.');
        if (index != -1 && index != fileName.length() - 1) {
            return mimeMappings.getMimeType(fileName.substring(index + 1));
        }
        return null;
    }
    @Override
    public void serve(final Sender senderfinal HttpServerExchange exchangefinal IoCallback completionCallback) {
        class ServerTask implements RunnableIoCallback {
            private InputStream inputStream;
            private byte[] buffer;
            @Override
            public void run() {
                if ( == null) {
                    try {
                         = .openStream();
                    } catch (IOException e) {
                        exchange.setResponseCode(500);
                        return;
                    }
                     = new byte[1024];//TODO: we should be pooling these
                }
                try {
                    int res = .read();
                    if (res == -1) {
                        //we are done, just return
                        IoUtils.safeClose();
                        completionCallback.onComplete(exchangesender);
                        return;
                    }
                    sender.send(ByteBuffer.wrap(, 0, res), this);
                } catch (IOException e) {
                    onException(exchangesendere);
                }
            }
            @Override
            public void onComplete(final HttpServerExchange exchangefinal Sender sender) {
                if (exchange.isInIoThread()) {
                    exchange.dispatch(this);
                } else {
                    run();
                }
            }
            @Override
            public void onException(final HttpServerExchange exchangefinal Sender senderfinal IOException exception) {
                ..ioException(exception);
                IoUtils.safeClose();
                if (!exchange.isResponseStarted()) {
                    exchange.setResponseCode(500);
                }
                completionCallback.onException(exchangesenderexception);
            }
        }
        ServerTask serveTask = new ServerTask();
        if (exchange.isInIoThread()) {
            exchange.dispatch(serveTask);
        } else {
            serveTask.run();
        }
    }
    @Override
    public Long getContentLength() {
        return (long.getContentLength();
    }
    @Override
    public String getCacheKey() {
        return .toString();
    }
    @Override
    public File getFile() {
        if(.getProtocol().equals("file")) {
            try {
                return new File(.toURI());
            } catch (URISyntaxException e) {
                return null;
            }
        }
        return null;
    }
    @Override
    public File getResourceManagerRoot() {
        return null;
    }
    @Override
    public URL getUrl() {
        return ;
    }
New to GrepCode? Check out our FAQ X