Search in sources :

Example 11 with ServletRequestAttributes

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

the class ValidationManagerForLegacyApiVersionsImpl method doSchemaValidation.

@Override
protected void doSchemaValidation(OrcidMessage orcidMessage) {
    // Hack to support legacy API versions
    RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
    if (requestAttributes instanceof ServletRequestAttributes) {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
        String contentType = servletRequestAttributes.getRequest().getContentType();
        if (!StringUtils.containsIgnoreCase(contentType, "json")) {
            super.doSchemaValidation(orcidMessage);
        }
    } else {
        super.doSchemaValidation(orcidMessage);
    }
}
Also used : ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes)

Example 12 with ServletRequestAttributes

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

the class ProfileLastModifiedAspect method updateLastModifiedDateAndIndexingStatus.

/** Updates the last modified date and clears the request-scope last modified cache.
     * 
     * @param orcid
     */
public void updateLastModifiedDateAndIndexingStatus(String orcid) {
    if (!enabled) {
        return;
    }
    profileDao.updateLastModifiedDateAndIndexingStatus(orcid, IndexingStatus.PENDING);
    ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    if (sra != null)
        sra.setAttribute(sraKey(orcid), null, ServletRequestAttributes.SCOPE_REQUEST);
}
Also used : ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes)

Example 13 with ServletRequestAttributes

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

the class ShellCommandsControllerJUnitTest method getControllerWebServiceEndpoints.

private List<String> getControllerWebServiceEndpoints() {
    RequestAttributes requestAttrs = RequestContextHolder.getRequestAttributes();
    HttpServletRequest servletRequest = ((ServletRequestAttributes) requestAttrs).getRequest();
    String scheme = servletRequest.getScheme();
    try {
        Set<Class<?>> controllerClasses = scanPackageForClassesExtending("org.apache.geode.management.internal.web.controllers", AbstractCommandsController.class);
        List<String> controllerWebServiceEndpoints = new ArrayList<>(controllerClasses.size());
        for (Class<?> controllerClass : controllerClasses) {
            if (!AbstractCommandsController.class.equals(controllerClass)) {
                for (Method method : controllerClass.getMethods()) {
                    if (method.isAnnotationPresent(RequestMapping.class)) {
                        RequestMapping requestMappingAnnotation = method.getAnnotation(RequestMapping.class);
                        String webServiceEndpoint = String.format("%1$s %2$s", requestMappingAnnotation.method()[0], UriUtils.decode(controller.toUri(requestMappingAnnotation.value()[0], scheme).toString()));
                        String[] requestParameters = requestMappingAnnotation.params();
                        if (requestParameters.length > 0) {
                            webServiceEndpoint += "?".concat(StringUtils.join(requestParameters, "&amp;"));
                        }
                        controllerWebServiceEndpoints.add(webServiceEndpoint);
                    }
                }
            }
        }
        return controllerWebServiceEndpoints;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) ArrayList(java.util.ArrayList) RequestAttributes(org.springframework.web.context.request.RequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) Method(java.lang.reflect.Method) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) BeforeClass(org.junit.BeforeClass) AfterClass(org.junit.AfterClass)

Example 14 with ServletRequestAttributes

use of org.springframework.web.context.request.ServletRequestAttributes 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();
    if (request == null) {
        return null;
    }
    HttpConnectionInformation rv = new HttpConnectionInformation();
    HttpSession session = request.getSession(false);
    if (session != null) {
        rv.setSessionId(session.getId());
    }
    rv.setLocalHostName(request.getLocalName());
    rv.setRemoteHostAddress(getRemoteHostAddress(request));
    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 15 with ServletRequestAttributes

use of org.springframework.web.context.request.ServletRequestAttributes in project ocvn by devgateway.

the class AbstractSpringDataRestControllerTest method mockHttpServletRequestForResouceAssemblerSupport.

/**
     * http://stackoverflow.com/a/36960968 
     * This is needed if you do
     * {@link PagedResourcesAssembler#toResource(org.springframework.data.domain.Page)}
     * in your controller
     */
private void mockHttpServletRequestForResouceAssemblerSupport() {
    String localHost = "http://localhost";
    HttpServletRequest httpServletRequestMock = mock(HttpServletRequest.class);
    when(httpServletRequestMock.getRequestURL()).thenReturn(new StringBuffer(localHost));
    when(httpServletRequestMock.getHeaderNames()).thenReturn(Collections.emptyEnumeration());
    when(httpServletRequestMock.getRequestURI()).thenReturn(localHost);
    when(httpServletRequestMock.getContextPath()).thenReturn(StringUtils.EMPTY);
    when(httpServletRequestMock.getServletPath()).thenReturn(StringUtils.EMPTY);
    ServletRequestAttributes servletRequestAttributes = new ServletRequestAttributes(httpServletRequestMock);
    RequestContextHolder.setRequestAttributes(servletRequestAttributes);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes)

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