use of org.apache.http.message.BasicStatusLine 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.apache.http.message.BasicStatusLine in project gocd by gocd.
the class RemoteRegistrationRequesterTest method shouldPassAllParametersToPostForRegistrationOfNonElasticAgent.
@Test
public void shouldPassAllParametersToPostForRegistrationOfNonElasticAgent() throws IOException, ClassNotFoundException {
String url = "http://cruise.com/go";
GoAgentServerHttpClient httpClient = mock(GoAgentServerHttpClient.class);
final CloseableHttpResponse response = mock(CloseableHttpResponse.class);
final ProtocolVersion protocolVersion = new ProtocolVersion("https", 1, 2);
when(response.getStatusLine()).thenReturn(new BasicStatusLine(protocolVersion, HttpStatus.OK.value(), null));
when(response.getEntity()).thenReturn(new StringEntity(RegistrationJSONizer.toJson(createRegistration())));
when(httpClient.execute(argThat(isA(HttpUriRequest.class)))).thenReturn(response);
final DefaultAgentRegistry defaultAgentRegistry = new DefaultAgentRegistry();
Properties properties = new Properties();
properties.put(AgentAutoRegistrationPropertiesImpl.AGENT_AUTO_REGISTER_KEY, "t0ps3cret");
properties.put(AgentAutoRegistrationPropertiesImpl.AGENT_AUTO_REGISTER_RESOURCES, "linux, java");
properties.put(AgentAutoRegistrationPropertiesImpl.AGENT_AUTO_REGISTER_ENVIRONMENTS, "uat, staging");
properties.put(AgentAutoRegistrationPropertiesImpl.AGENT_AUTO_REGISTER_HOSTNAME, "agent01.example.com");
remoteRegistryRequester(url, httpClient, defaultAgentRegistry, 200).requestRegistration("cruise.com", new AgentAutoRegistrationPropertiesImpl(null, properties));
verify(httpClient).execute(argThat(hasAllParams(defaultAgentRegistry.uuid(), "", "")));
}
use of org.apache.http.message.BasicStatusLine in project gocd by gocd.
the class SslInfrastructureServiceTest method shouldGetTokenFromServerIfOneNotExist.
@Test
public void shouldGetTokenFromServerIfOneNotExist() throws Exception {
final ArgumentCaptor<HttpRequestBase> httpRequestBaseArgumentCaptor = ArgumentCaptor.forClass(HttpRequestBase.class);
tokenService.delete();
when(agentRegistry.uuid()).thenReturn("some-uuid");
when(httpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("https", 1, 2), 200, null));
when(httpResponse.getEntity()).thenReturn(new StringEntity("token-from-server"));
when(httpClient.execute(httpRequestBaseArgumentCaptor.capture())).thenReturn(httpResponse);
sslInfrastructureService.getTokenIfNecessary();
verify(agentRegistry).storeTokenToDisk("token-from-server");
final HttpRequestBase httpRequestBase = httpRequestBaseArgumentCaptor.getValue();
final List<NameValuePair> nameValuePairs = URLEncodedUtils.parse(httpRequestBase.getURI(), StandardCharsets.UTF_8);
assertThat(findParam(nameValuePairs, "uuid").getValue(), is("some-uuid"));
}
use of org.apache.http.message.BasicStatusLine in project gocd by gocd.
the class SslInfrastructureServiceTest method shouldInvalidateKeystore.
@Test
public void shouldInvalidateKeystore() throws Exception {
temporaryFolder.create();
File configFile = temporaryFolder.newFile();
when(httpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("https", 1, 2), 200, null));
when(httpResponse.getEntity()).thenReturn(new StringEntity(RegistrationJSONizer.toJson(createRegistration())));
when(agentRegistry.guidPresent()).thenReturn(true);
when(httpClient.execute(any(HttpRequestBase.class))).thenReturn(httpResponse);
when(agentRegistry.tokenPresent()).thenReturn(true);
shouldCreateSslInfrastructure();
sslInfrastructureService.registerIfNecessary(new AgentAutoRegistrationPropertiesImpl(configFile));
assertThat(GoAgentServerClientBuilder.AGENT_CERTIFICATE_FILE, exists());
verify(httpClient, times(1)).execute(any(HttpRequestBase.class));
sslInfrastructureService.registerIfNecessary(new AgentAutoRegistrationPropertiesImpl(configFile));
verify(httpClient, times(1)).execute(any(HttpRequestBase.class));
sslInfrastructureService.invalidateAgentCertificate();
sslInfrastructureService.registerIfNecessary(new AgentAutoRegistrationPropertiesImpl(configFile));
verify(httpClient, times(2)).execute(any(HttpRequestBase.class));
}
use of org.apache.http.message.BasicStatusLine in project gocd by gocd.
the class SslInfrastructureServiceTest method shouldPassUUIDAndTokenDuringAgentRegistration.
@Test
public void shouldPassUUIDAndTokenDuringAgentRegistration() throws Exception {
final ArgumentCaptor<HttpEntityEnclosingRequestBase> httpRequestBaseArgumentCaptor = ArgumentCaptor.forClass(HttpEntityEnclosingRequestBase.class);
when(agentRegistry.uuid()).thenReturn("some-uuid");
when(agentRegistry.token()).thenReturn("some-token");
when(httpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("https", 1, 2), 200, null));
when(httpResponse.getEntity()).thenReturn(new StringEntity(RegistrationJSONizer.toJson(createRegistration())));
when(httpClient.execute(httpRequestBaseArgumentCaptor.capture())).thenReturn(httpResponse);
sslInfrastructureService.createSslInfrastructure();
sslInfrastructureService.registerIfNecessary(new AgentAutoRegistrationPropertiesImpl(new File("foo", "bar")));
assertThat(GoAgentServerClientBuilder.AGENT_CERTIFICATE_FILE, exists());
final HttpEntityEnclosingRequestBase httpRequestBase = httpRequestBaseArgumentCaptor.getValue();
final List<NameValuePair> nameValuePairs = URLEncodedUtils.parse(httpRequestBase.getEntity());
assertThat(findParam(nameValuePairs, "uuid").getValue(), is("some-uuid"));
assertThat(findParam(nameValuePairs, "token").getValue(), is("some-token"));
}
Aggregations