Search in sources :

Example 6 with RequestAttributes

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

the class FrameworkServlet method processRequest.

/**
	 * Process this request, publishing an event regardless of the outcome.
	 * <p>The actual event handling is performed by the abstract
	 * {@link #doService} template method.
	 */
protected final void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    long startTime = System.currentTimeMillis();
    Throwable failureCause = null;
    LocaleContext previousLocaleContext = LocaleContextHolder.getLocaleContext();
    LocaleContext localeContext = buildLocaleContext(request);
    RequestAttributes previousAttributes = RequestContextHolder.getRequestAttributes();
    ServletRequestAttributes requestAttributes = buildRequestAttributes(request, response, previousAttributes);
    WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
    asyncManager.registerCallableInterceptor(FrameworkServlet.class.getName(), new RequestBindingInterceptor());
    initContextHolders(request, localeContext, requestAttributes);
    try {
        doService(request, response);
    } catch (ServletException ex) {
        failureCause = ex;
        throw ex;
    } catch (IOException ex) {
        failureCause = ex;
        throw ex;
    } catch (Throwable ex) {
        failureCause = ex;
        throw new NestedServletException("Request processing failed", ex);
    } finally {
        resetContextHolders(request, previousLocaleContext, previousAttributes);
        if (requestAttributes != null) {
            requestAttributes.requestCompleted();
        }
        if (logger.isDebugEnabled()) {
            if (failureCause != null) {
                this.logger.debug("Could not complete request", failureCause);
            } else {
                if (asyncManager.isConcurrentHandlingStarted()) {
                    logger.debug("Leaving response open for concurrent processing");
                } else {
                    this.logger.debug("Successfully completed request");
                }
            }
        }
        publishRequestHandledEvent(request, response, startTime, failureCause);
    }
}
Also used : WebAsyncManager(org.springframework.web.context.request.async.WebAsyncManager) ServletException(javax.servlet.ServletException) NestedServletException(org.springframework.web.util.NestedServletException) SimpleLocaleContext(org.springframework.context.i18n.SimpleLocaleContext) LocaleContext(org.springframework.context.i18n.LocaleContext) NestedServletException(org.springframework.web.util.NestedServletException) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) IOException(java.io.IOException)

Example 7 with RequestAttributes

use of org.springframework.web.context.request.RequestAttributes in project grails-core by grails.

the class LayoutWriterStack method currentStack.

private static LayoutWriterStack currentStack() {
    RequestAttributes attributes = RequestContextHolder.currentRequestAttributes();
    if (attributes != null) {
        LayoutWriterStack stack = (LayoutWriterStack) attributes.getAttribute(ATTRIBUTE_NAME_WRITER_STACK, RequestAttributes.SCOPE_REQUEST);
        if (stack == null) {
            stack = new LayoutWriterStack();
            attributes.setAttribute(ATTRIBUTE_NAME_WRITER_STACK, stack, RequestAttributes.SCOPE_REQUEST);
        }
        return stack;
    }
    return null;
}
Also used : RequestAttributes(org.springframework.web.context.request.RequestAttributes)

Example 8 with RequestAttributes

use of org.springframework.web.context.request.RequestAttributes in project weixin-java-mp-demo-springboot by binarywang.

the class WxErrorController method getErrorAttributes.

private Map<String, Object> getErrorAttributes(HttpServletRequest request, boolean includeStackTrace) {
    RequestAttributes requestAttributes = new ServletRequestAttributes(request);
    Map<String, Object> map = this.errorAttributes.getErrorAttributes(requestAttributes, includeStackTrace);
    logger.error("map is [{}]", map);
    String url = request.getRequestURL().toString();
    map.put("URL", url);
    logger.error("[error info]: status-{}, request url-{}", map.get("status"), url);
    return map;
}
Also used : ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes)

Example 9 with RequestAttributes

use of org.springframework.web.context.request.RequestAttributes in project dq-easy-cloud by dq-open-cloud.

the class DqLogBO method buildRequestPath.

/**
 * <p>
 * 构建请求路径
 * </p>
 *
 * @return
 * @author daiqi
 * 创建时间    2018年2月9日 下午4:46:14
 */
private DqLogBO buildRequestPath() {
    RequestAttributes ra = RequestContextHolder.getRequestAttributes();
    ServletRequestAttributes sra = (ServletRequestAttributes) ra;
    HttpServletRequest request = sra.getRequest();
    String requestPath = request.getServletPath().toString();
    if (DqStringUtils.isEmpty(requestPath)) {
        requestPath = request.getPathInfo();
    }
    if (DqStringUtils.isEmpty(requestPath)) {
        requestPath = request.getRequestURI();
    }
    return buildRequestPath(requestPath);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes)

Example 10 with RequestAttributes

use of org.springframework.web.context.request.RequestAttributes in project topcom-cloud by 545314690.

the class MyWebAppConfigurer method extendMessageConverters.

/**
 * 添加返回结果缩进支持,如果存在pretty参数,则返回结果添加缩进
 * @param converters
 */
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
    converters.replaceAll(c -> {
        if (c instanceof MappingJackson2HttpMessageConverter) {
            MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(mapper) {

                @Override
                protected void writePrefix(JsonGenerator generator, Object object) throws IOException {
                    RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
                    if (attributes != null && attributes instanceof ServletRequestAttributes) {
                        String attribute = ((ServletRequestAttributes) attributes).getRequest().getParameter("pretty");
                        if (attribute != null) {
                            generator.setPrettyPrinter(new DefaultPrettyPrinter());
                        }
                    }
                    super.writePrefix(generator, object);
                }
            };
            return converter;
        } else {
            return c;
        }
    });
}
Also used : DefaultPrettyPrinter(com.fasterxml.jackson.core.util.DefaultPrettyPrinter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes)

Aggregations

RequestAttributes (org.springframework.web.context.request.RequestAttributes)76 ServletRequestAttributes (org.springframework.web.context.request.ServletRequestAttributes)46 HttpServletRequest (javax.servlet.http.HttpServletRequest)15 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)15 Test (org.junit.Test)11 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)3 DefaultPrettyPrinter (com.fasterxml.jackson.core.util.DefaultPrettyPrinter)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Response (javax.ws.rs.core.Response)3 XWorkRequestAttributes (org.onebusaway.presentation.impl.users.XWorkRequestAttributes)3 DBUnitTest (org.orcid.test.DBUnitTest)3 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)3 ProjectService (ca.corefacility.bioinformatics.irida.service.ProjectService)2 UserService (ca.corefacility.bioinformatics.irida.service.user.UserService)2 UserSession (com.haulmont.cuba.security.global.UserSession)2 ActionContext (com.opensymphony.xwork2.ActionContext)2 RecordDefinition (com.revolsys.record.schema.RecordDefinition)2 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)2