Java Code Examples for org.apache.http.entity.BasicHttpEntity#setContentType()

The following examples show how to use org.apache.http.entity.BasicHttpEntity#setContentType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConanClient.java    From nexus-repository-conan with Eclipse Public License 1.0 7 votes vote down vote up
public HttpResponse getHttpResponse(final String path) throws IOException {
  try (CloseableHttpResponse closeableHttpResponse = super.get(path)) {
    HttpEntity entity = closeableHttpResponse.getEntity();

    BasicHttpEntity basicHttpEntity = new BasicHttpEntity();
    String content = EntityUtils.toString(entity);
    basicHttpEntity.setContent(IOUtils.toInputStream(content));

    basicHttpEntity.setContentEncoding(entity.getContentEncoding());
    basicHttpEntity.setContentLength(entity.getContentLength());
    basicHttpEntity.setContentType(entity.getContentType());
    basicHttpEntity.setChunked(entity.isChunked());

    StatusLine statusLine = closeableHttpResponse.getStatusLine();
    HttpResponse response = new BasicHttpResponse(statusLine);
    response.setEntity(basicHttpEntity);
    response.setHeaders(closeableHttpResponse.getAllHeaders());
    response.setLocale(closeableHttpResponse.getLocale());
    return response;
  }
}
 
Example 2
Source File: ExtHttpClientStack.java    From android_volley_examples with Apache License 2.0 6 votes vote down vote up
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;
        h = ent.getContentEncoding();
        if (h != null) {
            ret.setContentEncoding(convertheaderNewToOld(h));
        }
        h = ent.getContentType();
        if (h != null) {
            ret.setContentType(convertheaderNewToOld(h));
        }
    }

    return ret;
}
 
Example 3
Source File: HurlStack.java    From volley with Apache License 2.0 6 votes vote down vote up
/**
 * Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.
 * @param connection
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    
    return entity;
}
 
Example 4
Source File: HttpKnife.java    From WeGit with Apache License 2.0 6 votes vote down vote up
/**
 * 获取响应报文实体
 * 
 * @return
 * @throws IOException
 */
private HttpEntity entityFromConnection() throws IOException {
	BasicHttpEntity entity = new BasicHttpEntity();
	InputStream inputStream;
	try {
		inputStream = connection.getInputStream();
	} catch (IOException ioe) {
		inputStream = connection.getErrorStream();
	}
	if (GZIP.equals(getResponseheader(ResponseHeader.HEADER_CONTENT_ENCODING))) {
		entity.setContent(new GZIPInputStream(inputStream));
	} else {
		entity.setContent(inputStream);
	}
	entity.setContentLength(connection.getContentLength());
	entity.setContentEncoding(connection.getContentEncoding());
	entity.setContentType(connection.getContentType());
	return entity;
}
 
Example 5
Source File: AuthenticationHandlerTest.java    From carbon-device-mgt with Apache License 2.0 6 votes vote down vote up
private CloseableHttpResponse getValidationResponse() throws UnsupportedEncodingException {
    ValidationResponce response = new ValidationResponce();
    response.setDeviceId("1234");
    response.setDeviceType("testdevice");
    response.setJWTToken("1234567788888888");
    response.setTenantId(-1234);
    Gson gson = new Gson();
    String jsonReponse = gson.toJson(response);
    CloseableHttpResponse mockDCRResponse = new MockHttpResponse();
    BasicHttpEntity responseEntity = new BasicHttpEntity();
    responseEntity.setContent(new ByteArrayInputStream(jsonReponse.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;
}
 
Example 6
Source File: HurlStack.java    From SaveVolley with Apache License 2.0 6 votes vote down vote up
/**
 * Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.
 *
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
/*
 *  通过一个 HttpURLConnection 获取其对应的 HttpEntity ( 这里就 HttpEntity 而言,耦合了 Apache )
 */
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    // 设置 HttpEntity 的内容
    entity.setContent(inputStream);
    // 设置 HttpEntity 的长度
    entity.setContentLength(connection.getContentLength());
    // 设置 HttpEntity 的编码
    entity.setContentEncoding(connection.getContentEncoding());
    // 设置 HttpEntity Content-Type
    entity.setContentType(connection.getContentType());
    return entity;
}
 
Example 7
Source File: HttpUrlConnStack.java    From simple_net_framework with MIT License 6 votes vote down vote up
/**
 * 执行HTTP请求之后获取到其数据流,即返回请求结果的流
 * 
 * @param connection
 * @return
 */
private HttpEntity entityFromURLConnwction(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream = null;
    try {
        inputStream = connection.getInputStream();
    } catch (IOException e) {
        e.printStackTrace();
        inputStream = connection.getErrorStream();
    }

    // TODO : GZIP 
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());

    return entity;
}
 
Example 8
Source File: HurlStack.java    From CrossBow with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.
 * @param connection
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    return entity;
}
 
Example 9
Source File: HurlStack.java    From device-database with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.
 * @param connection
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    return entity;
}
 
Example 10
Source File: OkHttpStack.java    From openshop.io-android with MIT License 5 votes vote down vote up
private static HttpEntity entityFromOkHttpResponse(Response r) throws IOException {
    BasicHttpEntity entity = new BasicHttpEntity();
    ResponseBody body = r.body();

    entity.setContent(body.byteStream());
    entity.setContentLength(body.contentLength());
    entity.setContentEncoding(r.header("Content-Encoding"));

    if (body.contentType() != null) {
        entity.setContentType(body.contentType().type());
    }
    return entity;
}
 
Example 11
Source File: OkHttpURLConnectionStack.java    From AndroidProjects with MIT License 5 votes vote down vote up
private static HttpEntity entityFromOkHttpResponse(Response r) throws IOException {
    BasicHttpEntity entity = new BasicHttpEntity();
    ResponseBody body = r.body();

    entity.setContent(body.byteStream());
    entity.setContentLength(body.contentLength());
    entity.setContentEncoding(r.header("Content-Encoding"));

    if (body.contentType() != null) {
        entity.setContentType(body.contentType().type());
    }
    return entity;
}
 
Example 12
Source File: HurlStack.java    From android-project-wo2b with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.
 * @param connection
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    return entity;
}
 
Example 13
Source File: AuthenticationHandlerTest.java    From carbon-device-mgt with Apache License 2.0 5 votes vote down vote up
private CloseableHttpResponse getDCRResponse() throws IOException {
    CloseableHttpResponse mockDCRResponse = new MockHttpResponse();
    String dcrResponseFile = TestUtils.getAbsolutePathOfConfig("dcr-response.json");
    BasicHttpEntity responseEntity = new BasicHttpEntity();
    responseEntity.setContent(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;
}
 
Example 14
Source File: AuthenticationHandlerTest.java    From carbon-device-mgt with Apache License 2.0 5 votes vote down vote up
private CloseableHttpResponse getAccessTokenReponse() throws IOException {
    CloseableHttpResponse mockDCRResponse = new MockHttpResponse();
    String dcrResponseFile = TestUtils.getAbsolutePathOfConfig("accesstoken-response.json");
    BasicHttpEntity responseEntity = new BasicHttpEntity();
    responseEntity.setContent(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;
}
 
Example 15
Source File: HurlStack.java    From product-emm with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.
 * @param connection
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    return entity;
}
 
Example 16
Source File: HurlStack.java    From android-discourse with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.
 *
 * @param connection
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    return entity;
}
 
Example 17
Source File: HurlStack.java    From SimplifyReader with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes an {@link org.apache.http.HttpEntity} from the given {@link java.net.HttpURLConnection}.
 * @param connection
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    return entity;
}
 
Example 18
Source File: ApiServer.java    From cosmic with Apache License 2.0 5 votes vote down vote up
private void writeResponse(final HttpResponse resp, final String responseText, final int statusCode, final String responseType, final String reasonPhrase) {
    try {
        resp.setStatusCode(statusCode);
        resp.setReasonPhrase(reasonPhrase);

        final BasicHttpEntity body = new BasicHttpEntity();
        if (HttpUtils.RESPONSE_TYPE_JSON.equalsIgnoreCase(responseType)) {
            // JSON response
            body.setContentType(getJSONContentType());
            if (responseText == null) {
                body.setContent(new ByteArrayInputStream("{ \"error\" : { \"description\" : \"Internal Server Error\" } }".getBytes(HttpUtils.UTF_8)));
            }
        } else {
            body.setContentType("text/xml");
            if (responseText == null) {
                body.setContent(new ByteArrayInputStream("<error>Internal Server Error</error>".getBytes(HttpUtils.UTF_8)));
            }
        }

        if (responseText != null) {
            body.setContent(new ByteArrayInputStream(responseText.getBytes(HttpUtils.UTF_8)));
        }
        resp.setEntity(body);
    } catch (final Exception ex) {
        s_logger.error("error!", ex);
    }
}
 
Example 19
Source File: OkHttp3Stack.java    From QuickLyric with GNU General Public License v3.0 5 votes vote down vote up
private static HttpEntity getEntity(Response response) throws IOException {
    BasicHttpEntity entity = new BasicHttpEntity();
    ResponseBody body = response.body();
    entity.setContent(body.byteStream());
    entity.setContentLength(body.contentLength());
    entity.setContentEncoding(response.header("Content-Encoding"));
    if (body.contentType() != null) {
        entity.setContentType(body.contentType().type());
    }
    return entity;
}
 
Example 20
Source File: HurlStack.java    From FeedListViewDemo with MIT License 5 votes vote down vote up
/**
 * Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.
 * @param connection
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    return entity;
}