use of org.apache.http.message.BasicStatusLine in project CodeUtils by boredream.
the class LeanCloudHttpUtils method updateBean.
public static String updateBean(Object object, String key) throws Exception {
String url = "https://api.leancloud.cn/1.1/classes/";
url += object.getClass().getSimpleName();
url += ("/" + key);
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("PUT");
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());
String responseContent = new String(bytes, responseCharset);
return responseContent;
}
use of org.apache.http.message.BasicStatusLine in project cdap-ingest by caskdata.
the class RestClientTest method testConflictResponseCodeAnalysis.
@Test
public void testConflictResponseCodeAnalysis() {
StatusLine statusLine = new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), HttpStatus.SC_CONFLICT, "Conflict");
when(response.getStatusLine()).thenReturn(statusLine);
TestUtils.verifyResponse(HttpStatus.SC_CONFLICT, response);
verify(response).getStatusLine();
}
use of org.apache.http.message.BasicStatusLine in project cdap-ingest by caskdata.
the class RestClientTest method testInternalServerErrorResponseCodeAnalysis.
@Test
public void testInternalServerErrorResponseCodeAnalysis() {
StatusLine statusLine = new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), HttpStatus.SC_INTERNAL_SERVER_ERROR, "Internal Server Error");
when(response.getStatusLine()).thenReturn(statusLine);
TestUtils.verifyResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR, response);
verify(response).getStatusLine();
}
use of org.apache.http.message.BasicStatusLine in project cdap-ingest by caskdata.
the class RestClientTest method testBadRequestResponseCodeAnalysis.
@Test
public void testBadRequestResponseCodeAnalysis() {
StatusLine statusLine = new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), HttpStatus.SC_BAD_REQUEST, "Bad Request");
when(response.getStatusLine()).thenReturn(statusLine);
TestUtils.verifyResponse(HttpStatus.SC_BAD_REQUEST, response);
verify(response).getStatusLine();
}
use of org.apache.http.message.BasicStatusLine in project ddf by codice.
the class BackupCommandTest method prepareResponse.
private HttpResponse prepareResponse(int statusCode, String responseBody) {
HttpResponse httpResponse = new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), statusCode, ""));
httpResponse.setStatusCode(statusCode);
try {
httpResponse.setEntity(new StringEntity(responseBody));
} catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException(e);
}
return httpResponse;
}
Aggregations