Example usage for org.apache.http.nio.entity NFileEntity NFileEntity

List of usage examples for org.apache.http.nio.entity NFileEntity NFileEntity

Introduction

In this page you can find the example usage for org.apache.http.nio.entity NFileEntity NFileEntity.

Prototype

public NFileEntity(final File file, final String contentType) 

Source Link

Usage

From source file:com.mycompany.allasync.NHttpRequestHandler.java

private void handleInternal(final HttpRequest request, final HttpResponse response, final HttpContext context)
        throws HttpException, IOException {

    String method = request.getRequestLine().getMethod().toUpperCase(Locale.ENGLISH);
    if (!method.equals("GET") && !method.equals("HEAD") && !method.equals("POST")) {
        throw new MethodNotSupportedException(method + " method not supported");
    }/*from  w  w  w .  jav  a  2s  .  c  o  m*/

    String target = request.getRequestLine().getUri();
    final File file = new File(this.docRoot, URLDecoder.decode(target, "UTF-8"));
    if (!file.exists()) {

        response.setStatusCode(HttpStatus.SC_NOT_FOUND);
        NStringEntity entity = new NStringEntity(
                "<html><body><h1>File" + file.getPath() + " not found</h1></body></html>",
                ContentType.create("text/html", "UTF-8"));
        response.setEntity(entity);
        System.out.println("File " + file.getPath() + " not found");

    } else if (!file.canRead() || file.isDirectory()) {

        response.setStatusCode(HttpStatus.SC_FORBIDDEN);
        NStringEntity entity = new NStringEntity("<html><body><h1>Access denied</h1></body></html>",
                ContentType.create("text/html", "UTF-8"));
        response.setEntity(entity);
        System.out.println("Cannot read file " + file.getPath());

    } else {
        NHttpConnection conn = (NHttpConnection) context.getAttribute(ExecutionContext.HTTP_CONNECTION);
        response.setStatusCode(HttpStatus.SC_OK);
        NFileEntity body = new NFileEntity(file, ContentType.create("text/html"));
        response.setEntity(body);
        System.out.println(conn + ": serving file " + file.getPath());
    }
}

From source file:com.aptana.webserver.internal.core.builtin.LocalWebServerHttpRequestHandler.java

private void handleRequest(HttpRequest request, HttpResponse response, boolean head)
        throws HttpException, IOException, CoreException, URISyntaxException {
    String target = URLDecoder.decode(request.getRequestLine().getUri(), IOUtil.UTF_8);
    URI uri = URIUtil.fromString(target);
    IFileStore fileStore = uriMapper.resolve(uri);
    IFileInfo fileInfo = fileStore.fetchInfo();
    if (fileInfo.isDirectory()) {
        fileInfo = getIndex(fileStore);//from   www .  j a  v a  2 s  . c om
        if (fileInfo.exists()) {
            fileStore = fileStore.getChild(fileInfo.getName());
        }
    }
    if (!fileInfo.exists()) {
        response.setStatusCode(HttpStatus.SC_NOT_FOUND);
        response.setEntity(createTextEntity(
                MessageFormat.format(Messages.LocalWebServerHttpRequestHandler_FILE_NOT_FOUND, uri.getPath())));
    } else if (fileInfo.isDirectory()) {
        response.setStatusCode(HttpStatus.SC_FORBIDDEN);
        response.setEntity(createTextEntity(Messages.LocalWebServerHttpRequestHandler_FORBIDDEN));
    } else {
        response.setStatusCode(HttpStatus.SC_OK);
        if (head) {
            response.setEntity(null);
        } else {
            File file = fileStore.toLocalFile(EFS.NONE, new NullProgressMonitor());
            final File temporaryFile = (file == null)
                    ? fileStore.toLocalFile(EFS.CACHE, new NullProgressMonitor())
                    : null;
            response.setEntity(
                    new NFileEntity((file != null) ? file : temporaryFile, getMimeType(fileStore.getName())) {
                        @Override
                        public void close() throws IOException {
                            try {
                                super.close();
                            } finally {
                                if (temporaryFile != null && !temporaryFile.delete()) {
                                    temporaryFile.deleteOnExit();
                                }
                            }
                        }
                    });
        }
    }
}