Example usage for javax.servlet ServletContext getRealPath

List of usage examples for javax.servlet ServletContext getRealPath

Introduction

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

Prototype

public String getRealPath(String path);

Source Link

Document

Gets the real path corresponding to the given virtual path.

Usage

From source file:edu.lternet.pasta.portal.ConfigurationListener.java

@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
    ServletContext servletContext = servletContextEvent.getServletContext();

    // Create an absolute path for accessing configuration properties
    String cwd = servletContext.getRealPath(".");

    // Initialize log4j
    String log4jPropertiesPath = cwd + "/WEB-INF/conf/log4j.properties";
    PropertyConfigurator.configureAndWatch(log4jPropertiesPath);

    // Initialize commons configuration
    String appConfigPath = cwd + "/WEB-INF/conf/dataportal.properties";
    try {/*  w  w w. j  a v  a  2 s .  c  o m*/
        config = new PropertiesConfiguration(appConfigPath);
        config.setProperty("system.cwd", cwd);
        config.save();
    } catch (ConfigurationException e) {
        logger.error(e);
        e.printStackTrace();
    }

}

From source file:org.n52.sensorweb.series.policy.editor.srv.impl.XmlFileSimplePermissionService.java

@Override
public void setServletContext(ServletContext sc) {
    permissionFile = sc.getRealPath(permissionFile);
}

From source file:by.creepid.jsf.fileupload.UploadRenderer.java

private String getServletRealPath(ExternalContext external, String target) {
    ServletContext servletContext = (ServletContext) external.getContext();

    String realPath = servletContext.getRealPath(target.toString());
    if (!realPath.endsWith("/")) {
        realPath += "/";
    }/*from w  ww  .  j  av a  2 s . com*/

    return realPath;
}

From source file:interactivespaces.example.activity.externalproxy.webappproxy.InteractiveSpacesExternalProxyServlet.java

/**
 * Get the content for the function of the mobile device.
 *
 * @param function//from ww  w  .jav  a 2  s  .c  om
 *          the function the mobile device should have
 *
 * @return the content for the function
 */
private String getFunctionContent(String function) {
    ServletContext context = getServletContext();
    String fullPath = context.getRealPath("/WEB-INF/functions/" + function + ".html");

    String serverHost = context.getInitParameter(CONTEXT_PARAMETER_MY_SERVER_HOST);

    String content = Files.readFile(new File(fullPath));

    content = content.replaceAll("\\@HOST\\@", serverHost);

    return content;
}

From source file:org.adeptnet.atlassian.common.Common.java

private Function<String, String> getFileName(final ServletContext servletContext) {
    return (fileName) -> {
        return servletContext.getRealPath(fileName);
    };/*  ww w.ja  va2  s.  c  o m*/
}

From source file:org.guzz.web.context.ContextLoader.java

public GuzzContext initGuzzContext(ServletContext sc) throws Exception {
    String configFile = sc.getInitParameter(CONFIG_LOCATION_PARAM);
    if (configFile == null) {
        configFile = "/WEB-INF/guzz.xml";
    }//from ww  w . ja va2s .c o  m

    String name = sc.getRealPath(configFile);

    FileResource fr = new FileResource(name);

    try {
        GuzzContext gf = new Configuration(fr).newGuzzContext();

        return initGuzzContext(sc, gf);
    } finally {
        CloseUtil.close(fr);
    }
}

From source file:alpine.servlets.FileSystemResourceServlet.java

@Override
protected StaticResource getStaticResource(HttpServletRequest request) throws IllegalArgumentException {
    final String pathInfo = request.getPathInfo();

    if (pathInfo == null || pathInfo.isEmpty() || "/".equals(pathInfo)) {
        throw new IllegalArgumentException();
    }//from ww  w  .j  a  v  a  2  s.  co  m

    String name = "";
    try {
        name = URLDecoder.decode(pathInfo.substring(1), StandardCharsets.UTF_8.name());
    } catch (UnsupportedEncodingException e) {
        LOGGER.error(e.getMessage());
    }

    final ServletContext context = request.getServletContext();
    final File file = (absolute) ? new File(directory, name)
            : new File(context.getRealPath("/"), name).getAbsoluteFile();

    return !file.exists() ? null : new StaticResource() {
        @Override
        public long getLastModified() {
            return file.lastModified();
        }

        @Override
        public InputStream getInputStream() throws IOException {
            return new FileInputStream(file);
        }

        @Override
        public String getFileName() {
            return file.getName();
        }

        @Override
        public long getContentLength() {
            return file.length();
        }
    };
}

From source file:myPackage.UploadBean.java

public boolean doFilePost(HttpServletRequest request, ServletContext context) {
    if (request.getContentType() == null)
        return false;

    if (!request.getContentType().startsWith("multipart/formdata"))
        return false;

    String path = context.getRealPath(getDiretorio());

    try {/*w w  w  .j a  v a2 s .c o m*/
        List list = sfu.parseRequest(request);
        Iterator iterator = list.iterator();

        while (iterator.hasNext()) {
            FileItem item = (FileItem) iterator.next();

            if (!item.isFormField()) {
                filename = item.getName();

                if ((filename != null) && (!filename.equals(""))) {
                    filename = (new File(filename)).getName();
                    item.write(new File(path + File.separator + filename));
                }
            }
        }
    }

    catch (FileUploadException e) {
        e.printStackTrace();
    }

    catch (Exception e) {
        e.printStackTrace();
    }

    return true;
}

From source file:edu.lternet.pasta.datapackagemanager.dataserver.ConfigurationListener.java

/**
 * Run initialization code when at web application start-up.
 * //from ww  w .ja  v  a2s .co  m
 * 
 * @param  servletContextEvent     The ServletContextEvent object
 * @throws ResourceNotFoundException
 *                 if the properties file can not be found
 * @throws IllegalStateException
 *                 if an {@link IOException} is thrown while reading the 
 *                 properties file
 */
public void contextInitialized(ServletContextEvent servletContextEvent) {
    ServletContext servletContext = servletContextEvent.getServletContext();

    // Create an absolute path for accessing configuration properties
    String cwd = servletContext.getRealPath(".");

    // Initialize log4j
    String log4jPropertiesPath = cwd + "/WEB-INF/conf/log4j.properties";
    PropertyConfigurator.configureAndWatch(log4jPropertiesPath);

    // Initialize commons configuration
    String appConfigPath = cwd + "/WEB-INF/conf/datapackagemanager.properties";
    try {
        config = new PropertiesConfiguration(appConfigPath);
        config.setProperty("system.cwd", cwd);
        config.save();
    } catch (ConfigurationException e) {
        logger.error(e);
        e.printStackTrace();
    }

}