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:gov.va.vinci.chartreview.util.CRSchemaXML.java

/**
 * Parses the give CRSchemaXML (read from the POST Body of the Request)
 *
 * @param request an HttpServletRequest/*from w w  w .j a  va2 s . c om*/
 * @return a groovy.util.XmlSlurper
 * @throws ConverterException
 */
public static Object parse(HttpServletRequest request) throws ConverterException {
    Object xml = request.getAttribute(CACHED_XML);
    if (xml != null)
        return xml;

    String encoding = request.getCharacterEncoding();
    if (encoding == null) {
        encoding = Converter.DEFAULT_REQUEST_ENCODING;
    }
    try {
        if (!request.getMethod().equalsIgnoreCase("GET")) {
            xml = parse(request.getInputStream(), encoding);
            request.setAttribute(CACHED_XML, xml);
        }
        return xml;
    } catch (IOException e) {
        throw new ConverterException("Error parsing CRSchemaXML", e);
    }
}

From source file:net.ontopia.topicmaps.webed.impl.utils.TagUtils.java

public static LockResult getReadOnlyLock(HttpServletRequest request) {
    LockResult retVal = (LockResult) request.getAttribute(Constants.LOCK_RESULT);
    if (retVal == null)
        log.warn("TagUtils.getReadOnlyLock returning null.");
    return retVal;
}

From source file:com.redhat.rhn.frontend.action.LoginHelper.java

/**
 * check whether we can login an externally authenticated user
 * @param request request/*from   ww  w  .  j a  va 2 s  .  c o  m*/
 * @param messages messages
 * @param errors errors
 * @return user, if externally authenticated
 */
public static User checkExternalAuthentication(HttpServletRequest request, ActionMessages messages,
        ActionErrors errors) {
    String remoteUserString = request.getRemoteUser();
    User remoteUser = null;
    if (remoteUserString != null) {

        String firstname = decodeFromIso88591((String) request.getAttribute("REMOTE_USER_FIRSTNAME"), "");
        String lastname = decodeFromIso88591((String) request.getAttribute("REMOTE_USER_LASTNAME"), "");
        String email = decodeFromIso88591((String) request.getAttribute("REMOTE_USER_EMAIL"), null);

        Set<String> extGroups = getExtGroups(request);
        Set<Role> roles = getRolesFromExtGroups(extGroups);

        log.warn("REMOTE_USER_GROUPS: " + request.getAttribute("REMOTE_USER_GROUPS"));

        try {
            remoteUser = UserFactory.lookupByLogin(remoteUserString);

            if (remoteUser.isDisabled()) {
                errors.add(ActionMessages.GLOBAL_MESSAGE,
                        new ActionMessage("account.user.disabled", new String[] { remoteUserString }));
                remoteUser = null;
            }
            if (remoteUser != null) {
                UpdateUserCommand updateCmd = new UpdateUserCommand(remoteUser);
                if (!StringUtils.isEmpty(firstname)) {
                    updateCmd.setFirstNames(firstname);
                }
                if (!StringUtils.isEmpty(lastname)) {
                    updateCmd.setLastName(lastname);
                }
                if (!StringUtils.isEmpty(email)) {
                    updateCmd.setEmail(email);
                }
                updateCmd.setTemporaryRoles(roles);
                updateCmd.updateUser();
                log.warn("Externally authenticated login " + remoteUserString + " (" + firstname + " "
                        + lastname + ")");
            }
        } catch (LookupException le) {
            Org newUserOrg = null;
            Boolean useOrgUnit = SatConfigFactory
                    .getSatConfigBooleanValue(SatConfigFactory.EXT_AUTH_USE_ORGUNIT);
            if (useOrgUnit) {
                String orgUnitString = (String) request.getAttribute("REMOTE_USER_ORGUNIT");
                newUserOrg = OrgFactory.lookupByName(orgUnitString);
                if (newUserOrg == null) {
                    log.error("Cannot find organization with name: " + orgUnitString);
                }
            }
            if (newUserOrg == null) {
                Long defaultOrgId = SatConfigFactory
                        .getSatConfigLongValue(SatConfigFactory.EXT_AUTH_DEFAULT_ORGID);
                if (defaultOrgId != null) {
                    newUserOrg = OrgFactory.lookupById(defaultOrgId);
                    if (newUserOrg == null) {
                        log.error("Cannot find organization with id: " + defaultOrgId);
                    }
                }
            }
            if (newUserOrg != null) {
                Set<ServerGroup> sgs = getSgsFromExtGroups(extGroups, newUserOrg);
                try {
                    CreateUserCommand createCmd = new CreateUserCommand();
                    createCmd.setLogin(remoteUserString);
                    // set a password, that cannot really be used
                    createCmd.setRawPassword(DEFAULT_KERB_USER_PASSWORD);
                    createCmd.setFirstNames(firstname);
                    createCmd.setLastName(lastname);
                    createCmd.setEmail(email);
                    createCmd.setOrg(newUserOrg);
                    createCmd.setTemporaryRoles(roles);
                    createCmd.setServerGroups(sgs);
                    createCmd.validate();
                    createCmd.storeNewUser();
                    remoteUser = createCmd.getUser();
                    log.warn("Externally authenticated login " + remoteUserString + " (" + firstname + " "
                            + lastname + ") created in " + newUserOrg.getName() + ".");
                } catch (WrappedSQLException wse) {
                    log.error("Creation of user failed with: " + wse.getMessage());
                    HibernateFactory.rollbackTransaction();
                }
            }
            if (remoteUser != null && remoteUser.getPassword().equals(DEFAULT_KERB_USER_PASSWORD)) {
                messages.add(ActionMessages.GLOBAL_MESSAGE,
                        new ActionMessage("message.kerbuserlogged", new String[] { remoteUserString }));
            }
        }
    }
    return remoteUser;
}

From source file:edu.cornell.mannlib.vitro.webapp.web.MiscWebUtils.java

/**
 * returns a table of the req attributes
 * @param req/*from  w  w  w.j ava2s  .  c  o  m*/
 * @return
 */
public static String getRequestAttributes(HttpServletRequest req) {
    String val = "<table>";
    Enumeration names = req.getAttributeNames();
    while (names.hasMoreElements()) {
        String name = (String) names.nextElement();
        val += "\n\t<tr><td>" + name + "</td><td><pre>";
        String value = null;
        try {
            Object obj = req.getAttribute(name);
            value = (obj instanceof Model || obj instanceof ModelCom) ? "[Jena model object]"
                    : (obj == null) ? "[null]" : StringEscapeUtils.escapeHtml(obj.toString());
        } catch (Exception ex) {
            value = "unable to get value";
        } catch (Error er) {
            value = "unable to get value";
        } catch (Throwable th) {
            value = "unable to get value";
        }
        val += value + "</pre></td></tr>\n";
    }
    return val + "</table>";
}

From source file:com.serotonin.m2m2.Common.java

public static User getUser(HttpServletRequest request) {
    // Check first to see if the user object is in the request.
    User user = (User) request.getAttribute(SESSION_USER);
    if (user != null)
        return user;

    // If not, get it from the session.
    user = (User) request.getSession().getAttribute(SESSION_USER);

    if (user != null) {
        // Add the user to the request. This prevents race conditions in which long-ish lasting requests have the
        // user object swiped from them by a quicker (logout) request.
        request.setAttribute(SESSION_USER, user);
    }/*from   ww w .java  2s .co m*/
    return user;
}

From source file:org.itracker.web.util.LoginUtilities.java

public static int getRequestAuthType(HttpServletRequest request) {
    int authType = AuthenticationConstants.AUTH_TYPE_UNKNOWN;

    try {/*from  www. j a  v a2 s  .c om*/
        if (request.getAttribute(Constants.AUTH_TYPE_KEY) != null) {
            authType = (Integer) request.getAttribute(Constants.AUTH_TYPE_KEY);
        }
        if (request.getParameter(Constants.AUTH_TYPE_KEY) != null) {
            authType = Integer.valueOf(request.getParameter(Constants.AUTH_TYPE_KEY));
        }
    } catch (Exception e) {
        logger.debug("Error retrieving auth type while checking auto login.  " + e.getMessage());
    }

    return authType;
}

From source file:com.ofbizcn.securityext.login.LoginEvents.java

/**
 * Save USERNAME and PASSWORD for use by auth pages even if we start in non-auth pages.
 *
 * @param request The HTTP request object for the current JSP or Servlet request.
 * @param response The HTTP response object for the current JSP or Servlet request.
 * @return String/*w ww . j av  a 2  s.com*/
 */
public static String saveEntryParams(HttpServletRequest request, HttpServletResponse response) {
    GenericValue userLogin = (GenericValue) request.getSession().getAttribute("userLogin");
    HttpSession session = request.getSession();
    Delegator delegator = (Delegator) request.getAttribute("delegator");

    // save entry login parameters if we don't have a valid login object
    if (userLogin == null) {

        String username = request.getParameter("USERNAME");
        String password = request.getParameter("PASSWORD");

        if ((username != null) && ("true".equalsIgnoreCase(EntityUtilProperties
                .getPropertyValue("security.properties", "username.lowercase", delegator)))) {
            username = username.toLowerCase();
        }
        if ((password != null) && ("true".equalsIgnoreCase(EntityUtilProperties
                .getPropertyValue("security.properties", "password.lowercase", delegator)))) {
            password = password.toLowerCase();
        }

        // save parameters into the session - so they can be used later, if needed
        if (username != null)
            session.setAttribute("USERNAME", username);
        if (password != null)
            session.setAttribute("PASSWORD", password);

    } else {
        // if the login object is valid, remove attributes
        session.removeAttribute("USERNAME");
        session.removeAttribute("PASSWORD");
    }

    return "success";
}

From source file:grails.converters.JSON.java

/**
 * Parses the given request's InputStream and returns ether a JSONObject or a JSONArry
 *
 * @param request the JSON Request/*from  www  .j  a  v  a  2  s  . c o m*/
 * @return ether a JSONObject or a JSONArray - depending on the given JSON
 * @throws ConverterException when the JSON content is not valid
 */
public static Object parse(HttpServletRequest request) throws ConverterException {
    Object json = request.getAttribute(CACHED_JSON);
    if (json != null) {
        return json;
    }

    String encoding = request.getCharacterEncoding();
    if (encoding == null) {
        encoding = Converter.DEFAULT_REQUEST_ENCODING;
    }
    try {
        PushbackInputStream pushbackInputStream = null;
        int firstByte = -1;
        try {
            pushbackInputStream = new PushbackInputStream(request.getInputStream());
            firstByte = pushbackInputStream.read();
        } catch (IOException ioe) {
        }

        if (firstByte == -1) {
            return new JSONObject();
        }

        pushbackInputStream.unread(firstByte);
        json = parse(pushbackInputStream, encoding);
        request.setAttribute(CACHED_JSON, json);
        return json;
    } catch (IOException e) {
        throw new ConverterException("Error parsing JSON", e);
    }
}

From source file:de.knowwe.jspwiki.KnowWEPlugin.java

private static boolean isFullParse(HttpServletRequest httpRequest) {
    String parse = UserContextUtil.getParameters(httpRequest).get("parse");
    Object fullParseFired = httpRequest.getAttribute(FULL_PARSE_FIRED);
    return parse != null && (parse.equals("full") || parse.equals("true")) && fullParseFired == null;
}

From source file:org.itracker.web.util.LoginUtilities.java

/**
 * get current user from request-attribute currUser, if not set from request-session
 *
 * @return current user or null if unauthenticated
 * @throws NullPointerException if the request was null
 *//* w  ww  .  j ava 2 s  .co m*/
@Deprecated
public static User getCurrentUser(HttpServletRequest request) {

    final String remoteUser = request.getRemoteUser();
    if (null == remoteUser) {
        return null;
    }
    User currUser = (User) request.getAttribute("currUser");
    if (null != currUser && currUser.getLogin().equals(remoteUser)) {
        if (logger.isDebugEnabled()) {
            logger.debug("found user in request: " + remoteUser);
        }
    }
    if (null == currUser) {
        currUser = (User) request.getSession().getAttribute("currUser");
        if (null != currUser && currUser.getLogin().equals(remoteUser)) {
            if (logger.isDebugEnabled()) {
                logger.debug("found user in session: " + remoteUser);
            }
        }
    }
    if (null == currUser) {
        currUser = ServletContextUtils.getItrackerServices().getUserService().getUserByLogin(remoteUser);
        if (null != currUser && currUser.getLogin().equals(remoteUser)) {
            if (logger.isDebugEnabled()) {
                logger.debug("found user by login: " + remoteUser);
            }
        }
    }

    return currUser;
}