Search in sources :

Example 21 with RequestAttributes

use of org.springframework.web.context.request.RequestAttributes in project onebusaway-application-modules by camsys.

the class TextmarksSessionInterceptor method intercept.

@Override
public String intercept(ActionInvocation invocation) throws Exception {
    processGoogleAnalytics();
    ActionContext context = invocation.getInvocationContext();
    Map<String, Object> parameters = context.getParameters();
    Object phoneNumber = parameters.get(_phoneNumberParameterName);
    if (phoneNumber == null)
        return invocation.invoke();
    if (phoneNumber instanceof String[]) {
        String[] values = (String[]) phoneNumber;
        if (values.length == 0)
            return invocation.invoke();
        phoneNumber = values[0];
    }
    String sessionId = phoneNumber.toString();
    Map<String, Object> persistentSession = _sessionManager.getContext(sessionId);
    Map<String, Object> originalSession = context.getSession();
    context.setSession(persistentSession);
    XWorkRequestAttributes attributes = new XWorkRequestAttributes(context, sessionId);
    RequestAttributes originalAttributes = RequestContextHolder.getRequestAttributes();
    RequestContextHolder.setRequestAttributes(attributes);
    Object action = invocation.getAction();
    if (action instanceof SessionAware)
        ((SessionAware) action).setSession(persistentSession);
    try {
        return invocation.invoke();
    } finally {
        RequestContextHolder.setRequestAttributes(originalAttributes);
        context.setSession(originalSession);
    }
}
Also used : SessionAware(org.apache.struts2.interceptor.SessionAware) XWorkRequestAttributes(org.onebusaway.presentation.impl.users.XWorkRequestAttributes) XWorkRequestAttributes(org.onebusaway.presentation.impl.users.XWorkRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes) ActionContext(com.opensymphony.xwork2.ActionContext)

Example 22 with RequestAttributes

use of org.springframework.web.context.request.RequestAttributes in project onebusaway-application-modules by camsys.

the class TwilioInterceptor method intercept.

@Override
public String intercept(ActionInvocation invocation) throws Exception {
    ActionContext context = invocation.getInvocationContext();
    Map<String, Object> parameters = context.getParameters();
    /* Stringify parameters for debugging output */
    String paramString = "";
    for (Entry<String, Object> entry : parameters.entrySet()) {
        paramString += entry.getKey() + "=";
        Object val = entry.getValue();
        if (val instanceof String[]) {
            paramString += Arrays.toString((String[]) val);
        } else {
            paramString += val.toString();
        }
        paramString += ", ";
    }
    int idx = paramString.lastIndexOf(',');
    if (idx >= 0) {
        paramString = paramString.substring(0, idx);
    }
    _log.debug("in with params={" + paramString + "} and session=" + context.getSession());
    Object phoneNumber = parameters.get(_phoneNumberParameterName);
    if (phoneNumber == null) {
        return invocation.invoke();
    }
    if (phoneNumber instanceof String[]) {
        String[] values = (String[]) phoneNumber;
        if (values.length == 0)
            return invocation.invoke();
        phoneNumber = values[0];
    }
    String sessionId = phoneNumber.toString();
    // Strip off leading '+', if any
    sessionId = sessionId.replaceFirst("\\+", "");
    Map<String, Object> persistentSession = _sessionManager.getContext(sessionId);
    _log.debug("remapping sesssionId " + sessionId + " to " + persistentSession);
    Map<String, Object> originalSession = context.getSession();
    context.setSession(persistentSession);
    XWorkRequestAttributes attributes = new XWorkRequestAttributes(context, sessionId);
    RequestAttributes originalAttributes = RequestContextHolder.getRequestAttributes();
    RequestContextHolder.setRequestAttributes(attributes);
    Object action = invocation.getAction();
    if (action instanceof SessionAware)
        ((SessionAware) action).setSession(persistentSession);
    try {
        return invocation.invoke();
    } finally {
        RequestContextHolder.setRequestAttributes(originalAttributes);
        context.setSession(originalSession);
    }
}
Also used : SessionAware(org.apache.struts2.interceptor.SessionAware) XWorkRequestAttributes(org.onebusaway.presentation.impl.users.XWorkRequestAttributes) XWorkRequestAttributes(org.onebusaway.presentation.impl.users.XWorkRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes) ActionContext(com.opensymphony.xwork2.ActionContext)

Example 23 with RequestAttributes

use of org.springframework.web.context.request.RequestAttributes in project onebusaway-application-modules by camsys.

the class DefaultSearchLocationServiceImpl method getDefaultSearchLocationForCurrentUser.

@Override
public DefaultSearchLocation getDefaultSearchLocationForCurrentUser() {
    UserBean user = _currentUserService.getCurrentUser();
    if (user != null && user.hasDefaultLocation()) {
        return new DefaultSearchLocation(user.getDefaultLocationName(), user.getDefaultLocationLat(), user.getDefaultLocationLon(), false);
    }
    RequestAttributes attributes = RequestContextHolder.currentRequestAttributes();
    DefaultSearchLocation location = (DefaultSearchLocation) attributes.getAttribute(KEY_DEFAULT_SEARCH_LOCATION_FOR_SESSSION, RequestAttributes.SCOPE_SESSION);
    return location;
}
Also used : UserBean(org.onebusaway.users.client.model.UserBean) DefaultSearchLocation(org.onebusaway.presentation.model.DefaultSearchLocation) RequestAttributes(org.springframework.web.context.request.RequestAttributes)

Example 24 with RequestAttributes

use of org.springframework.web.context.request.RequestAttributes in project onebusaway-application-modules by camsys.

the class CustomAgiEntryPoint method onActionTearDown.

@Override
protected void onActionTearDown() {
    super.onActionTearDown();
    RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
    RequestContextHolder.resetRequestAttributes();
    if (attributes instanceof XWorkRequestAttributes)
        ((XWorkRequestAttributes) attributes).requestCompleted();
}
Also used : XWorkRequestAttributes(org.onebusaway.presentation.impl.users.XWorkRequestAttributes) XWorkRequestAttributes(org.onebusaway.presentation.impl.users.XWorkRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes)

Example 25 with RequestAttributes

use of org.springframework.web.context.request.RequestAttributes in project flytecnologia-api by jullierme.

the class FlyTokenUserDetails method getCurrentUsername.

public static String getCurrentUsername() {
    SecurityContext securityContext = SecurityContextHolder.getContext();
    Authentication authentication = securityContext.getAuthentication();
    if (authentication != null) {
        if (authentication.getPrincipal() instanceof String)
            return (String) authentication.getPrincipal();
        if (authentication.getPrincipal() instanceof UserDetails)
            return ((UserDetails) authentication.getPrincipal()).getUsername();
    }
    RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
    return (String) requestAttributes.getAttribute("username", RequestAttributes.SCOPE_REQUEST);
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) Authentication(org.springframework.security.core.Authentication) SecurityContext(org.springframework.security.core.context.SecurityContext) 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