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:com.littcore.web.listener.InitSystemListener.java

/**
 * ?./*  w w w .j av  a 2s.c  o  m*/
 * 1????
 * 2?license
 *    2.1?Web????
 *  2.2??
 *  2.3?????
 * 
 * @param event 
 *            
 */
public void contextInitialized(ServletContextEvent event) {
    //??
    logger.info("System initializing...");
    super.contextInitialized(event);
    ServletContext application = event.getServletContext();
    CoreConstants.ROOT_PATH = application.getRealPath("/");
    logger.info("Run-time root path - " + CoreConstants.ROOT_PATH);
    //register date converter
    ConvertUtils.register(new DateConverter(), java.util.Date.class);
}

From source file:com.orchestra.portale.controller.ImageBackupRestoreController.java

@RequestMapping(value = "/restoreimg")
public ModelAndView restore(HttpServletRequest request) throws IOException {

    ModelAndView model = new ModelAndView("showResult");
    HttpSession session = request.getSession();
    ServletContext sc = session.getServletContext();
    File root = new File(sc.getRealPath("/"));
    File dir = new File(sc.getRealPath("/") + "dist");
    copy(root.getParentFile().getParentFile().getParentFile().getPath() + File.separator + "BackupImg"
            + File.separator + "poi", dir.getCanonicalPath());
    copy(root.getParentFile().getParentFile().getParentFile().getPath() + File.separator + "BackupImg"
            + File.separator + "page", dir.getCanonicalPath());
    copy(root.getParentFile().getParentFile().getParentFile().getPath() + File.separator + "BackupImg"
            + File.separator + "dpage", dir.getCanonicalPath());
    copy(root.getParentFile().getParentFile().getParentFile().getPath() + File.separator + "BackupImg"
            + File.separator + "user", dir.getCanonicalPath());

    model.addObject("mess", "Tutte le immagini sono state ripristinate");
    return model;
}

From source file:com.orchestra.portale.controller.ImageBackupRestoreController.java

@RequestMapping(value = "/backupimg")
public ModelAndView backup(HttpServletRequest request) throws IOException {
    ModelAndView model = new ModelAndView("showResult");
    HttpSession session = request.getSession();

    ServletContext sc = session.getServletContext();
    File root = new File(sc.getRealPath("/"));

    File dir = new File(sc.getRealPath("/") + "dist" + File.separator + "poi");
    File dir2 = new File(sc.getRealPath("/") + "dist" + File.separator + "user");
    File dir3 = new File(sc.getRealPath("/") + "dist" + File.separator + "page");
    File dir4 = new File(sc.getRealPath("/") + "dist" + File.separator + "dpage");
    copy(dir.getCanonicalPath(),//from   w  w  w. jav  a  2 s  . c o  m
            root.getParentFile().getParentFile().getParentFile().getPath() + File.separator + "BackupImg");
    copy(dir2.getCanonicalPath(),
            root.getParentFile().getParentFile().getParentFile().getPath() + File.separator + "BackupImg");
    copy(dir3.getCanonicalPath(),
            root.getParentFile().getParentFile().getParentFile().getPath() + File.separator + "BackupImg");
    copy(dir4.getCanonicalPath(),
            root.getParentFile().getParentFile().getParentFile().getPath() + File.separator + "BackupImg");
    model.addObject("mess", "Tutte le immagini sono state copiate");
    return model;
}

From source file:com.googlecode.psiprobe.controllers.apps.DownloadXMLConfController.java

protected ModelAndView handleContext(String contextName, Context context, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    if (downloadTarget == null) {
        throw new RuntimeException("Download target is not set for " + getClass().getName());
    }//from  ww  w.j  ava  2s.c o  m

    String xmlPath;

    if (TARGET_WEB_XML.equals(downloadTarget)) {
        ServletContext sctx = context.getServletContext();
        xmlPath = sctx.getRealPath("/WEB-INF/web.xml");
    } else if (TARGET_CONTEXT_XML.equals(downloadTarget)) {
        xmlPath = context.getConfigFile();
    } else {
        throw new RuntimeException("Unknown download target " + getDownloadTarget());
    }

    if (xmlPath != null) {
        File xmlFile = new File(xmlPath);
        if (xmlFile.exists()) {
            Utils.sendFile(request, response, xmlFile);
        } else {
            logger.debug("File " + xmlPath + " of " + contextName + " application does not exists.");
        }
    } else {
        logger.debug("Cannot determine path to " + getDownloadTarget() + " file of " + contextName
                + " application.");
    }

    return null;
}

From source file:com.egreen.tesla.server.api.ComponentContextLoader.java

public ComponentContextLoader(ServletContext context) {
    this.context = context;
    realPath = context.getRealPath("/");
    this.context.setAttribute("realpath", realPath);
    try {/*from   w  ww .jav a 2s  .c  o  m*/
        create();///init context
    } catch (ConfigurationException | IOException | NoSuchMethodException | IllegalAccessException
            | IllegalArgumentException | InvocationTargetException | ClassNotFoundException
            | InstantiationException | NotFoundException | CannotCompileException | SQLException ex) {
        java.util.logging.Logger.getLogger(ComponentContextLoader.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:org.shept.util.JarResourceCopier.java

public void initializeResources(ServletContext context) {
    String destPath = StringUtils.cleanPath(context.getRealPath(getTargetPath()));
    if (files != null && files.length > 0) {
        for (int i = 0; i < files.length; i++) {
            String source = StringUtils.applyRelativePath(getSourcePath(), files[i]);
            // surprise surprise you can't use StringUtils.applyRelativePath here this will cut off the last part of destPath
            JarUtils.copyResourcesOnce(new ClassPathResource(source), destPath, destPath + "/" + files[i]);
        }//from   w  ww.  j a v a 2s. c om
    } else {
        JarUtils.copyResourcesOnce(new ClassPathResource(getSourcePath()), destPath);
    }
}

From source file:cn.vlabs.umt.ui.UMTStartupListener.java

public void contextInitialized(ServletContextEvent event) {
    ServletContext context = event.getServletContext();
    PropertyConfigurator.configure(context.getRealPath("/WEB-INF/conf/log4j.properties"));
    String contextxml = context.getInitParameter("contextConfigLocation");
    if (contextxml == null) {
        contextxml = "/WEB-INF/conf/UMTContext.xml";
    }/*from   ww  w. j  a v  a  2  s. co m*/
    //FIX the bug in linux
    String realpath = context.getRealPath(contextxml);
    if (realpath != null && realpath.startsWith("/")) {
        realpath = "/" + realpath;
    }
    FileSystemXmlApplicationContext factory = new FileSystemXmlApplicationContext(realpath);
    PathMapper mapper = (PathMapper) factory.getBean("PathMapper");
    mapper.setContext(context);

    CreateTable createTable = (CreateTable) factory.getBean("CreateTable");
    if (!createTable.isTableExist()) {
        createTable.createTable();
    }
    factory.getBean("UMTCredUtil");
    context.setAttribute(Attributes.APPLICATION_CONTEXT_KEY, factory);
    UMTContext.setFactory(factory);

}

From source file:com.orchestra.portale.controller.UserInfoController.java

@RequestMapping(value = "/userInfo")
@Secured("ROLE_USER")
public ModelAndView getUserInfo(HttpServletRequest request) {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    ModelAndView model = new ModelAndView("userInfo");
    ModelAndView model2 = new ModelAndView("index");
    if (auth != null) {
        User user = pm.findUserByUsername(auth.getName());
        HttpSession session = request.getSession();
        ServletContext sc = session.getServletContext();
        File dir = new File(sc.getRealPath("/") + "dist" + File.separator + "user" + File.separator + "img"
                + File.separator + user.getId() + File.separator + "avatar.jpg");
        if (dir.exists()) {
            model.addObject("avatar", "./dist/user/img/" + user.getId() + "/avatar.jpg");
        } else {//from  ww w .  j  a  va2s  .c  om
            model.addObject("avatar", "./dist/img/default_avatar.png");
        }

        model.addObject("user", user);

        if (request.isUserInRole("ROLE_FB")) {
            Object categories = fbprofiler.getFBCategories();
            if (categories != null) {
                model.addObject("categories", categories);
            }
        }
    } else {
        return model2;
    }
    return model;
}

From source file:de.ingrid.iplug.web.DatatypeContextListener.java

public void contextInitialized(ServletContextEvent servletcontextevent) {
    ServletContext servletContext = servletcontextevent.getServletContext();
    String realPathToDatatypes = servletContext.getRealPath(DATA_TYPES);
    IDataTypeProvider dataTypeProvider = new DataTypeProvider(new File(realPathToDatatypes),
            new DataTypeEditor());
    BeanFactory beanFactory = (BeanFactory) servletContext.getAttribute("beanFactory");
    try {//from w ww .  jav a  2  s.com
        beanFactory.addBean("dataTypeProvider", dataTypeProvider);
    } catch (IOException e) {
        LOG.error("can not add plugdescription", e);
    }
}

From source file:com.artglorin.web.utils.ResourcesHelperTest.java

@BeforeClass
public void testGetInstance() {
    try {//from   www . j a  v a  2 s. co  m
        testFolder = Files.createTempDirectory(pathToResources);
    } catch (IOException e) {
        e.printStackTrace();
        fail("Cannot create Test directory");
    }
    ServletContext servletContext = mock(ServletContext.class);
    when(servletContext.getRealPath(pathToResources))
            .thenAnswer(answer -> testFolder.toAbsolutePath().toString());
    resourcesHelper = new ResourcesHelper(pathToResources, servletContext);
}