1
2
3
4 package org.rcfaces.core.internal.resource;
5
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.net.MalformedURLException;
9 import java.net.URL;
10 import java.net.URLConnection;
11 import java.util.Date;
12
13 import javax.servlet.ServletContext;
14 import javax.servlet.http.HttpServletRequest;
15 import javax.servlet.http.HttpServletResponse;
16
17 import org.apache.commons.logging.Log;
18 import org.apache.commons.logging.LogFactory;
19
20
21
22
23
24
25 public class ClassLoaderResourceLoaderFactory extends
26 AbstractResourceLoaderFactory {
27 private static final String REVISION = "$Revision: 1.1 $";
28
29 private static final Log LOG = LogFactory
30 .getLog(ClassLoaderResourceLoaderFactory.class);
31
32 public String getName() {
33 return "Load resource by classloader context";
34 }
35
36 public IResourceLoader loadResource(ServletContext context,
37 HttpServletRequest request, HttpServletResponse response, String uri) {
38 return new ClassLoaderResourceLoader(context, uri);
39 }
40
41
42
43
44
45
46 private static class ClassLoaderResourceLoader implements IResourceLoader {
47 private static final String REVISION = "$Revision: 1.1 $";
48
49 private final ServletContext servletContext;
50
51 private final String url;
52
53 private URLConnection urlConnection;
54
55 private boolean errored;
56
57 private int contentLength;
58
59 private long lastModified;
60
61 private String contentType;
62
63 public ClassLoaderResourceLoader(ServletContext servletContext,
64 String url) {
65 this.servletContext = servletContext;
66 this.url = url;
67 }
68
69 public boolean isErrored() {
70 if (errored == false && urlConnection == null) {
71 openURLConnection();
72 }
73 return errored;
74 }
75
76 private void openURLConnection() {
77
78 String url = this.url;
79 if (url.startsWith("/") == false) {
80 url = "/" + url;
81 }
82
83 URL resourceURL;
84 try {
85 resourceURL = servletContext.getResource(url);
86
87 } catch (MalformedURLException ex) {
88 LOG.error("Malformed url '" + url + "'.", ex);
89 errored = true;
90 return;
91 }
92
93 if (resourceURL == null) {
94 LOG.error("Can not get resource specified by path '" + url
95 + "'.");
96 errored = true;
97 return;
98 }
99 if (LOG.isDebugEnabled()) {
100 LOG.debug("Search resource '/" + url + "' => '" + resourceURL
101 + "'.");
102 }
103
104 try {
105 urlConnection = resourceURL.openConnection();
106 if (urlConnection == null) {
107 LOG.error("Can not get resource specified by path '" + url
108 + "'.");
109 errored = true;
110 return;
111 }
112
113 contentLength = urlConnection.getContentLength();
114 lastModified = urlConnection.getLastModified();
115 contentType = urlConnection.getContentType();
116
117 if (LOG.isDebugEnabled()) {
118 LOG
119 .debug("Resource '"
120 + resourceURL
121 + "' contentType='"
122 + contentType
123 + "' contentLength="
124 + contentLength
125 + " lastModified="
126 + ((lastModified > 0) ? new Date(
127 lastModified).toString() : String
128 .valueOf(lastModified)));
129 }
130
131 } catch (IOException ex) {
132 LOG.error("Can not get content of resource '" + resourceURL
133 + "'.", ex);
134 errored = true;
135 }
136 }
137
138 public InputStream openStream() {
139 if (isErrored()) {
140 return null;
141 }
142
143 try {
144 InputStream ins = urlConnection.getInputStream();
145 if (ins != null) {
146 return ins;
147 }
148
149 LOG.error("Can not get resource specified by path '" + url
150 + "'.");
151 errored = true;
152 return null;
153
154 } catch (IOException ex) {
155 LOG.error("Can not get content of resource '" + url + "'.", ex);
156 errored = true;
157 return null;
158 }
159 }
160
161 public int getContentLength() {
162 if (isErrored()) {
163 return -1;
164 }
165 return contentLength;
166 }
167
168 public long getLastModified() {
169 if (isErrored()) {
170 return -1;
171 }
172 return lastModified;
173 }
174
175 public String getContentType() {
176 if (isErrored()) {
177 return null;
178 }
179 return contentType;
180 }
181
182 }
183 }