use of org.springframework.http.HttpStatus in project spring-framework by spring-projects.
the class MockMvcClientHttpRequestFactory method createRequest.
@Override
public ClientHttpRequest createRequest(final URI uri, final HttpMethod httpMethod) throws IOException {
return new MockClientHttpRequest(httpMethod, uri) {
@Override
public ClientHttpResponse executeInternal() throws IOException {
try {
MockHttpServletRequestBuilder requestBuilder = request(httpMethod, uri);
requestBuilder.content(getBodyAsBytes());
requestBuilder.headers(getHeaders());
MvcResult mvcResult = MockMvcClientHttpRequestFactory.this.mockMvc.perform(requestBuilder).andReturn();
MockHttpServletResponse servletResponse = mvcResult.getResponse();
HttpStatus status = HttpStatus.valueOf(servletResponse.getStatus());
byte[] body = servletResponse.getContentAsByteArray();
HttpHeaders headers = getResponseHeaders(servletResponse);
MockClientHttpResponse clientResponse = new MockClientHttpResponse(body, status);
clientResponse.getHeaders().putAll(headers);
return clientResponse;
} catch (Exception ex) {
byte[] body = ex.toString().getBytes(StandardCharsets.UTF_8);
return new MockClientHttpResponse(body, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
};
}
use of org.springframework.http.HttpStatus in project spring-security-oauth by spring-projects.
the class AuthorizationCodeGrantTests method testTokenAcquisitionWithRegisteredRedirect.
@Test
public void testTokenAcquisitionWithRegisteredRedirect() throws Exception {
ResponseEntity<String> page = serverRunning.getForString("/tonr2/login.jsp");
String cookie = page.getHeaders().getFirst("Set-Cookie");
HttpHeaders headers = new HttpHeaders();
headers.set("Cookie", cookie);
Matcher matcher = Pattern.compile("(?s).*name=\"_csrf\".*?value=\"([^\"]+).*").matcher(page.getBody());
MultiValueMap<String, String> form;
form = new LinkedMultiValueMap<String, String>();
form.add("username", "marissa");
form.add("password", "wombat");
if (matcher.matches()) {
form.add("_csrf", matcher.group(1));
}
ResponseEntity<Void> response = serverRunning.postForStatus("/tonr2/login", headers, form);
cookie = response.getHeaders().getFirst("Set-Cookie");
headers = new HttpHeaders();
headers.set("Cookie", cookie);
// The registered redirect is /redirect, but /trigger is used as a test
// because it is different (i.e. not the current request URI).
String location = serverRunning.getForRedirect("/tonr2/sparklr/trigger", headers);
location = authenticateAndApprove(location);
assertTrue("Redirect location should be to the original photo URL: " + location, location.contains("sparklr/redirect"));
HttpStatus status = serverRunning.getStatusCode(location, headers);
assertEquals(HttpStatus.OK, status);
}
use of org.springframework.http.HttpStatus in project spring-framework by spring-projects.
the class ViewControllerBeanDefinitionParser method parse.
@Override
@SuppressWarnings("unchecked")
public BeanDefinition parse(Element element, ParserContext parserContext) {
Object source = parserContext.extractSource(element);
// Register SimpleUrlHandlerMapping for view controllers
BeanDefinition hm = registerHandlerMapping(parserContext, source);
// Ensure BeanNameUrlHandlerMapping (SPR-8289) and default HandlerAdapters are not "turned off"
MvcNamespaceUtils.registerDefaultComponents(parserContext, source);
// Create view controller bean definition
RootBeanDefinition controller = new RootBeanDefinition(ParameterizableViewController.class);
controller.setSource(source);
HttpStatus statusCode = null;
if (element.hasAttribute("status-code")) {
int statusValue = Integer.valueOf(element.getAttribute("status-code"));
statusCode = HttpStatus.valueOf(statusValue);
}
String name = element.getLocalName();
if (name.equals("view-controller")) {
if (element.hasAttribute("view-name")) {
controller.getPropertyValues().add("viewName", element.getAttribute("view-name"));
}
if (statusCode != null) {
controller.getPropertyValues().add("statusCode", statusCode);
}
} else if (name.equals("redirect-view-controller")) {
controller.getPropertyValues().add("view", getRedirectView(element, statusCode, source));
} else if (name.equals("status-controller")) {
controller.getPropertyValues().add("statusCode", statusCode);
controller.getPropertyValues().add("statusOnly", true);
} else {
// Should never happen...
throw new IllegalStateException("Unexpected tag name: " + name);
}
Map<String, BeanDefinition> urlMap;
if (hm.getPropertyValues().contains("urlMap")) {
urlMap = (Map<String, BeanDefinition>) hm.getPropertyValues().getPropertyValue("urlMap").getValue();
} else {
urlMap = new ManagedMap<>();
hm.getPropertyValues().add("urlMap", urlMap);
}
urlMap.put(element.getAttribute("path"), controller);
return null;
}
use of org.springframework.http.HttpStatus in project weixin-java-pay-demo by binarywang.
the class WxErrorController method error.
/**
* Supports other formats like JSON, XML
*
* @param request
*/
@RequestMapping(value = ERROR_PATH)
@ResponseBody
public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
Map<String, Object> body = this.getErrorAttributes(request, this.getTraceParameter(request));
HttpStatus status = this.getStatus(request);
return new ResponseEntity<>(body, status);
}
use of org.springframework.http.HttpStatus in project weixin-java-mp-demo-springboot by binarywang.
the class WxErrorController method error.
/**
* Supports other formats like JSON, XML
*
* @param request
*/
@RequestMapping(value = ERROR_PATH)
@ResponseBody
public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
Map<String, Object> body = this.getErrorAttributes(request, this.getTraceParameter(request));
HttpStatus status = this.getStatus(request);
return new ResponseEntity<>(body, status);
}
Aggregations