Java 类org.apache.http.nio.entity.NFileEntity 实例源码

项目:iot-server-appliances    文件:HttpServer.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");
    }

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

        response.setStatusCode(HttpStatus.SC_NOT_FOUND);
        log.severe("File " + file.getPath() + " not found");
        return;

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

        response.setStatusCode(HttpStatus.SC_FORBIDDEN);
        log.severe("Cannot read file " + file.getPath());
        return;

    } else if (file.isDirectory()) {

        if (target.endsWith("/")) {

            boolean isExist = false;
            for (String index : DEFAULT_INDEX_FILES_LIST) {
                final File t_file = new File(this.docRoot + File.separator + target, index);
                if (t_file.exists()) {
                    file = t_file;
                    isExist = true;
                    break;
                }
            }
            if(!isExist)return;

        } else {
            //redirect browser - doing basically what apache does
            response.setStatusCode(HttpStatus.SC_MOVED_PERMANENTLY);
            String newUrl = target + "/";
            response.setHeader("Location", newUrl);
            return;
        }

    }

    HttpCoreContext coreContext = HttpCoreContext.adapt(context);
    HttpConnection conn = coreContext.getConnection(HttpConnection.class);
    response.setStatusCode(HttpStatus.SC_OK);

    String displayAgentHome = ResourceUtil.getDisplayAgentHome();
    System.setProperty("content.types.user.table",displayAgentHome + File.separator + LauncherConstants.CONF_PATH + File.separator + "content-types.properties");
    String mimeType= URLConnection.guessContentTypeFromName(file.getName());

    NFileEntity body = new NFileEntity(file, ContentType.create(mimeType));
    response.setEntity(body);
    //log.info(conn + ": serving file " + file.getPath());
}
项目:iot-server-appliances    文件:HttpServer.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");
    }

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

        response.setStatusCode(HttpStatus.SC_NOT_FOUND);
        log.severe("File " + file.getPath() + " not found");
        return;

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

        response.setStatusCode(HttpStatus.SC_FORBIDDEN);
        log.severe("Cannot read file " + file.getPath());
        return;

    } else if (file.isDirectory()) {

        if (target.endsWith("/")) {

            boolean isExist = false;
            for (String index : DEFAULT_INDEX_FILES_LIST) {
                final File t_file = new File(this.docRoot + File.separator + target, index);
                if (t_file.exists()) {
                    file = t_file;
                    isExist = true;
                    break;
                }
            }
            if(!isExist)return;

        } else {
            //redirect browser - doing basically what apache does
            response.setStatusCode(HttpStatus.SC_MOVED_PERMANENTLY);
            String newUrl = target + "/";
            response.setHeader("Location", newUrl);
            return;
        }

    }

    HttpCoreContext coreContext = HttpCoreContext.adapt(context);
    HttpConnection conn = coreContext.getConnection(HttpConnection.class);
    response.setStatusCode(HttpStatus.SC_OK);

    String displayAgentHome = ResourceUtil.getDisplayAgentHome();
    System.setProperty("content.types.user.table",displayAgentHome + File.separator + LauncherConstants.CONF_PATH + File.separator + "content-types.properties");
    String mimeType= URLConnection.guessContentTypeFromName(file.getName());

    NFileEntity body = new NFileEntity(file, ContentType.create(mimeType));
    response.setEntity(body);
    //log.info(conn + ": serving file " + file.getPath());
}
项目:APICloud-Studio    文件: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);
        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();
                        }
                    }
                }
            });
        }
    }
}