Example usage for javax.servlet ServletContext getContextPath

List of usage examples for javax.servlet ServletContext getContextPath

Introduction

In this page you can find the example usage for javax.servlet ServletContext getContextPath.

Prototype

public String getContextPath();

Source Link

Document

Returns the context path of the web application.

Usage

From source file:com.evolveum.midpoint.web.security.MidPointAccessDeniedHandler.java

private String createUri(HttpServletRequest req, String uri) {
    StringBuilder sb = new StringBuilder();

    ServletContext ctx = req.getServletContext();
    String ctxPath = ctx.getContextPath();
    if (StringUtils.isNotEmpty(ctxPath)) {
        sb.append(ctxPath);/*from w  w  w .  j a  v  a 2  s  . c  o  m*/
    }
    sb.append(uri);

    return sb.toString();
}

From source file:it.scoppelletti.programmerpower.web.spring.config.WebPropertySource.java

/**
 * Restituisce il percorso del contesto dell’applicazione Web.
 * //  w  w w. ja v  a2s .co m
 * @param  servletCtx Contesto dell’applicazione Web.
 * @return            Valore.
 */
private String getContextPath(ServletContext servletCtx) {
    String value = servletCtx.getContextPath();

    if (value == null) {
        return null;
    }

    if (value.startsWith("/")) {
        return value.substring(1);
    }

    return value;
}

From source file:edu.cornell.mannlib.vitro.webapp.controller.freemarker.FreemarkerSetup.java

@Override
public void contextInitialized(ServletContextEvent event) {
    ServletContext sc = event.getServletContext();
    FreemarkerComponentGenerator.setServletContext(sc);
    UrlBuilder.contextPath = sc.getContextPath();

    log.info("Freemarker templating system initialized.");
}

From source file:org.red5.logging.LoggerContextFilter.java

public void init(FilterConfig config) throws ServletException {
    ServletContext servletContext = config.getServletContext();
    contextName = servletContext.getContextPath().replaceAll("/", "");
    if ("".equals(contextName)) {
        contextName = "root";
    }/*from   www  . j ava  2s.c o m*/
    System.out.printf("Filter init: %s%n", contextName);
    ConfigurableWebApplicationContext appctx = (ConfigurableWebApplicationContext) servletContext
            .getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
    if (appctx != null) {
        System.out.printf(
                "ConfigurableWebApplicationContext is not null in LoggerContextFilter for: %s, this indicates a misconfiguration or load order problem%n",
                contextName);
    }
}

From source file:org.jasig.springframework.test.ServletPortletAwareTester.java

@Override
public void setServletContext(ServletContext servletContext) {
    logger.debug("{} - set ServletContext {}", this.name, servletContext.getContextPath());
}

From source file:info.magnolia.init.DefaultMagnoliaInitPaths.java

protected String determineContextPath(ServletContext context) {
    return context.getContextPath();
}

From source file:org.red5.logging.ContextLoggingListener.java

public void contextInitialized(ServletContextEvent event) {
    ServletContext servletContext = event.getServletContext();
    String contextName = servletContext.getContextPath().replaceAll("/", "");
    if ("".equals(contextName)) {
        contextName = "root";
    }/*from w ww  .java  2s  .  c o  m*/
    System.out.printf("Context init: %s%n", contextName);
    ConfigurableWebApplicationContext appctx = (ConfigurableWebApplicationContext) servletContext
            .getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
    if (appctx != null) {
        System.out.printf(
                "ConfigurableWebApplicationContext is not null in ContextLoggingListener for: %s, this indicates a misconfiguration or load order problem%n",
                contextName);
    }
    try {
        // get the selector
        ContextSelector selector = Red5LoggerFactory.getContextSelector();
        // get the logger context for this servlet / app context by name
        URL url = servletContext.getResource(String.format("/WEB-INF/classes/logback-%s.xml", contextName));
        if (url != null && Files.exists(Paths.get(url.toURI()))) {
            System.out.printf("Context logger config found: %s%n", url.toURI());
        } else {
            url = servletContext.getResource("/WEB-INF/classes/logback.xml");
            if (url != null && Files.exists(Paths.get(url.toURI()))) {
                System.out.printf("Context logger config found: %s%n", url.toURI());
            }
        }
        // get the logger context for the servlet context
        LoggerContext loggerContext = url != null
                ? ((LoggingContextSelector) selector).getLoggerContext(contextName, url)
                : selector.getLoggerContext(contextName);
        // set the logger context for use elsewhere in the servlet context
        servletContext.setAttribute(Red5LoggerFactory.LOGGER_CONTEXT_ATTRIBUTE, loggerContext);
        // get the root logger for this context
        Logger logger = Red5LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME, contextName);
        logger.info("Starting up context: {}", contextName);
    } catch (Exception e) {
        System.err.printf("LoggingContextSelector is not the correct type: %s%n", e.getMessage());
        e.printStackTrace();
    }
}

From source file:com.eharmony.services.swagger.Swaggerizer.java

@Override
public void setServletContext(ServletContext servletContext) {
    String contextPath = servletContext.getContextPath();
    LOG.debug("Detected contextPath={}", contextPath);
    if (StringUtils.isNotEmpty(contextPath)) {
        setBasePath(contextPath);//from w  ww  . j  a  va 2 s . com
    }
}

From source file:com.fujitsu.dc.test.unit.core.jersey.filter.DcCoreContainerFilterTest.java

/**
 * ???/???????.// w  w w .j a  v a  2  s .com
 * X-FORWARDED-PROTO?X-FORWARDED-HOST?Uri, Base Uri?PROTO, HOST????????
 * @throws URISyntaxException URISyntaxException
 */
@Test
public void testFilterContainerRequest() throws URISyntaxException {
    // 
    DcCoreContainerFilter containerFilter = new DcCoreContainerFilter();
    // ContainerRequiest
    WebApplication wa = mock(WebApplication.class);
    InBoundHeaders headers = new InBoundHeaders();
    // ?
    headers.add(DcCoreUtils.HttpHeaders.X_HTTP_METHOD_OVERRIDE, HttpMethod.OPTIONS);
    // ?
    String authzValue = "Bearer tokenstring";
    String acceptValue = "text/html";
    String contentTypeValue = "application/xml";
    headers.add(DcCoreUtils.HttpHeaders.X_OVERRIDE, HttpHeaders.AUTHORIZATION + ": " + authzValue);
    headers.add(HttpHeaders.ACCEPT, contentTypeValue);
    headers.add(DcCoreUtils.HttpHeaders.X_OVERRIDE, HttpHeaders.ACCEPT + ": " + acceptValue);
    headers.add(HttpHeaders.CONTENT_TYPE, contentTypeValue);

    // X-FORWARDED-* ?
    String scheme = "https";
    String host = "example.org";
    headers.add(DcCoreUtils.HttpHeaders.X_FORWARDED_PROTO, scheme);
    headers.add(DcCoreUtils.HttpHeaders.X_FORWARDED_HOST, host);

    ContainerRequest request = new ContainerRequest(wa, HttpMethod.POST, new URI("http://dc1.example.com/hoge"),
            new URI("http://dc1.example.com/hoge/hoho"), headers, null);

    // HttpServletRequest?mock
    HttpServletRequest mockServletRequest = mock(HttpServletRequest.class);
    when(mockServletRequest.getRequestURL()).thenReturn(new StringBuffer("http://dc1.example.com"));

    ServletContext mockServletContext = mock(ServletContext.class);
    when(mockServletContext.getContextPath()).thenReturn("");
    when(mockServletRequest.getServletContext()).thenReturn(mockServletContext);
    containerFilter.setHttpServletRequest(mockServletRequest);

    // ??
    ContainerRequest filteredRequest = containerFilter.filter(request);

    // ??
    Assert.assertEquals(HttpMethod.OPTIONS, filteredRequest.getMethod());
    Assert.assertEquals(authzValue, filteredRequest.getHeaderValue(HttpHeaders.AUTHORIZATION));
    Assert.assertEquals(acceptValue, filteredRequest.getHeaderValue(HttpHeaders.ACCEPT));

    Assert.assertEquals(contentTypeValue, filteredRequest.getHeaderValue(HttpHeaders.CONTENT_TYPE));
    Assert.assertEquals(scheme, filteredRequest.getRequestUri().getScheme());
    Assert.assertEquals(host, filteredRequest.getRequestUri().getHost());
}

From source file:io.personium.test.unit.core.jersey.filter.PersoniumCoreContainerFilterTest.java

/**
 * ???/???????./*from  ww w  .  j  ava  2 s. co m*/
 * X-FORWARDED-PROTO?X-FORWARDED-HOST?Uri, Base Uri?PROTO, HOST????????
 * @throws URISyntaxException URISyntaxException
 */
@Test
public void testFilterContainerRequest() throws URISyntaxException {
    // 
    PersoniumCoreContainerFilter containerFilter = new PersoniumCoreContainerFilter();
    // ContainerRequiest
    WebApplication wa = mock(WebApplication.class);
    InBoundHeaders headers = new InBoundHeaders();
    // ?
    headers.add(PersoniumCoreUtils.HttpHeaders.X_HTTP_METHOD_OVERRIDE, HttpMethod.OPTIONS);
    // ?
    String authzValue = "Bearer tokenstring";
    String acceptValue = "text/html";
    String contentTypeValue = "application/xml";
    headers.add(PersoniumCoreUtils.HttpHeaders.X_OVERRIDE, HttpHeaders.AUTHORIZATION + ": " + authzValue);
    headers.add(HttpHeaders.ACCEPT, contentTypeValue);
    headers.add(PersoniumCoreUtils.HttpHeaders.X_OVERRIDE, HttpHeaders.ACCEPT + ": " + acceptValue);
    headers.add(HttpHeaders.CONTENT_TYPE, contentTypeValue);

    // X-FORWARDED-* ?
    String scheme = "https";
    String host = "example.org";
    headers.add(PersoniumCoreUtils.HttpHeaders.X_FORWARDED_PROTO, scheme);
    headers.add(PersoniumCoreUtils.HttpHeaders.X_FORWARDED_HOST, host);

    ContainerRequest request = new ContainerRequest(wa, HttpMethod.POST, new URI("http://dc1.example.com/hoge"),
            new URI("http://dc1.example.com/hoge/hoho"), headers, null);

    // HttpServletRequest?mock
    HttpServletRequest mockServletRequest = mock(HttpServletRequest.class);
    when(mockServletRequest.getRequestURL()).thenReturn(new StringBuffer("http://dc1.example.com"));

    ServletContext mockServletContext = mock(ServletContext.class);
    when(mockServletContext.getContextPath()).thenReturn("");
    when(mockServletRequest.getServletContext()).thenReturn(mockServletContext);
    containerFilter.setHttpServletRequest(mockServletRequest);

    // ??
    ContainerRequest filteredRequest = containerFilter.filter(request);

    // ??
    Assert.assertEquals(HttpMethod.OPTIONS, filteredRequest.getMethod());
    Assert.assertEquals(authzValue, filteredRequest.getHeaderValue(HttpHeaders.AUTHORIZATION));
    Assert.assertEquals(acceptValue, filteredRequest.getHeaderValue(HttpHeaders.ACCEPT));

    Assert.assertEquals(contentTypeValue, filteredRequest.getHeaderValue(HttpHeaders.CONTENT_TYPE));
    Assert.assertEquals(scheme, filteredRequest.getRequestUri().getScheme());
    Assert.assertEquals(host, filteredRequest.getRequestUri().getHost());
}