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]);
}
}
}
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]);
}
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);
}
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]);
}
}
}
}
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));
}
Aggregations