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.trinidad.resource;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.net.URL;
24  
25  import java.net.URLConnection;
26  import java.net.URLStreamHandler;
27  import java.util.List;
28  import java.util.Map;
29  import java.io.OutputStream;
30  import java.security.Permission;
31  
32  import org.apache.myfaces.trinidad.util.URLUtils;
33  
34  /**
35   * A resource loader implementation that proxies another
36   * resource loader, controlling the URLConnection.
37   *
38   */
39  public class ProxyResourceLoader extends ResourceLoader
40  {
41    /**
42     * Constructs a new ProxyResourceLoader with specified parent.
43     * 
44     * @param parent  the parent resource loader
45     */
46    public ProxyResourceLoader(
47      ResourceLoader parent)
48    {
49      super(parent);
50    }
51    
52    @Override
53    public URL getResource(
54      String path) throws IOException
55    {
56      URL url = super.getResource(path);
57      return (url != null) 
58                ? new URL("proxy", null, -1, url.toExternalForm(),  
59                          new ProxyURLStreamHandler(url))
60                : null;
61    }
62  
63    /**
64     * Helper class used to manage decorated URL input stream.
65     */
66    private class ProxyURLStreamHandler extends URLStreamHandler
67    {
68      public ProxyURLStreamHandler(
69        URL proxied)
70      {
71        _proxied = proxied;
72      }
73      
74      @Override
75      protected URLConnection openConnection(
76        URL url
77        ) throws IOException
78      {
79        return new ProxyURLConnection(url, _proxied);
80      }
81      
82      private final URL _proxied;
83    }
84  
85    /**
86     * Helper class used to manage aggregated URL input stream.
87     */
88    protected class ProxyURLConnection extends URLConnection
89    {
90      public ProxyURLConnection(
91        URL url,
92        URL proxied
93        ) throws IOException
94      {
95        super(url);
96        
97        _delegate = proxied.openConnection();
98      }
99  
100     @Override
101     public void addRequestProperty(String key, String value)
102     {
103       getURLConnection().addRequestProperty(key, value);
104     }
105 
106     @Override
107     public void connect() throws IOException
108     {
109       getURLConnection().connect();
110     }
111 
112     @Override
113     public boolean getAllowUserInteraction()
114     {
115       return getURLConnection().getAllowUserInteraction();
116     }
117 
118     @Override
119     public Object getContent() throws IOException
120     {
121       return getURLConnection().getContent();
122     }
123 
124     @Override
125     public Object getContent(Class[] classes) throws IOException
126     {
127       return getURLConnection().getContent(classes);
128     }
129 
130     @Override
131     public String getContentEncoding()
132     {
133       return getURLConnection().getContentEncoding();
134     }
135 
136     @Override
137     public int getContentLength()
138     {
139       return getURLConnection().getContentLength();
140     }
141 
142     @Override
143     public String getContentType()
144     {
145       return ProxyResourceLoader.this.getContentType(getURLConnection());
146     }
147 
148     @Override
149     public long getDate()
150     {
151       return getURLConnection().getDate();
152     }
153 
154     @Override
155     public boolean getDefaultUseCaches()
156     {
157       return getURLConnection().getDefaultUseCaches();
158     }
159 
160     @Override
161     public boolean getDoInput()
162     {
163       return getURLConnection().getDoInput();
164     }
165 
166     @Override
167     public boolean getDoOutput()
168     {
169       return getURLConnection().getDoOutput();
170     }
171 
172     @Override
173     public long getExpiration()
174     {
175       return getURLConnection().getExpiration();
176     }
177 
178     @Override
179     public String getHeaderField(int n)
180     {
181       return getURLConnection().getHeaderField(n);
182     }
183 
184     @Override
185     public String getHeaderField(String name)
186     {
187       return getURLConnection().getHeaderField(name);
188     }
189 
190     @Override
191     public long getHeaderFieldDate(String name, long Default)
192     {
193       return getURLConnection().getHeaderFieldDate(name, Default);
194     }
195 
196     @Override
197     public int getHeaderFieldInt(String name, int Default)
198     {
199       return getURLConnection().getHeaderFieldInt(name, Default);
200     }
201 
202     @Override
203     public String getHeaderFieldKey(int n)
204     {
205       return getURLConnection().getHeaderFieldKey(n);
206     }
207 
208     @Override
209     public Map<String, List<String>> getHeaderFields()
210     {
211       return getURLConnection().getHeaderFields();
212     }
213 
214     @Override
215     public long getIfModifiedSince()
216     {
217       return getURLConnection().getIfModifiedSince();
218     }
219 
220     @Override
221     public InputStream getInputStream() throws IOException
222     {
223       return getURLConnection().getInputStream();
224     }
225 
226     @Override
227     public long getLastModified()
228     {
229       try
230       {
231         return URLUtils.getLastModified(getURLConnection());
232       }
233       catch (IOException exception)
234       {
235         return -1;
236       }
237     }
238 
239     @Override
240     public OutputStream getOutputStream() throws IOException
241     {
242       return getURLConnection().getOutputStream();
243     }
244 
245     @Override
246     public Permission getPermission() throws IOException
247     {
248       return getURLConnection().getPermission();
249     }
250 
251     @Override
252     public Map<String, List<String>> getRequestProperties()
253     {
254       return getURLConnection().getRequestProperties();
255     }
256 
257     @Override
258     public String getRequestProperty(String key)
259     {
260       return getURLConnection().getRequestProperty(key);
261     }
262 
263     @Override
264     public boolean getUseCaches()
265     {
266       return getURLConnection().getUseCaches();
267     }
268 
269     @Override
270     public void setAllowUserInteraction(boolean allowuserinteraction)
271     {
272       getURLConnection().setAllowUserInteraction(allowuserinteraction);
273     }
274 
275     @Override
276     public void setDefaultUseCaches(boolean defaultusecaches)
277     {
278       getURLConnection().setDefaultUseCaches(defaultusecaches);
279     }
280 
281     @Override
282     public void setDoInput(boolean doinput)
283     {
284       getURLConnection().setDoInput(doinput);
285     }
286 
287     @Override
288     public void setDoOutput(boolean dooutput)
289     {
290       getURLConnection().setDoOutput(dooutput);
291     }
292 
293     @Override
294     public void setIfModifiedSince(long ifmodifiedsince)
295     {
296       getURLConnection().setIfModifiedSince(ifmodifiedsince);
297     }
298 
299     @Override
300     public void setRequestProperty(String key, String value)
301     {
302       getURLConnection().setRequestProperty(key, value);
303     }
304 
305     @Override
306     public void setUseCaches(boolean usecaches)
307     {
308       getURLConnection().setUseCaches(usecaches);
309     }
310     
311     protected URLConnection getURLConnection()
312     {
313       return _delegate;
314     }
315     
316     private final URLConnection _delegate;
317   }
318 }