MountPointResources.java
- /***************************************************************************
- Copyright 2013 Emily Estes
- 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 net.metanotion.simpletemplate;
- /** This resource factory wraps another resource factory and prepends a prefix to all resource requests. i.e. requests
- for a resource with a prefix like "/prefix" for "/prefix/foo" get passed on the wrapped resource factory as "/foo".
- */
- public final class MountPointResources implements ResourceFactory {
- private final String prefix;
- private final ResourceFactory delegate;
- private final ResourceFactory parent;
- /** Create a new prefixed resource factory.
- @param delegate The resource factory to use for the prefix
- @param prefixPath The path to strip from all requests against delegate.
- */
- public MountPointResources(final ResourceFactory delegate, final String prefixPath) {
- this(delegate, prefixPath, NullTemplateHelper.INSTANCE);
- }
- /** Create a new prefixed resource factory that delegates all unmatched requests to another resource factory.
- @param delegate The resource factory to use for the prefix
- @param prefixPath The path to strip from all requests against delegate.
- @param parent The resource factroy to delegate all unmatched prefixes to.
- */
- public MountPointResources(final ResourceFactory delegate, final String prefixPath, final ResourceFactory parent) {
- this.prefix = prefixPath;
- this.delegate = delegate;
- this.parent = parent;
- }
- @Override public Resource get(final String urn) {
- if(!urn.startsWith(prefix)) {
- return parent.get(urn);
- } else {
- return delegate.get(urn.substring(prefix.length()));
- }
- }
- }