/*
* =============================================================================
*
* Copyright (c) 2011-2016, The THYMELEAF team (http://www.thymeleaf.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* =============================================================================
*/
package org.thymeleaf.templateresource;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.MalformedURLException;
import javax.servlet.ServletContext;
import org.thymeleaf.util.StringUtils;
import org.thymeleaf.util.Validate;
/**
* <p>
* Implementation of {@link ITemplateResource} accessible from the {@link ServletContext} in a web application.
* The paths of these resources start at the web application root, and are normally stored inside
* <tt>/WEB-INF</tt>.
* </p>
* <p>
* Objects of this class are usually created by {@link org.thymeleaf.templateresolver.ServletContextTemplateResolver}.
* </p>
*
* @author Daniel Fernández
* @since 3.0.0
*
*/
public final class ServletContextTemplateResource implements ITemplateResource {
private final ServletContext servletContext;
private final String path;
private final String characterEncoding;
public ServletContextTemplateResource(final ServletContext servletContext, final String path, final String characterEncoding) {
super();
Validate.notNull(servletContext, "ServletContext cannot be null");
Validate.notEmpty(path, "Resource Path cannot be null or empty");
// Character encoding CAN be null (system default will be used)
this.servletContext = servletContext;
final String cleanPath = TemplateResourceUtils.cleanPath(path);
this.path = (cleanPath.charAt(0) != '/' ? ("/" + cleanPath) : cleanPath);
this.characterEncoding = characterEncoding;
}
public String getDescription() {
return this.path;
}
public String getBaseName() {
return TemplateResourceUtils.computeBaseName(this.path);
}
public Reader reader() throws IOException {
final InputStream inputStream = this.servletContext.getResourceAsStream(this.path);
if (inputStream == null) {
throw new FileNotFoundException(String.format("ServletContext resource \"%s\" does not exist", this.path));
}
if (!StringUtils.isEmptyOrWhitespace(this.characterEncoding)) {
return new BufferedReader(new InputStreamReader(new BufferedInputStream(inputStream), this.characterEncoding));
}
return new BufferedReader(new InputStreamReader(new BufferedInputStream(inputStream)));
}
public ITemplateResource relative(final String relativeLocation) {
Validate.notEmpty(relativeLocation, "Relative Path cannot be null or empty");
final String fullRelativeLocation = TemplateResourceUtils.computeRelativeLocation(this.path, relativeLocation);
return new ServletContextTemplateResource(this.servletContext, fullRelativeLocation, this.characterEncoding);
}
public boolean exists() {
try {
return (this.servletContext.getResource(this.path) != null);
} catch (final MalformedURLException e) {
return false;
}
}
}