use of org.springframework.http.HttpEntity in project cas by apereo.
the class RestGoogleAuthenticatorTokenCredentialRepository method save.
@Override
public void save(final String userName, final String secretKey, final int validationCode, final List<Integer> scratchCodes) {
final MultifactorAuthenticationProperties.GAuth.Rest rest = gauth.getRest();
final HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
headers.put("username", Arrays.asList(userName));
headers.put("validationCode", Arrays.asList(String.valueOf(validationCode)));
headers.put("secretKey", Arrays.asList(secretKey));
headers.put("scratchCodes", scratchCodes.stream().map(String::valueOf).collect(Collectors.toList()));
final HttpEntity<String> entity = new HttpEntity<>(headers);
final ResponseEntity<Boolean> result = restTemplate.exchange(rest.getEndpointUrl(), HttpMethod.POST, entity, Boolean.class);
if (result.getStatusCodeValue() == HttpStatus.OK.value()) {
LOGGER.debug("Posted google authenticator account successfully");
}
LOGGER.warn("Failed to save google authenticator account successfully");
}
use of org.springframework.http.HttpEntity in project cas by apereo.
the class RestGoogleAuthenticatorTokenCredentialRepository method getSecret.
@Override
public String getSecret(final String username) {
final MultifactorAuthenticationProperties.GAuth.Rest rest = gauth.getRest();
final HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
headers.put("username", Arrays.asList(username));
final HttpEntity<String> entity = new HttpEntity<>(headers);
final ResponseEntity<String> result = restTemplate.exchange(rest.getEndpointUrl(), HttpMethod.GET, entity, String.class);
if (result.getStatusCodeValue() == HttpStatus.OK.value()) {
return result.getBody();
}
return null;
}
use of org.springframework.http.HttpEntity in project spring-boot-admin by codecentric.
the class HipchatNotifierTest method test_onApplicationEvent_resolve.
@Test
public void test_onApplicationEvent_resolve() {
StatusInfo infoDown = StatusInfo.ofDown();
StatusInfo infoUp = StatusInfo.ofUp();
@SuppressWarnings("unchecked") ArgumentCaptor<HttpEntity<Map<String, Object>>> httpRequest = ArgumentCaptor.forClass((Class<HttpEntity<Map<String, Object>>>) (Class<?>) HttpEntity.class);
when(restTemplate.postForEntity(isA(String.class), httpRequest.capture(), eq(Void.class))).thenReturn(ResponseEntity.ok((Void) null));
notifier.notify(new ClientApplicationStatusChangedEvent(Application.create("App").withId("-id-").withHealthUrl("http://health").build(), infoDown, infoUp));
assertThat(httpRequest.getValue().getHeaders()).containsEntry("Content-Type", asList("application/json"));
Map<String, Object> body = httpRequest.getValue().getBody();
assertThat(body).containsEntry("color", "green");
assertThat(body).containsEntry("message", "<strong>App</strong>/-id- is <strong>UP</strong>");
assertThat(body).containsEntry("notify", Boolean.TRUE);
assertThat(body).containsEntry("message_format", "html");
}
use of org.springframework.http.HttpEntity in project zeppelin by apache.
the class BaseLivyInterprereter method callRestAPI.
private String callRestAPI(String targetURL, String method, String jsonData) throws LivyException {
targetURL = livyURL + targetURL;
LOGGER.debug("Call rest api in {}, method: {}, jsonData: {}", targetURL, method, jsonData);
RestTemplate restTemplate = getRestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/json");
headers.add("X-Requested-By", "zeppelin");
ResponseEntity<String> response = null;
try {
if (method.equals("POST")) {
HttpEntity<String> entity = new HttpEntity<>(jsonData, headers);
response = restTemplate.exchange(targetURL, HttpMethod.POST, entity, String.class);
} else if (method.equals("GET")) {
HttpEntity<String> entity = new HttpEntity<>(headers);
response = restTemplate.exchange(targetURL, HttpMethod.GET, entity, String.class);
} else if (method.equals("DELETE")) {
HttpEntity<String> entity = new HttpEntity<>(headers);
response = restTemplate.exchange(targetURL, HttpMethod.DELETE, entity, String.class);
}
} catch (HttpClientErrorException e) {
response = new ResponseEntity(e.getResponseBodyAsString(), e.getStatusCode());
LOGGER.error(String.format("Error with %s StatusCode: %s", response.getStatusCode().value(), e.getResponseBodyAsString()));
} catch (RestClientException e) {
// Exception happens when kerberos is enabled.
if (e.getCause() instanceof HttpClientErrorException) {
HttpClientErrorException cause = (HttpClientErrorException) e.getCause();
if (cause.getResponseBodyAsString().matches(SESSION_NOT_FOUND_PATTERN)) {
throw new SessionNotFoundException(cause.getResponseBodyAsString());
}
}
throw new LivyException(e);
}
if (response == null) {
throw new LivyException("No http response returned");
}
LOGGER.debug("Get response, StatusCode: {}, responseBody: {}", response.getStatusCode(), response.getBody());
if (response.getStatusCode().value() == 200 || response.getStatusCode().value() == 201) {
return response.getBody();
} else if (response.getStatusCode().value() == 404) {
if (response.getBody().matches(SESSION_NOT_FOUND_PATTERN)) {
throw new SessionNotFoundException(response.getBody());
} else {
throw new APINotFoundException("No rest api found for " + targetURL + ", " + response.getStatusCode());
}
} else {
String responseString = response.getBody();
if (responseString.contains("CreateInteractiveRequest[\\\"master\\\"]")) {
return responseString;
}
throw new LivyException(String.format("Error with %s StatusCode: %s", response.getStatusCode().value(), responseString));
}
}
use of org.springframework.http.HttpEntity in project spring-framework by spring-projects.
the class FormHttpMessageConverterTests method writeMultipartOrder.
// SPR-13309
@Test
public void writeMultipartOrder() throws Exception {
MyBean myBean = new MyBean();
myBean.setString("foo");
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>();
parts.add("part1", myBean);
HttpHeaders entityHeaders = new HttpHeaders();
entityHeaders.setContentType(MediaType.TEXT_XML);
HttpEntity<MyBean> entity = new HttpEntity<>(myBean, entityHeaders);
parts.add("part2", entity);
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
this.converter.setMultipartCharset(StandardCharsets.UTF_8);
this.converter.write(parts, new MediaType("multipart", "form-data", StandardCharsets.UTF_8), outputMessage);
final MediaType contentType = outputMessage.getHeaders().getContentType();
assertNotNull("No boundary found", contentType.getParameter("boundary"));
// see if Commons FileUpload can read what we wrote
FileItemFactory fileItemFactory = new DiskFileItemFactory();
FileUpload fileUpload = new FileUpload(fileItemFactory);
RequestContext requestContext = new MockHttpOutputMessageRequestContext(outputMessage);
List<FileItem> items = fileUpload.parseRequest(requestContext);
assertEquals(2, items.size());
FileItem item = items.get(0);
assertTrue(item.isFormField());
assertEquals("part1", item.getFieldName());
assertEquals("{\"string\":\"foo\"}", item.getString());
item = items.get(1);
assertTrue(item.isFormField());
assertEquals("part2", item.getFieldName());
// With developer builds we get: <MyBean><string>foo</string></MyBean>
// But on CI server we get: <MyBean xmlns=""><string>foo</string></MyBean>
// So... we make a compromise:
assertThat(item.getString(), allOf(startsWith("<MyBean"), endsWith("><string>foo</string></MyBean>")));
}
Aggregations