Example usage for org.apache.http.nio.entity NStringEntity setContentType

List of usage examples for org.apache.http.nio.entity NStringEntity setContentType

Introduction

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

Prototype

public void setContentType(Header header) 

Source Link

Usage

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

private static HttpEntity createTextEntity(String text) throws UnsupportedEncodingException {
    NStringEntity entity = new NStringEntity(
            MessageFormat.format("<html><body><h1>{0}</h1></body></html>", text), //$NON-NLS-1$
            IOUtil.UTF_8);//w  w w. ja  v a 2 s .  c  om
    entity.setContentType(HTML_TEXT_TYPE + HTTP.CHARSET_PARAM + IOUtil.UTF_8);
    return entity;
}

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

public static void errorMissingQueryParameter(HttpResponse response, String param) {
    int status = HttpStatus.SC_BAD_REQUEST;
    response.setStatusCode(status);// ww  w. j a v a 2 s.  co  m
    String message = "Request must contain the parameter " + param;
    logger.debug("Returning HTTP status " + status + ": " + message);
    try {
        NStringEntity entity = new NStringEntity(
                "<html><body><h1>Bad request</h1><p>" + message + ".</h1></body></html>", "UTF-8");
        entity.setContentType("text/html; charset=UTF-8");
        response.setEntity(entity);
    } catch (UnsupportedEncodingException e) {
    }
}

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

public static void errorFileNotFound(HttpResponse response, String uri) {
    int status = HttpStatus.SC_NOT_FOUND;
    response.setStatusCode(status);/*from w  ww .ja  va  2 s.c  o m*/
    String message = "File " + uri + " not found";
    logger.debug("Returning HTTP status " + status + ": " + message);
    try {
        NStringEntity entity = new NStringEntity(
                "<html><body><h1>File not found</h1><p>" + message + "</p></body></html>", "UTF-8");
        entity.setContentType("text/html; charset=UTF-8");
        response.setEntity(entity);
    } catch (UnsupportedEncodingException e) {
    }
}

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

public static void errorWrongQueryParameterValue(HttpResponse response, String paramName, String illegalValue,
        String explanation) {//from w  ww  .j  a v a 2s . c  o  m
    int status = HttpStatus.SC_BAD_REQUEST;
    response.setStatusCode(status);
    String message = "The value '" + illegalValue + "' of parameter '" + paramName + "' is not valid"
            + (explanation != null ? ": " + explanation : "");
    logger.debug("Returning HTTP status " + status + ": " + message);
    try {
        NStringEntity entity = new NStringEntity(
                "<html><body><h1>Bad request</h1><p>" + message + ".</h1></body></html>", "UTF-8");
        entity.setContentType("text/html; charset=UTF-8");
        response.setEntity(entity);
    } catch (UnsupportedEncodingException e) {
    }
}

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 w  w  w .  java  2s  .co 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());
    }
}

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

public static void errorInternalServerError(HttpResponse response, String message, Throwable exception) {
    int status = HttpStatus.SC_INTERNAL_SERVER_ERROR;
    response.setStatusCode(status);/*from   www .  j  a v a  2  s.c o  m*/
    StringWriter sw = new StringWriter();
    exception.printStackTrace(new PrintWriter(sw, true));
    String logMessage = (message != null ? message + "\n" : "") + sw.toString();
    logger.debug("Returning HTTP status " + status + ": " + logMessage);
    try {
        NStringEntity entity = new NStringEntity("<html><body><h1>Internal server error</h1><p>"
                + (message != null ? message : "") + "<pre>" + sw.toString() + "</pre></body></html>", "UTF-8");
        entity.setContentType("text/html; charset=UTF-8");
        response.setEntity(entity);
    } catch (UnsupportedEncodingException e) {
    }
}

From source file:org.hydracache.server.httpd.handler.UnsupportedHttpMethodHandler.java

@Override
public void handle(HttpRequest request, HttpResponse response, HttpContext context)
        throws HttpException, IOException {
    response.setStatusCode(HttpStatus.SC_METHOD_NOT_ALLOWED);

    NStringEntity body = new NStringEntity("<html><body><h1>Method not allowed</h1></body></html>", "UTF-8");

    body.setContentType("text/html; UTF-8");

    response.setEntity(body);/*  ww  w  . jav a2  s.  co m*/
}

From source file:marytts.tools.perceptiontest.PerceptionRequestHandler.java

@Override
protected void handleClientRequest(String absPath, Map<String, String> queryItems, HttpResponse response,
        Address serverAddressAtClient) throws IOException {

    Set<String> keySet = queryItems.keySet();

    if (absPath.equals("/perceptionTest") && keySet.size() > 0) {

        String infoResponse = null;

        if (keySet.size() == 1 && keySet.contains("EMAIL")) {
            infoResponse = getCaseOneInfoResponse(queryItems);
        } else if (keySet.size() > 1 && keySet.contains("EMAIL") && keySet.contains("RESULTS")
                && keySet.contains("PRESENT_SAMPLE_BASENAME") && keySet.contains("PRESENT_SAMPLE_NUMBER")) {
            infoResponse = storeUserRatings(queryItems);
        } else if (keySet.size() > 1 && keySet.contains("EMAIL") && keySet.contains("PRESENT_SAMPLE_NUMBER")) {
            infoResponse = getCaseTwoInfoResponse(queryItems);
        }//from   w w w .j ava2s  . c o  m

        if (infoResponse == null) { // error condition, handleInfoRequest has set an error message
            return;
        }

        response.setStatusCode(HttpStatus.SC_OK);
        try {
            NStringEntity entity = new NStringEntity(infoResponse, "UTF-8");
            entity.setContentType("text/plain; charset=UTF-8");
            response.setEntity(entity);
        } catch (UnsupportedEncodingException e) {
        }
    }

}

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

@Override
protected void handleClientRequest(String absPath, Map<String, String> queryItems, HttpResponse response,
        Address serverAddressAtClient) throws IOException {
    // Individual info request
    String infoResponse = handleInfoRequest(absPath, queryItems, response);
    if (infoResponse == null) { // error condition, handleInfoRequest has set an error message
        return;// w w  w  . java2  s.c om
    }

    response.setStatusCode(HttpStatus.SC_OK);
    try {
        NStringEntity entity = new NStringEntity(infoResponse, "UTF-8");
        entity.setContentType("text/plain; charset=UTF-8");
        response.setEntity(entity);
    } catch (UnsupportedEncodingException e) {
    }
}