Example usage for javax.servlet.http HttpServletRequest getSession

List of usage examples for javax.servlet.http HttpServletRequest getSession

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletRequest getSession.

Prototype

public HttpSession getSession();

Source Link

Document

Returns the current session associated with this request, or if the request does not have a session, creates one.

Usage

From source file:com.krawler.esp.handlers.AuthHandler.java

public static String getUserTimeFormat(HttpServletRequest request) throws SessionExpiredException {
    String timeformat = NullCheckAndThrow(request.getSession().getAttribute("timeformat"),
            SessionExpiredException.USERID_NULL);
    return timeformat;
}

From source file:fr.paris.lutece.plugins.mylutece.web.MyLuteceApp.java

/**
 * Returns the current url/* w ww.  j  a v a  2 s. c  om*/
 * @param request The Http request
 * @return The current url
 * 
 */
public static String getCurrentUrl(HttpServletRequest request) {
    HttpSession session = request.getSession();
    String strNextUrl = (String) session.getAttribute(ATTRIBUTE_CURRENT_URL);

    return strNextUrl;
}

From source file:com.lm.lic.manager.util.GenUtil.java

/**
 * @param request//  w  w  w  .j a v a 2s .com
 * @return
 */
public static String findProductUnderManagement(HttpServletRequest request) {
    HttpSession session = request.getSession();
    String prodId = (String) session.getAttribute("prodId");
    return prodId;
}

From source file:com.concursive.connect.web.modules.login.utils.UserUtils.java

public static int getUserId(HttpServletRequest request) {
    return ((User) request.getSession().getAttribute(Constants.SESSION_USER)).getId();
}

From source file:com.concursive.connect.web.modules.login.utils.UserUtils.java

/**
 * Gets the userRangeId attribute of the UserUtils class
 *
 * @param request Description of the Parameter
 * @return The userRangeId value/*from w ww . ja  v  a 2s  . c om*/
 */
public static String getUserIdRange(HttpServletRequest request) {
    return ((User) request.getSession().getAttribute(Constants.SESSION_USER)).getIdRange();
}

From source file:com.concursive.connect.web.modules.login.utils.UserUtils.java

/**
 * Gets the userTimeZone attribute of the UserUtils class
 *
 * @param request Description of the Parameter
 * @return The userTimeZone value/*  ww  w .  j  a v  a2 s. c o m*/
 */
public static String getUserTimeZone(HttpServletRequest request) {
    return ((User) request.getSession().getAttribute(Constants.SESSION_USER)).getTimeZone();
}

From source file:com.concursive.connect.web.modules.login.utils.UserUtils.java

/**
 * Gets the userLocale attribute of the UserUtils class
 *
 * @param request Description of the Parameter
 * @return The userLocale value//from  w ww .  j  a v a2s.  c  o m
 */
public static Locale getUserLocale(HttpServletRequest request) {
    return ((User) request.getSession().getAttribute(Constants.SESSION_USER)).getLocale();
}

From source file:com.concursive.connect.web.modules.login.utils.UserUtils.java

/**
 * Gets the userCurrency attribute of the UserUtils class
 *
 * @param request Description of the Parameter
 * @return The userCurrency value/*  www .jav a2 s  .  com*/
 */
public static String getUserCurrency(HttpServletRequest request) {
    return ((User) request.getSession().getAttribute(Constants.SESSION_USER)).getCurrency();
}

From source file:org.apache.ofbiz.passport.event.GitHubEvents.java

/**
 * Parse GitHub login response and login the user if possible.
 * /*from  w ww .ja  va  2 s  .c  o  m*/
 * @return 
 */
public static String parseGitHubResponse(HttpServletRequest request, HttpServletResponse response) {
    String authorizationCode = request.getParameter(PassportUtil.COMMON_CODE);
    String state = request.getParameter(PassportUtil.COMMON_STATE);
    if (!state.equals(request.getSession().getAttribute(SESSION_GITHUB_STATE))) {
        String errMsg = UtilProperties.getMessage(resource, "GitHubFailedToMatchState",
                UtilHttp.getLocale(request));
        request.setAttribute("_ERROR_MESSAGE_", errMsg);
        return "error";
    }
    if (UtilValidate.isEmpty(authorizationCode)) {
        String error = request.getParameter(PassportUtil.COMMON_ERROR);
        String errorDescpriton = request.getParameter(PassportUtil.COMMON_ERROR_DESCRIPTION);
        String errMsg = null;
        try {
            errMsg = UtilProperties.getMessage(resource, "FailedToGetGitHubAuthorizationCode",
                    UtilMisc.toMap(PassportUtil.COMMON_ERROR, error, PassportUtil.COMMON_ERROR_DESCRIPTION,
                            URLDecoder.decode(errorDescpriton, "UTF-8")),
                    UtilHttp.getLocale(request));
        } catch (UnsupportedEncodingException e) {
            errMsg = UtilProperties.getMessage(resource, "GetGitHubAuthorizationCodeError",
                    UtilHttp.getLocale(request));
        }
        request.setAttribute("_ERROR_MESSAGE_", errMsg);
        return "error";
    }
    Debug.logInfo("GitHub authorization code: " + authorizationCode, module);

    GenericValue oauth2GitHub = getOAuth2GitHubConfig(request);
    if (UtilValidate.isEmpty(oauth2GitHub)) {
        String errMsg = UtilProperties.getMessage(resource, "GetOAuth2GitHubConfigError",
                UtilHttp.getLocale(request));
        request.setAttribute("_ERROR_MESSAGE_", errMsg);
        return "error";
    }
    String clientId = oauth2GitHub.getString(PassportUtil.COMMON_CLIENT_ID);
    String secret = oauth2GitHub.getString(PassportUtil.COMMON_CLIENT_SECRET);
    String returnURI = oauth2GitHub.getString(PassportUtil.COMMON_RETURN_RUL);

    // Grant token from authorization code and oauth2 token
    // Use the authorization code to obtain an access token
    String accessToken = null;
    String tokenType = null;

    try {
        URI uri = new URIBuilder().setScheme(TokenEndpoint.substring(0, TokenEndpoint.indexOf(":")))
                .setHost(TokenEndpoint.substring(TokenEndpoint.indexOf(":") + 3)).setPath(TokenServiceUri)
                .setParameter("client_id", clientId).setParameter("client_secret", secret)
                .setParameter("code", authorizationCode).setParameter("redirect_uri", returnURI).build();
        HttpPost postMethod = new HttpPost(uri);
        CloseableHttpClient jsonClient = HttpClients.custom().build();
        // Debug.logInfo("GitHub get access token query string: " + postMethod.getURI(), module);
        postMethod.setConfig(PassportUtil.StandardRequestConfig);
        postMethod.setHeader(PassportUtil.ACCEPT_HEADER, "application/json");
        CloseableHttpResponse postResponse = jsonClient.execute(postMethod);
        String responseString = new BasicResponseHandler().handleResponse(postResponse);
        // Debug.logInfo("GitHub get access token response code: " + postResponse.getStatusLine().getStatusCode(), module);
        // Debug.logInfo("GitHub get access token response content: " + responseString, module);
        if (postResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            Debug.logInfo("Json Response from GitHub: " + responseString, module);
            JSON jsonObject = JSON.from(responseString);
            JSONToMap jsonMap = new JSONToMap();
            Map<String, Object> userMap = jsonMap.convert(jsonObject);
            accessToken = (String) userMap.get("access_token");
            tokenType = (String) userMap.get("token_type");
            // Debug.logInfo("Generated Access Token : " + accessToken, module);
            // Debug.logInfo("Token Type: " + tokenType, module);
        } else {
            String errMsg = UtilProperties.getMessage(resource, "GetOAuth2GitHubAccessTokenError",
                    UtilMisc.toMap("error", responseString), UtilHttp.getLocale(request));
            request.setAttribute("_ERROR_MESSAGE_", errMsg);
            return "error";
        }
    } catch (UnsupportedEncodingException e) {
        request.setAttribute("_ERROR_MESSAGE_", e.toString());
        return "error";
    } catch (IOException e) {
        request.setAttribute("_ERROR_MESSAGE_", e.toString());
        return "error";
    } catch (ConversionException e) {
        request.setAttribute("_ERROR_MESSAGE_", e.toString());
        return "error";
    } catch (URISyntaxException e) {
        request.setAttribute("_ERROR_MESSAGE_", e.toString());
        return "error";
    }

    // Get User Profile
    HttpGet getMethod = new HttpGet(ApiEndpoint + UserApiUri);
    Map<String, Object> userInfo = null;
    try {
        userInfo = GitHubAuthenticator.getUserInfo(getMethod, accessToken, tokenType,
                UtilHttp.getLocale(request));
    } catch (AuthenticatorException e) {
        request.setAttribute("_ERROR_MESSAGE_", e.toString());
        return "error";
    } finally {
        getMethod.releaseConnection();
    }
    // Debug.logInfo("GitHub User Info:" + userInfo, module);

    // Store the user info and check login the user
    return checkLoginGitHubUser(request, userInfo, accessToken);
}

From source file:gov.nih.nci.caIMAGE.util.NewDropdownUtil.java

private static WebApplicationContext getContext(HttpServletRequest inRequest) {
    return WebApplicationContextUtils
            .getRequiredWebApplicationContext(inRequest.getSession().getServletContext());
}