Search in sources :

Example 6 with HttpStatus

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);
            }
        }
    };
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpStatus(org.springframework.http.HttpStatus) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) MockClientHttpRequest(org.springframework.mock.http.client.MockClientHttpRequest) MvcResult(org.springframework.test.web.servlet.MvcResult) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) IOException(java.io.IOException) MockClientHttpResponse(org.springframework.mock.http.client.MockClientHttpResponse)

Example 7 with HttpStatus

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);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) Matcher(java.util.regex.Matcher) HttpStatus(org.springframework.http.HttpStatus) Test(org.junit.Test)

Example 8 with HttpStatus

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;
}
Also used : HttpStatus(org.springframework.http.HttpStatus) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 9 with HttpStatus

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);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) HttpStatus(org.springframework.http.HttpStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 10 with HttpStatus

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);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) HttpStatus(org.springframework.http.HttpStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

HttpStatus (org.springframework.http.HttpStatus)161 ResponseEntity (org.springframework.http.ResponseEntity)39 HttpHeaders (org.springframework.http.HttpHeaders)35 Test (org.junit.jupiter.api.Test)26 MediaType (org.springframework.http.MediaType)17 Mono (reactor.core.publisher.Mono)17 IOException (java.io.IOException)16 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)14 Collections (java.util.Collections)13 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)13 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)13 URI (java.net.URI)12 List (java.util.List)11 Optional (java.util.Optional)11 Test (org.junit.Test)11 Map (java.util.Map)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 ClassPathResource (org.springframework.core.io.ClassPathResource)9 Resource (org.springframework.core.io.Resource)9 HashMap (java.util.HashMap)8