Package org.mortbay.resource

Examples of org.mortbay.resource.Resource


        /* ------------------------------------------------------------ */
        /*
         */
        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


        String rrb = getInitParameter("relativeResourceBase");
        if (rrb!=null)
        {
            try
            {
                Resource root = _context.getContextHandler().getResource(URIUtil.SLASH);
                if (root == null)
                    throw new UnavailableException("No base resourceBase for relativeResourceBase in"+_context.getContextPath());
                _resourceBase = root.addPath(rrb);
            }
            catch (Exception e)
            {
                Log.warn(Log.EXCEPTION,e);
                throw new UnavailableException(e.toString());
View Full Code Here

     */
    public Resource getResource(String pathInContext)
    {
        if (_resourceBase==null)
            return null;
        Resource r=null;
        try
        {
            r = _resourceBase.addPath(pathInContext);
            if (!_aliases && r.getAlias()!=null)
            {
                if (r.exists())
                    Log.warn("Aliased resource: "+r+"=="+r.getAlias());
                return null;
            }
            if (Log.isDebugEnabled()) Log.debug("RESOURCE="+r);
        }
        catch (IOException e)
View Full Code Here

            if (accept!=null && accept.indexOf("gzip")>=0)
                gzip=true;
        }
       
        // Find the resource and content
        Resource resource=null;
        HttpContent content=null;
       
        Connector connector = HttpConnection.getCurrentConnection().getConnector();
        ResourceCache cache=(connector instanceof NIOConnector) ?_nioCache:_bioCache;
        try
        {  
            // Try gzipped content first
            if (gzip)
            {
                pathInContextGz=pathInContext+".gz"
                resource=getResource(pathInContextGz);

                if (resource==null || !resource.exists()|| resource.isDirectory())
                {
                    gzip=false;
                    pathInContextGz=null;
                }
                else if (cache!=null)
                {
                    content=cache.lookup(pathInContextGz,resource);
                    if (content!=null)
                        resource=content.getResource();
                }

                if (resource==null || !resource.exists()|| resource.isDirectory())
                {
                    gzip=false;
                    pathInContextGz=null;
                }
            }
       
            // find resource
            if (!gzip)
            {
                if (cache==null)
                    resource=getResource(pathInContext);
                else
                {
                    content=cache.lookup(pathInContext,this);

                    if (content!=null)
                        resource=content.getResource();
                    else
                        resource=getResource(pathInContext);
                }
            }
           
            if (Log.isDebugEnabled())
                Log.debug("resource="+resource+(content!=null?" content":""));
                       
            // Handle resource
            if (resource==null || !resource.exists())
                response.sendError(HttpServletResponse.SC_NOT_FOUND);
            else if (!resource.isDirectory())
            {  
                if (endsWithSlash && _aliases && pathInContext.length()>1)
                {
                    String q=request.getQueryString();
                    pathInContext=pathInContext.substring(0,pathInContext.length()-1);
                    if (q!=null&&q.length()!=0)
                        pathInContext+="?"+q;
                    response.sendRedirect(response.encodeRedirectURL(URIUtil.addPaths( _context.getContextPath(),pathInContext)));
                }
                else
                {
                    // ensure we have content
                    if (content==null)
                        content=new UnCachedContent(resource);

                    if (included.booleanValue() || passConditionalHeaders(request,response, resource,content)) 
                    {
                        if (gzip)
                        {
                            response.setHeader(HttpHeaders.CONTENT_ENCODING,"gzip");
                            String mt=_context.getMimeType(pathInContext);
                            if (mt!=null)
                                response.setContentType(mt);
                        }
                        sendData(request,response,included.booleanValue(),resource,content,reqRanges)
                    }
                }
            }
            else
            {
                String welcome=null;
               
                if (!endsWithSlash || (pathInContext.length()==1 && request.getAttribute("org.mortbay.jetty.nullPathInfo")!=null))
                {
                    StringBuffer buf=request.getRequestURL();
                    int param=buf.lastIndexOf(";");
                    if (param<0)
                        buf.append('/');
                    else
                        buf.insert(param,'/');
                    String q=request.getQueryString();
                    if (q!=null&&q.length()!=0)
                    {
                        buf.append('?');
                        buf.append(q);
                    }
                    response.setContentLength(0);
                    response.sendRedirect(response.encodeRedirectURL(buf.toString()));
                }
                // else look for a welcome file
                else if (null!=(welcome=getWelcomeFile(pathInContext)))
                {
                    if (_redirectWelcome)
                    {
                        // Redirect to the index
                        response.setContentLength(0);
                        String q=request.getQueryString();
                        if (q!=null&&q.length()!=0)
                            response.sendRedirect(response.encodeRedirectURL(URIUtil.addPaths( _context.getContextPath(),welcome)+"?"+q));
                        else
                            response.sendRedirect(response.encodeRedirectURL(URIUtil.addPaths( _context.getContextPath(),welcome)));
                    }
                    else
                    {
                        // Forward to the index
                        RequestDispatcher dispatcher=request.getRequestDispatcher(welcome);
                        if (dispatcher!=null)
                        {
                            if (included.booleanValue())
                                dispatcher.include(request,response);
                            else
                            {
                                request.setAttribute("org.mortbay.jetty.welcome",welcome);
                                dispatcher.forward(request,response);
                            }
                        }
                    }
                }
                else
                {
                    content=new UnCachedContent(resource);
                    if (included.booleanValue() || passConditionalHeaders(request,response, resource,content))
                        sendDirectory(request,response,resource,pathInContext.length()>1);
                }
            }
        }
        catch(IllegalArgumentException e)
        {
            Log.warn(Log.EXCEPTION,e);
            if(!response.isCommitted())
                response.sendError(500, e.getMessage());
        }
        finally
        {
            if (content!=null)
                content.release();
            else if (resource!=null)
                resource.release();
        }
       
    }
View Full Code Here

      
        String welcome_servlet=null;
        for (int i=0;i<_welcomes.length;i++)
        {
            String welcome_in_context=URIUtil.addPaths(pathInContext,_welcomes[i]);
            Resource welcome=getResource(welcome_in_context);
            if (welcome!=null && welcome.exists())
                return _welcomes[i];
           
            if (_welcomeServlets && welcome_servlet==null)
            {
                Map.Entry entry=_servletHandler.getHolderEntry(welcome_in_context);
View Full Code Here

        /* ------------------------------------------------------------ */
        protected void fill(Content content) throws IOException
        {
            Buffer buffer=null;
            Resource resource=content.getResource();
            long length=resource.length();

            if (_useFileMappedBuffer && resource.getFile()!=null)
            {   
                buffer = new DirectNIOBuffer(resource.getFile());
            }
            else
            {
                InputStream is = resource.getInputStream();
                try
                {
                    Connector connector = HttpConnection.getCurrentConnection().getConnector();
                    buffer = ((NIOConnector)connector).getUseDirectBuffers()?
                            (NIOBuffer)new DirectNIOBuffer((int)length):
View Full Code Here

    public Resource getResource(String path) throws MalformedURLException
    {
        if (path==null || !path.startsWith("/"))
            throw new MalformedURLException(path);
       
        Resource base = _baseResource;
        if (base==null)
        {
            if (_context==null)
                return null;           
            base=_context.getBaseResource();
            if (base==null)
                return null;
        }

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

    /* ------------------------------------------------------------ */
    protected Resource getWelcome(Resource directory) throws MalformedURLException, IOException
    {
        for (int i=0;i<_welcomeFiles.length;i++)
        {
            Resource welcome=directory.addPath(_welcomeFiles[i]);
            if (welcome.exists() && !welcome.isDirectory())
                return welcome;
        }

        return null;
    }
View Full Code Here

            if(!HttpMethods.HEAD.equals(request.getMethod()))
                return;
            skipContentBody = true;
        }
    
        Resource resource=getResource(request);
       
        if (resource==null || !resource.exists())
            return;
        if (!_aliases && resource.getAlias()!=null)
        {
            Log.info(resource+" aliased to "+resource.getAlias());
            return;
        }

        // We are going to server something
        base_request.setHandled(true);
       
        if (resource.isDirectory())
        {
            if (!request.getPathInfo().endsWith(URIUtil.SLASH))
            {
                response.sendRedirect(response.encodeRedirectURL(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);
        response.setDateHeader(HttpHeaders.LAST_MODIFIED,last_modified);
        if(skipContentBody)
            return;
        // 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
            ((HttpConnection.Output)out).sendContent(resource.getInputStream());
        }
        else
        {
            // Write content normally
            resource.writeTo(out,0,resource.length());
        }
    }
View Full Code Here

     */
    private ContextHandler createContext(String filename) throws Exception
    {
        // The config file can call any method on WebAppContext to configure
        // the webapp being deployed.
        Resource resource = Resource.newResource(filename);       
        if (!resource.exists())
            return null;

        XmlConfiguration xmlConfiguration=new XmlConfiguration(resource.getURL());
        HashMap properties = new HashMap();
        properties.put("Server", _contexts.getServer());
        if (_configMgr!=null)
            properties.putAll(_configMgr.getProperties());
          
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.