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, boolean useFileChannels) 

Source Link

Document

Creates new instance of NFileEntity from the given source File with the given content type.

Usage

From source file:marytts.server.http.MaryHttpServerUtils.java

public static void fileToHttpResponse(String fullPathFile, HttpResponse response, String contentType,
        boolean useFileChannels) {
    int status;//from ww w.  java 2s  . c o m
    final File file = new File(fullPathFile);
    if (!file.exists())
        status = HttpStatus.SC_NOT_FOUND;
    else if (!file.canRead() || file.isDirectory())
        status = HttpStatus.SC_FORBIDDEN;
    else {
        status = HttpStatus.SC_OK;
        NFileEntity entity = new NFileEntity(file, contentType, useFileChannels);
        response.setEntity(entity);
    }

    response.setStatusCode(status);
}

From source file:org.jclouds.http.httpnio.util.NioHttpUtils.java

public static void addEntityForContent(BasicHttpEntityEnclosingRequest apacheRequest, Object content,
        String contentType, long length) {
    if (content instanceof InputStream) {
        InputStream inputStream = (InputStream) content;
        if (length == -1)
            throw new IllegalArgumentException("you must specify size when content is an InputStream");
        InputStreamEntity entity = new InputStreamEntity(inputStream, length);
        entity.setContentType(contentType);
        apacheRequest.setEntity(entity);
    } else if (content instanceof String) {
        NStringEntity nStringEntity = null;
        try {/*from  ww  w  . ja va 2 s.c  o  m*/
            nStringEntity = new NStringEntity((String) content);
        } catch (UnsupportedEncodingException e) {
            throw new UnsupportedOperationException("Encoding not supported", e);
        }
        nStringEntity.setContentType(contentType);
        apacheRequest.setEntity(nStringEntity);
    } else if (content instanceof File) {
        apacheRequest.setEntity(new NFileEntity((File) content, contentType, true));
    } else if (content instanceof byte[]) {
        NByteArrayEntity entity = new NByteArrayEntity((byte[]) content);
        entity.setContentType(contentType);
        apacheRequest.setEntity(entity);
    } else {
        throw new UnsupportedOperationException("Content class not supported: " + content.getClass().getName());
    }
}