Search in sources :

Example 56 with BasicStatusLine

use of org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicStatusLine in project SimplifyReader by chentao0707.

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;
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) ProtocolVersion(org.apache.http.ProtocolVersion) URL(java.net.URL) BasicStatusLine(org.apache.http.message.BasicStatusLine) BasicStatusLine(org.apache.http.message.BasicStatusLine) StatusLine(org.apache.http.StatusLine) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpURLConnection(java.net.HttpURLConnection) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) List(java.util.List) BasicHeader(org.apache.http.message.BasicHeader)

Example 57 with BasicStatusLine

use of org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicStatusLine in project gocd by gocd.

the class SslInfrastructureServiceTest method shouldGetTokenFromServerIfOneNotExist.

@Test
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"));
}
Also used : StringEntity(org.apache.http.entity.StringEntity) NameValuePair(org.apache.http.NameValuePair) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) ProtocolVersion(org.apache.http.ProtocolVersion) BasicStatusLine(org.apache.http.message.BasicStatusLine) Test(org.junit.jupiter.api.Test)

Example 58 with BasicStatusLine

use of org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicStatusLine in project gocd by gocd.

the class SslInfrastructureServiceTest method shouldPassUUIDAndTokenDuringAgentRegistration.

@Test
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(""));
    when(httpClient.execute(httpRequestBaseArgumentCaptor.capture())).thenReturn(httpResponse);
    sslInfrastructureService.createSslInfrastructure();
    sslInfrastructureService.registerIfNecessary(new AgentAutoRegistrationPropertiesImpl(new File("foo", "bar")));
    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"));
}
Also used : StringEntity(org.apache.http.entity.StringEntity) NameValuePair(org.apache.http.NameValuePair) HttpEntityEnclosingRequestBase(org.apache.http.client.methods.HttpEntityEnclosingRequestBase) AgentAutoRegistrationPropertiesImpl(com.thoughtworks.go.agent.AgentAutoRegistrationPropertiesImpl) ProtocolVersion(org.apache.http.ProtocolVersion) File(java.io.File) BasicStatusLine(org.apache.http.message.BasicStatusLine) Test(org.junit.jupiter.api.Test)

Example 59 with BasicStatusLine

use of org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicStatusLine in project gocd by gocd.

the class RemoteRegistrationRequesterTest method shouldPassAllParametersToPostForRegistrationOfNonElasticAgent.

@Test
void shouldPassAllParametersToPostForRegistrationOfNonElasticAgent() throws IOException {
    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(""));
    when(httpClient.execute(isA(HttpRequestBase.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(), "", "")));
}
Also used : StringEntity(org.apache.http.entity.StringEntity) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) AgentAutoRegistrationPropertiesImpl(com.thoughtworks.go.agent.AgentAutoRegistrationPropertiesImpl) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) GoAgentServerHttpClient(com.thoughtworks.go.agent.common.ssl.GoAgentServerHttpClient) ProtocolVersion(org.apache.http.ProtocolVersion) DefaultAgentRegistry(com.thoughtworks.go.config.DefaultAgentRegistry) Properties(java.util.Properties) BasicStatusLine(org.apache.http.message.BasicStatusLine) Test(org.junit.jupiter.api.Test)

Example 60 with BasicStatusLine

use of org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicStatusLine in project gocd by gocd.

the class TokenRequesterTest method shouldGetTokenFromServer.

@Test
void shouldGetTokenFromServer() throws Exception {
    final ArgumentCaptor<HttpRequestBase> argumentCaptor = ArgumentCaptor.forClass(HttpRequestBase.class);
    final CloseableHttpResponse httpResponse = mock(CloseableHttpResponse.class);
    when(agentRegistry.uuid()).thenReturn("agent-uuid");
    when(httpClient.execute(any(HttpRequestBase.class))).thenReturn(httpResponse);
    when(httpResponse.getEntity()).thenReturn(new StringEntity("token-from-server"));
    when(httpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("https", 1, 2), SC_OK, null));
    final String token = tokenRequester.getToken();
    verify(httpClient).execute(argumentCaptor.capture());
    final HttpRequestBase requestBase = argumentCaptor.getValue();
    final List<NameValuePair> nameValuePairs = URLEncodedUtils.parse(requestBase.getURI(), StandardCharsets.UTF_8);
    assertThat(token).isEqualTo("token-from-server");
    assertThat(findParam(nameValuePairs, "uuid").getValue()).isEqualTo("agent-uuid");
}
Also used : StringEntity(org.apache.http.entity.StringEntity) NameValuePair(org.apache.http.NameValuePair) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) ProtocolVersion(org.apache.http.ProtocolVersion) BasicStatusLine(org.apache.http.message.BasicStatusLine) Test(org.junit.jupiter.api.Test)

Aggregations

BasicStatusLine (org.apache.http.message.BasicStatusLine)118 ProtocolVersion (org.apache.http.ProtocolVersion)67 Test (org.junit.Test)56 BasicHttpResponse (org.apache.http.message.BasicHttpResponse)54 StatusLine (org.apache.http.StatusLine)45 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)33 StringEntity (org.apache.http.entity.StringEntity)32 ByteArrayInputStream (java.io.ByteArrayInputStream)29 HttpResponse (org.apache.http.HttpResponse)27 IOException (java.io.IOException)19 HttpEntity (org.apache.http.HttpEntity)19 BasicHeader (org.apache.http.message.BasicHeader)19 URL (java.net.URL)17 HttpURLConnection (java.net.HttpURLConnection)16 List (java.util.List)16 Header (org.apache.http.Header)16 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)16 HashMap (java.util.HashMap)14 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)13 BasicHttpEntity (org.apache.http.entity.BasicHttpEntity)12