Search in sources :

Example 36 with RequestAttributes

use of org.springframework.web.context.request.RequestAttributes in project midpoint by Evolveum.

the class SecurityUtil method getCurrentConnectionInformation.

/**
 * Returns current connection information, as derived from HTTP request stored in current thread.
 * May be null if the thread is not associated with any HTTP request (e.g. task threads, operations invoked from GUI but executing in background).
 */
public static HttpConnectionInformation getCurrentConnectionInformation() {
    RequestAttributes attr = RequestContextHolder.getRequestAttributes();
    if (!(attr instanceof ServletRequestAttributes)) {
        return null;
    }
    ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) attr;
    HttpServletRequest request = servletRequestAttributes.getRequest();
    HttpConnectionInformation rv = new HttpConnectionInformation();
    HttpSession session = request.getSession(false);
    if (session != null) {
        rv.setSessionId(session.getId());
    }
    long start = System.currentTimeMillis();
    rv.setLocalHostName(request.getLocalName());
    long delta = System.currentTimeMillis() - start;
    if (delta > GET_LOCAL_NAME_THRESHOLD) {
        LOGGER.warn("getLocalName() on HTTP request took {} milliseconds that is too long; " + "please check your DNS configuration. Local name = {}, local address = {}", delta, request.getLocalName(), request.getLocalAddr());
    }
    rv.setRemoteHostAddress(getRemoteHostAddress(request));
    rv.setServerName(request.getServerName());
    return rv;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpSession(javax.servlet.http.HttpSession) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes)

Example 37 with RequestAttributes

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

the class WebUtils method clearGrailsWebRequest.

/**
 * Removes any GrailsWebRequest instance from the current request.
 */
public static void clearGrailsWebRequest() {
    RequestAttributes reqAttrs = RequestContextHolder.getRequestAttributes();
    if (reqAttrs != null) {
        // First remove the web request from the HTTP request attributes.
        GrailsWebRequest webRequest = (GrailsWebRequest) reqAttrs;
        webRequest.getRequest().removeAttribute(GrailsApplicationAttributes.WEB_REQUEST);
        // Now remove it from RequestContextHolder.
        RequestContextHolder.resetRequestAttributes();
    }
}
Also used : RequestAttributes(org.springframework.web.context.request.RequestAttributes) GrailsWebRequest(org.grails.web.servlet.mvc.GrailsWebRequest)

Example 38 with RequestAttributes

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

the class GrailsWebRequest method lookup.

/**
 * Looks up the current Grails WebRequest instance
 * @return The GrailsWebRequest instance
 */
@Nullable
public static GrailsWebRequest lookup() {
    GrailsWebRequest webRequest = null;
    RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
    if (requestAttributes instanceof GrailsWebRequest) {
        webRequest = (GrailsWebRequest) requestAttributes;
    }
    return webRequest;
}
Also used : RequestAttributes(org.springframework.web.context.request.RequestAttributes) Nullable(org.springframework.lang.Nullable)

Example 39 with RequestAttributes

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

the class AbstractGrailsView method renderWithinGrailsWebRequest.

private void renderWithinGrailsWebRequest(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
    RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
    boolean attributesChanged = false;
    try {
        GrailsWebRequest webRequest;
        if (!(requestAttributes instanceof GrailsWebRequest)) {
            webRequest = createGrailsWebRequest(request, response, request.getServletContext());
            attributesChanged = true;
            WebUtils.storeGrailsWebRequest(webRequest);
        } else {
            webRequest = (GrailsWebRequest) requestAttributes;
        }
        renderTemplate(model, webRequest, request, response);
    } finally {
        if (attributesChanged) {
            request.removeAttribute(GrailsApplicationAttributes.WEB_REQUEST);
            RequestContextHolder.setRequestAttributes(requestAttributes);
        }
    }
}
Also used : RequestAttributes(org.springframework.web.context.request.RequestAttributes) GrailsWebRequest(org.grails.web.servlet.mvc.GrailsWebRequest)

Example 40 with RequestAttributes

use of org.springframework.web.context.request.RequestAttributes in project ontrack by nemerosa.

the class DefaultURIBuilder method build.

@Override
public URI build(Object methodInvocation) {
    // Default builder
    UriComponentsBuilder builder = MvcUriComponentsBuilder.fromMethodCall(methodInvocation);
    // Default URI
    UriComponents uriComponents = builder.build();
    // TODO #251 Workaround for SPR-12771
    RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
    HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
    HttpRequest httpRequest = new ServletServerHttpRequest(request);
    String portHeader = httpRequest.getHeaders().getFirst("X-Forwarded-Port");
    if (StringUtils.hasText(portHeader)) {
        int port = Integer.parseInt(portHeader);
        String scheme = uriComponents.getScheme();
        if (("https".equals(scheme) && port == 443) || ("http".equals(scheme) && port == 80)) {
            port = -1;
        }
        builder.port(port);
    }
    // OK
    return builder.build().toUri();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpRequest(org.springframework.http.HttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) UriComponents(org.springframework.web.util.UriComponents) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) ServletUriComponentsBuilder(org.springframework.web.servlet.support.ServletUriComponentsBuilder) MvcUriComponentsBuilder(org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) 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