Java Code Examples for play.vfs.VirtualFile#exists()

The following examples show how to use play.vfs.VirtualFile#exists() . 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: TemplateLoader.java    From restcommander with Apache License 2.0 6 votes vote down vote up
/**
 * List all found templates
 * @return A list of executable templates
 */
public static List<Template> getAllTemplate() {
    List<Template> res = new ArrayList<Template>();
    for (VirtualFile virtualFile : Play.templatesPath) {
        scan(res, virtualFile);
    }
    for (VirtualFile root : Play.roots) {
        VirtualFile vf = root.child("conf/routes");
        if (vf != null && vf.exists()) {
            Template template = load(vf);
            if (template != null) {
                template.compile();
            }
        }
    }
    return res;
}
 
Example 2
Source File: Controller.java    From restcommander with Apache License 2.0 6 votes vote down vote up
/**
 * Send a 302 redirect response.
 * @param file The Location to redirect
 */
protected static void redirectToStatic(String file) {
    try {
        VirtualFile vf = Play.getVirtualFile(file);
        if (vf == null || !vf.exists()) {
            throw new NoRouteFoundException(file);
        }
        throw new RedirectToStatic(Router.reverse(Play.getVirtualFile(file)));
    } catch (NoRouteFoundException e) {
        StackTraceElement element = PlayException.getInterestingStrackTraceElement(e);
        if (element != null) {
            throw new NoRouteFoundException(file, Play.classes.getApplicationClass(element.getClassName()), element.getLineNumber());
        } else {
            throw e;
        }
    }
}
 
Example 3
Source File: ApplicationClassloader.java    From restcommander with Apache License 2.0 5 votes vote down vote up
/**
 * You know ...
 */
@Override
public InputStream getResourceAsStream(String name) {
    for (VirtualFile vf : Play.javaPath) {
        VirtualFile res = vf.child(name);
        if (res != null && res.exists()) {
            return res.inputstream();
        }
    }
    return super.getResourceAsStream(name);
}
 
Example 4
Source File: ApplicationClassloader.java    From restcommander with Apache License 2.0 5 votes vote down vote up
/**
 * You know ...
 */
@Override
public URL getResource(String name) {
    for (VirtualFile vf : Play.javaPath) {
        VirtualFile res = vf.child(name);
        if (res != null && res.exists()) {
            try {
                return res.getRealFile().toURI().toURL();
            } catch (MalformedURLException ex) {
                throw new UnexpectedException(ex);
            }
        }
    }
    return super.getResource(name);
}
 
Example 5
Source File: ApplicationClassloader.java    From restcommander with Apache License 2.0 5 votes vote down vote up
/**
 * You know ...
 */
@Override
public Enumeration<URL> getResources(String name) throws IOException {
    List<URL> urls = new ArrayList<URL>();
    for (VirtualFile vf : Play.javaPath) {
        VirtualFile res = vf.child(name);
        if (res != null && res.exists()) {
            try {
                urls.add(res.getRealFile().toURI().toURL());
            } catch (MalformedURLException ex) {
                throw new UnexpectedException(ex);
            }
        }
    }
    Enumeration<URL> parent = super.getResources(name);
    while (parent.hasMoreElements()) {
        URL next = parent.nextElement();
        if (!urls.contains(next)) {
            urls.add(next);
        }
    }
    final Iterator<URL> it = urls.iterator();
    return new Enumeration<URL>() {

        public boolean hasMoreElements() {
            return it.hasNext();
        }

        public URL nextElement() {
            return it.next();
        }
    };
}
 
Example 6
Source File: ApplicationClasses.java    From restcommander with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieve the corresponding source file for a given class name.
 * It handles innerClass too !
 * @param name The fully qualified class name 
 * @return The virtualFile if found
 */
public static VirtualFile getJava(String name) {
    String fileName = name;
    if (fileName.contains("$")) {
        fileName = fileName.substring(0, fileName.indexOf("$"));
    }
    fileName = fileName.replace(".", "/") + ".java";
    for (VirtualFile path : Play.javaPath) {
        VirtualFile javaFile = path.child(fileName);
        if (javaFile.exists()) {
            return javaFile;
        }
    }
    return null;
}
 
Example 7
Source File: ServletWrapper.java    From restcommander with Apache License 2.0 5 votes vote down vote up
public void serveStatic(HttpServletResponse servletResponse, HttpServletRequest servletRequest, RenderStatic renderStatic) throws IOException {

        VirtualFile file = Play.getVirtualFile(renderStatic.file);
        if (file == null || file.isDirectory() || !file.exists()) {
            serve404(servletRequest, servletResponse, new NotFound("The file " + renderStatic.file + " does not exist"));
        } else {
            servletResponse.setContentType(MimeTypes.getContentType(file.getName()));
            boolean raw = Play.pluginCollection.serveStatic(file, Request.current(), Response.current());
            if (raw) {
                copyResponse(Request.current(), Response.current(), servletRequest, servletResponse);
            } else {
                if (Play.mode == Play.Mode.DEV) {
                    servletResponse.setHeader("Cache-Control", "no-cache");
                    servletResponse.setHeader("Content-Length", String.valueOf(file.length()));
                    if (!servletRequest.getMethod().equals("HEAD")) {
                        copyStream(servletResponse, file.inputstream());
                    } else {
                        copyStream(servletResponse, new ByteArrayInputStream(new byte[0]));
                    }
                } else {
                    long last = file.lastModified();
                    String etag = "\"" + last + "-" + file.hashCode() + "\"";
                    if (!isModified(etag, last, servletRequest)) {
                        servletResponse.setHeader("Etag", etag);
                        servletResponse.setStatus(304);
                    } else {
                        servletResponse.setHeader("Last-Modified", Utils.getHttpDateFormatter().format(new Date(last)));
                        servletResponse.setHeader("Cache-Control", "max-age=" + Play.configuration.getProperty("http.cacheControl", "3600"));
                        servletResponse.setHeader("Etag", etag);
                        copyStream(servletResponse, file.inputstream());
                    }
                }
            }
        }
    }
 
Example 8
Source File: PlayGrizzlyAdapter.java    From restcommander with Apache License 2.0 4 votes vote down vote up
public void serveStatic(GrizzlyRequest grizzlyRequest, GrizzlyResponse grizzlyResponse, RenderStatic renderStatic) {
    VirtualFile file = Play.getVirtualFile(renderStatic.file);
    if (file == null || file.isDirectory() || !file.exists()) {
        serve404(grizzlyRequest, grizzlyResponse, new NotFound("The file " + renderStatic.file + " does not exist"));
    } else {
        grizzlyResponse.setContentType(MimeTypes.getContentType(file.getName()));
        boolean raw = false;
        for (PlayPlugin plugin : Play.plugins) {
            if (plugin.serveStatic(file, Request.current(), Response.current())) {
                raw = true;
                break;
            }
        }
        try {
            if (raw) {
                copyResponse(Request.current(), Response.current(), grizzlyRequest, grizzlyResponse);
            } else {
                if (Play.mode == Play.Mode.DEV) {
                    grizzlyResponse.setHeader("Cache-Control", "no-cache");
                    grizzlyResponse.setHeader("Content-Length", String.valueOf(file.length()));
                    if (!grizzlyRequest.getMethod().equals("HEAD")) {
                        copyStream(grizzlyResponse, file.inputstream());
                    } else {
                        copyStream(grizzlyResponse, new ByteArrayInputStream(new byte[0]));
                    }
                } else {
                    long last = file.lastModified();
                    String etag = "\"" + last + "-" + file.hashCode() + "\"";
                    if (!isModified(etag, last, grizzlyRequest)) {
                        grizzlyResponse.setHeader("Etag", etag);
                        grizzlyResponse.setStatus(304);
                    } else {
                        grizzlyResponse.setHeader("Last-Modified", Utils.getHttpDateFormatter().format(new Date(last)));
                        grizzlyResponse.setHeader("Cache-Control", "max-age=" + Play.configuration.getProperty("http.cacheControl", "3600"));
                        grizzlyResponse.setHeader("Etag", etag);
                        copyStream(grizzlyResponse, file.inputstream());
                    }
                }

            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
 
Example 9
Source File: Router.java    From restcommander with Apache License 2.0 4 votes vote down vote up
public static String reverseWithCheck(String name, VirtualFile file, boolean absolute) {
    if (file == null || !file.exists()) {
        throw new NoRouteFoundException(name + " (file not found)");
    }
    return reverse(file, absolute);
}
 
Example 10
Source File: Fixtures.java    From restcommander with Apache License 2.0 4 votes vote down vote up
/**
 *
 * TODO: reuse beanutils or MapUtils?
 *
 * @param entityProperties
 * @param prefix
 * @return an hash with the resolved entity name and the corresponding value
 */
static Map<String, String[]> serialize(Map<?, ?> entityProperties, String prefix) {

    if (entityProperties == null) {
        return MapUtils.EMPTY_MAP;
    }

    final Map<String, String[]> serialized = new HashMap<String, String[]>();

    for (Object key : entityProperties.keySet()) {

        Object value = entityProperties.get(key);
        if (value == null) {
            continue;
        }
        if (value instanceof Map<?, ?>) {
            serialized.putAll(serialize((Map<?, ?>) value, prefix + "." + key));
        } else if (value instanceof Date) {
            serialized.put(prefix + "." + key.toString(), new String[]{new SimpleDateFormat(DateBinder.ISO8601).format(((Date) value))});
        } else if (Collection.class.isAssignableFrom(value.getClass())) {
            Collection<?> l = (Collection<?>) value;
            String[] r = new String[l.size()];
            int i = 0;
            for (Object el : l) {
                r[i++] = el.toString();
            }
            serialized.put(prefix + "." + key.toString(), r);
        } else if (value instanceof String && value.toString().matches("<<<\\s*\\{[^}]+}\\s*")) {
            Matcher m = Pattern.compile("<<<\\s*\\{([^}]+)}\\s*").matcher(value.toString());
            m.find();
            String file = m.group(1);
            VirtualFile f = Play.getVirtualFile(file);
            if (f != null && f.exists()) {
                serialized.put(prefix + "." + key.toString(), new String[]{f.contentAsString()});
            }
        } else {
            serialized.put(prefix + "." + key.toString(), new String[]{value.toString()});
        }
    }

    return serialized;
}