Example usage for javax.servlet ServletRequest removeAttribute

List of usage examples for javax.servlet ServletRequest removeAttribute

Introduction

In this page you can find the example usage for javax.servlet ServletRequest removeAttribute.

Prototype

public void removeAttribute(String name);

Source Link

Document

Removes an attribute from this request.

Usage

From source file:com.netspective.sparx.util.HttpUtils.java

public static void includeServletResourceContent(Writer writer, HttpServletValueContext vc, String includePath,
        String valueContextAttrName) throws IOException, ServletException {
    ServletRequest request = vc.getRequest();
    ServletResponse response = vc.getResponse();

    RequestDispatcher rd = request.getRequestDispatcher(includePath);

    if (writer != response.getWriter())
        response = new AlternateOutputDestServletResponse(writer, response);

    request.setAttribute(valueContextAttrName, vc);
    rd.include(request, response);/*from   w  ww .ja v a  2  s  . c  om*/
    request.removeAttribute(valueContextAttrName);
}

From source file:org.withinsea.izayoi.adapter.springmvc.SpringIzayoiDispatcherFilter.java

@Override
public void doFilter(ServletRequest req, ServletResponse resp, final FilterChain chain)
        throws IOException, ServletException {
    req.setAttribute(CARRIER_CHAIN_ATTR, chain);
    service(req, resp);//from   w  w w . ja v  a  2s.  c  o  m
    req.removeAttribute(CARRIER_CHAIN_ATTR);
}

From source file:com.jaspersoft.jasperserver.war.tags.BaseTagSupport.java

protected void restoreRequestAttributes(Map restoreMap) {
    ServletRequest request = pageContext.getRequest();
    for (Iterator it = restoreMap.entrySet().iterator(); it.hasNext();) {
        Map.Entry entry = (Map.Entry) it.next();
        String attribute = (String) entry.getKey();
        Object value = entry.getValue();
        if (value == NO_ATTRIBUTE) {
            request.removeAttribute(attribute);
        } else {// w w w .j a  va  2  s .  c  om
            request.setAttribute(attribute, value);
        }
    }
}

From source file:com.mtgi.analytics.servlet.BehaviorTrackingListener.java

public void requestDestroyed(ServletRequestEvent event) {
    if (adapters != null) {
        ServletRequest request = event.getServletRequest();
        BehaviorEvent[] events = (BehaviorEvent[]) request.getAttribute(ATT_EVENTS);
        if (events == null) {
            log.error("no behavior events stored in the current request ("
                    + ((HttpServletRequest) request).getRequestURI());
        } else {//  ww w .j  a  v a2  s . c  o  m
            request.removeAttribute(ATT_EVENTS);
            for (int i = adapters.length - 1; i >= 0; --i)
                try {
                    adapters[i].stop(events[i]);
                } catch (Exception e) {
                    log.error("Error stopping http event", e);
                }
        }
    }
}

From source file:com.day.cq.wcm.foundation.forms.FormsHelper.java

/**
 * Sets a flag to redirect to the HTTP referrer after the forward of a form
 * POST request. This will usually only be used if no explicit redirect is
 * already given in the ":redirect" parameter used by the Sling POST servlet.
 * /*from  w  w w  . ja  va2  s  .  c  o  m*/
 * @since 5.5
 * 
 * @param request
 *            current request
 * @param redirectToReferrer
 *            <code>true</code> to enable the redirect to the referrer
 */
public static void setRedirectToReferrer(ServletRequest request, boolean redirectToReferrer) {
    if (redirectToReferrer) {
        request.setAttribute(REQ_ATTR_REDIRECT_TO_REFERRER, "true");
    } else {
        request.removeAttribute(REQ_ATTR_REDIRECT_TO_REFERRER);
    }
}

From source file:com.vilt.spring.context.response.OncePerRequestContextFilter.java

/**
 * This <code>doFilter</code> implementation stores a request attribute for
 * "already filtered", proceeding without filtering again if the attribute
 * is already there./*from  w w  w. ja  v  a2 s  .  com*/
 * 
 * @see #getAlreadyFilteredAttributeName
 * @see #doFilterInternal
 */
public final void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
        throws ServletException, IOException {
    if (!(request instanceof HttpServletRequest) || !(response instanceof HttpServletResponse)) {
        throw new ServletException("OncePerRequestContextFilter just supports HTTP requests");
    }
    HttpServletRequest httpRequest = (HttpServletRequest) request;
    HttpServletResponse httpResponse = (HttpServletResponse) response;

    String alreadyFilteredAttributeName = getAlreadyFilteredAttributeName();
    if (request.getAttribute(alreadyFilteredAttributeName) != null) {
        // Proceed without invoking this filter...
        filterChain.doFilter(request, response);
        return;
    }
    // Do invoke this filter...
    request.setAttribute(alreadyFilteredAttributeName, TRUE);
    try {
        doFilterInternal(httpRequest, httpResponse, filterChain);
    } finally {
        // Remove the "already filtered" request attribute for this
        // request.
        request.removeAttribute(alreadyFilteredAttributeName);
    }
}

From source file:com.xpn.xwiki.web.ActionFilter.java

/**
 * {@inheritDoc}/*from  w ww . ja va  2  s.c  o  m*/
 * 
 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
 */
@SuppressWarnings("unchecked")
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    // Only HTTP requests can be dispatched.
    if (request instanceof HttpServletRequest
            && !Boolean.valueOf((String) request.getAttribute(ATTRIBUTE_ACTION_DISPATCHED))) {
        HttpServletRequest hrequest = (HttpServletRequest) request;
        Enumeration<String> parameterNames = hrequest.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String parameter = parameterNames.nextElement();
            if (parameter.startsWith(ACTION_PREFIX)) {
                String targetURL = getTargetURL(hrequest, parameter);
                RequestDispatcher dispatcher = hrequest.getRequestDispatcher(targetURL);
                if (dispatcher != null) {
                    LOG.debug("Forwarding request to " + targetURL);
                    request.setAttribute(ATTRIBUTE_ACTION_DISPATCHED, "true");
                    dispatcher.forward(hrequest, response);
                    // Allow multiple calls to this filter as long as they are not nested.
                    request.removeAttribute(ATTRIBUTE_ACTION_DISPATCHED);
                    // If the request was forwarder to another path, don't continue the normal processing chain.
                    return;
                }
            }
        }
    }
    // Let the request pass through unchanged.
    chain.doFilter(request, response);
}

From source file:com.lti.system.MyLogoutFilter.java

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    if (!(request instanceof HttpServletRequest)) {
        throw new ServletException("Can only process HttpServletRequest");
    }//from www.  j  a va2  s .co m

    if (!(response instanceof HttpServletResponse)) {
        throw new ServletException("Can only process HttpServletResponse");
    }

    HttpServletRequest httpRequest = (HttpServletRequest) request;
    HttpServletResponse httpResponse = (HttpServletResponse) response;

    if (requiresLogout(httpRequest, httpResponse)) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();

        if (logger.isDebugEnabled()) {
            logger.debug("Logging out user '" + auth + "' and redirecting to logout page");
        }

        for (int i = 0; i < handlers.length; i++) {
            handlers[i].logout(httpRequest, httpResponse, auth);
        }

        Cookie cookie = new Cookie("jforumSSOCookie", null);
        cookie.setMaxAge(0);
        cookie.setPath("/jforum");
        httpResponse.addCookie(cookie);

        cookie = new Cookie("jforumSSOGroupCookie", null);
        cookie.setMaxAge(0);
        cookie.setPath("/jforum");
        httpResponse.addCookie(cookie);

        request.removeAttribute("legalDate");

        sendRedirect(httpRequest, httpResponse, logoutSuccessUrl);

        return;
    }

    chain.doFilter(request, response);
}

From source file:com.netspective.sparx.navigate.NavigationPage.java

/**
 * Main method for handling the logic and content of the page. Generally, a navigation page is broken into
 * several sections: header, metadata, body, and footer. If there are no forwards/redirects configured, the page
 * sections are handled by calling the following methods: <code>handlePageMetaData()</code>, <code>handlePageHeader()</code>,
 * <code>handlePageBody()</code>, and <code>handlePageFooter()</code> methods.
 *
 * @param writer Writer object to write the page output to
 * @param nc     current navigation context
 *//*w  w w.  j a  va  2s  .c  o m*/
public void handlePage(Writer writer, NavigationContext nc) throws ServletException, IOException {
    Flags flags = (Flags) nc.getActiveState().getFlags();

    enterPage(nc);
    if (getBodyType().getValueIndex() == NavigationPageBodyType.FORWARD) {
        // if we're forwarding to another resource we don't want to put anything into the response otherwise
        // there will be an illegal state exception -- so, we don't create headers, footers, etc because that's
        // the user's responsibility in the forwarded resource.

        String forwardUrl = getForward().getTextValue(nc);
        ServletRequest req = nc.getRequest();
        RequestDispatcher rd = req.getRequestDispatcher(forwardUrl);
        req.setAttribute(REQATTRNAME_NAVIGATION_CONTEXT, nc);
        rd.forward(req, nc.getResponse());
        req.removeAttribute(REQATTRNAME_NAVIGATION_CONTEXT);
    } else if (bodyAffectsNavigationContext(nc)) {
        // render the body first and let it modify the navigation context
        StringWriter body = new StringWriter();
        boolean hasError = false;
        try {
            handlePageBody(body, nc);
        } catch (Exception e) {
            getLog().error("Error occurred while handling the page.", e);
            if (!findErrorPage(nc, e))
                nc.setErrorPageException(getOwner().getDefaultErrorPage(), e, e.getClass());
            nc.getErrorPage().handlePageBody(writer, nc);
            hasError = true;
        }

        if (!hasError && !nc.isRedirected()) {
            if (flags.flagIsSet(Flags.HANDLE_META_DATA))
                handlePageMetaData(writer, nc);
            if (flags.flagIsSet(Flags.HANDLE_HEADER))
                handlePageHeader(writer, nc);
            writer.write(body.getBuffer().toString());
            if (flags.flagIsSet(Flags.HANDLE_FOOTER))
                handlePageFooter(writer, nc);
        }

        // try and do an early GC if possible
        body = null;
    } else {
        if (flags.flagIsSet(Flags.HANDLE_META_DATA))
            handlePageMetaData(writer, nc);
        if (flags.flagIsSet(Flags.HANDLE_HEADER))
            handlePageHeader(writer, nc);
        try {
            handlePageBody(writer, nc);
        } catch (Exception e) {
            getLog().error("Error occurred while handling the page.", e);
            if (!findErrorPage(nc, e))
                nc.setErrorPageException(getOwner().getDefaultErrorPage(), e, e.getClass());
            nc.getErrorPage().handlePageBody(writer, nc);
        }
        if (flags.flagIsSet(Flags.HANDLE_FOOTER))
            handlePageFooter(writer, nc);
    }
    exitPage(nc);
}

From source file:gov.nih.nci.cabig.caaers.web.security.FabricatedAuthenticationFilter.java

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    if (!(request instanceof HttpServletRequest)) {
        throw new ServletException("Can only process HttpServletRequest");
    }//from  w  w w . j  a va  2s . co m
    if (!(response instanceof HttpServletResponse)) {
        throw new ServletException("Can only process HttpServletResponse");
    }

    HttpServletRequest httpRequest = (HttpServletRequest) request;
    HttpServletResponse httpResponse = (HttpServletResponse) response;
    HttpSession httpSession = httpRequest.getSession();
    final SecurityContext contextBeforeExec = SecurityContextHolder.getContext();
    Authentication authBeforeExec = contextBeforeExec.getAuthentication();

    OriginalAuthenticationHolder.setAuthentication(authBeforeExec);

    try {
        if (request.getAttribute(FILTER_APPLIED) == null) {
            doProcessing(httpRequest, httpResponse, chain);
            request.setAttribute(FILTER_APPLIED, true);
        }

        Authentication fabricatedAuth = SecurityContextHolder.getContext().getAuthentication();
        if (fabricatedAuth != null) {
            GrantedAuthority[] fabAuthorities = fabricatedAuth.getAuthorities();
            if (fabAuthorities == null || fabAuthorities.length < 1) {
                throw new AccessDeniedException(
                        "Your account permissions do not provide you access to this page.");
            }
        }

        prepareRolesCollections(httpRequest);
        chain.doFilter(httpRequest, httpResponse);
    } finally {
        request.removeAttribute(FILTER_APPLIED);
        SecurityContextHolder.setContext(contextBeforeExec);
        SecurityContextHolder.getContext().setAuthentication(authBeforeExec);
        if (httpSession != null) {
            httpSession.setAttribute(HttpSessionContextIntegrationFilter.ACEGI_SECURITY_CONTEXT_KEY,
                    SecurityContextHolder.getContext());
        }
        OriginalAuthenticationHolder.setAuthentication(null);
        CurrentEntityHolder.setEntity(null);
    }
    return;
}