Java Code Examples for org.apache.catalina.Context#setPath()

The following examples show how to use org.apache.catalina.Context#setPath() . 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: Tomcat.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * @param host The host in which the context will be deployed
 * @param contextPath The context mapping to use, "" for root context.
 * @param contextName The context name
 * @param dir Base directory for the context, for static files.
 *  Must exist, relative to the server home
 * @return the deployed context
 * @see #addContext(String, String)
 */
public Context addContext(Host host, String contextPath, String contextName,
        String dir) {
    silence(host, contextName);
    Context ctx = createContext(host, contextPath);
    ctx.setName(contextName);
    ctx.setPath(contextPath);
    ctx.setDocBase(dir);
    ctx.addLifecycleListener(new FixContextListener());

    if (host == null) {
        getHost().addChild(ctx);
    } else {
        host.addChild(ctx);
    }
    return ctx;
}
 
Example 2
Source File: StandardHost.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * Add a child Container, only if the proposed child is an implementation
 * of Context.
 *
 * @param child Child container to be added
 */
@Override
public void addChild(Container child) {

    if (!(child instanceof Context))
        throw new IllegalArgumentException
            (sm.getString("standardHost.notContext"));

    child.addLifecycleListener(new MemoryLeakTrackingListener());

    // Avoid NPE for case where Context is defined in server.xml with only a
    // docBase
    Context context = (Context) child;
    if (context.getPath() == null) {
        ContextName cn = new ContextName(context.getDocBase(), true);
        context.setPath(cn.getPath());
    }

    super.addChild(child);

}
 
Example 3
Source File: Tomcat.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
public Context addContext(Host host, String contextPath, String contextName,
        String dir) {
    silence(host, contextPath);
    Context ctx = createContext(host, contextPath);
    ctx.setName(contextName);
    ctx.setPath(contextPath);
    ctx.setDocBase(dir);
    ctx.addLifecycleListener(new FixContextListener());
    
    if (host == null) {
        getHost().addChild(ctx);
    } else {
        host.addChild(ctx);
    }
    return ctx;
}
 
Example 4
Source File: Tomcat.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * @see #addWebapp(String, String)
 *
 * @param name Ignored. The contextPath will be used
 *
 * @deprecated Use {@link #addWebapp(Host, String, String)}
 */
@Deprecated
public Context addWebapp(Host host, String contextPath, String name, String docBase) {
    silence(host, contextPath);

    Context ctx = createContext(host, contextPath);
    ctx.setPath(contextPath);
    ctx.setDocBase(docBase);

    ctx.addLifecycleListener(new DefaultWebXmlListener());
    ctx.setConfigFile(getWebappConfigFile(docBase, contextPath));

    ContextConfig ctxCfg = new ContextConfig();
    ctx.addLifecycleListener(ctxCfg);
    
    // prevent it from looking ( if it finds one - it'll have dup error )
    ctxCfg.setDefaultWebXml(noDefaultWebXmlPath());

    if (host == null) {
        getHost().addChild(ctx);
    } else {
        host.addChild(ctx);
    }

    return ctx;
}
 
Example 5
Source File: Tomcat.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
public Context addContext(Host host, String contextPath, String contextName,
        String dir) {
    silence(host, contextName);
    Context ctx = createContext(host, contextPath);
    ctx.setName(contextName);
    ctx.setPath(contextPath);
    ctx.setDocBase(dir);
    ctx.addLifecycleListener(new FixContextListener());
    
    if (host == null) {
        getHost().addChild(ctx);
    } else {
        host.addChild(ctx);
    }
    return ctx;
}
 
Example 6
Source File: Tomcat.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * @see #addWebapp(String, String)
 *
 * @param name Ignored. The contextPath will be used
 *
 * @deprecated Use {@link #addWebapp(Host, String, String)}
 */
@Deprecated
public Context addWebapp(Host host, String contextPath, String name, String docBase) {
    silence(host, contextPath);

    Context ctx = createContext(host, contextPath);
    ctx.setPath(contextPath);
    ctx.setDocBase(docBase);

    ctx.addLifecycleListener(new DefaultWebXmlListener());
    ctx.setConfigFile(getWebappConfigFile(docBase, contextPath));

    ContextConfig ctxCfg = new ContextConfig();
    ctx.addLifecycleListener(ctxCfg);
    
    // prevent it from looking ( if it finds one - it'll have dup error )
    ctxCfg.setDefaultWebXml(noDefaultWebXmlPath());

    if (host == null) {
        getHost().addChild(ctx);
    } else {
        host.addChild(ctx);
    }

    return ctx;
}
 
Example 7
Source File: WebappAuthenticationValveTest.java    From carbon-device-mgt with Apache License 2.0 6 votes vote down vote up
/**
 * To create a request with the given authorization header
 *
 * @param authorizationHeader Authorization header
 * @return the relevant request.
 * @throws IllegalAccessException Illegal Access Exception.
 * @throws NoSuchFieldException   No Such Field Exception.
 */
private Request createRequest(String authorizationHeader) throws IllegalAccessException, NoSuchFieldException {
    Request request = new TestRequest("", "");
    Context context = new StandardContext();
    context.addParameter("basicAuth", "true");
    context.addParameter("managed-api-enabled", "true");
    context.setPath("carbon1");
    context.addParameter("doAuthentication", String.valueOf(true));
    request.setContext(context);
    MimeHeaders mimeHeaders = new MimeHeaders();
    MessageBytes bytes = mimeHeaders.addValue(BaseWebAppAuthenticatorFrameworkTest.AUTHORIZATION_HEADER);
    bytes.setString(authorizationHeader);
    Field headersField = org.apache.coyote.Request.class.getDeclaredField("headers");
    headersField.setAccessible(true);
    org.apache.coyote.Request coyoteRequest = new org.apache.coyote.Request();
    headersField.set(coyoteRequest, mimeHeaders);
    request.setCoyoteRequest(coyoteRequest);
    return request;
}
 
Example 8
Source File: Tomcat.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * @param host The host in which the context will be deployed
 * @param contextPath The context mapping to use, "" for root context.
 * @param docBase Base directory for the context, for static files.
 *  Must exist, relative to the server home
 * @param config Custom context configurator helper
 * @return the deployed context
 * @see #addWebapp(String, String)
 */
public Context addWebapp(Host host, String contextPath, String docBase,
        LifecycleListener config) {

    silence(host, contextPath);

    Context ctx = createContext(host, contextPath);
    ctx.setPath(contextPath);
    ctx.setDocBase(docBase);
    ctx.addLifecycleListener(getDefaultWebXmlListener());
    ctx.setConfigFile(getWebappConfigFile(docBase, contextPath));

    ctx.addLifecycleListener(config);

    if (config instanceof ContextConfig) {
        // prevent it from looking ( if it finds one - it'll have dup error )
        ((ContextConfig) config).setDefaultWebXml(noDefaultWebXmlPath());
    }

    if (host == null) {
        getHost().addChild(ctx);
    } else {
        host.addChild(ctx);
    }

    return ctx;
}
 
Example 9
Source File: UserConfig.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Deploy a web application for the specified user if they have such an
 * application in the defined directory within their home directory.
 *
 * @param user Username owning the application to be deployed
 * @param home Home directory of this user
 */
private void deploy(String user, String home) {

    // Does this user have a web application to be deployed?
    String contextPath = "/~" + user;
    if (host.findChild(contextPath) != null)
        return;
    File app = new File(home, directoryName);
    if (!app.exists() || !app.isDirectory())
        return;

    host.getLogger().info(sm.getString("userConfig.deploy", user));

    // Deploy the web application for this user
    try {
        Class<?> clazz = Class.forName(contextClass);
        Context context = (Context) clazz.getConstructor().newInstance();
        context.setPath(contextPath);
        context.setDocBase(app.toString());
        clazz = Class.forName(configClass);
        LifecycleListener listener = (LifecycleListener) clazz.getConstructor().newInstance();
        context.addLifecycleListener(listener);
        host.addChild(context);
    } catch (Exception e) {
        host.getLogger().error(sm.getString("userConfig.error", user), e);
    }

}
 
Example 10
Source File: UserConfig.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Deploy a web application for the specified user if they have such an
 * application in the defined directory within their home directory.
 *
 * @param user Username owning the application to be deployed
 * @param home Home directory of this user
 */
private void deploy(String user, String home) {

    // Does this user have a web application to be deployed?
    String contextPath = "/~" + user;
    if (host.findChild(contextPath) != null)
        return;
    File app = new File(home, directoryName);
    if (!app.exists() || !app.isDirectory())
        return;
    /*
    File dd = new File(app, "/WEB-INF/web.xml");
    if (!dd.exists() || !dd.isFile() || !dd.canRead())
        return;
    */
    host.getLogger().info(sm.getString("userConfig.deploy", user));

    // Deploy the web application for this user
    try {
        Class<?> clazz = Class.forName(contextClass);
        Context context =
          (Context) clazz.newInstance();
        context.setPath(contextPath);
        context.setDocBase(app.toString());
        clazz = Class.forName(configClass);
        LifecycleListener listener =
            (LifecycleListener) clazz.newInstance();
        context.addLifecycleListener(listener);
        host.addChild(context);
    } catch (Exception e) {
        host.getLogger().error(sm.getString("userConfig.error", user), e);
    }

}
 
Example 11
Source File: UserConfig.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Deploy a web application for the specified user if they have such an
 * application in the defined directory within their home directory.
 *
 * @param user Username owning the application to be deployed
 * @param home Home directory of this user
 */
private void deploy(String user, String home) {

    // Does this user have a web application to be deployed?
    String contextPath = "/~" + user;
    if (host.findChild(contextPath) != null)
        return;
    File app = new File(home, directoryName);
    if (!app.exists() || !app.isDirectory())
        return;
    /*
    File dd = new File(app, "/WEB-INF/web.xml");
    if (!dd.exists() || !dd.isFile() || !dd.canRead())
        return;
    */
    host.getLogger().info(sm.getString("userConfig.deploy", user));

    // Deploy the web application for this user
    try {
        Class<?> clazz = Class.forName(contextClass);
        Context context =
          (Context) clazz.newInstance();
        context.setPath(contextPath);
        context.setDocBase(app.toString());
        clazz = Class.forName(configClass);
        LifecycleListener listener =
            (LifecycleListener) clazz.newInstance();
        context.addLifecycleListener(listener);
        host.addChild(context);
    } catch (Exception e) {
        host.getLogger().error(sm.getString("userConfig.error", user), e);
    }

}
 
Example 12
Source File: WebappAuthenticationValveTest.java    From carbon-device-mgt with Apache License 2.0 5 votes vote down vote up
@Test(description = "This method tests the behaviour of the invoke method of WebAuthenticationValve when "
        + "un-secured endpoints are invoked.")
public void testInvokeUnSecuredEndpoints() {
    Request request = new TestRequest("", "test");
    Context context = new StandardContext();
    context.setPath("carbon1");
    context.addParameter("doAuthentication", String.valueOf(true));
    context.addParameter("nonSecuredEndPoints", "test, test1");
    CompositeValve compositeValve = Mockito.mock(CompositeValve.class);
    Mockito.doNothing().when(compositeValve).continueInvocation(Mockito.any(), Mockito.any());
    request.setContext(context);
    webappAuthenticationValve.invoke(request, null, compositeValve);
}
 
Example 13
Source File: TestApplicationContext.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Test
public void testBug57190() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    Context foo1 = new StandardContext();
    foo1.setName("/foo##1");
    foo1.setPath("/foo");
    foo1.setWebappVersion("1");
    foo1.addLifecycleListener(new FixContextListener());
    foo1.addLifecycleListener(new SetIdListener("foo1"));
    tomcat.getHost().addChild(foo1);

    Context foo2 = new StandardContext();
    foo2.setName("/foo##2");
    foo2.setPath("/foo");
    foo2.setWebappVersion("2");
    foo2.addLifecycleListener(new FixContextListener());
    foo2.addLifecycleListener(new SetIdListener("foo2"));
    tomcat.getHost().addChild(foo2);

    Context bar = tomcat.addContext("/bar", null);
    bar.addLifecycleListener(new SetIdListener("bar"));

    Context ctx = tomcat.addContext("", null);
    ctx.addLifecycleListener(new SetIdListener("ROOT"));
    ctx.setCrossContext(true);

    Tomcat.addServlet(ctx, "Bug57190Servlet", new Bug57190Servlet());
    ctx.addServletMappingDecoded("/", "Bug57190Servlet");

    tomcat.start();

    ByteChunk res = getUrl("http://localhost:" + getPort() + "/");
    String body = res.toString();

    Assert.assertTrue(body, body.contains("01-bar"));
    Assert.assertTrue(body, body.contains("02-foo2"));
    Assert.assertTrue(body, body.contains("03-foo1"));
    Assert.assertTrue(body, body.contains("04-foo2"));
    Assert.assertTrue(body, body.contains("05-foo2"));
    Assert.assertTrue(body, body.contains("06-ROOT"));
    Assert.assertTrue(body, body.contains("07-ROOT"));
    Assert.assertTrue(body, body.contains("08-foo2"));
    Assert.assertTrue(body, body.contains("09-ROOT"));
}
 
Example 14
Source File: TestApplicationContext.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@Test
public void testBug57190() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    Context foo1 = new StandardContext();
    foo1.setName("/foo##1");
    foo1.setPath("/foo");
    foo1.setWebappVersion("1");
    foo1.setDocBase(System.getProperty("java.io.tmpdir"));
    foo1.addLifecycleListener(new FixContextListener());
    foo1.addLifecycleListener(new SetIdListener("foo1"));
    tomcat.getHost().addChild(foo1);

    Context foo2 = new StandardContext();
    foo2.setName("/foo##2");
    foo2.setPath("/foo");
    foo2.setWebappVersion("2");
    foo2.setDocBase(System.getProperty("java.io.tmpdir"));
    foo2.addLifecycleListener(new FixContextListener());
    foo2.addLifecycleListener(new SetIdListener("foo2"));
    tomcat.getHost().addChild(foo2);

    // No file system docBase required
    Context bar = tomcat.addContext("/bar", null);
    bar.addLifecycleListener(new SetIdListener("bar"));

    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addLifecycleListener(new SetIdListener("ROOT"));
    ctx.setCrossContext(true);

    Tomcat.addServlet(ctx, "Bug57190Servlet", new Bug57190Servlet());
    ctx.addServletMapping("/", "Bug57190Servlet");

    tomcat.start();

    ByteChunk res = getUrl("http://localhost:" + getPort() + "/");
    String body = res.toString();

    Assert.assertTrue(body, body.contains("01-bar"));
    Assert.assertTrue(body, body.contains("02-foo2"));
    Assert.assertTrue(body, body.contains("03-foo1"));
    Assert.assertTrue(body, body.contains("04-foo2"));
    Assert.assertTrue(body, body.contains("05-foo2"));
    Assert.assertTrue(body, body.contains("06-ROOT"));
    Assert.assertTrue(body, body.contains("07-ROOT"));
    Assert.assertTrue(body, body.contains("08-foo2"));
    Assert.assertTrue(body, body.contains("09-ROOT"));
}
 
Example 15
Source File: TestApplicationContext.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Test
public void testBug57190() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    Context foo1 = new StandardContext();
    foo1.setName("/foo##1");
    foo1.setPath("/foo");
    foo1.setWebappVersion("1");
    foo1.setDocBase(System.getProperty("java.io.tmpdir"));
    foo1.addLifecycleListener(new FixContextListener());
    foo1.addLifecycleListener(new SetIdListener("foo1"));
    tomcat.getHost().addChild(foo1);

    Context foo2 = new StandardContext();
    foo2.setName("/foo##2");
    foo2.setPath("/foo");
    foo2.setWebappVersion("2");
    foo2.setDocBase(System.getProperty("java.io.tmpdir"));
    foo2.addLifecycleListener(new FixContextListener());
    foo2.addLifecycleListener(new SetIdListener("foo2"));
    tomcat.getHost().addChild(foo2);

    // No file system docBase required
    Context bar = tomcat.addContext("/bar", null);
    bar.addLifecycleListener(new SetIdListener("bar"));

    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addLifecycleListener(new SetIdListener("ROOT"));
    ctx.setCrossContext(true);

    Tomcat.addServlet(ctx, "Bug57190Servlet", new Bug57190Servlet());
    ctx.addServletMapping("/", "Bug57190Servlet");

    tomcat.start();

    ByteChunk res = getUrl("http://localhost:" + getPort() + "/");
    String body = res.toString();

    Assert.assertTrue(body, body.contains("01-bar"));
    Assert.assertTrue(body, body.contains("02-foo2"));
    Assert.assertTrue(body, body.contains("03-foo1"));
    Assert.assertTrue(body, body.contains("04-foo2"));
    Assert.assertTrue(body, body.contains("05-foo2"));
    Assert.assertTrue(body, body.contains("06-ROOT"));
    Assert.assertTrue(body, body.contains("07-ROOT"));
    Assert.assertTrue(body, body.contains("08-foo2"));
    Assert.assertTrue(body, body.contains("09-ROOT"));
}