Example usage for javax.servlet.http HttpServletRequest getAttribute

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

Introduction

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

Prototype

public Object getAttribute(String name);

Source Link

Document

Returns the value of the named attribute as an Object, or null if no attribute of the given name exists.

Usage

From source file:net.naijatek.myalumni.util.utilities.ParamUtil.java

public static String getAttribute(final HttpServletRequest request, final String name) {
    String ret = (String) request.getAttribute(name);
    if (ret == null) {
        ret = "";
    }//from  w w w.  java2 s  . co  m
    return ret.trim();
}

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

private static String checkLoginGitHubUser(HttpServletRequest request, Map<String, Object> userInfo,
        String accessToken) {//from  w  ww  .j a  va  2 s . c om
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
    String productStoreId = ProductStoreWorker.getProductStoreId(request);
    String gitHubUserId = (String) userInfo.get("login");
    GenericValue gitHubUser = null;
    try {
        gitHubUser = delegator.findOne("GitHubUser", UtilMisc.toMap("gitHubUserId", gitHubUserId), false);
    } catch (GenericEntityException e) {
        request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
        return "error";
    }
    if (UtilValidate.isNotEmpty(gitHubUser)) {
        boolean dataChanged = false;
        if (!accessToken.equals(gitHubUser.getString("accessToken"))) {
            gitHubUser.set("accessToken", accessToken);
            dataChanged = true;
        }
        if (!envPrefix.equals(gitHubUser.getString("envPrefix"))) {
            gitHubUser.set("envPrefix", envPrefix);
            dataChanged = true;
        }
        if (!productStoreId.equals(gitHubUser.getString("productStoreId"))) {
            gitHubUser.set("productStoreId", productStoreId);
            dataChanged = true;
        }
        if (dataChanged) {
            try {
                gitHubUser.store();
            } catch (GenericEntityException e) {
                Debug.logError(e.getMessage(), module);
            }
        }
    } else {
        gitHubUser = delegator.makeValue("GitHubUser", UtilMisc.toMap("accessToken", accessToken,
                "productStoreId", productStoreId, "envPrefix", envPrefix, "gitHubUserId", gitHubUserId));
        try {
            gitHubUser.create();
        } catch (GenericEntityException e) {
            Debug.logError(e.getMessage(), module);
        }
    }
    try {
        GenericValue userLogin = EntityUtil.getFirst(
                delegator.findByAnd("UserLogin", UtilMisc.toMap("externalAuthId", gitHubUserId), null, false));
        GitHubAuthenticator authn = new GitHubAuthenticator();
        authn.initialize(dispatcher);
        if (UtilValidate.isEmpty(userLogin)) {
            String userLoginId = authn.createUser(userInfo);
            userLogin = delegator.findOne("UserLogin", UtilMisc.toMap("userLoginId", userLoginId), false);
        }
        String autoPassword = RandomStringUtils.randomAlphanumeric(
                EntityUtilProperties.getPropertyAsInteger("security", "password.length.min", 5).intValue());
        boolean useEncryption = "true".equals(UtilProperties.getPropertyValue("security", "password.encrypt"));
        userLogin.set("currentPassword",
                useEncryption ? HashCrypt.digestHash(LoginServices.getHashType(), null, autoPassword)
                        : autoPassword);
        userLogin.store();
        request.setAttribute("USERNAME", userLogin.getString("userLoginId"));
        request.setAttribute("PASSWORD", autoPassword);
    } catch (GenericEntityException e) {
        Debug.logError(e.getMessage(), module);
        request.setAttribute("_ERROR_MESSAGE_", e.toString());
        return "error";
    } catch (AuthenticatorException e) {
        Debug.logError(e.getMessage(), module);
        request.setAttribute("_ERROR_MESSAGE_", e.toString());
        return "error";
    }
    return "success";
}

From source file:com.liangc.hq.base.utils.AlertDefUtil.java

/**
 * Retrieve the alert definition from either the request or from the bizapp
 * as necessary. First check to see if the alertDef is already in the
 * request attributes. If it is, return it. If not, look for an "ad"
 * parameter and then get the alert definition from the bizapp and return
 * it./*from w  w  w .  j  a v  a2 s .  c  o m*/
 */
public static AlertDefinitionValue getAlertDefinition(HttpServletRequest request, int sessionID, EventsBoss eb)
        throws Exception {
    AlertDefinitionValue adv = (AlertDefinitionValue) request.getAttribute(Constants.ALERT_DEFINITION_ATTR);
    if (null == adv) {
        String adS = request.getParameter(Constants.ALERT_DEFINITION_PARAM);
        if (null == adS) {
            throw new Exception(Constants.ALERT_DEFINITION_PARAM);
        } else {
            Integer ad = new Integer(adS);
            adv = eb.getAlertDefinition(sessionID, ad);
            request.setAttribute(Constants.ALERT_DEFINITION_ATTR, adv);
        }
        log.trace("adv.id=" + adv.getId());
    }

    return adv;
}

From source file:com.activecq.api.utils.HttpRequestUtil.java

/**
 * <p>/*w w  w.ja va  2s .  c  om*/
 * Gets the Attribute or Parameter value from the Request.
 * </p><p>
 * Attribute takes precedence
 * </p><p>
 * If neither a Parameter or Attribute value exists @ key, return the dfault
 * </p>
 *
 * @param request
 * @param key
 * @param dfault
 * @return
 */
public static String getParameterOrAttribute(HttpServletRequest request, String key, String dfault) {
    String value = null;
    if (request == null) {
        return value;
    }

    if (hasParameter(request, key)) {
        value = request.getParameter(key);
    } else if (hasAttribute(request, key)) {
        value = (String) request.getAttribute(key);
    }

    if (StringUtils.isBlank(value)) {
        value = dfault;
    }

    return value;
}

From source file:net.sourceforge.vulcan.web.struts.actions.ProjectReportBaseAction.java

static PreferencesDto findPreferences(HttpServletRequest request) {
    final HttpSession session = request.getSession(false);

    if (session != null) {
        final PreferencesDto prefs = (PreferencesDto) session.getAttribute(Keys.PREFERENCES);
        if (prefs != null) {
            return prefs;
        }/*from  w  w  w  .  j a  va2s.com*/
    }

    return (PreferencesDto) request.getAttribute(Keys.PREFERENCES);
}

From source file:com.ocpsoft.pretty.PrettyContext.java

/**
 * Get the current {@link PrettyContext}, or construct a new one if it does
 * not yet exist for this request.//w ww .j av  a 2 s .  co  m
 * 
 * @return current context instance
 */
public static PrettyContext getCurrentInstance(final HttpServletRequest request) {
    Assert.notNull(request, "HttpServletRequest argument was null");
    PrettyContext prettyContext = (PrettyContext) request.getAttribute(CONTEXT_REQUEST_KEY);
    if (prettyContext instanceof PrettyContext) {
        log.trace("Retrieved PrettyContext from Request");
        return prettyContext;
    } else {
        Assert.notNull(request, "HttpServletRequest argument was null");
        prettyContext = newDetachedInstance(request);
        log.trace("PrettyContext not found in Request - building new instance");
        setCurrentContext(request, prettyContext);
        return prettyContext;
    }
}

From source file:com.glaf.core.util.HttpQueryUtils.java

protected static Object getObjectValue(HttpServletRequest request, String name) {
    Object value = getParameter(request, name);
    if (request.getAttribute(name) != null) {
        value = request.getAttribute(name);
    }//w w w . j a  v a2s .  c  o  m
    return value;
}

From source file:com.glaf.core.util.HttpQueryUtils.java

protected static String getStringValue(HttpServletRequest request, String name) {
    String value = getParameter(request, name);
    if (request.getAttribute(name) != null) {
        value = (String) request.getAttribute(name);
    }//from   w w  w. j  a  v  a  2 s.  c o m
    return value;
}

From source file:com.glaf.core.util.HttpQueryUtils.java

protected static Boolean getBooleanValue(HttpServletRequest request, String name) {
    String value = request.getParameter(name);
    if (request.getAttribute(name) != null) {
        value = (String) request.getAttribute(name);
    }/*  ww w. j ava  2  s  . c  o  m*/
    if (StringUtils.equalsIgnoreCase(value, "true")) {
        return true;
    }
    return false;
}

From source file:com.salesmanager.core.util.CategoryUtil.java

/**
 * Get the number of products per category
 * /*from  w w  w .  j  a va  2 s  . c o  m*/
 * @param req
 * @param categoryid
 * @return
 */
public static String getItemPerCategoryCount(HttpServletRequest req, String lang, Category category) {

    try {

        CatalogService service = (CatalogService) ServiceFactory.getService(ServiceFactory.CatalogService);

        int count = service.countProductsPerCategoryAndSubCategories((List) req.getAttribute("PRODUCTS"), lang,
                category);

        if (count == 0) {
            return "";
        } else {
            return "<b><font color='red'>[" + count + "]</font></b>";
        }

    } catch (Exception e) {
        log.error(e);
        return "";
    }

}