View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 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  
31  /**
32   * A HttpContent.Factory for transient content.  The HttpContent's created by 
33   * this factory are not intended to be cached, so memory limits for individual
34   * HttpOutput streams are enforced.
35   */
36  public class ResourceContentFactory implements Factory
37  {
38      private final ResourceFactory _factory;
39      private final MimeTypes _mimeTypes;
40      private final boolean _gzip;
41      
42      /* ------------------------------------------------------------ */
43      public ResourceContentFactory(ResourceFactory factory, MimeTypes mimeTypes, boolean gzip)
44      {
45          _factory=factory;
46          _mimeTypes=mimeTypes;
47          _gzip=gzip;
48      }
49  
50      /* ------------------------------------------------------------ */
51      @Override
52      public HttpContent getContent(String pathInContext,int maxBufferSize)
53          throws IOException
54      {
55          // try loading the content from our factory.
56          Resource resource=_factory.getResource(pathInContext);
57          HttpContent loaded = load(pathInContext,resource,maxBufferSize);
58          return loaded;
59      }
60      
61      
62      /* ------------------------------------------------------------ */
63      private HttpContent load(String pathInContext, Resource resource, int maxBufferSize)
64          throws IOException
65      {   
66          if (resource==null || !resource.exists())
67              return null;
68          
69          if (resource.isDirectory())
70              return new ResourceHttpContent(resource,_mimeTypes.getMimeByExtension(resource.toString()),maxBufferSize);
71          
72          // Look for a gzip resource or content
73          String mt = _mimeTypes.getMimeByExtension(pathInContext);
74          if (_gzip)
75          {
76              // Is there a gzip resource? 
77              String pathInContextGz=pathInContext+".gz";
78              Resource resourceGz=_factory.getResource(pathInContextGz);
79              if (resourceGz.exists() && resourceGz.lastModified()>=resource.lastModified() && resourceGz.length()<resource.length())
80                  return new ResourceHttpContent(resource,mt,maxBufferSize,
81                         new ResourceHttpContent(resourceGz,_mimeTypes.getMimeByExtension(pathInContextGz),maxBufferSize));
82          }
83          
84          return new ResourceHttpContent(resource,mt,maxBufferSize);
85      }
86      
87      
88      /* ------------------------------------------------------------ */
89      @Override
90      public String toString()
91      {
92          return "ResourceContentFactory["+_factory+"]@"+hashCode();
93      }
94      
95  
96  }