Search in sources :

Example 1 with ValveBase

use of org.apache.catalina.valves.ValveBase in project tomcat70 by apache.

the class MBeanFactory method removeValve.

/**
 * Remove an existing Valve.
 *
 * @param name MBean Name of the component to remove
 *
 * @exception Exception if a component cannot be removed
 */
public void removeValve(String name) throws Exception {
    // Acquire a reference to the component to be removed
    ObjectName oname = new ObjectName(name);
    ContainerBase container = getParentContainerFromChild(oname);
    Valve[] valves = container.getPipeline().getValves();
    for (int i = 0; i < valves.length; i++) {
        ObjectName voname = ((ValveBase) valves[i]).getObjectName();
        if (voname.equals(oname)) {
            container.getPipeline().removeValve(valves[i]);
        }
    }
}
Also used : ContainerBase(org.apache.catalina.core.ContainerBase) Valve(org.apache.catalina.Valve) AccessLogValve(org.apache.catalina.valves.AccessLogValve) RemoteHostValve(org.apache.catalina.valves.RemoteHostValve) RemoteAddrValve(org.apache.catalina.valves.RemoteAddrValve) ValveBase(org.apache.catalina.valves.ValveBase) ObjectName(javax.management.ObjectName)

Example 2 with ValveBase

use of org.apache.catalina.valves.ValveBase in project tomcat70 by apache.

the class StandardPipeline method getValveObjectNames.

public ObjectName[] getValveObjectNames() {
    ArrayList<ObjectName> valveList = new ArrayList<ObjectName>();
    Valve current = first;
    if (current == null) {
        current = basic;
    }
    while (current != null) {
        if (current instanceof ValveBase) {
            valveList.add(((ValveBase) current).getObjectName());
        }
        current = current.getNext();
    }
    return valveList.toArray(new ObjectName[0]);
}
Also used : ArrayList(java.util.ArrayList) Valve(org.apache.catalina.Valve) ValveBase(org.apache.catalina.valves.ValveBase) ObjectName(javax.management.ObjectName)

Example 3 with ValveBase

use of org.apache.catalina.valves.ValveBase in project tomcat70 by apache.

the class MBeanUtils method createObjectName.

/**
 * Create an <code>ObjectName</code> for this
 * <code>Valve</code> object.
 *
 * @param domain Domain in which this name is to be created
 * @param valve The Valve to be named
 *
 * @exception MalformedObjectNameException if a name cannot be created
 * @deprecated  Unused. Will be removed in Tomcat 8.0.x
 */
@Deprecated
static ObjectName createObjectName(String domain, Valve valve) throws MalformedObjectNameException {
    if (valve instanceof ValveBase) {
        ObjectName name = ((ValveBase) valve).getObjectName();
        if (name != null)
            return name;
    }
    ObjectName name = null;
    Container container = null;
    String className = valve.getClass().getName();
    int period = className.lastIndexOf('.');
    if (period >= 0)
        className = className.substring(period + 1);
    if (valve instanceof Contained) {
        container = ((Contained) valve).getContainer();
    }
    if (container == null) {
        throw new MalformedObjectNameException("Cannot create mbean for non-contained valve " + valve);
    }
    if (container instanceof Engine) {
        String local = "";
        int seq = getSeq(local);
        String ext = "";
        if (seq > 0) {
            ext = ",seq=" + seq;
        }
        name = new ObjectName(domain + ":type=Valve,name=" + className + ext + local);
    } else if (container instanceof Host) {
        String local = ",host=" + container.getName();
        int seq = getSeq(local);
        String ext = "";
        if (seq > 0) {
            ext = ",seq=" + seq;
        }
        name = new ObjectName(domain + ":type=Valve,name=" + className + ext + local);
    } else if (container instanceof Context) {
        Context context = ((Context) container);
        ContextName cn = new ContextName(context.getName(), false);
        Container host = context.getParent();
        String local = ",context=" + cn.getDisplayName() + ",host=" + host.getName();
        int seq = getSeq(local);
        String ext = "";
        if (seq > 0) {
            ext = ",seq=" + seq;
        }
        name = new ObjectName(domain + ":type=Valve,name=" + className + ext + local);
    }
    return (name);
}
Also used : Contained(org.apache.catalina.Contained) Context(org.apache.catalina.Context) Container(org.apache.catalina.Container) MalformedObjectNameException(javax.management.MalformedObjectNameException) Host(org.apache.catalina.Host) ValveBase(org.apache.catalina.valves.ValveBase) Engine(org.apache.catalina.Engine) ObjectName(javax.management.ObjectName) ContextName(org.apache.catalina.util.ContextName)

Example 4 with ValveBase

use of org.apache.catalina.valves.ValveBase in project tomcat70 by apache.

the class ContainerMBean method removeValve.

/**
 * Remove an existing Valve.
 *
 * @param valveName MBean Name of the Valve to remove
 *
 * @exception MBeanException if a component cannot be removed
 */
public void removeValve(String valveName) throws MBeanException {
    ContainerBase container = null;
    try {
        container = (ContainerBase) getManagedResource();
    } catch (InstanceNotFoundException e) {
        throw new MBeanException(e);
    } catch (RuntimeOperationsException e) {
        throw new MBeanException(e);
    } catch (InvalidTargetObjectTypeException e) {
        throw new MBeanException(e);
    }
    ObjectName oname;
    try {
        oname = new ObjectName(valveName);
    } catch (MalformedObjectNameException e) {
        throw new MBeanException(e);
    } catch (NullPointerException e) {
        throw new MBeanException(e);
    }
    if (container != null) {
        Valve[] valves = container.getPipeline().getValves();
        for (int i = 0; i < valves.length; i++) {
            ObjectName voname = ((ValveBase) valves[i]).getObjectName();
            if (voname.equals(oname)) {
                container.getPipeline().removeValve(valves[i]);
            }
        }
    }
}
Also used : ContainerBase(org.apache.catalina.core.ContainerBase) MalformedObjectNameException(javax.management.MalformedObjectNameException) InstanceNotFoundException(javax.management.InstanceNotFoundException) MBeanException(javax.management.MBeanException) Valve(org.apache.catalina.Valve) ValveBase(org.apache.catalina.valves.ValveBase) InvalidTargetObjectTypeException(javax.management.modelmbean.InvalidTargetObjectTypeException) RuntimeOperationsException(javax.management.RuntimeOperationsException) ObjectName(javax.management.ObjectName)

Example 5 with ValveBase

use of org.apache.catalina.valves.ValveBase in project nzbhydra2 by theotherp.

the class HydraEmbeddedServletContainer method customize.

@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
    if (!(container instanceof TomcatEmbeddedServletContainerFactory)) {
        // Is the case in tests
        return;
    }
    TomcatEmbeddedServletContainerFactory containerFactory = (TomcatEmbeddedServletContainerFactory) container;
    containerFactory.addContextValves(new ValveBase() {

        @Override
        public void invoke(Request request, Response response) throws IOException, ServletException {
            int originalPort = -1;
            final String forwardedPort = request.getHeader("X-Forwarded-Port");
            if (forwardedPort != null) {
                try {
                    originalPort = request.getServerPort();
                    request.setServerPort(Integer.valueOf(forwardedPort));
                } catch (final NumberFormatException e) {
                    logger.debug("ignoring forwarded port {}", forwardedPort);
                }
            }
            final MessageBytes serverNameMB = request.getCoyoteRequest().serverName();
            String originalServerName = null;
            String forwardedHost = request.getHeader("X-Forwarded-Host");
            if (forwardedHost == null) {
                forwardedHost = request.getHeader("host");
            }
            if (forwardedHost != null) {
                int colonIndex = forwardedHost.indexOf(":");
                if (colonIndex > -1) {
                    if (originalPort == -1) {
                        originalPort = request.getServerPort();
                    }
                    request.setServerPort(Integer.valueOf(forwardedHost.substring(colonIndex + 1)));
                    forwardedHost = forwardedHost.substring(0, colonIndex);
                }
                originalServerName = serverNameMB.getString();
                serverNameMB.setString(forwardedHost);
            }
            Boolean originallySecure = null;
            final String forwardedProto = request.getHeader("X-Forwarded-Proto");
            if (forwardedProto != null) {
                originallySecure = request.isSecure();
                request.setSecure(forwardedProto.equalsIgnoreCase("https"));
            }
            try {
                getNext().invoke(request, response);
            } finally {
                if (originallySecure != null) {
                    request.setSecure(originallySecure);
                }
                if (forwardedHost != null) {
                    serverNameMB.setString(originalServerName);
                }
                if (forwardedPort != null) {
                    request.setServerPort(originalPort);
                }
            }
        }
    });
    ((TomcatEmbeddedServletContainerFactory) container).addContextCustomizers(context -> context.setMapperContextRootRedirectEnabled(true));
}
Also used : Response(org.apache.catalina.connector.Response) ServletException(javax.servlet.ServletException) TomcatEmbeddedServletContainerFactory(org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory) Request(org.apache.catalina.connector.Request) MessageBytes(org.apache.tomcat.util.buf.MessageBytes) ValveBase(org.apache.catalina.valves.ValveBase) IOException(java.io.IOException)

Aggregations

ValveBase (org.apache.catalina.valves.ValveBase)5 ObjectName (javax.management.ObjectName)4 Valve (org.apache.catalina.Valve)3 MalformedObjectNameException (javax.management.MalformedObjectNameException)2 ContainerBase (org.apache.catalina.core.ContainerBase)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 InstanceNotFoundException (javax.management.InstanceNotFoundException)1 MBeanException (javax.management.MBeanException)1 RuntimeOperationsException (javax.management.RuntimeOperationsException)1 InvalidTargetObjectTypeException (javax.management.modelmbean.InvalidTargetObjectTypeException)1 ServletException (javax.servlet.ServletException)1 Contained (org.apache.catalina.Contained)1 Container (org.apache.catalina.Container)1 Context (org.apache.catalina.Context)1 Engine (org.apache.catalina.Engine)1 Host (org.apache.catalina.Host)1 Request (org.apache.catalina.connector.Request)1 Response (org.apache.catalina.connector.Response)1 ContextName (org.apache.catalina.util.ContextName)1