@SuppressWarnings("unchecked") protected <T> T requestEntity(T entity, String url) throws StorageException { HttpResponse<String> response; try { response = Unirest.post(url).basicAuth(username, password).header("Accept", "application/json") .header("Content-Type", "application/json; charset=UTF-8") .body(new ObjectMapper().writeValueAsString(entity)).asString(); } catch (UnirestException | JsonProcessingException e) { throw new StorageException("Unable to execute entity request on url " + url, e); } if (response.getStatus() != 200) { throw new StorageException( "Wrong status code " + response.getStatus() + " when requesting entity url " + url); } try { entity = (T) new ObjectMapper().readValue(response.getBody(), entity.getClass()); } catch (UnsupportedOperationException | IOException e) { throw new StorageException("Unable to parse result from response while requesting url " + url, e); } return entity; }
postRequest = (basicAuth != null) ? postRequest.basicAuth(basicAuth.username, basicAuth.password) : postRequest;
/** * Returns the OAuth token to use when accessing Microcks. * * @throws MicrocksConnectorException */ private String getKeycloakOAuthToken() throws MicrocksConnectorException { String keycloakURL = getKeycloakURL(); String keycloakClientId = config.getMicrocksClientId(); String keycloakClientSecret = config.getMicrocksClientSecret(); // Retrieve a token using client_credentials flow. HttpRequestWithBody tokenRequest = Unirest.post(keycloakURL + "/protocol/openid-connect/token") .header("Content-Type", "application/x-www-form-urlencoded") .header("Accept", "application/json").basicAuth(keycloakClientId, keycloakClientSecret); HttpResponse<JsonNode> tokenResponse = null; try { tokenResponse = tokenRequest.body("grant_type=client_credentials").asJson(); } catch (UnirestException e) { logger.error("Exception while connecting to Keycloak backend", e); throw new MicrocksConnectorException( "Exception while connecting Microcks Keycloak backend. Check Keycloak configuration."); } if (tokenResponse.getStatus() != 200) { logger.error( "OAuth token cannot be retrieved for Microcks server, check keycloakClient configuration"); throw new MicrocksConnectorException( "OAuth token cannot be retrieved for Microcks. Check keycloakClient."); } return tokenResponse.getBody().getObject().getString("access_token"); }
.basicAuth(username, password).header("Accept", "application/json").field("file", file); if (path != null) { body.field("path", path);
public EmailResponse sendPlainText(Email email) { HttpRequestWithBody requestWithBody = Unirest.post("https://{url}/{version}/{domain}/messages") .routeParam("domain", mailGunSettings.getDomain()) .routeParam("url", mailGunSettings.getUrl()) .routeParam("version", mailGunSettings.getVersion()) .basicAuth("api", mailGunSettings.getApiKey()) .queryString("subject", email.getSubject()) .queryString("text", email.getMessage()); if (email.getFrom() != null) { requestWithBody.queryString("from", email.getFrom()); } else { requestWithBody.queryString("from", mailGunSettings.getFrom()); } if (mailGunSettings.isTestMode()) { requestWithBody.queryString("to", mailGunSettings.getTestModeTo()); } else { requestWithBody.queryString("to", email.getTo()); } if(mailGunSettings.isTestMode()) { requestWithBody.field("o:testmode", "true"); } return getEmailResponse(requestWithBody); }
.routeParam("url", mailGunSettings.getUrl()) .routeParam("version", mailGunSettings.getVersion()) .basicAuth("api", mailGunSettings.getApiKey());