Search in sources :

Example 96 with HttpEntity

use of org.springframework.http.HttpEntity in project cas by apereo.

the class RestPasswordManagementService method changeInternal.

@Override
public boolean changeInternal(final Credential c, final PasswordChangeBean bean) {
    final PasswordManagementProperties.Rest rest = properties.getRest();
    if (StringUtils.isBlank(rest.getEndpointUrlChange())) {
        return false;
    }
    final UsernamePasswordCredential upc = (UsernamePasswordCredential) c;
    final HttpHeaders headers = new HttpHeaders();
    headers.setAccept(CollectionUtils.wrap(MediaType.APPLICATION_JSON));
    headers.put("username", CollectionUtils.wrap(upc.getUsername()));
    headers.put("password", CollectionUtils.wrap(bean.getPassword()));
    headers.put("oldPassword", CollectionUtils.wrap(upc.getPassword()));
    final HttpEntity<String> entity = new HttpEntity<>(headers);
    final ResponseEntity<Boolean> result = restTemplate.exchange(rest.getEndpointUrlChange(), HttpMethod.POST, entity, Boolean.class);
    if (result.getStatusCodeValue() == HttpStatus.OK.value()) {
        return result.getBody();
    }
    return false;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) PasswordManagementProperties(org.apereo.cas.configuration.model.support.pm.PasswordManagementProperties) UsernamePasswordCredential(org.apereo.cas.authentication.UsernamePasswordCredential)

Example 97 with HttpEntity

use of org.springframework.http.HttpEntity in project dhis2-core by dhis2.

the class DefaultMonitoringService method pushMonitoringInfo.

@Override
public void pushMonitoringInfo() {
    String url = (String) systemSettingManager.getSystemSetting(SettingKey.SYSTEM_MONITORING_URL);
    String username = (String) systemSettingManager.getSystemSetting(SettingKey.SYSTEM_MONITORING_USERNAME);
    String password = (String) systemSettingManager.getSystemSetting(SettingKey.SYSTEM_MONITORING_PASSWORD);
    if (StringUtils.isBlank(url)) {
        log.debug("Monitoring service URL not configured, aborting monitoring request");
        return;
    }
    SystemInfo systemInfo = systemService.getSystemInfo();
    if (systemInfo == null) {
        log.warn("System info not available, aborting monitoring request");
        return;
    }
    systemInfo.clearSensitiveInfo();
    HttpHeadersBuilder headersBuilder = new HttpHeadersBuilder().withContentTypeJson();
    if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password)) {
        headersBuilder.withBasicAuth(username, password);
    }
    HttpEntity<SystemInfo> requestEntity = new HttpEntity<>(systemInfo, headersBuilder.build());
    ResponseEntity<String> response = null;
    HttpStatus sc = null;
    try {
        response = restTemplate.postForEntity(url, requestEntity, String.class);
        sc = response.getStatusCode();
    } catch (HttpClientErrorException | HttpServerErrorException ex) {
        log.warn("Monitoring request failed, status code: " + sc, ex);
        return;
    } catch (ResourceAccessException ex) {
        log.info("Monitoring request failed, network is unreachable");
        return;
    }
    if (response != null && sc != null && sc.is2xxSuccessful()) {
        log.debug("Monitoring request successfully sent");
    } else {
        log.warn("Monitoring request was unsuccessful, status code: " + sc);
    }
}
Also used : SystemInfo(org.hisp.dhis.system.SystemInfo) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) HttpEntity(org.springframework.http.HttpEntity) HttpStatus(org.springframework.http.HttpStatus) HttpHeadersBuilder(org.hisp.dhis.system.util.HttpHeadersBuilder) HttpServerErrorException(org.springframework.web.client.HttpServerErrorException) ResourceAccessException(org.springframework.web.client.ResourceAccessException)

Example 98 with HttpEntity

use of org.springframework.http.HttpEntity in project dhis2-core by dhis2.

the class ClickatellGateway method send.

@Override
public OutboundMessageResponse send(String subject, String text, Set<String> recipients, SmsGatewayConfig config) {
    ClickatellGatewayConfig clickatellConfiguration = (ClickatellGatewayConfig) config;
    HttpEntity<ClickatellRequestEntity> request = new HttpEntity<>(getRequestBody(text, recipients), getRequestHeaderParameters(clickatellConfiguration));
    HttpStatus httpStatus = send(clickatellConfiguration.getUrlTemplate() + MAX_MESSAGE_PART, request, ClickatellResponseEntity.class);
    return wrapHttpStatus(httpStatus);
}
Also used : ClickatellRequestEntity(org.hisp.dhis.sms.outbound.ClickatellRequestEntity) HttpEntity(org.springframework.http.HttpEntity) HttpStatus(org.springframework.http.HttpStatus)

Example 99 with HttpEntity

use of org.springframework.http.HttpEntity in project metron by apache.

the class RestTestingUtil method getPcapsByKeys.

/**
 * Gets the pcaps by keys.
 *
 * @param keys
 *          the keys
 * @return the pcaps by keys
 */
@SuppressWarnings("unchecked")
private static void getPcapsByKeys(String keys) {
    System.out.println("**********************getPcapsByKeys ******************************************************************************************");
    // 1.
    String url = "http://" + hostName + "/cisco-rest/pcapGetter/getPcapsByKeys?keys={keys}" + "&includeReverseTraffic={includeReverseTraffic}" + "&startTime={startTime}" + "&endTime={endTime}" + "&maxResponseSize={maxResponseSize}";
    // default values
    String startTime = "-1";
    String endTime = "-1";
    String maxResponseSize = "6";
    String includeReverseTraffic = "false";
    @SuppressWarnings("rawtypes") Map map = new HashMap();
    map.put("keys", keys);
    map.put("includeReverseTraffic", includeReverseTraffic);
    map.put("startTime", startTime);
    map.put("endTime", endTime);
    map.put("maxResponseSize", maxResponseSize);
    RestTemplate template = new RestTemplate();
    // set headers and entity to send
    HttpHeaders headers = new HttpHeaders();
    headers.set("Accept", MediaType.APPLICATION_OCTET_STREAM_VALUE);
    HttpEntity<Object> requestEntity = new HttpEntity<Object>(headers);
    // 1.
    ResponseEntity<byte[]> response1 = template.exchange(url, HttpMethod.GET, requestEntity, byte[].class, map);
    System.out.println("----------------------------------------------------------------------------------------------------");
    System.out.format("getPcapsByKeys : request= <keys=%s; includeReverseTraffic=%s; startTime=%s; endTime=%s; maxResponseSize=%s> \n response= %s \n", keys, includeReverseTraffic, startTime, endTime, maxResponseSize, response1);
    System.out.println("----------------------------------------------------------------------------------------------------");
    System.out.println();
    // 2. with reverse traffic
    includeReverseTraffic = "true";
    map.put("includeReverseTraffic", includeReverseTraffic);
    ResponseEntity<byte[]> response2 = template.exchange(url, HttpMethod.GET, requestEntity, byte[].class, map);
    System.out.println("----------------------------------------------------------------------------------------------------");
    System.out.format("getPcapsByKeys : request= <keys=%s; includeReverseTraffic=%s; startTime=%s; endTime=%s; maxResponseSize=%s> \n response= %s \n", keys, includeReverseTraffic, startTime, endTime, maxResponseSize, response2);
    System.out.println("----------------------------------------------------------------------------------------------------");
    System.out.println();
    // 3.with time range
    startTime = System.getProperty("startTime", "-1");
    endTime = System.getProperty("endTime", "-1");
    map.put("startTime", startTime);
    map.put("endTime", endTime);
    ResponseEntity<byte[]> response3 = template.exchange(url, HttpMethod.GET, requestEntity, byte[].class, map);
    System.out.println("----------------------------------------------------------------------------------------------------");
    System.out.format("getPcapsByKeys : request= <keys=%s; includeReverseTraffic=%s; startTime=%s; endTime=%s; maxResponseSize=%s> \n response= %s \n", keys, includeReverseTraffic, startTime, endTime, maxResponseSize, response3);
    System.out.println("----------------------------------------------------------------------------------------------------");
    System.out.println();
    // 4.with maxResponseSize
    maxResponseSize = System.getProperty("maxResponseSize", "6");
    map.put("maxResponseSize", maxResponseSize);
    ResponseEntity<byte[]> response4 = template.exchange(url, HttpMethod.GET, requestEntity, byte[].class, map);
    System.out.println("----------------------------------------------------------------------------------------------------");
    System.out.format("getPcapsByKeys : request= <keys=%s; includeReverseTraffic=%s; startTime=%s; endTime=%s; maxResponseSize=%s> \n response= %s \n", keys, includeReverseTraffic, startTime, endTime, maxResponseSize, response4);
    System.out.println("----------------------------------------------------------------------------------------------------");
    System.out.println();
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) HashMap(java.util.HashMap) RestTemplate(org.springframework.web.client.RestTemplate) Map(java.util.Map) HashMap(java.util.HashMap)

Example 100 with HttpEntity

use of org.springframework.http.HttpEntity in project mica2 by obiba.

the class MailService method sendEmail.

private synchronized void sendEmail(String subject, String templateName, Map<String, String> context, String recipient) {
    try {
        RestTemplate template = newRestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.set(APPLICATION_AUTH_HEADER, getApplicationAuth());
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        StringBuilder form = new StringBuilder(Strings.isNullOrEmpty(recipient) ? "" : recipient + "&");
        form.append("subject=").append(encode(subject, "UTF-8")).append("&template=").append(encode(templateName, "UTF-8"));
        context.forEach((k, v) -> {
            try {
                form.append("&").append(k).append("=").append(encode(v, "UTF-8"));
            } catch (UnsupportedEncodingException ignored) {
            }
        });
        HttpEntity<String> entity = new HttpEntity<>(form.toString(), headers);
        ResponseEntity<String> response = template.exchange(getNotificationsUrl(), HttpMethod.POST, entity, String.class);
        if (response.getStatusCode().is2xxSuccessful()) {
            log.info("Email sent via Agate");
        } else {
            log.error("Sending email via Agate failed with status: {}", response.getStatusCode());
        }
    } catch (Exception e) {
        log.error("Agate connection failure: {}", e.getMessage());
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) RestTemplate(org.springframework.web.client.RestTemplate) UnsupportedEncodingException(java.io.UnsupportedEncodingException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

HttpEntity (org.springframework.http.HttpEntity)104 HttpHeaders (org.springframework.http.HttpHeaders)81 Test (org.junit.Test)46 RestTemplate (org.springframework.web.client.RestTemplate)17 URI (java.net.URI)15 ArrayList (java.util.ArrayList)13 ResponseEntity (org.springframework.http.ResponseEntity)12 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)12 Map (java.util.Map)11 MediaType (org.springframework.http.MediaType)11 HashMap (java.util.HashMap)10 List (java.util.List)10 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)10 MultiValueMap (org.springframework.util.MultiValueMap)9 ByteArrayInputStream (java.io.ByteArrayInputStream)7 ByteArrayHttpMessageConverter (org.springframework.http.converter.ByteArrayHttpMessageConverter)7 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)7 HttpStatus (org.springframework.http.HttpStatus)6 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)6 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)6