1/*2 * $Id$3 *4 * Licensed to the Apache Software Foundation (ASF) under one5 * or more contributor license agreements. See the NOTICE file6 * distributed with this work for additional information7 * regarding copyright ownership. The ASF licenses this file8 * to you under the Apache License, Version 2.0 (the9 * "License"); you may not use this file except in compliance10 * with the License. You may obtain a copy of the License at11 *12 * http://www.apache.org/licenses/LICENSE-2.013 *14 * Unless required by applicable law or agreed to in writing,15 * software distributed under the License is distributed on an16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY17 * KIND, either express or implied. See the License for the18 * specific language governing permissions and limitations19 * under the License.20 */2122package org.apache.tiles.request.locale;
2324import java.io.File;
25import java.io.FileInputStream;
26import java.io.FileNotFoundException;
27import java.io.IOException;
28import java.io.InputStream;
29import java.net.JarURLConnection;
30import java.net.URI;
31import java.net.URISyntaxException;
32import java.net.URL;
33import java.net.URLConnection;
34import java.util.HashSet;
35import java.util.Locale;
36import java.util.Set;
3738import org.slf4j.Logger;
39import org.slf4j.LoggerFactory;
4041importstatic java.lang.System.getProperty;
42importstatic java.util.Collections.unmodifiableSet;
4344/**45 * A {@link PostfixedApplicationResource} that can be accessed through a URL.46 *47 * @version $Rev$ $Date$48 */4950publicclassURLApplicationResourceextendsPostfixedApplicationResource {
51/**52 * System parameter to specify additional remote protocols. If a url has a remote protocol, then any53 * {@link IOException} will be thrown directly. If a url has a local protocol, then any {@link IOException}54 * will be caught and transformed into a {@link FileNotFoundException}.55 */56staticfinal String REMOTE_PROTOCOLS_PROPERTY = "tiles.remoteProtocols";
57privatestaticfinal Logger LOG = LoggerFactory.getLogger(URLApplicationResource.class);
58privatestaticfinal Set<String> REMOTE_PROTOCOLS;
5960static {
61 REMOTE_PROTOCOLS = initRemoteProtocols();
62 }
6364/**65 * Creates an unmodifiable set of <em>remote</em> protocols which are used in {@link URL} objects, see {@link URL#getProtocol()}.66 * A url with a remote protocol establishes a network connection when its {@link URL#openConnection()} is being called.67 * The set will always contain the built-in remote protocols below:68 * <ul>69 * <li><a href="http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/sun/net/www/protocol/ftp">ftp</a></li>70 * <li><a href="http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/sun/net/www/protocol/http">http</a></li>71 * <li><a href="http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/sun/net/www/protocol/https">https</a></li>72 * <li><a href="http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/sun/net/www/protocol/mailto">mailto</a></li>73 * <li><a href="http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/sun/net/www/protocol/netdoc">netdoc</a></li>74 * </ul>75 * It's possible, that your environment provides additional remote protocols because of following reasons:76 * <ul>77 * <li>your application server adds more remote protocols, see its documentation for further details.</li>78 * <li>your application supplies custom remote protocols trough its own {@link java.net.URLStreamHandlerFactory}79 * (see following excellent <a href="https://stackoverflow.com/questions/26363573/registering-and-using-a-custom-java-net-url-protocol">explanation</a>80 * for getting an idea how to do this)</li>81 * </ul>82 * If you need to use such extra remote protocols in Tiles, you may enhance the set via system property {@code tiles.remoteProtocols}. Suppose83 * you need to add your custom remote protocols "foo" and "bar". To do so, add following parameter to the command line (use ";" as separator):84 * <pre>85 * -Dtiles.remoteProtocols=foo;bar86 * </pre>87 * The resulting set will then contain the built-in protocols plus "foo" and "bar".88 *89 * @return Unmodifiable set of remote protocols, never {@code null}90 */91static Set<String> initRemoteProtocols() {
92 Set<String> remoteProtocols = new HashSet<String>();
93 remoteProtocols.add("ftp");
94 remoteProtocols.add("http");
95 remoteProtocols.add("https");
96 remoteProtocols.add("mailto");
97 remoteProtocols.add("netdoc");
9899 String protocolsProp = getProperty(REMOTE_PROTOCOLS_PROPERTY);
100if (protocolsProp != null) {
101for (String protocol : protocolsProp.split(";")) {
102 remoteProtocols.add(protocol.trim());
103 }
104 }
105return unmodifiableSet(remoteProtocols);
106 }
107108privatestaticboolean isLocal(URL url) {
109return !REMOTE_PROTOCOLS.contains(url.getProtocol());
110 }
111112/** the URL where the contents can be found. */113privatefinal URL url;
114/** if the URL matches a file, this is the file. */115private File file;
116/** if the URL points to a local resource */117privatefinalboolean local;
118119/**120 * Creates a URLApplicationResource for the specified path that can be accessed through the specified URL.121 *122 * @param localePath the path including localization.123 * @param url the URL where the contents can be found.124 */125publicURLApplicationResource(String localePath, URL url) {
126super(localePath);
127this.url = url;
128if ("file".equals(url.getProtocol())) {
129 file = getFile(url);
130 }
131 local = isLocal(url);
132 }
133134/**135 * Creates a URLApplicationResource for the specified path that can be accessed through the specified URL.136 *137 * @param path the path excluding localization.138 * @param locale the Locale.139 * @param url the URL where the contents can be found.140 */141publicURLApplicationResource(String path, Locale locale, URL url) {
142super(path, locale);
143this.url = url;
144if ("file".equals(url.getProtocol())) {
145 file = getFile(url);
146 }
147 local = isLocal(url);
148 }
149150private URLConnection openConnection() throws IOException {
151try {
152return url.openConnection();
153 } catch (IOException e) {
154// If the url points to a local resource but it cannot be155// opened, then the resource actually does not exist. In this156// case throw a FileNotFoundException157if (local) {
158 FileNotFoundException fne = new FileNotFoundException(url.toString());
159 fne.initCause(e);
160throw fne;
161 }
162throw e;
163 }
164 }
165166privatestatic File getFile(URL url) {
167try {
168returnnew File(new URI(url.toExternalForm()).getSchemeSpecificPart());
169 } catch (URISyntaxException e) {
170 LOG.debug("Cannot translate URL to file name, expect a performance impact", e);
171returnnull;
172 }
173 }
174175/** {@inheritDoc} */176 @Override
177public InputStream getInputStream() throws IOException {
178if (file != null) {
179returnnew FileInputStream(file);
180 } else {
181return openConnection().getInputStream();
182 }
183 }
184185/** {@inheritDoc} */186 @Override
187publiclong getLastModified() throws IOException {
188if (file != null) {
189return file.lastModified();
190 } else {
191 URLConnection connection = openConnection();
192if (connection instanceof JarURLConnection) {
193return ((JarURLConnection) connection).getJarEntry().getTime();
194 } else {
195long result = connection.getLastModified();
196return result;
197 }
198 }
199 }
200201/** {@inheritDoc} */202 @Override
203public String toString() {
204return"Resource " + getLocalePath() + " at " + url.toString();
205 }
206207protected URL getURL(){
208return url;
209 }
210211protected File getFile(){
212return file;
213 }
214 }