Search in sources :

Example 26 with ServletRequestAttributes

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

the class ProfileLastModifiedAspect method retrieveLastModifiedDate.

/** Fetches the last modified from the request-scope last modified cache
     * If not present, fetches from the DB and populates the request-scope last modified cache.
     * 
     * @param orcid
     * @return
     */
public Date retrieveLastModifiedDate(String orcid) {
    ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    Date lastMod = null;
    if (sra != null)
        lastMod = (Date) sra.getAttribute(sraKey(orcid), ServletRequestAttributes.SCOPE_REQUEST);
    if (lastMod == null) {
        lastMod = profileDao.retrieveLastModifiedDate(orcid);
        if (sra != null)
            sra.setAttribute(sraKey(orcid), lastMod, ServletRequestAttributes.SCOPE_REQUEST);
    }
    return lastMod;
}
Also used : ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) Date(java.util.Date)

Example 27 with ServletRequestAttributes

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

the class SourceNameCacheManagerImpl method getProfileSourceNameFromDb.

private String getProfileSourceNameFromDb(String orcid) {
    RecordNameEntity recordName = null;
    try {
        recordName = recordNameDao.getRecordName(orcid);
        if (recordName == null) {
            throw new IllegalArgumentException("Unable to find source name for: " + orcid);
        }
    } catch (Exception e) {
        LOGGER.warn("Cannot find source name from profile matching " + orcid, e);
        throw new IllegalArgumentException("Unable to find source name for: " + orcid);
    }
    String name = RecordNameUtils.getPublicName(recordName);
    ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    if (sra != null) {
        sra.setAttribute(getProfileNameSRAKey(orcid), name != null ? name : StringUtils.EMPTY, ServletRequestAttributes.SCOPE_REQUEST);
    }
    return name;
}
Also used : ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RecordNameEntity(org.orcid.persistence.jpa.entities.RecordNameEntity)

Example 28 with ServletRequestAttributes

use of org.springframework.web.context.request.ServletRequestAttributes in project series-rest-api by 52North.

the class RequestUtils method resolveQueryLessRequestUrl.

/**
     * Get the request {@link URL} without the query parameter
     *
     * @param externalUrl the external URL.
     * @return Request {@link URL} without query parameter
     */
public static String resolveQueryLessRequestUrl(String externalUrl) {
    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
    if (LOGGER.isDebugEnabled()) {
        StringBuilder sb = new StringBuilder("\n----- Start of HTTP Header -----\n");
        Enumeration<?> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String headerName = (String) headerNames.nextElement();
            sb.append(headerName + ": " + request.getHeader(headerName));
            sb.append("\n");
        }
        sb.append("----- END of HTTP Header -----");
        LOGGER.debug(sb.toString());
    }
    return externalUrl == null || externalUrl.isEmpty() ? createRequestUrl(request) : createRequestUrl(externalUrl);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes)

Example 29 with ServletRequestAttributes

use of org.springframework.web.context.request.ServletRequestAttributes in project uPortal by Jasig.

the class PortalWebUtils method currentRequestContextPath.

/**
     * Get the request context path from the current request. Copes with both HttpServletRequest and
     * PortletRequest and so usable when handling Spring-processed Servlet or Portlet requests.
     * Requires that Spring have bound the request, as in the case of dispatcher servlet or portlet
     * or when the binding filter or listener is active. This should be the case for all requests in
     * the uPortal framework and framework portlets.
     *
     * @return request.getContextPath() for the relevant servlet or portlet request
     * @throws IllegalStateException if the request is not Spring-bound or is neither Servlet nor
     *     Portlet flavored
     */
public static String currentRequestContextPath() {
    final RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
    if (null == requestAttributes) {
        throw new IllegalStateException("Request attributes are not bound.  " + "Not operating in context of a Spring-processed Request?");
    }
    if (requestAttributes instanceof ServletRequestAttributes) {
        final ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
        final HttpServletRequest request = servletRequestAttributes.getRequest();
        return request.getContextPath();
    } else if (requestAttributes instanceof PortletRequestAttributes) {
        final PortletRequestAttributes portletRequestAttributes = (PortletRequestAttributes) requestAttributes;
        final PortletRequest request = portletRequestAttributes.getRequest();
        return request.getContextPath();
    } else {
        throw new IllegalStateException("Request attributes are an unrecognized implementation.");
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) PortletRequestAttributes(org.springframework.web.portlet.context.PortletRequestAttributes) PortletRequest(javax.portlet.PortletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) PortletRequestAttributes(org.springframework.web.portlet.context.PortletRequestAttributes)

Example 30 with ServletRequestAttributes

use of org.springframework.web.context.request.ServletRequestAttributes in project geode by apache.

the class ShellCommandsControllerJUnitTest method setupBeforeClass.

@BeforeClass
public static void setupBeforeClass() {
    controller = new ShellCommandsController();
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setContextPath("gemfire");
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) BeforeClass(org.junit.BeforeClass)

Aggregations

ServletRequestAttributes (org.springframework.web.context.request.ServletRequestAttributes)30 RequestAttributes (org.springframework.web.context.request.RequestAttributes)13 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)11 HttpServletRequest (javax.servlet.http.HttpServletRequest)8 Before (org.junit.Before)6 Test (org.junit.Test)3 MockHttpSession (org.springframework.mock.web.MockHttpSession)3 Date (java.util.Date)2 HttpSession (javax.servlet.http.HttpSession)2 BeforeClass (org.junit.BeforeClass)2 RecordNameEntity (org.orcid.persistence.jpa.entities.RecordNameEntity)2 ITestBean (org.springframework.tests.sample.beans.ITestBean)2 TestBean (org.springframework.tests.sample.beans.TestBean)2 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 Principal (java.security.Principal)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 PortletRequest (javax.portlet.PortletRequest)1 ServletException (javax.servlet.ServletException)1