Example usage for org.apache.http.entity BasicHttpEntity setContentType

List of usage examples for org.apache.http.entity BasicHttpEntity setContentType

Introduction

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

Prototype

public void setContentType(Header header) 

Source Link

Usage

From source file:org.wso2.carbon.apimgt.handlers.AuthenticationHandlerTest.java

private CloseableHttpResponse getInvalidResponse() throws UnsupportedEncodingException {
    CloseableHttpResponse mockDCRResponse = new MockHttpResponse();
    BasicHttpEntity responseEntity = new BasicHttpEntity();
    responseEntity//  w  w w  . ja v a2 s.co m
            .setContent(new ByteArrayInputStream("invalid response".getBytes(StandardCharsets.UTF_8.name())));
    responseEntity.setContentType(TestUtils.CONTENT_TYPE);
    mockDCRResponse.setEntity(responseEntity);
    mockDCRResponse.setStatusLine(new BasicStatusLine(new ProtocolVersion("http", 1, 0), 400, "Bad Request"));
    return mockDCRResponse;
}

From source file:ste.web.http.velocity.VelocityHandler.java

@Override
public void handle(HttpRequest request, HttpResponse response, HttpContext context)
        throws HttpException, IOException {
    String view = (String) context.getAttribute(ATTR_VIEW);
    if (view == null) {
        return;/*from   ww w  .ja va  2s  .c om*/
    }

    view = getViewPath(request.getRequestLine().getUri(), view);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Writer out = new OutputStreamWriter(baos);
    try {
        Template t = engine.getTemplate(view);
        t.merge(buildContext(request, (HttpSessionContext) context), out);
        out.flush();
    } catch (ResourceNotFoundException e) {
        response.setStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_NOT_FOUND, "View " + view + " not found.");
        return;
    } catch (ParseErrorException e) {
        throw new HttpException("Parse error evaluating " + view + ": " + e, e);
    } catch (MethodInvocationException e) {
        throw new HttpException("Method invocation error evaluating " + view + ": " + e, e);
    }

    BasicHttpEntity body = (BasicHttpEntity) response.getEntity();
    body.setContentLength(baos.size());
    body.setContent(new ByteArrayInputStream(baos.toByteArray()));
    if ((body.getContentType() == null) || StringUtils.isBlank(body.getContentType().getValue())) {
        body.setContentType(ContentType.TEXT_HTML.getMimeType());
    }
}

From source file:org.wso2.carbon.apimgt.handlers.AuthenticationHandlerTest.java

private CloseableHttpResponse getDCRResponse() throws IOException {
    CloseableHttpResponse mockDCRResponse = new MockHttpResponse();
    String dcrResponseFile = TestUtils.getAbsolutePathOfConfig("dcr-response.json");
    BasicHttpEntity responseEntity = new BasicHttpEntity();
    responseEntity.setContent(/*from   www  . j a  v  a 2s .  c  om*/
            new ByteArrayInputStream(getContent(dcrResponseFile).getBytes(StandardCharsets.UTF_8.name())));
    responseEntity.setContentType(TestUtils.CONTENT_TYPE);
    mockDCRResponse.setEntity(responseEntity);
    mockDCRResponse.setStatusLine(new BasicStatusLine(new ProtocolVersion("http", 1, 0), 200, "OK"));
    return mockDCRResponse;
}

From source file:org.wso2.carbon.apimgt.handlers.AuthenticationHandlerTest.java

private CloseableHttpResponse getAccessTokenReponse() throws IOException {
    CloseableHttpResponse mockDCRResponse = new MockHttpResponse();
    String dcrResponseFile = TestUtils.getAbsolutePathOfConfig("accesstoken-response.json");
    BasicHttpEntity responseEntity = new BasicHttpEntity();
    responseEntity.setContent(/*from  w  w w  .  j a v a2s .c  o m*/
            new ByteArrayInputStream(getContent(dcrResponseFile).getBytes(StandardCharsets.UTF_8.name())));
    responseEntity.setContentType(TestUtils.CONTENT_TYPE);
    mockDCRResponse.setEntity(responseEntity);
    mockDCRResponse.setStatusLine(new BasicStatusLine(new ProtocolVersion("http", 1, 0), 200, "OK"));
    return mockDCRResponse;
}

From source file:com.gistlabs.mechanize.PageRequest.java

public HttpResponse consume(final HttpClient client, final HttpRequestBase request) throws Exception {
    if (!wasExecuted) {
        this.client = client;
        this.request = request;

        if (!request.getMethod().equalsIgnoreCase(httpMethod))
            throw new IllegalArgumentException(
                    String.format("Expected %s, but was %s", httpMethod, request.getMethod()));

        if (request.getURI().toString().equals(uri)) {
            HttpResponse response = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), 200, "OK");
            BasicHttpEntity entity = new BasicHttpEntity();
            if (contentLocation != null)
                response.addHeader(new BasicHeader("Content-Location", contentLocation));
            entity.setContentEncoding(charset);
            entity.setContentType(this.contentType);
            entity.setContent(this.body);
            response.setEntity(new BufferedHttpEntity(entity));

            assertParameters(request);/*from  ww w  . j  a  v  a  2 s . c  o  m*/
            assertHeaders(request);

            this.wasExecuted = true;
            return response;
        } else {
            assertEquals("URI of the next PageRequest does not match", uri, request.getURI().toString());
            return null;
        }
    } else
        throw new UnsupportedOperationException("Request already executed");
}

From source file:at.deder.ybr.test.cukes.HttpServerSimulator.java

@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
    Object[] args = invocation.getArguments();
    HttpResponse response = null;/*from   w w w.  j  a v  a  2s  .c  o  m*/

    if (args.length != 1 && !(args[0] instanceof HttpGet)) {
        response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_INTERNAL_SERVER_ERROR,
                "wrong arguments");
        return response;
    }

    HttpGet request = (HttpGet) args[0];
    BasicHttpEntity entity = new BasicHttpEntity();

    String requestedPath = request.getURI().getPath();
    VirtualResource requestedResource = getResource(requestedPath);
    if (requestedResource == null) {
        response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_NOT_FOUND, "not found");
    } else {
        entity.setContent(new ByteArrayInputStream(requestedResource.content));
        entity.setContentType(requestedResource.contentType);
        response = new BasicHttpResponse(HttpVersion.HTTP_1_1, requestedResource.httpStatus,
                requestedResource.httpStatusText);
        response.setEntity(entity);
    }

    return response;
}

From source file:com.overture.questdroid.utility.ExtHttpClientStack.java

private HttpEntity convertEntityNewToOld(HttpEntity ent) throws IllegalStateException, IOException {

    BasicHttpEntity ret = new BasicHttpEntity();
    if (ent != null) {
        ret.setContent(ent.getContent());
        ret.setContentLength(ent.getContentLength());
        Header h;//from   w  ww  . j av a 2s . c  o m
        h = ent.getContentEncoding();
        if (h != null) {
            ret.setContentEncoding(convertheaderNewToOld(h));
        }
        h = ent.getContentType();
        if (h != null) {
            ret.setContentType(convertheaderNewToOld(h));
        }
    }

    return ret;
}

From source file:carleton150.edu.carleton.carleton150.CertificateManagement.ExtHttpClientStack.java

private org.apache.http.HttpEntity convertEntityNewToOld(HttpEntity ent)
        throws IllegalStateException, IOException {

    BasicHttpEntity ret = new BasicHttpEntity();
    if (ent != null) {
        ret.setContent(ent.getContent());
        ret.setContentLength(ent.getContentLength());
        Header h;/*from  w ww .j a v  a 2 s.  c  o  m*/
        h = ent.getContentEncoding();
        if (h != null) {
            ret.setContentEncoding(convertheaderNewToOld(h));
        }
        h = ent.getContentType();
        if (h != null) {
            ret.setContentType(convertheaderNewToOld(h));
        }
    }

    return ret;
}

From source file:com.baasbox.android.HttpUrlConnectionClient.java

private HttpEntity asEntity(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream in;//from  ww  w  .  j  av  a  2 s.  co m
    try {
        in = connection.getInputStream();
    } catch (IOException e) {
        in = connection.getErrorStream();
    }
    entity.setContent(in);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    return entity;
}

From source file:dk.slott.super_volley.stacks.ExtHttpClientStack.java

private org.apache.http.HttpEntity convertEntityNewToOld(HttpEntity ent)
        throws IllegalStateException, IOException {
    final BasicHttpEntity ret = new BasicHttpEntity();
    if (ent != null) {
        ret.setContent(ent.getContent());
        ret.setContentLength(ent.getContentLength());
        Header h;//from  ww w  .j  a va2 s .  co  m
        h = ent.getContentEncoding();
        if (h != null) {
            ret.setContentEncoding(convertheaderNewToOld(h));
        }
        h = ent.getContentType();
        if (h != null) {
            ret.setContentType(convertheaderNewToOld(h));
        }
    }
    return ret;
}