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:ste.web.beanshell.jetty.BeanShellUtils.java

public static void setup(final Interpreter interpreter, final HttpServletRequest request,
        final HttpServletResponse response) throws EvalError, IOException {
    ///*  w w  w.j  ava 2  s .c  o m*/
    // Set attributes as script variables
    //
    String k, key;
    for (Enumeration e = request.getAttributeNames(); e.hasMoreElements();) {
        k = (String) e.nextElement();
        key = normalizeVariableName(k);
        interpreter.set(key, request.getAttribute(k));
    }

    //
    // Set request parameters as script variables. Note that parameters
    // override attributes
    //
    for (Enumeration e = request.getParameterNames(); e.hasMoreElements();) {
        k = (String) e.nextElement();
        key = normalizeVariableName(k);
        interpreter.set(key, request.getParameter(k));
    }

    interpreter.set(VAR_REQUEST, request);
    interpreter.set(VAR_RESPONSE, response);
    interpreter.set(VAR_SESSION, request.getSession(false));
    interpreter.set(VAR_OUT, response.getWriter());
    interpreter.set(VAR_LOG, log);
    if (hasJSONBody(request)) {
        interpreter.set(VAR_BODY, getJSONBody(request.getInputStream()));
    }
}

From source file:com.github.ziplet.filter.compression.CompressingStreamFactory.java

/**
 * Determines best content encoding for the response, based on the request
 * -- in particular, based on its "Accept-Encoding" header.
 *
 * @param httpRequest request/*ww w  . j ava  2  s. c  o m*/
 * @return best content encoding
 */
static String getBestContentEncoding(HttpServletRequest httpRequest) {

    String forcedEncoding = (String) httpRequest.getAttribute(CompressingFilter.FORCE_ENCODING_KEY);
    String bestEncoding;
    if (forcedEncoding != null) {

        bestEncoding = forcedEncoding;

    } else {

        String acceptEncodingHeader = httpRequest
                .getHeader(CompressingHttpServletResponse.ACCEPT_ENCODING_HEADER);
        if (acceptEncodingHeader == null) {

            bestEncoding = NO_ENCODING;

        } else {

            bestEncoding = bestEncodingCache.get(acceptEncodingHeader);

            if (bestEncoding == null) {

                // No cached value; must parse header to determine best encoding
                // I don't synchronize on bestEncodingCache; it's not worth it to avoid the rare case where
                // two thread get in here and both parse the header. It's only a tiny bit of extra work, and
                // avoids the synchronization overhead.

                if (acceptEncodingHeader.indexOf((int) ',') >= 0) {
                    // multiple encodings are accepted
                    bestEncoding = selectBestEncoding(acceptEncodingHeader);
                } else {
                    // one encoding is accepted
                    bestEncoding = parseBestEncoding(acceptEncodingHeader);
                }

                bestEncodingCache.put(acceptEncodingHeader, bestEncoding);
            }
        }
    }

    // User-specified encoding might not be supported
    if (!isSupportedResponseContentEncoding(bestEncoding)) {
        bestEncoding = NO_ENCODING;
    }

    return bestEncoding;
}

From source file:org.hdiv.util.HDIVUtil.java

/**
 * Returns BaseURL value from <code>HttpServletRequest</code>
 * //from   w w w  . ja va 2  s .  c  o  m
 * @param request
 *            HttpServletRequest
 * @return String
 */
public static String getBaseURL(HttpServletRequest request) {

    String baseURL = (String) request.getAttribute(BASEURL_REQUEST_KEY);
    return baseURL;
}

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

public static void setUsername(HttpServletRequest request, HttpServletResponse response) {
    HttpSession session = request.getSession();
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    String domain = EntityUtilProperties.getPropertyValue("url.properties", "cookie.domain", delegator);
    // first try to get the username from the cookie
    synchronized (session) {
        if (UtilValidate.isEmpty(getUsername(request))) {
            // create the cookie and send it back
            Cookie cookie = new Cookie(usernameCookieName, request.getParameter("USERNAME"));
            cookie.setMaxAge(60 * 60 * 24 * 365);
            cookie.setPath("/");
            cookie.setDomain(domain);/*w  w  w. jav a  2 s.c om*/
            response.addCookie(cookie);
        }
    }
}

From source file:org.hdiv.util.HDIVUtil.java

/**
 * Returns RequestURI value from <code>HttpServletRequest</code>
 * /*from w w  w .j a  v a2s  .  c o  m*/
 * @param request
 *            HttpServletRequest
 * @return String
 */
public static String getRequestURI(HttpServletRequest request) {

    String requestURI = (String) request.getAttribute(REQUESTURI_REQUEST_KEY);
    if (requestURI == null) {
        throw new HDIVException("RequestURI has not been initialized in request.");
    }
    return requestURI;
}

From source file:cn.vlabs.duckling.vwb.VWBContext.java

public static VWBContext getContext(HttpServletRequest request) {
    return (VWBContext) request.getAttribute(CONTEXT_KEY);
}

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

public static Boolean allowSaveLogin(HttpServletRequest request) {
    return (boolean) request.getAttribute("allowSaveLogin");
}

From source file:com.activecq.samples.clientcontext.impl.ClientContextBuilderImpl.java

private static String getParameterOrAttribute(HttpServletRequest request, String key, String dfault) {
    String value = null;//  w w w . j  a v a 2s  .  c  o m
    if (request == null) {
        return value;
    }

    if (StringUtils.isNotBlank(request.getParameter(key))) {
        value = request.getParameter(key);
    } else if (StringUtils.isNotBlank((String) request.getAttribute(key))) {
        value = (String) request.getAttribute(key);
    }

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

    return value;
}

From source file:org.hdiv.util.HDIVUtil.java

/**
 * Returns true if a data composer object exist in <code>HttpServletRequest</code>
 * /* w  ww.java2  s .  c  o  m*/
 * @param request
 *            HttpServletRequest
 * @return boolean
 */
public static boolean isDataComposer(HttpServletRequest request) {

    IDataComposer requestDataComposer = (IDataComposer) request.getAttribute(DATACOMPOSER_REQUEST_KEY);
    return requestDataComposer != null;
}

From source file:com.threewks.thundr.request.servlet.ServletSupport.java

/**
 * Get the full set of request attirubtes in the given request.
 * /*w  w  w  .j  a va  2  s.c o  m*/
 * @param request
 * @return
 */
public static Map<String, Object> getAttributes(HttpServletRequest request) {
    Map<String, Object> attributes = new HashMap<String, Object>();
    if (request != null) {
        Enumeration<String> names = request.getAttributeNames();
        while (names.hasMoreElements()) {
            String name = names.nextElement();
            attributes.put(name, request.getAttribute(name));
        }
    }
    return attributes;
}