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:org.sventon.appl.ConfigDirectory.java

@Override
public void setServletContext(final ServletContext servletContext) {
    this.servletContext = servletContext;
    handleConfigDirectoryOverride();/*from   w w  w  . j  a v a2  s  .c o m*/

    try {
        contextPath = servletContext.getContextPath();
    } catch (NoSuchMethodError e) {
        // For backwards compatibility, simply set to "svn"
        logger.info(
                "ServletContext.getContextPath() is not supported by your servlet container. Defaulting to [svn].");
    }

    configRootDirectory = new File(sventonConfigDirectory, contextPath);
    exportDirectory = new File(getConfigRootDirectory(), exportDirectoryName);
    repositoriesDirectory = new File(getConfigRootDirectory(), repositoriesDirectoryName);

    if (createDirectories) {
        createDirectoryStructure();
    }
    logger.info("Config root directory for current servlet context set to: "
            + configRootDirectory.getAbsolutePath());
}

From source file:utils.CopiaArchivos.java

public String getContexto() {
    ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext()
            .getContext();//ww w. ja  va 2  s  .c  o  m
    return servletContext.getContextPath();
}

From source file:net.community.chest.gitcloud.facade.AbstractEnvironmentInitializer.java

@Override
public void contextDestroyed(ServletContextEvent sce) {
    Log curLogger = logger;// w w  w . ja v  a2 s  . co  m
    try {
        ServletContext context = sce.getServletContext();
        logger = ServletUtils.wrapServletContext(context, Level.CONFIG);
        contextDestroyed(context);
        logger.info("contextDestroyed(" + context.getContextPath() + ")");
    } catch (Throwable t) {
        logger.error("Failed (" + t.getClass().getSimpleName() + ") to destroy: " + t.getMessage(), t);
        throw ExtendedExceptionUtils.toRuntimeException(t, true);
    } finally {
        logger = curLogger;
    }
}

From source file:org.smigo.config.WebAppInitializer.java

@Override
protected void beforeSpringSecurityFilterChain(ServletContext servletContext) {
    super.beforeSpringSecurityFilterChain(servletContext);
    log.info("Starting servlet context");
    log.info("contextName: " + servletContext.getServletContextName());
    log.info("contextPath:" + servletContext.getContextPath());
    log.info("effectiveMajorVersion:" + servletContext.getEffectiveMajorVersion());
    log.info("effectiveMinorVersion:" + servletContext.getEffectiveMinorVersion());
    log.info("majorVersion:" + servletContext.getMajorVersion());
    log.info("minorVersion:" + servletContext.getMinorVersion());
    log.info("serverInfo:" + servletContext.getServerInfo());
    //               ", virtualServerName:" + servletContext.getVirtualServerName() +
    log.info("toString:" + servletContext.toString());

    for (Enumeration<String> e = servletContext.getAttributeNames(); e.hasMoreElements();) {
        log.info("Attribute:" + e.nextElement());
    }//from w w w  . j ava2  s.c  o  m
    for (Map.Entry<String, String> env : System.getenv().entrySet()) {
        log.info("System env:" + env.toString());
    }
    for (Map.Entry<Object, Object> prop : System.getProperties().entrySet()) {
        log.info("System prop:" + prop.toString());
    }

    final String profile = System.getProperty("smigoProfile", EnvironmentProfile.PRODUCTION);
    log.info("Starting with profile " + profile);

    WebApplicationContext context = new AnnotationConfigWebApplicationContext() {
        {
            register(WebConfiguration.class);
            setDisplayName("SomeRandomName");
            getEnvironment().setActiveProfiles(profile);
        }
    };

    FilterRegistration.Dynamic characterEncodingFilter = servletContext.addFilter("CharacterEncodingFilter",
            new CharacterEncodingFilter());
    characterEncodingFilter.setInitParameter("encoding", "UTF-8");
    characterEncodingFilter.setInitParameter("forceEncoding", "true");
    characterEncodingFilter.addMappingForUrlPatterns(null, false, "/*");

    servletContext.addListener(new RequestContextListener());
    servletContext.addListener(new ContextLoaderListener(context));

    //http://stackoverflow.com/questions/4811877/share-session-data-between-2-subdomains
    //        servletContext.getSessionCookieConfig().setDomain(getDomain());

    DispatcherServlet dispatcherServlet = new DispatcherServlet(context);
    dispatcherServlet.setThrowExceptionIfNoHandlerFound(false);
    servletContext.addServlet("dispatcher", dispatcherServlet).addMapping("/");
}

From source file:net.community.chest.gitcloud.facade.AbstractEnvironmentInitializer.java

@Override
public void contextInitialized(ServletContextEvent sce) {
    Log curLogger = logger;// w  w w  . j  a v  a 2 s  .  c  o m
    try {
        ServletContext context = sce.getServletContext();
        logger = ServletUtils.wrapServletContext(context, Level.CONFIG);
        contextInitialized(context);
        logger.info("contextInitialized(" + context.getContextPath() + ")");
    } catch (Throwable t) {
        logger.error("Failed (" + t.getClass().getSimpleName() + ") to initialize: " + t.getMessage(), t);
        throw ExtendedExceptionUtils.toRuntimeException(t, true);
    } finally {
        logger = curLogger;
    }
}

From source file:com.afis.jx.ckfinder.connector.utils.FileUtils.java

/**
 * Gets an absolute path to CKFinder resource folder for which path is calculated from baseURL configuration property.<br>
 * This method has limited capabilities. First it will check is folder should be created in application context. If not it will try to
 * create folder in Tomcat ROOT folder or GlassFish docroot folder. If this fails this method will fall back and try to create folder
 * inside application context (features like gallery may not work in this case as most likely baseURL and baseDir point to different
 * locations).<br>/* w  ww  .  j a va2  s.  c  o m*/
 * Finally if nothing worked this method returns {@code null}.
 *
 * @param path relative or absolute path to a CKFinder folder.
 * @return an absolute path to a folder in CKFinder
 * @throws ConnectorException when {@code ServletContext} is {@code null} or path to resource cannot be obtained.
 */
public static String calculatePathFromBaseUrl(String path) throws ConnectorException {
    if (path != null && !path.equals("")) {
        ServletContext sc = ServletContextFactory.getServletContext();
        String tempPath = PathUtils.addSlashToBeginning(path);
        String finalPath;
        if (tempPath.startsWith(sc.getContextPath() + "/")) {
            //Try creating path relative to application context.
            if ((finalPath = sc.getRealPath(tempPath.replace(sc.getContextPath(), ""))) != null) {
                return finalPath;
            } else if ((finalPath = sc.getRealPath(CKFINDER_FOLDER_NAME)) != null) {
                //If above is null, try getting path to direct subfolder in application context.
                finalPath = PathUtils.escape(finalPath);
                return finalPath.substring(0, finalPath.lastIndexOf(CKFINDER_FOLDER_NAME))
                        + tempPath.replace(sc.getContextPath(), "");
            } else {
                finalPath = getClassPath();
                if (finalPath.indexOf(sc.getContextPath()) >= 0) {
                    finalPath = finalPath.substring(0, finalPath.indexOf(sc.getContextPath()));
                    finalPath = finalPath + tempPath;
                    return finalPath;
                } else {
                    finalPath = null;
                }
            }
        } else {
            //Try creating path to ROOT on TC or docroot on GF
            finalPath = getClassPath();

            String tcPath = getTomcatRootPath(sc, finalPath);
            String gfPath = getGlassFishRootPath(sc, finalPath);
            if (!tcPath.equals("")) {
                tempPath = filterRelativePathChars(tempPath);
                finalPath = tcPath + tempPath;
            } else if (!gfPath.equals("")) {
                tempPath = filterRelativePathChars(tempPath);
                finalPath = gfPath + tempPath;
            } else {
                //Fall back and try creating path relative application context
                String realPath = sc.getRealPath(tempPath);
                if (realPath != null) {
                    return realPath;
                } else if (finalPath.indexOf(sc.getContextPath() + "/") >= 0) {
                    finalPath = finalPath.substring(0,
                            finalPath.indexOf(sc.getContextPath()) + sc.getContextPath().length());
                    tempPath = filterRelativePathChars(tempPath);
                    finalPath = finalPath + tempPath;
                } else {
                    finalPath = null;
                }
            }
        }
        return finalPath;
    }
    return null;
}

From source file:org.jbpm.formbuilder.server.RESTFormService.java

private String createHtmlTemplate(Object html, String language, ServletContext context) throws IOException {
    String contextPath = context.getContextPath();
    File file = File.createTempFile("createHtmlTemplate", ".temp");
    FileUtils.writeStringToFile(file, html.toString());
    String url = contextPath + "/rest/form/template/file/" + file.getName();
    return url;//w w w  .ja va  2  s.co  m
}

From source file:org.jasig.schedassist.web.VisibleScheduleTag.java

/**
 * // w ww .  j a v  a 2  s.  com
 * @return the path to the silk icons directory (dependency on {@link ServletContext} of the request)
 */
protected String getSilkIconPrefix(ServletContext servletContext) {
    final String silkPrefix = servletContext.getContextPath() + "/rs/famfamfam/silk/1.3/";
    return silkPrefix;
}

From source file:org.openmrs.module.jmx.impl.JMXServiceImpl.java

/**
 * Generates a qualified object name for a management bean
 * @param name the name//from w w  w.  j a va  2 s.  c  o  m
 * @param path the path
 * @return the object name
 * @throws MalformedObjectNameException
 */
private ObjectName getObjectName(String name, String path) throws MalformedObjectNameException {
    // Server name is based on the context path, minus the preceding slash
    ServletContext ctx = ContextProvider.getServletContext();

    Map<String, String> components = new HashMap<String, String>();
    components.put("name", name);

    // Host is based on the context root to keep different OpenMRS instances separate
    if (ctx != null) {
        String ctxPath = ctx.getContextPath();
        components.put("host", ctxPath.charAt(0) == '/' ? ctxPath.substring(1) : ctxPath);
    }

    // Append folder name to path
    if (path != null && path.length() > 0)
        components.put("path", path);

    // Build qualified name from domain and components      
    return new ObjectName(Constants.MBEAN_DOMAIN + ":" + Utils.nameValueList(components));
}