001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.core.util.file;
018
019import java.net.URL;
020
021import javax.servlet.ServletContext;
022
023import org.apache.wicket.core.util.resource.UrlResourceStream;
024import org.apache.wicket.util.file.IResourceFinder;
025import org.apache.wicket.util.resource.IResourceStream;
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028
029
030/**
031 * An {@link IResourceFinder} that looks in a folder in the webapp context path. It will
032 * <em>not</em> load files inside WEB-INF.
033 * 
034 * @author Johan Compagner
035 * @author Carl-Eric Menzel
036 */
037public final class WebApplicationPath implements IResourceFinder
038{
039        private final static Logger log = LoggerFactory.getLogger(WebApplicationPath.class);
040
041        private static final String WEB_INF = "WEB-INF/";
042
043        /** The web apps servlet context */
044        private final ServletContext servletContext;
045
046        private final String path;
047
048        /**
049         * Constructor
050         * 
051         * @param servletContext
052         *            The webapplication context where the resources must be loaded from
053         * @param path
054         *            The path inside the app context where to look.
055         */
056        public WebApplicationPath(final ServletContext servletContext, String path)
057        {
058                this.servletContext = servletContext;
059                if (!path.startsWith("/"))
060                {
061                        path = "/" + path;
062                }
063                if (!path.endsWith("/"))
064                {
065                        path += "/";
066                }
067                this.path = path;
068        }
069
070
071        /**
072         * 
073         * @see org.apache.wicket.util.file.IResourceFinder#find(Class, String)
074         */
075        @Override
076        public IResourceStream find(final Class<?> clazz, final String pathname)
077        {
078                IResourceStream resourceStream = null;
079                if (pathname.startsWith(WEB_INF) == false)
080                {
081                        try
082                        {
083                                final URL url = servletContext.getResource(path + pathname);
084                                if (url != null)
085                                {
086                                        resourceStream = new UrlResourceStream(url);
087                                }
088                        }
089                        catch (Exception ex)
090                        {
091                                // ignore, file couldn't be found
092                        }
093                }
094
095                return resourceStream;
096        }
097
098
099        /**
100         * @see java.lang.Object#toString()
101         */
102        @Override
103        public String toString()
104        {
105                return "[webapppath: " + path + "]";
106        }
107}