Search in sources :

Example 1 with ServletRequestAttributeListener

use of javax.servlet.ServletRequestAttributeListener in project jetty.project by eclipse.

the class Request method setAttribute.

/* ------------------------------------------------------------ */
/*
     * Set a request attribute. if the attribute name is "org.eclipse.jetty.server.server.Request.queryEncoding" then the value is also passed in a call to
     * {@link #setQueryEncoding}.
     *
     * @see javax.servlet.ServletRequest#setAttribute(java.lang.String, java.lang.Object)
     */
@Override
public void setAttribute(String name, Object value) {
    Object old_value = _attributes == null ? null : _attributes.getAttribute(name);
    if ("org.eclipse.jetty.server.Request.queryEncoding".equals(name))
        setQueryEncoding(value == null ? null : value.toString());
    else if ("org.eclipse.jetty.server.sendContent".equals(name))
        LOG.warn("Deprecated: org.eclipse.jetty.server.sendContent");
    if (_attributes == null)
        _attributes = new AttributesMap();
    _attributes.setAttribute(name, value);
    if (!_requestAttributeListeners.isEmpty()) {
        final ServletRequestAttributeEvent event = new ServletRequestAttributeEvent(_context, this, name, old_value == null ? value : old_value);
        for (ServletRequestAttributeListener l : _requestAttributeListeners) {
            if (old_value == null)
                l.attributeAdded(event);
            else if (value == null)
                l.attributeRemoved(event);
            else
                l.attributeReplaced(event);
        }
    }
}
Also used : ServletRequestAttributeListener(javax.servlet.ServletRequestAttributeListener) ServletRequestAttributeEvent(javax.servlet.ServletRequestAttributeEvent) AttributesMap(org.eclipse.jetty.util.AttributesMap)

Example 2 with ServletRequestAttributeListener

use of javax.servlet.ServletRequestAttributeListener in project Payara by payara.

the class Request method removeAttribute.

/**
 * Remove the specified request attribute if it exists.
 *
 * @param name Name of the request attribute to remove
 */
@Override
public void removeAttribute(String name) {
    Object value = null;
    boolean found = attributes.containsKey(name);
    if (found) {
        value = attributes.get(name);
        attributes.remove(name);
    } else {
        return;
    }
    // Notify interested application event listeners
    List<EventListener> listeners = context.getApplicationEventListeners();
    if (listeners.isEmpty()) {
        return;
    }
    ServletRequestAttributeEvent event = new ServletRequestAttributeEvent(servletContext, getRequest(), name, value);
    Iterator<EventListener> iter = listeners.iterator();
    while (iter.hasNext()) {
        EventListener eventListener = iter.next();
        if (!(eventListener instanceof ServletRequestAttributeListener)) {
            continue;
        }
        ServletRequestAttributeListener listener = (ServletRequestAttributeListener) eventListener;
        try {
            listener.attributeRemoved(event);
        } catch (Throwable t) {
            log(rb.getString(LogFacade.ATTRIBUTE_EVENT_LISTENER_EXCEPTION), t);
            // Error valve will pick this exception up and display it to user
            attributes.put(RequestDispatcher.ERROR_EXCEPTION, t);
        }
    }
}
Also used : ServletRequestAttributeListener(javax.servlet.ServletRequestAttributeListener) ServletRequestAttributeEvent(javax.servlet.ServletRequestAttributeEvent)

Example 3 with ServletRequestAttributeListener

use of javax.servlet.ServletRequestAttributeListener in project nutzboot by nutzam.

the class TomcatStarter method addNutzSupport.

private void addNutzSupport() {
    List<WebFilterFace> filters = appContext.getBeans(WebFilterFace.class);
    Collections.sort(filters, Comparator.comparing(WebFilterFace::getOrder));
    filters.forEach((face) -> addFilter(face));
    appContext.getBeans(WebServletFace.class).forEach((face) -> {
        if (face.getServlet() == null) {
            return;
        }
        addServlet(face);
    });
    appContext.getBeans(WebEventListenerFace.class).forEach((face) -> {
        EventListener listener = face.getEventListener();
        if (listener != null) {
            if ((listener instanceof ServletContextAttributeListener) || (listener instanceof ServletRequestAttributeListener) || (listener instanceof ServletRequestListener) || (listener instanceof HttpSessionIdListener) || (listener instanceof HttpSessionAttributeListener)) {
                this.tomcatContext.addApplicationEventListener(listener);
            }
            if ((listener instanceof ServletContextListener) || (listener instanceof HttpSessionListener)) {
                this.tomcatContext.addApplicationLifecycleListener(listener);
            }
        }
    });
}
Also used : ServletContextAttributeListener(javax.servlet.ServletContextAttributeListener) ServletRequestAttributeListener(javax.servlet.ServletRequestAttributeListener) HttpSessionListener(javax.servlet.http.HttpSessionListener) WebEventListenerFace(org.nutz.boot.starter.WebEventListenerFace) ServletContextListener(javax.servlet.ServletContextListener) ServletRequestListener(javax.servlet.ServletRequestListener) HttpSessionAttributeListener(javax.servlet.http.HttpSessionAttributeListener) WebFilterFace(org.nutz.boot.starter.WebFilterFace) WebServletFace(org.nutz.boot.starter.WebServletFace) EventListener(java.util.EventListener) HttpSessionIdListener(javax.servlet.http.HttpSessionIdListener)

Example 4 with ServletRequestAttributeListener

use of javax.servlet.ServletRequestAttributeListener in project Payara by payara.

the class StandardContext method addListener.

/**
 * Adds the given listener instance to this ServletContext.
 *
 * @param t the listener to be added
 * @param isProgrammatic true if the listener is being added
 * programmatically, and false if it has been declared in the deployment
 * descriptor
 */
private <T extends EventListener> void addListener(T t, boolean isProgrammatic) {
    if (isContextInitializedCalled) {
        String msg = MessageFormat.format(rb.getString(LogFacade.SERVLET_CONTEXT_ALREADY_INIT_EXCEPTION), new Object[] { "addListener", getName() });
        throw new IllegalStateException(msg);
    }
    if ((t instanceof ServletContextListener) && isProgrammatic && !isProgrammaticServletContextListenerRegistrationAllowed) {
        throw new IllegalArgumentException("Not allowed to register " + "ServletContextListener programmatically");
    }
    boolean added = false;
    if (t instanceof ServletContextAttributeListener || t instanceof ServletRequestAttributeListener || t instanceof ServletRequestListener || t instanceof HttpSessionAttributeListener || t instanceof HttpSessionIdListener) {
        eventListeners.add(t);
        added = true;
    }
    if (t instanceof HttpSessionListener) {
        sessionListeners.add((HttpSessionListener) t);
        if (!added) {
            added = true;
        }
    }
    if (t instanceof ServletContextListener) {
        ServletContextListener proxy = (ServletContextListener) t;
        if (isProgrammatic) {
            proxy = new RestrictedServletContextListener((ServletContextListener) t);
        }
        // Always add the JSF listener as the first element,
        // see GlassFish Issue 2563 for details
        boolean isFirst = "com.sun.faces.config.ConfigureListener".equals(t.getClass().getName());
        if (isFirst) {
            contextListeners.add(0, proxy);
        } else {
            contextListeners.add(proxy);
        }
        if (!added) {
            added = true;
        }
    }
    if (!added) {
        throw new IllegalArgumentException("Invalid listener type " + t.getClass().getName());
    }
}
Also used : ServletContextAttributeListener(javax.servlet.ServletContextAttributeListener) ServletRequestAttributeListener(javax.servlet.ServletRequestAttributeListener) HttpSessionListener(javax.servlet.http.HttpSessionListener) ServletContextListener(javax.servlet.ServletContextListener) ServletRequestListener(javax.servlet.ServletRequestListener) HttpSessionAttributeListener(javax.servlet.http.HttpSessionAttributeListener) HttpSessionIdListener(javax.servlet.http.HttpSessionIdListener)

Example 5 with ServletRequestAttributeListener

use of javax.servlet.ServletRequestAttributeListener in project Payara by payara.

the class Request method setAttribute.

/**
 * Set the specified request attribute to the specified value.
 *
 * @param name Name of the request attribute to set
 * @param value The associated value
 */
@Override
public void setAttribute(String name, Object value) {
    // Name cannot be null
    if (name == null) {
        throw new IllegalArgumentException(rb.getString(LogFacade.NULL_ATTRIBUTE_NAME_EXCEPTION));
    }
    // Null value is the same as removeAttribute()
    if (value == null) {
        removeAttribute(name);
        return;
    }
    if (name.equals(Globals.DISPATCHER_TYPE_ATTR)) {
        dispatcherTypeAttr = value;
        return;
    } else if (name.equals(Globals.DISPATCHER_REQUEST_PATH_ATTR)) {
        requestDispatcherPath = value;
        return;
    }
    boolean replaced = false;
    // Do the security check before any updates are made
    if (Globals.IS_SECURITY_ENABLED && name.equals("org.apache.tomcat.sendfile.filename")) {
        // Use the canonical file name to avoid any possible symlink and
        // relative path issues
        String canonicalPath;
        try {
            canonicalPath = new File(value.toString()).getCanonicalPath();
        } catch (IOException e) {
            String msg = MessageFormat.format(rb.getString(LogFacade.UNABLE_DETERMINE_CANONICAL_NAME), value);
            throw new SecurityException(msg, e);
        }
        // Sendfile is performed in Tomcat's security context so need to
        // check if the web app is permitted to access the file while still
        // in the web app's security context
        System.getSecurityManager().checkRead(canonicalPath);
        // Update the value so the canonical path is used
        value = canonicalPath;
    }
    Object oldValue = attributes.put(name, value);
    if (oldValue != null) {
        replaced = true;
    }
    // Pass special attributes to the ngrizzly layer
    if (name.startsWith("grizzly.")) {
        coyoteRequest.setAttribute(name, value);
    }
    // END SJSAS 6231069
    // Notify interested application event listeners
    List<EventListener> listeners = context.getApplicationEventListeners();
    if (listeners.isEmpty()) {
        return;
    }
    ServletRequestAttributeEvent event = null;
    if (replaced) {
        event = new ServletRequestAttributeEvent(servletContext, getRequest(), name, oldValue);
    } else {
        event = new ServletRequestAttributeEvent(servletContext, getRequest(), name, value);
    }
    Iterator<EventListener> iter = listeners.iterator();
    while (iter.hasNext()) {
        EventListener eventListener = iter.next();
        if (!(eventListener instanceof ServletRequestAttributeListener)) {
            continue;
        }
        ServletRequestAttributeListener listener = (ServletRequestAttributeListener) eventListener;
        try {
            if (replaced) {
                listener.attributeReplaced(event);
            } else {
                listener.attributeAdded(event);
            }
        } catch (Throwable t) {
            log(rb.getString(LogFacade.ATTRIBUTE_EVENT_LISTENER_EXCEPTION), t);
            // Error valve will pick this exception up and display it to user
            attributes.put(RequestDispatcher.ERROR_EXCEPTION, t);
        }
    }
}
Also used : ServletRequestAttributeListener(javax.servlet.ServletRequestAttributeListener) IOException(java.io.IOException) ServletRequestAttributeEvent(javax.servlet.ServletRequestAttributeEvent) File(java.io.File)

Aggregations

ServletRequestAttributeListener (javax.servlet.ServletRequestAttributeListener)8 ServletRequestAttributeEvent (javax.servlet.ServletRequestAttributeEvent)5 ServletContextAttributeListener (javax.servlet.ServletContextAttributeListener)3 ServletContextListener (javax.servlet.ServletContextListener)3 ServletRequestListener (javax.servlet.ServletRequestListener)3 HttpSessionAttributeListener (javax.servlet.http.HttpSessionAttributeListener)3 HttpSessionListener (javax.servlet.http.HttpSessionListener)3 HttpSessionIdListener (javax.servlet.http.HttpSessionIdListener)2 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 EventListener (java.util.EventListener)1 ServletContextEvent (javax.servlet.ServletContextEvent)1 ApplicationListener (org.apache.catalina.deploy.ApplicationListener)1 SecurityConstraint (org.apache.catalina.deploy.SecurityConstraint)1 AttributesMap (org.eclipse.jetty.util.AttributesMap)1 WebEventListenerFace (org.nutz.boot.starter.WebEventListenerFace)1 WebFilterFace (org.nutz.boot.starter.WebFilterFace)1 WebServletFace (org.nutz.boot.starter.WebServletFace)1