Search in sources :

Example 16 with HttpRequest

use of java.net.http.HttpRequest in project alf.io by alfio-event.

the class MollieWebhookPaymentManager method callGetPayment.

private HttpResponse<InputStream> callGetPayment(String paymentId, Map<ConfigurationKeys, MaybeConfiguration> configuration, ConfigurationLevel configurationLevel) throws IOException, InterruptedException {
    var paymentResourceUrl = PAYMENTS_ENDPOINT + "/" + paymentId;
    if (configuration.get(PLATFORM_MODE_ENABLED).getValueAsBooleanOrDefault()) {
        paymentResourceUrl += ("?testmode=" + !configuration.get(MOLLIE_CONNECT_LIVE_MODE).getValueAsBooleanOrDefault());
    }
    HttpRequest request = requestFor(paymentResourceUrl, configuration, configurationLevel).GET().build();
    return client.send(request, HttpResponse.BodyHandlers.ofInputStream());
}
Also used : HttpRequest(java.net.http.HttpRequest)

Example 17 with HttpRequest

use of java.net.http.HttpRequest in project alf.io by alfio-event.

the class MailjetMailer method send.

@Override
public void send(Configurable configurable, String fromName, String to, List<String> cc, String subject, String text, Optional<String> html, Attachment... attachment) {
    var conf = configurationManager.getFor(EnumSet.of(MAILJET_APIKEY_PUBLIC, MAILJET_APIKEY_PRIVATE, MAILJET_FROM, MAIL_REPLY_TO), configurable.getConfigurationLevel());
    String apiKeyPublic = conf.get(MAILJET_APIKEY_PUBLIC).getRequiredValue();
    String apiKeyPrivate = conf.get(MAILJET_APIKEY_PRIVATE).getRequiredValue();
    String fromEmail = conf.get(MAILJET_FROM).getRequiredValue();
    // https://dev.mailjet.com/guides/?shell#sending-with-attached-files
    Map<String, Object> mailPayload = new HashMap<>();
    List<Map<String, String>> recipients = new ArrayList<>();
    recipients.add(Collections.singletonMap("Email", to));
    if (cc != null && !cc.isEmpty()) {
        recipients.addAll(cc.stream().map(email -> Collections.singletonMap("Email", email)).collect(Collectors.toList()));
    }
    mailPayload.put("FromEmail", fromEmail);
    mailPayload.put("FromName", fromName);
    mailPayload.put("Subject", subject);
    mailPayload.put("Text-part", text);
    html.ifPresent(h -> mailPayload.put("Html-part", h));
    mailPayload.put("Recipients", recipients);
    String replyTo = conf.get(MAIL_REPLY_TO).getValueOrDefault("");
    if (StringUtils.isNotBlank(replyTo)) {
        mailPayload.put("Headers", Collections.singletonMap("Reply-To", replyTo));
    }
    if (attachment != null && attachment.length > 0) {
        mailPayload.put("Attachments", Arrays.stream(attachment).map(MailjetMailer::fromAttachment).collect(Collectors.toList()));
    }
    HttpRequest request = HttpRequest.newBuilder(URI.create("https://api.mailjet.com/v3/send")).header(HttpUtils.AUTHORIZATION, HttpUtils.basicAuth(apiKeyPublic, apiKeyPrivate)).header(HttpUtils.CONTENT_TYPE, HttpUtils.APPLICATION_JSON).POST(HttpRequest.BodyPublishers.ofString(Json.GSON.toJson(mailPayload))).build();
    try {
        HttpResponse<Void> response = client.send(request, HttpResponse.BodyHandlers.discarding());
        if (!HttpUtils.callSuccessful(response)) {
            log.warn("sending email was not successful:" + response);
            throw new IllegalStateException("Attempt to send a message failed. Result is: " + response.statusCode());
        }
    } catch (IOException e) {
        log.warn("error while sending email", e);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        log.warn("error while sending email", e);
    }
}
Also used : HttpRequest(java.net.http.HttpRequest) IOException(java.io.IOException)

Example 18 with HttpRequest

use of java.net.http.HttpRequest in project alf.io by alfio-event.

the class NormalFlowE2ETest method init.

@BeforeEach
public void init() throws Exception {
    if (environment.acceptsProfiles(Profiles.of("e2e"))) {
        var serverBaseUrl = environment.getRequiredProperty("e2e.server.url");
        var serverApiKey = environment.getRequiredProperty("e2e.server.apikey");
        slug = UUID.randomUUID().toString();
        var now = LocalDateTime.now(clockProvider.getClock());
        var requestBody = JSON_BODY.replace("--CATEGORY_START_SELLING--", now.minusDays(1).toString()).replace("--SLUG--", slug).replace("--EVENT_START_DATE--", now.plusDays(2).toString()).replace("--EVENT_END_DATE--", now.plusDays(2).plusHours(2).toString());
        // create temporary event
        HttpRequest request = HttpRequest.newBuilder().uri(URI.create(serverBaseUrl + "/api/v1/admin/event/create")).header("Content-Type", "application/json").header("Authorization", "ApiKey " + serverApiKey).POST(HttpRequest.BodyPublishers.ofString(requestBody)).build();
        var response = HTTP_CLIENT.send(request, BodyHandlers.ofString());
        if (!HttpUtils.callSuccessful(response)) {
            throw new IllegalStateException(response.statusCode() + ": " + response.body());
        }
        eventUrl = serverBaseUrl + "/event/" + slug;
    }
}
Also used : HttpRequest(java.net.http.HttpRequest)

Example 19 with HttpRequest

use of java.net.http.HttpRequest in project alf.io by alfio-event.

the class NormalFlowE2ETest method destroy.

@AfterEach
public void destroy() throws Exception {
    if (environment.acceptsProfiles(Profiles.of("e2e"))) {
        var serverBaseUrl = environment.getRequiredProperty("e2e.server.url");
        var serverApiKey = environment.getRequiredProperty("e2e.server.apikey");
        HttpRequest request = HttpRequest.newBuilder().uri(URI.create(serverBaseUrl + "/api/v1/admin/event/" + slug)).header("Content-Type", "application/json").header("Authorization", "ApiKey " + serverApiKey).DELETE().build();
        var response = HTTP_CLIENT.send(request, BodyHandlers.ofString());
        if (!HttpUtils.callSuccessful(response)) {
            throw new IllegalStateException(response.body());
        }
    }
}
Also used : HttpRequest(java.net.http.HttpRequest)

Example 20 with HttpRequest

use of java.net.http.HttpRequest in project alf.io by alfio-event.

the class BaseOpenIdAuthenticationManager method retrieveAccessToken.

private Map<String, Object> retrieveAccessToken(String code) {
    try {
        HttpRequest request = HttpRequest.newBuilder().uri(URI.create(buildClaimsRetrieverUrl())).header("Content-Type", openIdConfiguration().getContentType()).POST(HttpRequest.BodyPublishers.ofString(buildRetrieveClaimsUrlBody(code))).build();
        HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
        if (HttpUtils.callSuccessful(response)) {
            log.trace("Access Token successfully retrieved");
            return Json.fromJson(response.body(), new TypeReference<>() {
            });
        } else {
            log.warn("cannot retrieve access token");
            throw new OpenIdAuthenticationException("cannot retrieve access token. Response from server: " + response.body());
        }
    } catch (OpenIdAuthenticationException e) {
        throw e;
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        log.error("Request was interrupted while retrieving access token", e);
        throw new OpenIdAuthenticationException(e);
    } catch (Exception e) {
        log.error("There has been an error retrieving the access token from the idp using the authorization code", e);
        throw new OpenIdAuthenticationException(e);
    }
}
Also used : HttpRequest(java.net.http.HttpRequest)

Aggregations

HttpRequest (java.net.http.HttpRequest)38 InputStream (java.io.InputStream)15 HttpClient (java.net.http.HttpClient)10 TypedInputStream (org.apache.jena.atlas.web.TypedInputStream)10 URI (java.net.URI)9 IOException (java.io.IOException)7 HttpResponse (java.net.http.HttpResponse)7 Duration (java.time.Duration)6 Test (org.junit.Test)5 HttpException (org.apache.jena.atlas.web.HttpException)4 SerializedClassRunner (io.pravega.test.common.SerializedClassRunner)3 TestUtils (io.pravega.test.common.TestUtils)3 URISyntaxException (java.net.URISyntaxException)3 Pattern (java.util.regex.Pattern)3 Cleanup (lombok.Cleanup)3 Assert.assertTrue (org.junit.Assert.assertTrue)3 RunWith (org.junit.runner.RunWith)3 JsonParser (com.google.gson.JsonParser)2 Counter (io.pravega.shared.metrics.Counter)2 MetricsConfig (io.pravega.shared.metrics.MetricsConfig)2