View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.server;
20  
21  import java.io.IOException;
22  
23  import org.eclipse.jetty.http.HttpContent;
24  import org.eclipse.jetty.http.HttpContent.Factory;
25  import org.eclipse.jetty.http.MimeTypes;
26  import org.eclipse.jetty.http.ResourceHttpContent;
27  import org.eclipse.jetty.util.resource.Resource;
28  import org.eclipse.jetty.util.resource.ResourceFactory;
29  
30  public class ResourceContentFactory implements Factory
31  {
32      private final ResourceFactory _factory;
33      private final MimeTypes _mimeTypes;
34      private final int _maxBufferSize;
35      private final boolean _gzip;
36      
37  
38      /* ------------------------------------------------------------ */
39      public ResourceContentFactory(ResourceFactory factory, MimeTypes mimeTypes, int maxBufferSize, boolean gzip)
40      {
41          _factory=factory;
42          _mimeTypes=mimeTypes;
43          _maxBufferSize=maxBufferSize;
44          _gzip=gzip;
45      }
46  
47      /* ------------------------------------------------------------ */
48      /** Get a Entry from the cache.
49       * Get either a valid entry object or create a new one if possible.
50       *
51       * @param pathInContext The key into the cache
52       * @return The entry matching <code>pathInContext</code>, or a new entry 
53       * if no matching entry was found. If the content exists but is not cachable, 
54       * then a {@link ResourceHttpContent} instance is return. If 
55       * the resource does not exist, then null is returned.
56       * @throws IOException Problem loading the resource
57       */
58      @Override
59      public HttpContent getContent(String pathInContext)
60          throws IOException
61      {
62         
63          // try loading the content from our factory.
64          Resource resource=_factory.getResource(pathInContext);
65          HttpContent loaded = load(pathInContext,resource);
66          return loaded;
67      }
68      
69      
70      /* ------------------------------------------------------------ */
71      private HttpContent load(String pathInContext, Resource resource)
72          throws IOException
73      {   
74          if (resource==null || !resource.exists())
75              return null;
76          
77          if (resource.isDirectory())
78              return new ResourceHttpContent(resource,_mimeTypes.getMimeByExtension(resource.toString()),_maxBufferSize);
79          
80          // Look for a gzip resource or content
81          String mt = _mimeTypes.getMimeByExtension(pathInContext);
82          if (_gzip)
83          {
84              // Is there a gzip resource? 
85              String pathInContextGz=pathInContext+".gz";
86              Resource resourceGz=_factory.getResource(pathInContextGz);
87              if (resourceGz.exists() && resourceGz.lastModified()>=resource.lastModified() && resourceGz.length()<resource.length())
88                  return new ResourceHttpContent(resource,mt,_maxBufferSize,
89                         new ResourceHttpContent(resourceGz,_mimeTypes.getMimeByExtension(pathInContextGz),_maxBufferSize));
90          }
91          
92          return new ResourceHttpContent(resource,mt,_maxBufferSize);
93      }
94      
95      
96      /* ------------------------------------------------------------ */
97      @Override
98      public String toString()
99      {
100         return "ResourceContentFactory["+_factory+"]@"+hashCode();
101     }
102     
103 
104 }