Java Code Examples for javax.servlet.ServletRequest#removeAttribute()

The following examples show how to use javax.servlet.ServletRequest#removeAttribute() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OncePerRequestFilter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * This {@code doFilter} implementation stores a request attribute for
 * "already filtered", proceeding without filtering again if the
 * attribute is already there.
 * @see #getAlreadyFilteredAttributeName
 * @see #shouldNotFilter
 * @see #doFilterInternal
 */
@Override
public final void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
		throws ServletException, IOException {

	if (!(request instanceof HttpServletRequest) || !(response instanceof HttpServletResponse)) {
		throw new ServletException("OncePerRequestFilter just supports HTTP requests");
	}
	HttpServletRequest httpRequest = (HttpServletRequest) request;
	HttpServletResponse httpResponse = (HttpServletResponse) response;

	String alreadyFilteredAttributeName = getAlreadyFilteredAttributeName();
	boolean hasAlreadyFilteredAttribute = request.getAttribute(alreadyFilteredAttributeName) != null;

	if (hasAlreadyFilteredAttribute || skipDispatch(httpRequest) || shouldNotFilter(httpRequest)) {

		// Proceed without invoking this filter...
		filterChain.doFilter(request, response);
	}
	else {
		// Do invoke this filter...
		request.setAttribute(alreadyFilteredAttributeName, Boolean.TRUE);
		try {
			doFilterInternal(httpRequest, httpResponse, filterChain);
		}
		finally {
			// Remove the "already filtered" request attribute for this request.
			request.removeAttribute(alreadyFilteredAttributeName);
		}
	}
}
 
Example 2
Source File: OncePerRequestFilter.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * This {@code doFilter} implementation stores a request attribute for
 * "already filtered", proceeding without filtering again if the
 * attribute is already there.
 * @see #getAlreadyFilteredAttributeName
 * @see #shouldNotFilter
 * @see #doFilterInternal
 */
@Override
public final void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
		throws ServletException, IOException {

	if (!(request instanceof HttpServletRequest) || !(response instanceof HttpServletResponse)) {
		throw new ServletException("OncePerRequestFilter just supports HTTP requests");
	}
	HttpServletRequest httpRequest = (HttpServletRequest) request;
	HttpServletResponse httpResponse = (HttpServletResponse) response;

	String alreadyFilteredAttributeName = getAlreadyFilteredAttributeName();
	boolean hasAlreadyFilteredAttribute = request.getAttribute(alreadyFilteredAttributeName) != null;

	if (hasAlreadyFilteredAttribute || skipDispatch(httpRequest) || shouldNotFilter(httpRequest)) {

		// Proceed without invoking this filter...
		filterChain.doFilter(request, response);
	}
	else {
		// Do invoke this filter...
		request.setAttribute(alreadyFilteredAttributeName, Boolean.TRUE);
		try {
			doFilterInternal(httpRequest, httpResponse, filterChain);
		}
		finally {
			// Remove the "already filtered" request attribute for this request.
			request.removeAttribute(alreadyFilteredAttributeName);
		}
	}
}
 
Example 3
Source File: RestAsyncListener.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Override
public void onTimeout(AsyncEvent event) throws IOException {
  // in this time, maybe:
  // 1.invocation in executor's queue
  // 2.already executing in executor
  // 3.already send response
  // to avoid concurrent, must lock request
  ServletRequest request = event.getSuppliedRequest();
  HttpServletRequestEx requestEx = (HttpServletRequestEx) request.getAttribute(RestConst.REST_REQUEST);
  LOGGER.error("Rest request timeout, method {}, path {}.", requestEx.getMethod(), requestEx.getRequestURI());

  // Waiting till executing in executor done. This operation may block container pool and make timeout requests in executor's
  // queue getting executed, and will cause bad performance. So default timeout is setting to -1 to disable timeout.
  synchronized (requestEx) {
    ServletResponse response = event.getAsyncContext().getResponse();
    if (!response.isCommitted()) {
      // invocation in executor's queue
      response.setContentType(MediaType.APPLICATION_JSON);

      // we don't know if developers declared one statusCode in contract
      // so we use cse inner statusCode here
      ((HttpServletResponse) response).setStatus(Status.INTERNAL_SERVER_ERROR.getStatusCode());
      PrintWriter out = response.getWriter();
      out.write(TIMEOUT_MESSAGE);
      response.flushBuffer();
    }

    request.removeAttribute(RestConst.REST_REQUEST);
  }

  LOGGER.error("Rest request timeout committed, method {}, path {}.", requestEx.getMethod(), requestEx.getRequestURI());
}
 
Example 4
Source File: AWSXRayServletFilter.java    From aws-xray-sdk-java with Apache License 2.0 5 votes vote down vote up
@Override
public void doFilter(
    ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    if (logger.isDebugEnabled()) {
        logger.debug("AWSXRayServletFilter is beginning to process request: " + request.toString());
    }
    Segment segment = preFilter(request, response);

    try {
        chain.doFilter(request, response);
    } catch (Throwable e) {
        if (segment != null) {
            segment.addException(e);
        }
        throw e;
    } finally {
        if (request.isAsyncStarted()) {
            if (segment != null) {
                request.setAttribute(AWSXRayServletAsyncListener.ENTITY_ATTRIBUTE_KEY, segment);
            } else {
                request.removeAttribute(AWSXRayServletAsyncListener.ENTITY_ATTRIBUTE_KEY);
            }
            try {
                request.getAsyncContext().addListener(listener);
                if (recorder != null) {
                    recorder.clearTraceEntity();
                }
            } catch (IllegalStateException ise) {
                // race condition that occurs when async processing finishes before adding the listener
                postFilter(request, response);
            }
        } else {
            postFilter(request, response);
        }

        if (logger.isDebugEnabled()) {
            logger.debug("AWSXRayServletFilter is finished processing request: " + request.toString());
        }
    }
}
 
Example 5
Source File: OncePerRequestFilter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * This {@code doFilter} implementation stores a request attribute for
 * "already filtered", proceeding without filtering again if the
 * attribute is already there.
 * @see #getAlreadyFilteredAttributeName
 * @see #shouldNotFilter
 * @see #doFilterInternal
 */
@Override
public final void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
		throws ServletException, IOException {

	if (!(request instanceof HttpServletRequest) || !(response instanceof HttpServletResponse)) {
		throw new ServletException("OncePerRequestFilter just supports HTTP requests");
	}
	HttpServletRequest httpRequest = (HttpServletRequest) request;
	HttpServletResponse httpResponse = (HttpServletResponse) response;

	String alreadyFilteredAttributeName = getAlreadyFilteredAttributeName();
	boolean hasAlreadyFilteredAttribute = request.getAttribute(alreadyFilteredAttributeName) != null;

	if (hasAlreadyFilteredAttribute || skipDispatch(httpRequest) || shouldNotFilter(httpRequest)) {

		// Proceed without invoking this filter...
		filterChain.doFilter(request, response);
	}
	else {
		// Do invoke this filter...
		request.setAttribute(alreadyFilteredAttributeName, Boolean.TRUE);
		try {
			doFilterInternal(httpRequest, httpResponse, filterChain);
		}
		finally {
			// Remove the "already filtered" request attribute for this request.
			request.removeAttribute(alreadyFilteredAttributeName);
		}
	}
}
 
Example 6
Source File: RequestAttrPolicy.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
public static boolean setServletAttribute(ServletRequest attrContainer, String attrName, Object value) {
    if (value == AttrPolicy.VALUE_IGNORE) {
        return false;
    }
    if (value == AttrPolicy.VALUE_UNSET) {
        attrContainer.removeAttribute(attrName);
        return false;
    }
    attrContainer.setAttribute(attrName, value);
    return true;
}
 
Example 7
Source File: OncePerRequestFilter.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * This {@code doFilter} implementation stores a request attribute for
 * "already filtered", proceeding without filtering again if the
 * attribute is already there.
 * @see #getAlreadyFilteredAttributeName
 * @see #shouldNotFilter
 * @see #doFilterInternal
 */
@Override
public final void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
		throws ServletException, IOException {

	if (!(request instanceof HttpServletRequest) || !(response instanceof HttpServletResponse)) {
		throw new ServletException("OncePerRequestFilter just supports HTTP requests");
	}
	HttpServletRequest httpRequest = (HttpServletRequest) request;
	HttpServletResponse httpResponse = (HttpServletResponse) response;

	String alreadyFilteredAttributeName = getAlreadyFilteredAttributeName();
	boolean hasAlreadyFilteredAttribute = request.getAttribute(alreadyFilteredAttributeName) != null;

	if (hasAlreadyFilteredAttribute || skipDispatch(httpRequest) || shouldNotFilter(httpRequest)) {

		// Proceed without invoking this filter...
		filterChain.doFilter(request, response);
	}
	else {
		// Do invoke this filter...
		request.setAttribute(alreadyFilteredAttributeName, Boolean.TRUE);
		try {
			doFilterInternal(httpRequest, httpResponse, filterChain);
		}
		finally {
			// Remove the "already filtered" request attribute for this request.
			request.removeAttribute(alreadyFilteredAttributeName);
		}
	}
}
 
Example 8
Source File: RequestAttributeStoreServletFilter.java    From ontopia with Apache License 2.0 5 votes vote down vote up
@Override
public void doFilter(ServletRequest request, ServletResponse response,
                     FilterChain chain) throws IOException,
                                               ServletException {
  String repositoryId = getRepositoryId(request);
  String topicMapId = getTopicMapId(request);
  boolean readOnly = getReadOnly(request);
  String requestAttribute = getRequestAttribute(request);
  
  TopicMapStoreIF store;
  if (repositoryId == null)
    store = TopicMaps.createStore(topicMapId, readOnly);
  else
    store = TopicMaps.createStore(topicMapId, readOnly, repositoryId);      

  try {
    request.setAttribute(requestAttribute, store);
    chain.doFilter(request, response);
    if (!readOnly) store.commit();
  } catch (Exception e) {
    if (!readOnly) store.abort();      
    log.error("Exception thrown from doFilter.", e);      
  } finally {
    request.removeAttribute(requestAttribute);
    store.close();
  }
}
 
Example 9
Source File: OncePerRequestFilter.java    From spring-session with Apache License 2.0 5 votes vote down vote up
/**
 * This {@code doFilter} implementation stores a request attribute for "already
 * filtered", proceeding without filtering again if the attribute is already there.
 * @param request the request
 * @param response the response
 * @param filterChain the filter chain
 * @throws ServletException if request is not HTTP request
 * @throws IOException in case of I/O operation exception
 */
@Override
public final void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
		throws ServletException, IOException {

	if (!(request instanceof HttpServletRequest) || !(response instanceof HttpServletResponse)) {
		throw new ServletException("OncePerRequestFilter just supports HTTP requests");
	}
	HttpServletRequest httpRequest = (HttpServletRequest) request;
	HttpServletResponse httpResponse = (HttpServletResponse) response;
	String alreadyFilteredAttributeName = this.alreadyFilteredAttributeName;
	boolean hasAlreadyFilteredAttribute = request.getAttribute(alreadyFilteredAttributeName) != null;

	if (hasAlreadyFilteredAttribute) {
		if (DispatcherType.ERROR.equals(request.getDispatcherType())) {
			doFilterNestedErrorDispatch(httpRequest, httpResponse, filterChain);
			return;
		}
		// Proceed without invoking this filter...
		filterChain.doFilter(request, response);
	}
	else {
		// Do invoke this filter...
		request.setAttribute(alreadyFilteredAttributeName, Boolean.TRUE);
		try {
			doFilterInternal(httpRequest, httpResponse, filterChain);
		}
		finally {
			// Remove the "already filtered" request attribute for this request.
			request.removeAttribute(alreadyFilteredAttributeName);
		}
	}
}
 
Example 10
Source File: MonitorFilter.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void disposeDataRecord(ServletRequest req) { 

	if(debug) log("disposeDataRecord()"); //NOI18N

	// Remove the attributes used by the monitor - we need to do
	// this in case there is an error...
	req.removeAttribute(attNameRequest); 
	req.removeAttribute(attNameResponse); 
	req.removeAttribute(attNameFilter); 

	MonitorData monData = null;

	Stack stack = (Stack)(req.getAttribute(attNameMonData)); 
	req.removeAttribute(attNameMonData); 

	if(stack != null && !stack.empty()) { 
	    if(debug) { 
		log("found mondata stack"); //NOI18N	
		log("stack size=" + stack.size()); //NOI18N	
	    } 
	    Object o = stack.pop(); 
	    if(o instanceof MonitorData) 
		monData = (MonitorData)o;
	    else if(debug) { 
		log(o.toString()); 
		log("ERROR - wrong type object on stack"); //NOI18N
	    } 
	}
	else if(debug) { 
	    log("ERROR - mondata stack empty"); //NOI18N
	} 

	if(monData == null) { 
	    return; 
	} 

	StringBuffer buf = 
	    new StringBuffer(monData.getAttributeValue("id")); //NOI18N
	buf.append(Constants.Punctuation.itemSep);
	buf.append(monData.getAttributeValue("method")); //NOI18N
	buf.append(Constants.Punctuation.itemSep);
	buf.append(monData.getAttributeValue("resource")); //NOI18N
	
	if(debug) { 
	    log(" Notify client"); //NOI18N	
	    log(" Query string is "  + //NOI18N
		buf.toString());
	    log("Notify util is " + notifyUtil.toString()); //NOI18N

	    String file = 
		monData.createTempFile("filter-send.xml"); // NOI18N
	    log("Wrote data to " + file); // NOI18N
	}

	notifyUtil.sendRecord(monData, buf.toString()); 
	if(debug) log("Notify util has terminated"); //NOI18N

    }
 
Example 11
Source File: FilterUtil.java    From two-token-sw with Apache License 2.0 4 votes vote down vote up
public static void clearSessionInfoInAttributeAfterLogout(ServletRequest request) {
  request.removeAttribute(REQUEST_USER);
  request.removeAttribute(REQUEST_SESSION_INFO);
}
 
Example 12
Source File: FilterUtil.java    From two-token-sw with Apache License 2.0 4 votes vote down vote up
public static void clearSessionInfoInAttributeAfterLogout(ServletRequest request) {
  request.removeAttribute(REQUEST_USER);
  request.removeAttribute(REQUEST_SESSION_INFO);
}
 
Example 13
Source File: ShoppingCartFilter.java    From yes-cart with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void doAfter(final ServletRequest servletRequest, final ServletResponse servletResponse) throws IOException, ServletException {
    ApplicationDirector.setShoppingCart(null);
    servletRequest.removeAttribute("ShoppingCart");
}
 
Example 14
Source File: ServletAttrContainer.java    From scipio-erp with Apache License 2.0 votes vote down vote up
public static void removeAttribute(ServletRequest container, String name) { if (container != null) { container.removeAttribute(name); } }