Package org.openqa.jetty.util

Examples of org.openqa.jetty.util.Resource


        {
            StringTokenizer tokenizer= new StringTokenizer(classPath, ",;");

            while (tokenizer.hasMoreTokens())
            {
                Resource resource= Resource.newResource(tokenizer.nextToken());
                if (log.isDebugEnabled())
                    log.debug("Path resource=" + resource);

                // Resolve file path if possible
                File file= resource.getFile();

                if (file != null)
                {
                    URL url= resource.getURL();
                    addURL(url);
                    _urlClassPath= (_urlClassPath == null) ? url.toString() : (_urlClassPath + "," + url.toString());
                }
                else
                {
                    // Add resource or expand jar/
                    if (!resource.isDirectory() && file == null)
                    {
                        InputStream in= resource.getInputStream();
                        File lib= new File(context.getTempDirectory(), "lib");
                        if (!lib.exists())
                        {
                            lib.mkdir();
                            lib.deleteOnExit();
                        }
                        File jar= File.createTempFile("Jetty-", ".jar", lib);

                        jar.deleteOnExit();
                        if (log.isDebugEnabled())
                            log.debug("Extract " + resource + " to " + jar);
                        FileOutputStream out = null;
                        try
                        {
                            out= new FileOutputStream(jar);
                            IO.copy(in, out);
                        }
                        finally
                        {
                            IO.close(out);
                        }

                        URL url= jar.toURL();
                        addURL(url);
                        _urlClassPath=
                            (_urlClassPath == null) ? url.toString() : (_urlClassPath + "," + url.toString());
                    }
                    else
                    {
                        URL url= resource.getURL();
                        addURL(url);
                        _urlClassPath=
                            (_urlClassPath == null) ? url.toString() : (_urlClassPath + "," + url.toString());
                    }
                }
View Full Code Here


                String location = (String)iter.next();
                if (location!=null && location.toLowerCase().endsWith(".tld"))
                {
                    if (!location.startsWith("/"))
                        location="/WEB-INF/"+location;
                    Resource l=_context.getBaseResource().addPath(location);
                    tlds.add(l);
                }
            }
        }
       
        // Look for any tlds in WEB-INF directly.
        if (_context.getWebInf()!=null)
        {
            String[] contents = _context.getWebInf().list();
            for (int i=0;i<contents.length;i++)
            {
                if (contents[i]!=null && contents[i].toLowerCase().endsWith(".tld"))
                {
                    Resource l=_context.getWebInf().addPath(contents[i]);
                    tlds.add(l);
                }
               
            }

            // Look for any tlds in the META-INF of included jars
            Resource lib=_context.getWebInf().addPath("lib/");
            if (lib.exists() && lib.isDirectory())
            {
                contents = lib.list();
                for (int i=0;i<contents.length;i++)
                {
                    if (contents[i]!=null && contents[i].toLowerCase().endsWith(".jar"))
                    {
                        Resource l=lib.addPath(contents[i]);
                        Resource meta=Resource.newResource("jar:"+l+"!/META-INF/");
                        if (meta.exists())
                        {
                            String[] meta_contents=meta.list();
                           
                            for (int j=0;j<meta_contents.length;j++)
                            {
                                if (meta_contents[j]!=null && meta_contents[j].toLowerCase().endsWith(".tld"))
                                {
                                    Resource t=meta.addPath(meta_contents[j]);
                                    tlds.add(t);
                                }
                            }
                        }
                    }
                }
            }
        }
       
        // Create a TLD parser
        XmlParser parser = new XmlParser(false);
       
        parser.redirectEntity("web-jsptaglibrary_1_1.dtd",WebApplicationContext.class.getResource("/javax/servlet/jsp/resources/web-jsptaglibrary_1_1.dtd"));
        parser.redirectEntity("web-jsptaglibrary_1_2.dtd",WebApplicationContext.class.getResource("/javax/servlet/jsp/resources/web-jsptaglibrary_1_2.dtd"));
        parser.redirectEntity("web-jsptaglibrary_2_0.xsd",WebApplicationContext.class.getResource("/javax/servlet/jsp/resources/web-jsptaglibrary_2_0.xsd"));
        parser.setXpath("/taglib/listener/listener-class");
        // Parse all the discovered TLDs
        Iterator iter = tlds.iterator();
        while (iter.hasNext())
        {
            try
            {
                Resource tld = (Resource)iter.next();
                if (log.isDebugEnabled()) log.debug("TLD="+tld);
               
                XmlParser.Node root = parser.parse(tld.getURL());
               
                for (int i=0;i<root.size();i++)
                {
                    Object o=root.get(i);
                    if (o instanceof XmlParser.Node)
View Full Code Here

    {
        if(log.isTraceEnabled())log.trace("getResource "+pathInContext);
        if (_resourceBase==null)
            return null;

        Resource resource=null;

        // Cache operations
        synchronized(_cache)
        {
            // Look for it in the cache
            CachedResource cached = (CachedResource)_cache.get(pathInContext);
            if (cached!=null)
            {
                if(log.isTraceEnabled())log.trace("CACHE HIT: "+cached);
                CachedMetaData cmd = (CachedMetaData)cached.getAssociate();
                if (cmd!=null && cmd.isValid())
                    return cached;
            }

            // Make the resource
            resource=_resourceBase.addPath(_resourceBase.encode(pathInContext));
            if(log.isTraceEnabled())log.trace("CACHE MISS: "+resource);
            if (resource==null)
                return null;

           
            // Check for file aliasing
            if (resource.getAlias()!=null)
            {
                log.warn("Alias request of '"+resource.getAlias()+
                             "' for '"+resource+"'");
                return null;
            }

            // Is it an existing file?
            long len = resource.length();
            if (resource.exists())
            {
                // Is it badly named?
                if (!resource.isDirectory() && pathInContext.endsWith("/"))
                    return null;

                // Guess directory length.
                if (resource.isDirectory())
                {
                    if (resource.list()!=null)
                        len=resource.list().length*100;
                    else
                        len=0;
                }

                // Is it cacheable?
                if (len>0 && len<_maxCachedFileSize && len<_maxCacheSize)
                {
                    int needed=_maxCacheSize-(int)len;
                    while(_cacheSize>needed)
                        _leastRecentlyUsed.invalidate();

                    cached=resource.cache();
                    if(log.isTraceEnabled())log.trace("CACHED: "+resource);
                    new CachedMetaData(cached,pathInContext);
                    return cached;
                }
            }
View Full Code Here

     * @param pathInContext The path to find a resource for.
     * @return The resource to serve.
     */
    protected Resource getResource(String pathInContext) throws IOException
    {
        Resource r = (_resourceBase == null) ? _httpContext.getResource(pathInContext) : _resourceBase.addPath(pathInContext);
       
        if (log.isDebugEnabled())
            log.debug("RESOURCE=" + r);
        return r;
    }
View Full Code Here

            pathInfo = (String) request.getAttribute(Dispatcher.__INCLUDE_PATH_INFO);

        String pathInContext = URI.addPaths(servletPath, pathInfo);
               
        boolean endsWithSlash = pathInContext.endsWith("/");
        Resource resource = getResource(pathInContext);

        // Is the method allowed?
        String method = request.getMethod();
        if (_AllowString.indexOf(method) < 0)
        {
            if (resource != null && resource.exists())
            {
                response.setHeader(HttpFields.__Allow, _AllowString);
                response.sendError(HttpResponse.__405_Method_Not_Allowed);
            }
            else
                response.sendError(HttpResponse.__404_Not_Found);
            return;
        }

        // Handle the request
        try
        {
            // handle by method.
            if (method.equals(HttpRequest.__GET) || method.equals(HttpRequest.__POST) || method.equals(HttpRequest.__HEAD))
                handleGet(request, response, pathInContext, resource, endsWithSlash);
            else if (_putAllowed && method.equals(HttpRequest.__PUT))
                handlePut(request, response, pathInContext, resource);
            else if (_delAllowed && method.equals(HttpRequest.__DELETE))
                handleDelete(request, response, pathInContext, resource);
            else if (_putAllowed && _delAllowed && method.equals(HttpRequest.__MOVE))
                handleMove(request, response, pathInContext, resource);
            else if (method.equals(HttpRequest.__OPTIONS))
                handleOptions(request, response);
            else if (method.equals(HttpRequest.__TRACE))
                _servletHandler.handleTrace(request, response);
            else
            {
                // anything else...
                try
                {
                    if (resource.exists())
                        response.sendError(HttpResponse.__501_Not_Implemented);
                    else
                        _servletHandler.notFound(request, response);
                }
                catch (Exception e)
                {
                    LogSupport.ignore(log, e);
                }
            }
        }
        catch (IllegalArgumentException e)
        {
            LogSupport.ignore(log, e);
        }
        finally
        {
            if (resource != null && !(resource instanceof CachedResource))
                resource.release();
        }

    }
View Full Code Here

        try
        {
            String newInfo = newPath;
            if (contextPath != null)
                newInfo = newInfo.substring(contextPath.length());
            Resource newFile = _httpContext.getBaseResource().addPath(newInfo);

            resource.renameTo(newFile);
            response.setStatus(HttpResponse.__204_No_Content);
            response.flushBuffer();
        }
View Full Code Here

        Enumeration reqRanges = include ? null : request.getHeaders(HttpFields.__Range);

        if (reqRanges == null || !reqRanges.hasMoreElements())
        {
            // if there were no ranges, send entire entity
            Resource data = resource;
            if (!include)
            {
                // look for a gziped content.
                if (_minGzipLength > 0)
                {
                    String accept = request.getHeader(HttpFields.__AcceptEncoding);
                    if (accept != null && resLength > _minGzipLength && !pathInContext.endsWith(".gz"))
                    {
                        Resource gz = getResource(pathInContext + ".gz");
                        if (gz.exists() && accept.indexOf("gzip") >= 0 && request.getAttribute(Dispatcher.__INCLUDE_REQUEST_URI) == null)
                        {
                            response.setHeader(HttpFields.__ContentEncoding, "gzip");
                            data = gz;
                            resLength = data.length();
                        }
View Full Code Here

        this.rootDir = directory;
    }

    public Resource getResource(HttpContext context, String pathInContext) throws IOException {
        File file = new File(rootDir, pathInContext);
        Resource resource = createFileResource(file, context);
        // Throw in a hack to make it easier to install user extensions
        if (!resource.exists() && file.getName().equals(USER_EXTENSIONS_JS_NAME)) {
            resource = userExtensionResource(context);
            if (resource.exists()) return resource;
        }
        // And another hack to make the -htmlSuite appear in the /tests directory
        if (!resource.exists() && pathInContext.startsWith(TEST_DIR)) {
            File testFile = new File(rootDir, pathInContext.substring(TEST_DIR.length()));
            resource = createFileResource(testFile, context);
            if (resource.exists()) return resource;
        }
        return resource;
    }
View Full Code Here

        File extensions = new File(rootDir, USER_EXTENSIONS_JS_NAME);
        return createFileResource(extensions, context);
    }

    private Resource createFileResource(File file, HttpContext context) throws IOException {
           Resource resource = new FutureFileResource(file.toURI().toURL());
          context.getResourceMetaData(resource);
            return resource;       
    }
View Full Code Here

    {
        if (_pathInfo==null || _pathInfo.length()==0)
            return null;
        if (_pathTranslated==null)
        {
            Resource resource =
                _servletHandler.getHttpContext().getBaseResource();

            if (resource==null)
                return null;

            try
            {
                resource=resource.addPath(_pathInfo);
                File file = resource.getFile();
                if (file==null)
                    return null;
                _pathTranslated=file.getAbsolutePath();
            }
            catch(Exception e)
View Full Code Here

TOP

Related Classes of org.openqa.jetty.util.Resource

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.