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.renderkit.html.util;
20
21 import javax.servlet.ServletContext;
22 import java.io.InputStream;
23 import java.io.IOException;
24 import java.net.URL;
25
26 /**
27 * A class which provide the resource using the standard <code>class.getResource</code> lookup
28 * stuff.
29 *
30 * @author imario (latest modification by $Author: skitching $)
31 * @version $Revision: 673833 $ $Date: 2008-07-03 16:58:05 -0500 (Thu, 03 Jul 2008) $
32 */
33 public class DefaultResourceProvider implements ResourceProvider
34 {
35 private final Class clazz;
36
37 public DefaultResourceProvider(Class clazz)
38 {
39 this.clazz = clazz;
40 }
41
42 protected URL getResource(String resource)
43 {
44 resource = "resource/" + resource;
45 return clazz.getResource(resource);
46 }
47
48 public boolean exists(ServletContext context, String resource)
49 {
50 return getResource(resource) != null;
51 }
52
53 public long getLastModified(ServletContext context, String resource) throws IOException
54 {
55 return getResource(resource).openConnection().getLastModified();
56 }
57
58 public int getContentLength(ServletContext context, String resource) throws IOException
59 {
60 return getResource(resource).openConnection().getContentLength();
61 }
62
63 public InputStream getInputStream(ServletContext context, String resource) throws IOException
64 {
65 return getResource(resource).openConnection().getInputStream();
66 }
67
68 public String getEncoding(ServletContext context, String resource) throws IOException
69 {
70 return null; //Tomahawk-877, this has to be null for now to avoid encoding issues
71 }
72 }