Package org.mortbay.resource

Examples of org.mortbay.resource.Resource


    {
        Request base_request = request instanceof Request?(Request)request:HttpConnection.getCurrentConnection().getRequest();
        if (base_request.isHandled() || !request.getMethod().equals(HttpMethods.GET))
            return;
    
        Resource resource=getResource(request);
       
        if (resource==null || !resource.exists())
            return;

        // We are going to server something
        base_request.setHandled(true);
       
        if (resource.isDirectory())
        {
            if (!request.getPathInfo().endsWith(URIUtil.SLASH))
            {
                response.sendRedirect(URIUtil.addPaths(request.getRequestURI(),URIUtil.SLASH));
                return;
            }
            resource=getWelcome(resource);

            if (resource==null || !resource.exists() || resource.isDirectory())
            {
                response.sendError(HttpServletResponse.SC_FORBIDDEN);
                return;
            }
        }
       
        // set some headers
        long last_modified=resource.lastModified();
        if (last_modified>0)
        {
            long if_modified=request.getDateHeader(HttpHeaders.IF_MODIFIED_SINCE);
            if (if_modified>0 && last_modified/1000<=if_modified/1000)
            {
                response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
                return;
            }
        }
       
        Buffer mime=_mimeTypes.getMimeByExtension(resource.toString());
        if (mime==null)
            mime=_mimeTypes.getMimeByExtension(request.getPathInfo());
       
        // set the headers
        doResponseHeaders(response,resource,mime!=null?mime.toString():null);

        // Send the content
        OutputStream out =null;
        try {out = response.getOutputStream();}
        catch(IllegalStateException e) {out = new WriterOutputStream(response.getWriter());}
       
        // See if a short direct method can be used?
        if (out instanceof HttpConnection.Output)
        {
            // TODO file mapped buffers
            response.setDateHeader(HttpHeaders.LAST_MODIFIED,last_modified);
            ((HttpConnection.Output)out).sendContent(resource.getInputStream());
        }
        else
        {
            // Write content normally
            response.setDateHeader(HttpHeaders.LAST_MODIFIED,last_modified);
            resource.writeTo(out,0,resource.length());
        }
    }
View Full Code Here


        StringBuffer classpath=new StringBuffer();
        for (int i=0;i<urls.length;i++)
        {
            try
            {
                Resource resource = Resource.newResource(urls[i]);
                File file=resource.getFile();
                if (file.exists())
                {
                    if (classpath.length()>0)
                        classpath.append(File.pathSeparatorChar);
                    classpath.append(file.getAbsolutePath());
View Full Code Here

            return null;

        try
        {
            path=URIUtil.canonicalPath(path);
            Resource resource=_baseResource.addPath(path);
            return resource;
        }
        catch(Exception e)
        {
            Log.ignore(e);
View Full Code Here

    public Set getResourcePaths(String path)
    {          
        try
        {
            path=URIUtil.canonicalPath(path);
            Resource resource=getResource(path);
           
            if (resource!=null && resource.exists())
            {
                if (!path.endsWith(URIUtil.SLASH))
                    path=path+URIUtil.SLASH;
               
                String[] l=resource.list();
                if (l!=null)
                {
                    HashSet set = new HashSet();
                    for(int i=0;i<l.length;i++)
                        set.add(path+l[i]);
View Full Code Here

        /* ------------------------------------------------------------ */
        /*
         */
        public URL getResource(String path) throws MalformedURLException
        {
            Resource resource=ContextHandler.this.getResource(path);
            if (resource!=null && resource.exists())
                return resource.getURL();
            return null;
        }
View Full Code Here

        }

        /* ------------------------------------------------------------ */
        public void sendContent(Object content) throws IOException
        {
            Resource resource=null;
           
            if (_closed)
                throw new IOException("Closed");
           
            if (_generator.getContentWritten() > 0) throw new IllegalStateException("!empty");

            if (content instanceof HttpContent)
            {
                HttpContent c = (HttpContent) content;
                if (c.getContentType() != null && !_responseFields.containsKey(HttpHeaders.CONTENT_TYPE_BUFFER))
                    _responseFields.add(HttpHeaders.CONTENT_TYPE_BUFFER, c.getContentType());
                if (c.getContentLength() > 0)
                    _responseFields.putLongField(HttpHeaders.CONTENT_LENGTH_BUFFER, c.getContentLength());
                Buffer lm = c.getLastModified();
                long lml=c.getResource().lastModified();
                if (lm != null)
                    _responseFields.put(HttpHeaders.LAST_MODIFIED_BUFFER, lm,lml);
                else if (c.getResource()!=null)
                {
                    if (lml!=-1)
                        _responseFields.putDateField(HttpHeaders.LAST_MODIFIED_BUFFER, lml);
                }
                   
                content = c.getBuffer();
                if (content==null)
                    content=c.getInputStream();
            }
            else if (content instanceof Resource)
            {
                resource=(Resource)content;
                _responseFields.putDateField(HttpHeaders.LAST_MODIFIED_BUFFER, resource.lastModified());
                content=resource.getInputStream();
            }
           
           
            if (content instanceof Buffer)
            {
                _generator.addContent((Buffer) content, HttpGenerator.LAST);
                commitResponse(HttpGenerator.LAST);
            }
            else if (content instanceof InputStream)
            {
                InputStream in = (InputStream)content;
               
                try
                {
                    int max = _generator.prepareUncheckedAddContent();
                    Buffer buffer = _generator.getUncheckedBuffer();

                    int len=buffer.readFrom(in,max);

                    while (len>=0)
                    {
                        _generator.completeUncheckedAddContent();
                        _out.flush();

                        max = _generator.prepareUncheckedAddContent();
                        buffer = _generator.getUncheckedBuffer();
                        len=buffer.readFrom(in,max);
                    }
                    _generator.completeUncheckedAddContent();
                    _out.flush();  
                }
                finally
                {
                    if (resource!=null)
                        resource.release();
                    else
                        in.close();
                     
                }
            }
View Full Code Here

public class Main
{
  public static void main(String[] args)
    throws Exception 
  {   
    Resource config=Resource.newResource(args.length==1?args[0]:"etc/jetty-spring.xml");
    XmlBeanFactory bf = new XmlBeanFactory(new UrlResource(config.getURL()));
    Server server = (Server) bf.getBean(args.length==2?args[1]:"Server");
    server.join();
  }
View Full Code Here

        }

        /* ------------------------------------------------------------ */
        public void sendContent(Object content) throws IOException
        {
            Resource resource = null;

            if (_closed)
                throw new IOException("Closed");

            if (_generator.getContentWritten() > 0)
                throw new IllegalStateException("!empty");

            if (content instanceof HttpContent)
            {
                HttpContent c = (HttpContent)content;
                Buffer contentType = c.getContentType();
                if (contentType != null && !_responseFields.containsKey(HttpHeaders.CONTENT_TYPE_BUFFER))
                {
                    String enc = _response.getSetCharacterEncoding();
                    if(enc==null)
                        _responseFields.add(HttpHeaders.CONTENT_TYPE_BUFFER, contentType);
                    else
                    {
                        if(contentType instanceof CachedBuffer)
                        {
                            CachedBuffer content_type = ((CachedBuffer)contentType).getAssociate(enc);
                            if(content_type!=null)
                                _responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER, content_type);
                            else
                            {
                                _responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,
                                        contentType+";charset="+QuotedStringTokenizer.quote(enc,";= "));
                            }
                        }
                        else
                        {
                            _responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,
                                    contentType+";charset="+QuotedStringTokenizer.quote(enc,";= "));
                        }
                    }
                }
                if (c.getContentLength() > 0)
                    _responseFields.putLongField(HttpHeaders.CONTENT_LENGTH_BUFFER,c.getContentLength());
                Buffer lm = c.getLastModified();
                long lml = c.getResource().lastModified();
                if (lm != null)
                    _responseFields.put(HttpHeaders.LAST_MODIFIED_BUFFER,lm,lml);
                else if (c.getResource() != null)
                {
                    if (lml != -1)
                        _responseFields.putDateField(HttpHeaders.LAST_MODIFIED_BUFFER,lml);
                }

                content = c.getBuffer();
                if (content == null)
                    content = c.getInputStream();
            }
            else if (content instanceof Resource)
            {
                resource = (Resource)content;
                _responseFields.putDateField(HttpHeaders.LAST_MODIFIED_BUFFER,resource.lastModified());
                content = resource.getInputStream();
            }

            if (content instanceof Buffer)
            {
                _generator.addContent((Buffer)content,HttpGenerator.LAST);
                commitResponse(HttpGenerator.LAST);
            }
            else if (content instanceof InputStream)
            {
                InputStream in = (InputStream)content;

                try
                {
                    int max = _generator.prepareUncheckedAddContent();
                    Buffer buffer = _generator.getUncheckedBuffer();

                    int len = buffer.readFrom(in,max);

                    while (len >= 0)
                    {
                        _generator.completeUncheckedAddContent();
                        _out.flush();

                        max = _generator.prepareUncheckedAddContent();
                        buffer = _generator.getUncheckedBuffer();
                        len = buffer.readFrom(in,max);
                    }
                    _generator.completeUncheckedAddContent();
                    _out.flush();
                }
                finally
                {
                    if (resource != null)
                        resource.release();
                    else
                        in.close();

                }
            }
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.
        Resource web_inf = _context.getWebInf();
        if (web_inf!=null)
        {
            String[] contents = web_inf.list();
            for (int i=0;contents!=null && i<contents.length;i++)
            {
                if (contents[i]!=null && contents[i].toLowerCase().endsWith(".tld"))
                {
                    Resource l=_context.getWebInf().addPath(contents[i]);
                    tlds.add(l);
                }
               
            }
        }
       
        // Get the pattern for noTLDJars
        String no_TLD_attr = _context.getInitParameter("org.mortbay.jetty.webapp.NoTLDJarPattern");
        Pattern no_TLD_pattern = no_TLD_attr==null?null:Pattern.compile(no_TLD_attr);
       
        // Look for tlds in any jars
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        boolean parent=false;
       
        while (loader!=null)
        {
            if (loader instanceof URLClassLoader)
            {
                URL[] urls = ((URLClassLoader)loader).getURLs();

                if (urls!=null)
                {
                    for (int i=0;i<urls.length;i++)
                    {  
                        if (urls[i].toString().toLowerCase().endsWith(".jar"))
                        {

                            String jar = urls[i].toString();
                            int slash=jar.lastIndexOf('/');
                            jar=jar.substring(slash+1);

                            if (parent && (
                                    (!_context.isParentLoaderPriority() && jars.contains(jar)) ||
                                    (no_TLD_pattern!=null && no_TLD_pattern.matcher(jar).matches())))
                                continue;
                            jars.add(jar);
                           
                            Log.debug("TLD search of {}",urls[i]);
                           
                            File file=Resource.newResource(urls[i]).getFile();
                            if (file==null || !file.exists() || !file.canRead())
                                continue;
                           
                            JarFile jarfile = null;
                            try
                            {
                                jarfile = new JarFile(file);
                                Enumeration e = jarfile.entries();
                                while (e.hasMoreElements())
                                {
                                    ZipEntry entry = (ZipEntry)e.nextElement();
                                    String name = entry.getName();
                                    if (name.startsWith("META-INF/") && name.toLowerCase().endsWith(".tld"))
                                    {
                                        Resource tld=Resource.newResource("jar:"+urls[i]+"!/"+name);
                                        tlds.add(tld);
                                        Log.debug("TLD found {}",tld);
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                Log.warn("Failed to read file: " + file, e);
                            }
                            finally
                            {
                                if (jarfile != null)
                                {
                                    jarfile.close();
                                }
                            }  
                        }
                    }
                }
            }

            loader=loader.getParent();
            parent=true;
           
        }
       
        // Create a TLD parser
        XmlParser parser = new XmlParser(false);
        parser.redirectEntity("web-jsptaglib_1_1.dtd",Loader.getResource(TagLibConfiguration.class,"javax/servlet/jsp/resources/web-jsptaglibrary_1_1.dtd", false));
        parser.redirectEntity("web-jsptaglib_1_2.dtd",Loader.getResource(TagLibConfiguration.class,"javax/servlet/jsp/resources/web-jsptaglibrary_1_2.dtd", false));
        parser.redirectEntity("web-jsptaglib_2_0.xsd",Loader.getResource(TagLibConfiguration.class,"javax/servlet/jsp/resources/web-jsptaglibrary_2_0.xsd", false));
        parser.redirectEntity("web-jsptaglibrary_1_1.dtd",Loader.getResource(TagLibConfiguration.class,"javax/servlet/jsp/resources/web-jsptaglibrary_1_1.dtd", false));
        parser.redirectEntity("web-jsptaglibrary_1_2.dtd",Loader.getResource(TagLibConfiguration.class,"javax/servlet/jsp/resources/web-jsptaglibrary_1_2.dtd", false));
        parser.redirectEntity("web-jsptaglibrary_2_0.xsd",Loader.getResource(TagLibConfiguration.class,"javax/servlet/jsp/resources/web-jsptaglibrary_2_0.xsd", false));
        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;
               
                try
                {
                    //xerces on apple appears to sometimes close the zip file instead
                    //of the inputstream, so try opening the input stream, but if
                    //that doesn't work, fallback to opening a new url
                    root = parser.parse(tld.getInputStream());
                }
                catch (Exception e)
                {
                    root = parser.parse(tld.getURL().toString());
                }

    if (root==null)
    {
        Log.warn("No TLD root in {}",tld);
View Full Code Here

        {
            if (Log.isDebugEnabled()){Log.debug("Cannot configure webapp after it is started");}
            return;
        }

        Resource web_inf=_context.getWebInf();

        // Add WEB-INF classes and lib classpaths
        if (web_inf != null && web_inf.isDirectory() && _context.getClassLoader() instanceof WebAppClassLoader)
        {
            // Look for classes directory
            Resource classes= web_inf.addPath("classes/");
            if (classes.exists())
                ((WebAppClassLoader)_context.getClassLoader()).addClassPath(classes.toString());

            // Look for jars
            Resource lib= web_inf.addPath("lib/");
            if (lib.exists() || lib.isDirectory())
                ((WebAppClassLoader)_context.getClassLoader()).addJars(lib);
        }
       
     }
View Full Code Here

TOP

Related Classes of org.mortbay.resource.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.