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:com.benfante.taglib.frontend.utils.FlashHelper.java

public static void doSet(HttpServletRequest req, String code, String type, String attribute) {
    Map<String, String> flash = (Map<String, String>) req.getAttribute(attribute);
    if (flash == null)
        flash = new HashMap<String, String>();
    if (type == null) {
        type = DEFAULT_ERROR_TYPE;/* www  .ja  va 2 s. c  om*/
    }
    if (attribute == null) {
        attribute = DEFAULT_FLASH_ATTRIBUTE;
    }
    flash.put(type, code);
    req.setAttribute(attribute, flash);
}

From source file:net.sourceforge.fenixedu.presentationTier.Action.base.FenixContextDispatchAction.java

public static String getFromRequest(String parameter, HttpServletRequest request) {
    if (request.getParameter(parameter) != null) {
        return request.getParameter(parameter);
    } else if (request.getAttribute(parameter) != null) {
        if (request.getAttribute(parameter) instanceof String) {
            return (String) request.getAttribute(parameter);
        }/*from ww  w. j a va2 s  .  c o  m*/
    }
    return null;
}

From source file:de.itsvs.cwtrpc.core.CwtRpcUtils.java

public static RpcSessionInvalidationPolicy getRpcSessionInvalidationPolicy(HttpServletRequest request) {
    RpcSessionInvalidationPolicy policy;

    policy = (RpcSessionInvalidationPolicy) request.getAttribute(RPC_SESSION_INVALIDATION_POLICY_ATTR_NAME);
    if (policy == null) {
        policy = getDefaultRpcSessionInvalidationPolicy();
    }/*from   w  ww  .  jav  a  2 s  .  c  o  m*/
    return policy;
}

From source file:net.sourceforge.fenixedu.presentationTier.Action.base.FenixContextDispatchAction.java

public static Boolean getFromRequestBoolean(String parameter, HttpServletRequest request) {
    return (request.getParameter(parameter) != null) ? Boolean.valueOf(request.getParameter(parameter))
            : (Boolean) request.getAttribute(parameter);
}

From source file:com.redhat.rhn.common.util.ServletUtils.java

/** util function to take a servlet request and compute the path
 * relative to the server (not relative to the webapp).  needed
 * when getPath() is relative to the webapp instead of the server
 * @param req The request to inspect/* w  w  w .  java2 s.  c o  m*/
 * @return The path requested.
 */
public static String getRequestPath(HttpServletRequest req) {
    try {
        String requestUri = (String) req.getAttribute("javax.servlet.forward.request_uri");
        if (StringUtils.isBlank(requestUri)) {
            requestUri = new URL(req.getRequestURL().toString()).getPath();
        }
        return requestUri;
    } catch (Exception e) {
        throw new IllegalArgumentException("Unable to parse url: " + req.getRequestURL());
    }
}

From source file:info.magnolia.cms.util.ServletUtil.java

/**
 * Returns the original request url. If the request has been forwarded it reconstructs the url from  request
 * attributes. The returned url is not decoded.
 *//*ww w  . j  a  va  2  s  .  c om*/
public static String getOriginalRequestURLIncludingQueryString(HttpServletRequest request) {
    if (request.getAttribute(FORWARD_REQUEST_URI_ATTRIBUTE) != null) {
        StringBuilder builder = new StringBuilder();

        String scheme = request.getScheme();
        builder.append(scheme).append("://").append(request.getServerName());

        int port = request.getServerPort();
        if ((scheme.equalsIgnoreCase("http") && port == 80)
                || (scheme.equalsIgnoreCase("https") && port == 443)) {
            // adding port is not necessary
        } else {
            builder.append(":").append(port);
        }

        String requestUri = (String) request.getAttribute(FORWARD_REQUEST_URI_ATTRIBUTE);
        builder.append(requestUri);

        String queryString = (String) request.getAttribute(FORWARD_QUERY_STRING_ATTRIBUTE);
        if (StringUtils.isNotEmpty(queryString)) {
            builder.append("?").append(queryString);
        }

        return builder.toString();
    }
    StringBuilder builder = new StringBuilder();
    builder.append(request.getRequestURL());
    String queryString = request.getQueryString();
    if (StringUtils.isNotEmpty(queryString)) {
        builder.append("?").append(queryString);
    }
    return builder.toString();
}

From source file:org.keycloak.example.CamelClient.java

public static String sendRequest(HttpServletRequest req) throws CxfRsClient.Failure {
    KeycloakSecurityContext session = (KeycloakSecurityContext) req
            .getAttribute(KeycloakSecurityContext.class.getName());

    HttpClient client = new HttpClientBuilder().disableTrustManager().build();

    StringBuilder sb = new StringBuilder();
    try {//from   www.  ja v  a 2  s  . c  o  m
        // Initially let's invoke a simple Camel-Jetty exposed endpoint
        HttpGet get = new HttpGet("http://localhost:8383/admin-camel-endpoint");
        get.addHeader("Authorization", "Bearer " + session.getTokenString());
        try {
            HttpResponse response = client.execute(get);
            if (response.getStatusLine().getStatusCode() != 200) {
                return "There was a failure processing request.  You either didn't configure Keycloak properly or you don't have admin permission? Status code is "
                        + response.getStatusLine().getStatusCode();
            }
            HttpEntity entity = response.getEntity();
            InputStream is = entity.getContent();
            try {
                sb.append(getStringFromInputStream(is));
            } finally {
                is.close();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        // Here we invoke a Jetty endpoint, published using Camel RestDSL
        get = new HttpGet("http://localhost:8484/restdsl/hello/world");
        get.addHeader("Authorization", "Bearer " + session.getTokenString());
        try {
            HttpResponse response = client.execute(get);
            if (response.getStatusLine().getStatusCode() != 200) {
                return "There was a failure processing request with the RestDSL endpoint.  You either didn't configure Keycloak properly or you don't have admin permission? Status code is "
                        + response.getStatusLine().getStatusCode();
            }
            HttpEntity entity = response.getEntity();
            InputStream is = entity.getContent();
            try {
                sb.append(getStringFromInputStream(is));
            } finally {
                is.close();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    } finally {
        client.getConnectionManager().shutdown();
    }

    return sb.toString();
}

From source file:io.lavagna.web.helper.UserSession.java

static UserWithPermission fetchFromRequest(HttpServletRequest request, UserService userService) {

    Object userAttr = request.getAttribute(UserWithPermission.class.getName());

    if (userAttr != null) {
        return (UserWithPermission) userAttr;
    } else {//from  w  ww.  ja va 2  s . c  o m
        UserWithPermission res = userService.findUserWithPermission(getUserId(request));
        request.setAttribute(UserWithPermission.class.getName(), res);
        return res;
    }
}

From source file:org.keycloak.example.oauth.ProductDatabaseClient.java

public static List<String> getProducts(HttpServletRequest request, String accessToken) throws Failure {
    KeycloakSecurityContext session = (KeycloakSecurityContext) request
            .getAttribute(KeycloakSecurityContext.class.getName());

    // The ServletOAuthClient is obtained by getting a context attribute
    // that is set in the Bootstrap context listener in this project.
    // You really should come up with a better way to initialize
    // and obtain the ServletOAuthClient.  I actually suggest downloading the ServletOAuthClient code
    // and take a look how it works. You can also take a look at third-party-cdi example
    ServletOAuthClient oAuthClient = (ServletOAuthClient) request.getServletContext()
            .getAttribute(ServletOAuthClient.class.getName());
    HttpClient client = new DefaultHttpClient();

    HttpGet get = new HttpGet(UriUtils.getOrigin(request.getRequestURL().toString()) + "/database/products");
    get.addHeader("Authorization", "Bearer " + accessToken);
    try {/* w w w  .j a v  a2  s.  c o  m*/
        HttpResponse response = client.execute(get);
        if (response.getStatusLine().getStatusCode() != 200) {
            throw new Failure(response.getStatusLine().getStatusCode());
        }
        HttpEntity entity = response.getEntity();
        InputStream is = entity.getContent();
        try {
            return JsonSerialization.readValue(is, TypedList.class);
        } finally {
            is.close();
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:net.sourceforge.fenixedu.presentationTier.Action.utils.RequestUtils.java

public static String getAndSetStringToRequest(HttpServletRequest request, String name) {
    String parameter = request.getParameter(name);
    if (parameter == null) {
        parameter = (String) request.getAttribute(name);
    }/*  w ww. j  ava  2  s .c  o m*/
    request.setAttribute(name, parameter);
    return parameter;
}