use of org.graylog.shaded.elasticsearch7.org.apache.http.ProtocolVersion in project android-uploader by nightscout.
the class AbstractRestUploaderTest method setUpExecuteCaptor.
public void setUpExecuteCaptor(int status) throws IOException {
HttpResponse response = new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("mock", 1, 2), status, ""));
response.setEntity(new StringEntity(""));
when(mockHttpClient.execute(captor.capture())).thenReturn(response);
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.ProtocolVersion in project pentaho-kettle by pentaho.
the class HTTPProtocolTest method httpClientGetsClosed.
@Test
public void httpClientGetsClosed() throws IOException, AuthenticationException {
CloseableHttpClient httpClient = Mockito.mock(CloseableHttpClient.class);
CloseableHttpResponse response = Mockito.mock(CloseableHttpResponse.class);
HTTPProtocol httpProtocol = new HTTPProtocol() {
@Override
CloseableHttpClient openHttpClient(String username, String password) {
return httpClient;
}
};
String urlAsString = "http://url/path";
when(httpClient.execute(Matchers.argThat(matchesGet()))).thenReturn(response);
StatusLine statusLine = new BasicStatusLine(new ProtocolVersion("http", 2, 0), HttpStatus.SC_OK, "blah");
BasicHttpEntity entity = new BasicHttpEntity();
String content = "plenty of mocks for this test";
entity.setContent(new ByteArrayInputStream(content.getBytes()));
when(response.getEntity()).thenReturn(entity);
when(response.getStatusLine()).thenReturn(statusLine);
assertEquals(content, httpProtocol.get(urlAsString, "", ""));
verify(httpClient).close();
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.ProtocolVersion in project CodeUtils by boredream.
the class LeanCloudHttpUtils method postBean.
public static String postBean(Object object) throws Exception {
String url = "https://api.leancloud.cn/1.1/classes/";
url += object.getClass().getSimpleName();
HashMap<String, String> map = getHeaderMap();
map.put("Content-Type", HEADER_CONTENT_TYPE);
URL parsedUrl = new URL(url);
HttpURLConnection connection = openConnection(parsedUrl);
for (String headerName : map.keySet()) {
connection.addRequestProperty(headerName, map.get(headerName));
}
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.addRequestProperty("Content-Type", HEADER_CONTENT_TYPE);
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.write(new Gson().toJson(object).getBytes());
out.close();
// Initialize HttpResponse with data from the HttpURLConnection.
ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
int responseCode = connection.getResponseCode();
if (responseCode == -1) {
// 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);
}
}
Header contentTypeHeader = response.getHeaders(HTTP.CONTENT_TYPE)[0];
String responseCharset = parseCharset(contentTypeHeader);
byte[] bytes = entityToBytes(response.getEntity());
return new String(bytes, responseCharset);
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.ProtocolVersion in project CodeUtils by boredream.
the class LeanCloudHttpUtils method postFile.
public static String postFile(String url, File file) throws Exception {
// LeanCloud上传限制, 最多1秒1个
Thread.sleep(1000);
HashMap<String, String> map = getHeaderMap();
map.put("Content-Type", new MimetypesFileTypeMap().getContentType(file));
URL parsedUrl = new URL(url);
HttpURLConnection connection = openConnection(parsedUrl);
for (String headerName : map.keySet()) {
connection.addRequestProperty(headerName, map.get(headerName));
}
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.addRequestProperty("Content-Type", new MimetypesFileTypeMap().getContentType(file));
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
int len;
byte[] buf = new byte[1024];
FileInputStream fis = new FileInputStream(file);
while ((len = fis.read(buf)) != -1) {
out.write(buf, 0, len);
}
out.close();
// Initialize HttpResponse with data from the HttpURLConnection.
ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
int responseCode = connection.getResponseCode();
if (responseCode == -1) {
// 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);
}
}
Header contentTypeHeader = response.getHeaders(HTTP.CONTENT_TYPE)[0];
String responseCharset = parseCharset(contentTypeHeader);
byte[] bytes = entityToBytes(response.getEntity());
String responseContent = new String(bytes, responseCharset);
return responseContent;
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.ProtocolVersion in project CodeUtils by boredream.
the class BmobHttpUtils method getOrPostString.
private static String getOrPostString(int method, String url, Map<String, String> postParams) throws Exception {
HashMap<String, String> map = new HashMap<String, String>();
// bmob header
map.put("X-Bmob-Application-Id", APP_ID);
map.put("X-Bmob-REST-API-Key", REST_API_KEY);
map.put("Content-Type", "application/json");
URL parsedUrl = new URL(url);
HttpURLConnection connection = openConnection(parsedUrl);
for (String headerName : map.keySet()) {
connection.addRequestProperty(headerName, map.get(headerName));
}
setConnectionParametersForRequest(connection, method, postParams);
// Initialize HttpResponse with data from the HttpURLConnection.
ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
int responseCode = connection.getResponseCode();
if (responseCode == -1) {
// 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);
}
}
Header contentTypeHeader = response.getHeaders(HTTP.CONTENT_TYPE)[0];
String responseCharset = parseCharset(contentTypeHeader);
byte[] bytes = entityToBytes(response.getEntity());
String responseContent = new String(bytes, responseCharset);
return responseContent;
}
Aggregations