use of org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicStatusLine 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.message.BasicStatusLine in project dataverse by IQSS.
the class DataCaptureModuleUtilTest method testGetScriptFromRequestNotFound.
@Test
public void testGetScriptFromRequestNotFound() throws UnsupportedEncodingException {
System.out.println("getScriptFromRequestNotFound");
HttpResponseFactory factory = new DefaultHttpResponseFactory();
org.apache.http.HttpResponse response = factory.newHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_NOT_FOUND, null), null);
JsonObjectBuilder jab = Json.createObjectBuilder();
jab.add("userId", 42);
jab.add("datasetIdentifier", "123");
jab.add("script", "#!/bin/sh");
response.setEntity(new StringEntity(jab.build().toString()));
HttpResponse<JsonNode> httpResponse = new HttpResponse<>(response, JsonNode.class);
ScriptRequestResponse result = DataCaptureModuleUtil.getScriptFromRequest(httpResponse);
assertEquals(404, result.getHttpStatusCode());
assertEquals(-1, result.getDatasetId());
assertEquals(-1, result.getUserId());
assertEquals(null, result.getScript());
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicStatusLine in project dataverse by IQSS.
the class DataCaptureModuleUtilTest method testGetScriptFromRequestOk.
@Test
public void testGetScriptFromRequestOk() throws UnsupportedEncodingException {
System.out.println("getScriptFromRequestOk");
HttpResponseFactory factory = new DefaultHttpResponseFactory();
org.apache.http.HttpResponse response = factory.newHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, null), null);
JsonObjectBuilder jab = Json.createObjectBuilder();
jab.add("userId", 42);
jab.add("datasetIdentifier", "123");
jab.add("script", "#!/bin/sh");
response.setEntity(new StringEntity(jab.build().toString()));
HttpResponse<JsonNode> httpResponse = new HttpResponse<>(response, JsonNode.class);
ScriptRequestResponse result = DataCaptureModuleUtil.getScriptFromRequest(httpResponse);
assertEquals(200, result.getHttpStatusCode());
assertEquals("123", result.getDatasetIdentifier());
assertEquals(42, result.getUserId());
assertEquals("#!/bin/sh", result.getScript());
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicStatusLine in project vespa by vespa-engine.
the class CoredumpHandlerTest method setNextHttpResponse.
private void setNextHttpResponse(int code, Optional<String> message) throws IOException {
DefaultHttpResponseFactory responseFactory = new DefaultHttpResponseFactory();
HttpResponse httpResponse = responseFactory.newHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, code, null), null);
if (message.isPresent())
httpResponse.setEntity(new StringEntity(message.get()));
when(httpClient.execute(any())).thenReturn(httpResponse);
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicStatusLine in project vespa by vespa-engine.
the class ConfigServerApiImplTest method initExecutor.
@Before
public void initExecutor() throws IOException {
SelfCloseableHttpClient httpMock = mock(SelfCloseableHttpClient.class);
when(httpMock.execute(any())).thenAnswer(invocationOnMock -> {
HttpGet get = (HttpGet) invocationOnMock.getArguments()[0];
mockLog.append(get.getMethod()).append(" ").append(get.getURI()).append(" ");
if (mockReturnCode == 100000)
throw new RuntimeException("FAIL");
BasicStatusLine statusLine = new BasicStatusLine(HttpVersion.HTTP_1_1, mockReturnCode, null);
BasicHttpEntity entity = new BasicHttpEntity();
String returnMessage = "{\"foo\":\"bar\", \"no\":3, \"error-code\": " + mockReturnCode + "}";
InputStream stream = new ByteArrayInputStream(returnMessage.getBytes(StandardCharsets.UTF_8));
entity.setContent(stream);
CloseableHttpResponse response = mock(CloseableHttpResponse.class);
when(response.getEntity()).thenReturn(entity);
when(response.getStatusLine()).thenReturn(statusLine);
return response;
});
executor = new ConfigServerApiImpl(configServers, httpMock);
}
Aggregations