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;
}
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);
}
}
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);
}
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();
}
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());
}
}
Aggregations