Example usage for javax.servlet ServletRequestAttributeListener attributeAdded

List of usage examples for javax.servlet ServletRequestAttributeListener attributeAdded

Introduction

In this page you can find the example usage for javax.servlet ServletRequestAttributeListener attributeAdded.

Prototype

default public void attributeAdded(ServletRequestAttributeEvent srae) 

Source Link

Document

Receives notification that an attribute has been added to the ServletRequest.

Usage

From source file:org.apache.coyote.tomcat5.CoyoteRequest.java

/**
 * Set the specified request attribute to the specified value.
 *
 * @param name Name of the request attribute to set
 * @param value The associated value//from w  ww  .ja v  a  2s .co  m
 */
public void setAttribute(String name, Object value) {

    // Name cannot be null
    if (name == null)
        throw new IllegalArgumentException(sm.getString("coyoteRequest.setAttribute.namenull"));

    // Null value is the same as removeAttribute()
    if (value == null) {
        removeAttribute(name);
        return;
    }

    if (name.equals(Globals.DISPATCHER_TYPE_ATTR)) {
        dispatcherType = value;
        return;
    } else if (name.equals(Globals.DISPATCHER_REQUEST_PATH_ATTR)) {
        requestDispatcherPath = value;
        return;
    }

    Object oldValue = null;
    boolean replaced = false;

    // Add or replace the specified attribute
    // Check for read only attribute
    // requests are per thread so synchronization unnecessary
    if (readOnlyAttributes.containsKey(name)) {
        return;
    }

    oldValue = attributes.put(name, value);
    if (oldValue != null) {
        replaced = true;
    }

    // Notify interested application event listeners
    Object listeners[] = context.getApplicationEventListeners();
    if ((listeners == null) || (listeners.length == 0))
        return;
    ServletRequestAttributeEvent event = null;
    if (replaced)
        event = new ServletRequestAttributeEvent(context.getServletContext(), getRequest(), name, oldValue);
    else
        event = new ServletRequestAttributeEvent(context.getServletContext(), getRequest(), name, value);

    for (int i = 0; i < listeners.length; i++) {
        if (!(listeners[i] instanceof ServletRequestAttributeListener))
            continue;
        ServletRequestAttributeListener listener = (ServletRequestAttributeListener) listeners[i];
        try {
            if (replaced) {
                listener.attributeReplaced(event);
            } else {
                listener.attributeAdded(event);
            }
        } catch (Throwable t) {
            log(sm.getString("coyoteRequest.attributeEvent"), t);
            // Error valve will pick this execption up and display it to user
            attributes.put(Globals.EXCEPTION_ATTR, t);
        }
    }
}

From source file:org.ireland.jnetty.http.HttpServletRequestImpl.java

/**
 * Notify interested ServletRequestAttributeListeners that attribute has been assigned a value.
 * /*w  w  w .  j a v a2  s.  c o m*/
 * ?
 * 
 */
private void notifyAttributeAdded(String name, Object value) {
    if (_requestAttributeListeners != null && !_requestAttributeListeners.isEmpty()) {
        final ServletRequestAttributeEvent event = new ServletRequestAttributeEvent(servletContext, this, name,
                value);

        for (ServletRequestAttributeListener listener : _requestAttributeListeners) {
            listener.attributeAdded(event);
        }
    }
}