View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.custom.stylesheet;
20  
21  import org.apache.myfaces.renderkit.html.util.ResourceProvider;
22  
23  import javax.servlet.ServletContext;
24  import java.io.ByteArrayInputStream;
25  import java.io.IOException;
26  import java.io.InputStream;
27  
28  /**
29   * Allow URLs to return data that is cached by the TextResourceFilter class.
30   * <p>
31   * This implements the Tomahawk ResourceProvider interface which works together
32   * with the Tomahawk AddResourceFactory, ExtensionsFilter and AddResource classes.
33   * The AddResourceFactory can generate a URL which can be embedded in an HTML page.
34   * When invoked, that URL triggers the ExtensionsFilter which invokes AddResource
35   * which then calls back into this class. And this class then retrieves the
36   * requested data from the TextResourceFilter.  
37   */
38  public class TextResourceFilterProvider implements ResourceProvider
39  {
40      // Hack note: a slash has to be prefixed to the resource value here because
41      // we are abusing the AddResource API in StylesheetRenderer; see comments
42      // in StylesheetRenderer.encodeEnd for details.
43  
44      public boolean exists(ServletContext context, String resource)
45      {
46          resource = "/" + resource; // hack
47          return TextResourceFilter.getInstance(context).getFilteredResource(resource) != null;
48      }
49  
50      public int getContentLength(ServletContext context, String resource) throws IOException
51      {
52          resource = "/" + resource; // hack
53          return TextResourceFilter.getInstance(context).getFilteredResource(resource).getSize();
54      }
55  
56      public long getLastModified(ServletContext context, String resource) throws IOException
57      {
58          resource = "/" + resource; // hack
59          return TextResourceFilter.getInstance(context).getFilteredResource(resource).getLastModified();
60      }
61  
62      public InputStream getInputStream(ServletContext context, String resource) throws IOException
63      {
64          resource = "/" + resource; // hack
65          return new ByteArrayInputStream(
66              TextResourceFilter.getInstance(context).getFilteredResource(resource).getText().getBytes(
67                  getEncoding(context, resource)
68              ));
69      }
70  
71      public String getEncoding(ServletContext context, String resource) throws IOException
72      {
73          return "UTF-8";
74      }
75  }