Example usage for org.springframework.web.context.request RequestContextHolder currentRequestAttributes

List of usage examples for org.springframework.web.context.request RequestContextHolder currentRequestAttributes

Introduction

In this page you can find the example usage for org.springframework.web.context.request RequestContextHolder currentRequestAttributes.

Prototype

public static RequestAttributes currentRequestAttributes() throws IllegalStateException 

Source Link

Document

Return the RequestAttributes currently bound to the thread.

Usage

From source file:uk.ac.ebi.intact.editor.util.HybridSessionThreadScope.java

public String getConversationId() {
    if (RequestContextHolder.getRequestAttributes() != null) {
        return RequestContextHolder.currentRequestAttributes().getSessionId();
    }//from w w w .  j av a2 s  .c o m

    return super.getConversationId();
}

From source file:org.n52.web.common.RequestUtils.java

/**
 * Get the request {@link URL} without the query parameter
 *
 * @return Request {@link URL} without query parameter
 * @throws IOException//w  ww . j av a  2s. co  m
 * @throws URISyntaxException
 */
public static String resolveQueryLessRequestUrl() throws IOException, URISyntaxException {
    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
            .getRequest();

    URL url = new URL(request.getRequestURL().toString());

    String scheme = url.getProtocol();
    String userInfo = url.getUserInfo();
    String host = url.getHost();

    int port = url.getPort();

    String path = request.getRequestURI();
    if (path != null && path.endsWith("/")) {
        path = path.substring(0, path.length() - 1);
    }

    URI uri = new URI(scheme, userInfo, host, port, path, null, null);
    return uri.toString();
}

From source file:org.ngrinder.common.util.HttpContainerContext.java

/**
 * Get current container nGrinder context base path.
 * <p/>//  w w w  . ja  v a2  s . c  o m
 * E.g) if user requests http://hostname:port/context_path/realurl, This
 * will return http://hostname:port/context_path
 * <p/>
 * In case of providing "http.url" property in system.properties file, this
 * method will return pre-set value.
 *
 * @return ngrinder context base path on http request.
 */
public String getCurrentContextUrlFromUserRequest() {
    String httpUrl = StringUtils.trimToEmpty(config.getControllerProperties().getProperty(PROP_CONTROLLER_URL));
    // if provided
    if (StringUtils.isNotBlank(httpUrl)) {
        return httpUrl;
    }

    // if empty
    SecurityContextHolderAwareRequestWrapper request = cast(
            RequestContextHolder.currentRequestAttributes().resolveReference("request"));
    int serverPort = request.getServerPort();
    // If it's http default port it will ignore the port part.
    // However, if ngrinder is provided in HTTPS.. it can be a problem.
    // FIXME : Later fix above.
    String portString = (serverPort == DEFAULT_WEB_PORT) ? StringUtils.EMPTY : ":" + serverPort;
    return httpUrl + request.getScheme() + "://" + request.getServerName() + portString
            + request.getContextPath();
}

From source file:org.craftercms.core.util.HttpServletUtils.java

public static void removeAttribute(String name, int scope) throws IllegalStateException {
    RequestContextHolder.currentRequestAttributes().removeAttribute(name, scope);
}

From source file:org.dspace.webmvc.view.freemarker.SpringThemeAwareFreemarkerConfiguration.java

@Override
public Template getTemplate(String name, Locale locale, String encoding, boolean parse) throws IOException {

    String themePath = SpringThemeContextUtils.getProperty("theme.template.path");

    // If we have a theme path, attempt locating the template within the theme first
    if (themePath != null) {
        try {//from   ww  w  .  ja va2  s  . c  o  m
            return super.getTemplate(themePath + name, locale, encoding, parse);
        } catch (IOException ioe) {
            // Will throw an exception if there is no template in the theme path,
            // we will ignore it, and try the standard path locations
        }
    }

    String themeParentName = SpringThemeContextUtils.getProperty("theme.parent", null);
    if (!StringUtils.isEmpty(themeParentName)) {
        RequestAttributes ra = RequestContextHolder.currentRequestAttributes();
        if (ra instanceof ServletRequestAttributes) {
            ServletRequestAttributes sra = (ServletRequestAttributes) ra;
            ThemeSource ts = RequestContextUtils.getThemeSource(sra.getRequest());

            if (ts != null) {
                Theme parentTheme = ts.getTheme(themeParentName);

                while (parentTheme != null) {
                    themePath = SpringThemeUtils.getProperty(parentTheme, "theme.template.path", locale, null);
                    if (themePath != null) {
                        try {
                            return super.getTemplate(themePath + name, locale, encoding, parse);
                        } catch (IOException ioe) {
                            // Will throw an exception if there is no template in the theme path,
                            // we will ignore it, and try the standard path locations
                        }
                    }

                    themeParentName = SpringThemeUtils.getProperty(parentTheme, "theme.parent", locale, null);
                    parentTheme = ts.getTheme(themeParentName);
                }

            }
        }
    }

    // No template in the theme path, so get a template from the standard location
    return super.getTemplate(name, locale, encoding, parse);
}

From source file:ru.smsl.webkrona.util.FlashMap.java

/**
 * Returns the flash map typically stored in window or session scope and creates a new one, if not existing and if
 * needed.//from  w w  w . ja v  a2 s.c  o  m
 * 
 * @param createIfNotExisting flag, indicating whether to create the flash map, if it does not exist already
 * @return the flash map currently bound to the session or window scope, if available
 */
@SuppressWarnings("unchecked")
public static Map<String, Object> getFlashMap(boolean createIfNotExisting) {
    RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
    Map<String, Object> flashMap = (Map<String, Object>) requestAttributes.getAttribute(FLASH_MAP_ATTRIBUTE,
            getFlashMapScope());

    if (flashMap == null && createIfNotExisting) {
        flashMap = new HashMap<String, Object>();
        requestAttributes.setAttribute(FLASH_MAP_ATTRIBUTE, flashMap, getFlashMapScope());
    }

    return flashMap;
}

From source file:uk.ac.ebi.intact.editor.util.HybridSessionThreadScope.java

@Override
public Object remove(String name) {
    if (RequestContextHolder.getRequestAttributes() != null) {
        Object mutex = RequestContextHolder.currentRequestAttributes().getSessionMutex();
        synchronized (mutex) {
            return super.remove(name);
        }/*from  w  ww. java  2s . c o  m*/
    }
    return super.remove(name);
}

From source file:org.lareferencia.xoai.services.impl.xoai.LRRepositoryConfiguration.java

@Override
public String getBaseUrl() {
    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
            .getRequest();/*from  w w  w.  j  a va  2  s. c  om*/
    if (baseUrl == null) {
        baseUrl = request.getRequestURL().toString().replace(request.getPathInfo(), "");
    }
    return baseUrl + request.getPathInfo();
}

From source file:org.vaadin.spring.internal.UIStore.java

public String currentSessionId() {
    return RequestContextHolder.currentRequestAttributes().getSessionId();
}

From source file:com.hack23.cia.web.impl.ui.application.action.PageActionEventHelperImpl.java

@Override
public void createPageEvent(final ViewAction viewAction, final ApplicationEventGroup applicationEventGroup,
        final String page, final String pageMode, final String elementId) {

    String pageModeToUse;/*from   w ww. j ava  2s  .  c o  m*/

    if (pageMode != null && elementId != null && pageMode.contains(elementId)) {
        pageModeToUse = pageMode.replace(elementId, "").replace("/", "");
    } else {
        pageModeToUse = pageMode;
    }

    if ((pageModeToUse == null || "".equals(pageModeToUse))
            && ApplicationEventGroup.USER == applicationEventGroup) {
        pageModeToUse = "Overview";
    }

    final CreateApplicationEventRequest serviceRequest = new CreateApplicationEventRequest();
    serviceRequest.setSessionId(RequestContextHolder.currentRequestAttributes().getSessionId());

    serviceRequest.setEventGroup(applicationEventGroup);
    serviceRequest.setApplicationOperation(ApplicationOperationType.READ);

    serviceRequest.setPage(StringUtils.defaultString(page));
    serviceRequest.setPageMode(StringUtils.defaultString(pageModeToUse));
    serviceRequest.setElementId(StringUtils.defaultString(elementId));

    serviceRequest.setActionName(viewAction.toString());

    serviceRequest.setUserId(UserContextUtil.getUserIdFromSecurityContext());

    serviceRequest.setApplicationMessage(viewAction.toString());

    applicationManager.service(serviceRequest);
}