Search in sources :

Example 1 with ServletWebRequest

use of org.springframework.web.context.request.ServletWebRequest in project ORCID-Source by ORCID.

the class OrcidOAuth2AuthenticationEntryPoint method handleAsOrcidError.

public void handleAsOrcidError(HttpServletRequest request, HttpServletResponse response, Exception authException) throws IOException, ServletException {
    try {
        ResponseEntity<OAuth2Exception> result = exceptionTranslator.translate(authException);
        result = enhanceResponse(result, authException);
        OrcidError orcidError = new OrcidError();
        orcidError.setResponseCode(result.getStatusCode().value());
        orcidError.setDeveloperMessage(result.getBody().getLocalizedMessage());
        ResponseEntity<OrcidError> errorResponseEntity = new ResponseEntity<>(orcidError, result.getHeaders(), result.getStatusCode());
        exceptionRenderer.handleHttpEntityResponse(errorResponseEntity, new ServletWebRequest(request, response));
        response.flushBuffer();
    } catch (ServletException e) {
        // (even if there is one)
        if (handlerExceptionResolver.resolveException(request, response, this, e) == null) {
            throw e;
        }
    } catch (IOException e) {
        throw e;
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        // Wrap other Exceptions. These are not expected to happen
        throw new RuntimeException(e);
    }
}
Also used : ServletException(javax.servlet.ServletException) OrcidError(org.orcid.jaxb.model.error_v2.OrcidError) ResponseEntity(org.springframework.http.ResponseEntity) IOException(java.io.IOException) OAuth2Exception(org.springframework.security.oauth2.common.exceptions.OAuth2Exception) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) AccessDeniedException(org.springframework.security.access.AccessDeniedException) OAuth2Exception(org.springframework.security.oauth2.common.exceptions.OAuth2Exception) AuthenticationException(org.springframework.security.core.AuthenticationException)

Example 2 with ServletWebRequest

use of org.springframework.web.context.request.ServletWebRequest in project spring-framework by spring-projects.

the class RequestHeaderMapMethodArgumentResolverTests method setUp.

@Before
public void setUp() throws Exception {
    resolver = new RequestHeaderMapMethodArgumentResolver();
    Method method = getClass().getMethod("params", Map.class, MultiValueMap.class, HttpHeaders.class, Map.class);
    paramMap = new SynthesizingMethodParameter(method, 0);
    paramMultiValueMap = new SynthesizingMethodParameter(method, 1);
    paramHttpHeaders = new SynthesizingMethodParameter(method, 2);
    paramUnsupported = new SynthesizingMethodParameter(method, 3);
    request = new MockHttpServletRequest();
    webRequest = new ServletWebRequest(request, new MockHttpServletResponse());
}
Also used : SynthesizingMethodParameter(org.springframework.core.annotation.SynthesizingMethodParameter) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Method(java.lang.reflect.Method) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Before(org.junit.Before)

Example 3 with ServletWebRequest

use of org.springframework.web.context.request.ServletWebRequest in project spring-framework by spring-projects.

the class HttpEntityMethodProcessor method isResourceNotModified.

private boolean isResourceNotModified(ServletServerHttpRequest inputMessage, ServletServerHttpResponse outputMessage) {
    ServletWebRequest servletWebRequest = new ServletWebRequest(inputMessage.getServletRequest(), outputMessage.getServletResponse());
    HttpHeaders responseHeaders = outputMessage.getHeaders();
    String etag = responseHeaders.getETag();
    long lastModifiedTimestamp = responseHeaders.getLastModified();
    if (inputMessage.getMethod() == HttpMethod.GET || inputMessage.getMethod() == HttpMethod.HEAD) {
        responseHeaders.remove(HttpHeaders.ETAG);
        responseHeaders.remove(HttpHeaders.LAST_MODIFIED);
    }
    return servletWebRequest.checkNotModified(etag, lastModifiedTimestamp);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest)

Example 4 with ServletWebRequest

use of org.springframework.web.context.request.ServletWebRequest in project spring-framework by spring-projects.

the class RequestMappingInfoHandlerMappingTests method testHttpOptions.

private void testHttpOptions(String requestURI, String allowHeader) throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("OPTIONS", requestURI);
    HandlerMethod handlerMethod = getHandler(request);
    ServletWebRequest webRequest = new ServletWebRequest(request);
    ModelAndViewContainer mavContainer = new ModelAndViewContainer();
    Object result = new InvocableHandlerMethod(handlerMethod).invokeForRequest(webRequest, mavContainer);
    assertNotNull(result);
    assertEquals(HttpHeaders.class, result.getClass());
    assertEquals(allowHeader, ((HttpHeaders) result).getFirst("Allow"));
}
Also used : ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) InvocableHandlerMethod(org.springframework.web.method.support.InvocableHandlerMethod) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) InvocableHandlerMethod(org.springframework.web.method.support.InvocableHandlerMethod) HandlerMethod(org.springframework.web.method.HandlerMethod)

Example 5 with ServletWebRequest

use of org.springframework.web.context.request.ServletWebRequest in project thymeleaf-tests by thymeleaf.

the class DecoupledSTSMTest method test.

private static void test(final String templateName, final String expectedResultLocation, final Map<String, Object> vars) throws Exception {
    final Map<String, Object> requestAttributes = new LinkedHashMap<String, Object>();
    final Map<String, Object[]> requestParameters = new LinkedHashMap<String, Object[]>();
    final HttpServletRequest mockRequest = TestMockServletUtil.createHttpServletRequest("WebVariablesMap", null, requestAttributes, requestParameters, Locale.US);
    final HttpServletResponse mockResponse = TestMockServletUtil.createHttpServletResponse();
    final Map<String, Object> servletContextAttributes = new LinkedHashMap<String, Object>();
    final ServletContext mockServletContext = TestMockServletUtil.createServletContext(servletContextAttributes);
    final XmlWebApplicationContext appCtx = new XmlWebApplicationContext();
    appCtx.setServletContext(mockServletContext);
    appCtx.setConfigLocation("classpath:templateparser/decoupled/stsm/applicationContext.xml");
    appCtx.refresh();
    mockServletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, appCtx);
    // can be null!
    final ConversionService conversionService = getConversionService(appCtx);
    mockRequest.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, appCtx);
    final ServletWebRequest servletWebRequest = new ServletWebRequest(mockRequest, mockResponse);
    RequestContextHolder.setRequestAttributes(servletWebRequest);
    final RequestContext requestContext = new RequestContext(mockRequest, mockResponse, mockServletContext, vars);
    vars.put(AbstractTemplateView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE, requestContext);
    vars.put(SpringContextVariableNames.SPRING_REQUEST_CONTEXT, requestContext);
    vars.put(SpringContextVariableNames.THYMELEAF_REQUEST_CONTEXT, new SpringWebMvcThymeleafRequestContext(requestContext, mockRequest));
    final List<String> variableNames = new ArrayList<String>(vars.keySet());
    for (final String variableName : variableNames) {
        final Object bindingObject = vars.get(variableName);
        if (isBindingCandidate(variableName, bindingObject)) {
            final String bindingVariableName = BindingResult.MODEL_KEY_PREFIX + variableName;
            if (!vars.containsKey(bindingVariableName)) {
                final WebDataBinder dataBinder = new WebDataBinder(bindingObject, bindingVariableName);
                dataBinder.setConversionService(conversionService);
                vars.put(bindingVariableName, dataBinder.getBindingResult());
            }
        }
    }
    final WebContext ctx = new WebContext(mockRequest, mockResponse, mockServletContext, Locale.US);
    for (final Map.Entry<String, Object> entry : vars.entrySet()) {
        ctx.setVariable(entry.getKey(), entry.getValue());
    }
    final ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
    templateResolver.setTemplateMode("HTML");
    templateResolver.setPrefix("templateparser/decoupled/stsm/templates/");
    templateResolver.setSuffix(".html");
    templateResolver.setUseDecoupledLogic(true);
    final SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    templateEngine.setTemplateResolver(templateResolver);
    templateEngine.setLinkBuilder(new StandardLinkBuilder() {

        @Override
        protected String computeContextPath(final IExpressionContext context, final String base, final Map<String, Object> parameters) {
            return "";
        }
    });
    appCtx.getAutowireCapableBeanFactory().initializeBean(templateEngine, "templateEngine");
    final String result = templateEngine.process(templateName, ctx);
    final String expected = ResourceUtils.read(ClassLoaderUtils.getClassLoader(DecoupledSTSMTest.class).getResourceAsStream(expectedResultLocation), "ISO-8859-1", true);
    Assert.assertEquals(ResourceUtils.normalize(expected), ResourceUtils.normalize(result));
}
Also used : SpringTemplateEngine(org.thymeleaf.spring5.SpringTemplateEngine) StandardLinkBuilder(org.thymeleaf.linkbuilder.StandardLinkBuilder) WebContext(org.thymeleaf.context.WebContext) SpringWebMvcThymeleafRequestContext(org.thymeleaf.spring5.context.webmvc.SpringWebMvcThymeleafRequestContext) ArrayList(java.util.ArrayList) HttpServletResponse(javax.servlet.http.HttpServletResponse) LinkedHashMap(java.util.LinkedHashMap) HttpServletRequest(javax.servlet.http.HttpServletRequest) WebDataBinder(org.springframework.web.bind.WebDataBinder) ClassLoaderTemplateResolver(org.thymeleaf.templateresolver.ClassLoaderTemplateResolver) XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) IExpressionContext(org.thymeleaf.context.IExpressionContext) ConversionService(org.springframework.core.convert.ConversionService) ServletContext(javax.servlet.ServletContext) RequestContext(org.springframework.web.servlet.support.RequestContext) SpringWebMvcThymeleafRequestContext(org.thymeleaf.spring5.context.webmvc.SpringWebMvcThymeleafRequestContext) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)224 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)80 Test (org.junit.jupiter.api.Test)79 MethodParameter (org.springframework.core.MethodParameter)50 BeforeEach (org.junit.jupiter.api.BeforeEach)41 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)32 ModelAndViewContainer (org.springframework.web.method.support.ModelAndViewContainer)30 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)28 Method (java.lang.reflect.Method)21 Test (org.junit.Test)21 MockMultipartHttpServletRequest (org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest)21 MockMultipartFile (org.springframework.web.testfixture.servlet.MockMultipartFile)18 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)14 TestBean (org.springframework.beans.testfixture.beans.TestBean)14 RequestParam (org.springframework.web.bind.annotation.RequestParam)14 MockPart (org.springframework.web.testfixture.servlet.MockPart)14 HttpServletRequest (javax.servlet.http.HttpServletRequest)13 MultipartFile (org.springframework.web.multipart.MultipartFile)13 SynthesizingMethodParameter (org.springframework.core.annotation.SynthesizingMethodParameter)11 IOException (java.io.IOException)10