Search in sources :

Example 1 with HttpClient

use of org.jboss.pnc.buildagent.common.http.HttpClient in project pnc by project-ncl.

the class DefaultClientFactory method createHttpBuildAgentClient.

@Override
public BuildAgentClient createHttpBuildAgentClient(String terminalUrl, String executionId, Map<String, String> callbackHeaders, List<Request.Header> requestHeaders) throws BuildAgentClientException {
    List<Request.Header> headers = callbackHeaders.entrySet().stream().map(e -> new Request.Header(e.getKey(), e.getValue())).collect(Collectors.toList());
    HttpClientConfiguration configuration = null;
    try {
        URL callbackUrl = new URL(StringUtils.stripEndingSlash(pncBaseUrl) + "/build-execution/" + executionId + "/completed");
        Request callback = new Request(Request.Method.POST, callbackUrl.toURI(), headers);
        configuration = HttpClientConfiguration.newBuilder().termBaseUrl(terminalUrl).callback(callback).livenessResponseTimeout(30000L).retryConfig(retryConfig).requestHeaders(requestHeaders).build();
    } catch (MalformedURLException | URISyntaxException e) {
        new BuildAgentClientException("Invalid callback URL.", e);
    }
    return new BuildAgentHttpClient(httpClient, configuration);
}
Also used : BuildAgentClient(org.jboss.pnc.buildagent.client.BuildAgentClient) GlobalModuleGroup(org.jboss.pnc.common.json.GlobalModuleGroup) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) StringUtils(org.jboss.pnc.common.util.StringUtils) Inject(javax.inject.Inject) BuildAgentSocketClient(org.jboss.pnc.buildagent.client.BuildAgentSocketClient) PreDestroy(javax.annotation.PreDestroy) RetryConfig(org.jboss.pnc.buildagent.api.httpinvoke.RetryConfig) Map(java.util.Map) BuildAgentHttpClient(org.jboss.pnc.buildagent.client.BuildAgentHttpClient) SocketClientConfiguration(org.jboss.pnc.buildagent.client.SocketClientConfiguration) HttpClientConfiguration(org.jboss.pnc.buildagent.client.HttpClientConfiguration) Logger(org.slf4j.Logger) TaskStatusUpdateEvent(org.jboss.pnc.buildagent.api.TaskStatusUpdateEvent) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) List(java.util.List) BuildAgentClientException(org.jboss.pnc.buildagent.client.BuildAgentClientException) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) Request(org.jboss.pnc.api.dto.Request) TermdBuildDriverModuleConfig(org.jboss.pnc.common.json.moduleconfig.TermdBuildDriverModuleConfig) ApplicationScoped(javax.enterprise.context.ApplicationScoped) HttpClient(org.jboss.pnc.buildagent.common.http.HttpClient) ResponseMode(org.jboss.pnc.buildagent.api.ResponseMode) MalformedURLException(java.net.MalformedURLException) BuildAgentHttpClient(org.jboss.pnc.buildagent.client.BuildAgentHttpClient) Request(org.jboss.pnc.api.dto.Request) BuildAgentClientException(org.jboss.pnc.buildagent.client.BuildAgentClientException) URISyntaxException(java.net.URISyntaxException) HttpClientConfiguration(org.jboss.pnc.buildagent.client.HttpClientConfiguration) URL(java.net.URL)

Example 2 with HttpClient

use of org.jboss.pnc.buildagent.common.http.HttpClient in project pnc by project-ncl.

the class BuildExecutionTest method shouldReceiveBuildResultViaHttpCallback.

@Test(timeout = 10000L)
public void shouldReceiveBuildResultViaHttpCallback() throws IOException, NoSuchAlgorithmException, URISyntaxException, InterruptedException, ExecutionException, TimeoutException {
    // given
    TaskStatusUpdateEvent.Builder updateEventBuilder = TaskStatusUpdateEvent.newBuilder();
    updateEventBuilder.taskId(UUID.randomUUID().toString()).newStatus(Status.COMPLETED).outputChecksum(Md5.digest("black"));
    Credentials user = Credentials.USER;
    List<Request.Header> headers = new ArrayList<>();
    headers.add(new Request.Header(Headers.CONTENT_TYPE_STRING, MediaType.APPLICATION_JSON));
    user.createAuthHeader((k, v) -> {
        headers.add(new Request.Header(k, v));
        return null;
    });
    String data = objectMapper.writeValueAsString(updateEventBuilder.build());
    BlockingQueue<TaskStatusUpdateEvent> events = new ArrayBlockingQueue<>(10);
    Consumer<TaskStatusUpdateEvent> statusChangeConsumer = (e) -> {
        events.add(e);
    };
    BuildExecutionSession session = createFakeExectionSession(statusChangeConsumer);
    // when
    String executionId = "11";
    ((BuildExecutorMock) buildExecutor).addRunningExecution(executionId, session);
    HttpClient httpClient = new HttpClient();
    Request request = new Request(Request.Method.POST, URI.create("http://localhost:8080/pnc-rest/v2/build-execution/" + executionId + "/completed"), headers);
    CompletableFuture<HttpClient.Response> responseFuture = httpClient.invoke(request, data);
    // then
    // event received
    TaskStatusUpdateEvent event = events.take();
    Assert.assertEquals(Status.COMPLETED, event.getNewStatus());
    Assert.assertEquals(200, responseFuture.get(5, TimeUnit.SECONDS).getCode());
}
Also used : Arquillian(org.jboss.arquillian.junit.Arquillian) Credentials(org.jboss.pnc.integration.setup.Credentials) URISyntaxException(java.net.URISyntaxException) RunWith(org.junit.runner.RunWith) TimeoutException(java.util.concurrent.TimeoutException) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) ContainerTest(org.jboss.pnc.test.category.ContainerTest) Inject(javax.inject.Inject) MediaType(javax.ws.rs.core.MediaType) BuildExecutorMock(org.jboss.pnc.mock.executor.BuildExecutorMock) URI(java.net.URI) Deployments(org.jboss.pnc.integration.setup.Deployments) TaskStatusUpdateEvent(org.jboss.pnc.buildagent.api.TaskStatusUpdateEvent) EnterpriseArchive(org.jboss.shrinkwrap.api.spec.EnterpriseArchive) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test) IOException(java.io.IOException) BlockingQueue(java.util.concurrent.BlockingQueue) BuildExecutionSessionMock(org.jboss.pnc.mock.executor.BuildExecutionSessionMock) UUID(java.util.UUID) BuildExecutor(org.jboss.pnc.spi.executor.BuildExecutor) Category(org.junit.experimental.categories.Category) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) List(java.util.List) Deployment(org.jboss.arquillian.container.test.api.Deployment) JavaArchive(org.jboss.shrinkwrap.api.spec.JavaArchive) Md5(org.jboss.pnc.common.security.Md5) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Headers(io.undertow.util.Headers) Request(org.jboss.pnc.api.dto.Request) Status(org.jboss.pnc.buildagent.api.Status) EXECUTOR_JAR(org.jboss.pnc.integration.setup.Deployments.EXECUTOR_JAR) HttpClient(org.jboss.pnc.buildagent.common.http.HttpClient) Assert(org.junit.Assert) BuildExecutionSession(org.jboss.pnc.spi.executor.BuildExecutionSession) BuildExecutionSession(org.jboss.pnc.spi.executor.BuildExecutionSession) BuildExecutorMock(org.jboss.pnc.mock.executor.BuildExecutorMock) ArrayList(java.util.ArrayList) Request(org.jboss.pnc.api.dto.Request) TaskStatusUpdateEvent(org.jboss.pnc.buildagent.api.TaskStatusUpdateEvent) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) HttpClient(org.jboss.pnc.buildagent.common.http.HttpClient) Credentials(org.jboss.pnc.integration.setup.Credentials) ContainerTest(org.jboss.pnc.test.category.ContainerTest) Test(org.junit.Test)

Example 3 with HttpClient

use of org.jboss.pnc.buildagent.common.http.HttpClient in project pnc by project-ncl.

the class DefaultClientFactory method init.

@PostConstruct
public void init() throws IOException {
    httpClient = new HttpClient();
    logger.info("DefaultClientFactory initialized.");
}
Also used : BuildAgentHttpClient(org.jboss.pnc.buildagent.client.BuildAgentHttpClient) HttpClient(org.jboss.pnc.buildagent.common.http.HttpClient) PostConstruct(javax.annotation.PostConstruct)

Aggregations

HttpClient (org.jboss.pnc.buildagent.common.http.HttpClient)3 IOException (java.io.IOException)2 URISyntaxException (java.net.URISyntaxException)2 List (java.util.List)2 TimeoutException (java.util.concurrent.TimeoutException)2 Consumer (java.util.function.Consumer)2 PostConstruct (javax.annotation.PostConstruct)2 Inject (javax.inject.Inject)2 Request (org.jboss.pnc.api.dto.Request)2 TaskStatusUpdateEvent (org.jboss.pnc.buildagent.api.TaskStatusUpdateEvent)2 BuildAgentHttpClient (org.jboss.pnc.buildagent.client.BuildAgentHttpClient)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Headers (io.undertow.util.Headers)1 MalformedURLException (java.net.MalformedURLException)1 URI (java.net.URI)1 URL (java.net.URL)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Optional (java.util.Optional)1