use of org.graylog.shaded.elasticsearch7.org.apache.http.ProtocolVersion in project timbuctoo by HuygensING.
the class HttpCallerTest method response.
private HttpResponse response(final int status, String value) throws IOException {
HttpResponse httpResponse = mock(HttpResponse.class);
when(httpResponse.getStatusLine()).thenReturn(new StatusLine() {
@Override
public ProtocolVersion getProtocolVersion() {
return new ProtocolVersion("http", 1, 1);
}
@Override
public int getStatusCode() {
return status;
}
@Override
public String getReasonPhrase() {
return "";
}
});
when(httpResponse.getAllHeaders()).thenReturn(new Header[] {});
HttpEntity entity = mock(HttpEntity.class);
when(entity.getContent()).thenReturn(new ByteArrayInputStream(value.getBytes()));
when(httpResponse.getEntity()).thenReturn(entity);
return httpResponse;
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.ProtocolVersion in project AndroidLife by CaMnter.
the class HurlStack method performRequest.
/*
* 执行处理 Volley内的 抽象请求 Request<?>
* 这里会调用 HttpURLConnection 去处理网络请求
* 但是 HttpURLConnection 处理后,都返回 Apache 的请求结果( HttpResponse )
* performRequest(...) 接下来会将:Apache HttpResponse -> Volley NetworkResponse 进行转化
*/
@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders) throws IOException, AuthFailureError {
// 获取 Volley 抽象请求 Request 中的 String 类型的 Url
String url = request.getUrl();
// 实例化一个 HashMap 来存放 Header 信息
HashMap<String, String> map = new HashMap<String, String>();
map.putAll(request.getHeaders());
map.putAll(additionalHeaders);
// 判断是否有 Url 重写的逻辑
if (mUrlRewriter != null) {
String rewritten = mUrlRewriter.rewriteUrl(url);
if (rewritten == null) {
throw new IOException("URL blocked by rewriter: " + url);
}
// 重新赋值上 UrlRewriter 接口 重写的 Url
url = rewritten;
}
// 实例化一个 java.net.Url 对象
URL parsedUrl = new URL(url);
/*
* 将 URL 对象 和 Volley 的抽象请求 Request 传入到 openConnection(...) 方法内
* 完成 Volley 抽象请求 Request -> HttpURLConnection 的转换过渡
* 此时拿到一个 HttpURLConnection 对象
*/
HttpURLConnection connection = openConnection(parsedUrl, request);
// 根据刚才存放 Header 信息的 Map,给 HttpURLConnection 添加头信息
for (String headerName : map.keySet()) {
connection.addRequestProperty(headerName, map.get(headerName));
}
// 设置 HttpURLConnection 请求方法类型
setConnectionParametersForRequest(connection, request);
// Initialize HttpResponse with data from the HttpURLConnection.
/*
* 初始化 一个 Apache 的 HTTP 协议 ( ProtocolVersion )
*/
ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
/*
* 正常情况下 HttpURLConnection 是依靠 connect() 发请求的
*
* 但是 HttpURLConnection 的 getInputStream() 和 getOutputStream() 也会自动调用 connect()
*
* HttpURLConnection 的 getResponseCode() 会调用 getInputStream()
* 然后 getInputStream() 又会自动调用 connect(),于是
*
* 这里就是 发请求了
*/
int responseCode = connection.getResponseCode();
// responseCode == -1 表示 没有返回内容
if (responseCode == -1) {
// Signal to the caller that something was wrong with the connection.
throw new IOException("Could not retrieve response code from HttpUrlConnection.");
}
// 实例化 org.apache.http.StatusLine 对象
StatusLine responseStatus = new BasicStatusLine(protocolVersion, connection.getResponseCode(), connection.getResponseMessage());
// 用 org.apache.http.StatusLine 去实例化一个 Apache 的 Response
BasicHttpResponse response = new BasicHttpResponse(responseStatus);
/*
* 判断请求结果 Response 是否存在 body
*
* 有的话,给刚才实例话的 Apache Response 设置 HttpEntity( 调用 entityFromConnection(...)
* 通过一个 HttpURLConnection 获取其对应的 HttpEntity )
*/
if (hasResponseBody(request.getMethod(), responseStatus.getStatusCode())) {
response.setEntity(entityFromConnection(connection));
}
// 设置 请求结果 Response 的头信息
for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
if (header.getKey() != null) {
Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
response.addHeader(h);
}
}
// 返回设置好的 Apache Response
return response;
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.ProtocolVersion in project dropwizard by dropwizard.
the class DropwizardApacheConnectorTest method multiple_headers_with_the_same_name_are_processed_successfully.
@Test
void multiple_headers_with_the_same_name_are_processed_successfully() throws Exception {
final CloseableHttpClient client = mock(CloseableHttpClient.class);
final DropwizardApacheConnector dropwizardApacheConnector = new DropwizardApacheConnector(client, null, false);
final Header[] apacheHeaders = { new BasicHeader("Set-Cookie", "test1"), new BasicHeader("Set-Cookie", "test2") };
final CloseableHttpResponse apacheResponse = mock(CloseableHttpResponse.class);
when(apacheResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 200, "OK"));
when(apacheResponse.getAllHeaders()).thenReturn(apacheHeaders);
when(client.execute(Mockito.any())).thenReturn(apacheResponse);
final ClientRequest jerseyRequest = mock(ClientRequest.class);
when(jerseyRequest.getUri()).thenReturn(URI.create("http://localhost"));
when(jerseyRequest.getMethod()).thenReturn("GET");
when(jerseyRequest.getHeaders()).thenReturn(new MultivaluedHashMap<>());
final ClientResponse jerseyResponse = dropwizardApacheConnector.apply(jerseyRequest);
assertThat(jerseyResponse.getStatus()).isEqualTo(apacheResponse.getStatusLine().getStatusCode());
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.ProtocolVersion in project saga-android by AnandChowdhary.
the class HurlStack method performRequest.
@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders) throws IOException, AuthFailureError {
String url = request.getUrl();
HashMap<String, String> map = new HashMap<String, String>();
map.putAll(request.getHeaders());
map.putAll(additionalHeaders);
if (mUrlRewriter != null) {
String rewritten = mUrlRewriter.rewriteUrl(url);
if (rewritten == null) {
throw new IOException("URL blocked by rewriter: " + url);
}
url = rewritten;
}
URL parsedUrl = new URL(url);
HttpURLConnection connection = openConnection(parsedUrl, request);
for (String headerName : map.keySet()) {
connection.addRequestProperty(headerName, map.get(headerName));
}
setConnectionParametersForRequest(connection, request);
// Initialize HttpResponse with data from the HttpURLConnection.
ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
int responseCode = connection.getResponseCode();
if (responseCode == -1) {
// Signal to the caller that something was wrong with the connection.
throw new IOException("Could not retrieve response code from HttpUrlConnection.");
}
StatusLine responseStatus = new BasicStatusLine(protocolVersion, connection.getResponseCode(), connection.getResponseMessage());
BasicHttpResponse response = new BasicHttpResponse(responseStatus);
response.setEntity(entityFromConnection(connection));
for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
if (header.getKey() != null) {
Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
response.addHeader(h);
}
}
return response;
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.ProtocolVersion in project cdap-ingest by caskdata.
the class RestClientTest method testForbiddenResponseCodeAnalysis.
@Test
public void testForbiddenResponseCodeAnalysis() {
StatusLine statusLine = new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), HttpStatus.SC_FORBIDDEN, "Forbidden");
when(response.getStatusLine()).thenReturn(statusLine);
TestUtils.verifyResponse(HttpStatus.SC_FORBIDDEN, response);
verify(response).getStatusLine();
}
Aggregations