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.request.resource;
018
019import org.apache.wicket.Application;
020import org.apache.wicket.util.lang.Args;
021import org.apache.wicket.util.resource.ResourceUtils;
022
023/**
024 * This is a ResourceReference to handle context-relative resources such as js, css and 
025 * picture files placed in a folder on the context root (ex: '/css/coolTheme.css'). 
026 * The class has a flag (see {@link #isMinifyIt()}) to decide if referenced resource can be 
027 * minified (ex: '/css/coolTheme.min.css') or not.
028 *
029 * @author Andrea Del Bene
030 */
031public class ContextRelativeResourceReference extends ResourceReference
032{
033        
034        /** The Constant serialVersionUID. */
035        private static final long serialVersionUID = 1L;
036        
037        /** Says if the resource name can be minified or not. */
038        private final boolean minifyIt;
039        
040        /** The minfied postfix. */
041        private final String minPostfix;        
042        
043        /**
044         * Instantiates a new context relative resource reference for the given name. The resource
045         * will be minified in DEPLOYMENT mode and "min" will be used as postfix.
046         * 
047         * @param name
048         *                              the resource name
049         */
050        public ContextRelativeResourceReference(final String name)
051        {
052                this(name, ResourceUtils.MIN_POSTFIX_DEFAULT, true);
053        }
054        
055        
056        /**
057         * Instantiates a new context relative resource reference for the given name.
058         * Parameter {@code minifyIt} says if the resource can be minified (true) or not (false). 
059         *
060         * @param name 
061         *                              the resource name
062         * @param minifyIt 
063         *                              says if the resource name can be minified or not
064         */
065        public ContextRelativeResourceReference(final String name, final boolean minifyIt)
066        {
067                this(name, ResourceUtils.MIN_POSTFIX_DEFAULT, minifyIt);
068        }
069        
070        /**
071         * Instantiates a new context relative resource reference for the given name.  We can
072         * specify which postfix we want to use for minification with parameter @code minPostfix}
073         * 
074         * @param name
075         *                              the resource name
076         * @param minPostfix
077         *                      the minfied postfix
078         */
079        public ContextRelativeResourceReference(final String name, final String minPostfix)
080        {
081                this(name, minPostfix, true);
082        }
083        
084        /**
085         * Instantiates a new context relative resource reference for the given name. We can
086         * specify which postfix we want to use for minification with parameter @code minPostfix}
087         * while parameter {@code minifyIt} says if the resource can be minified (true) or not (false). 
088         * @param name 
089         *                              the resource name
090         * @param minPostfix 
091         *                              the minfied postfix
092         * @param minifyIt 
093         *                              says if the resource name can be minified or not
094         */
095        public ContextRelativeResourceReference(final String name, final String minPostfix, final boolean minifyIt)
096        {
097                super(name);
098                
099                Args.notNull(minPostfix, "minPostfix");
100                
101                this.minPostfix = minPostfix;
102                this.minifyIt = minifyIt;
103        }
104        
105        /**
106         * Build the context-relative resource for this resource reference.
107         * 
108         * @param name
109         *                              the resource name
110         * @param minPostfix
111         *                              the postfix to use to minify the resource name (typically "min")
112         * @return the context-relative resource 
113         */
114        protected ContextRelativeResource buildContextRelativeResource(final String name, final String minPostfix)
115        {
116                String minifiedName = name;
117                
118                if (canBeMinified()) 
119                {
120                        minifiedName = ResourceUtils.getMinifiedName(name, minPostfix);
121                }
122                
123                return new ContextRelativeResource(minifiedName);
124        }
125
126        /**
127         * Says if the referenced resource can be minified. It returns {@code true} if 
128         * both flag {@link #minifyIt} and application's resource settings method
129         * {@link org.apache.wicket.settings.ResourceSettings#getUseMinifiedResources()}} 
130         * are true.
131         * 
132         * @return {@code true} if resource can be minified, {@code false} otherwise
133         */
134        protected boolean canBeMinified()
135        {
136                return minifyIt && Application.exists()
137            && Application.get().getResourceSettings().getUseMinifiedResources();
138        }
139
140        @Override
141        public final ContextRelativeResource getResource()
142        {
143                return buildContextRelativeResource(getName(), minPostfix);
144        }
145        
146        /**
147         * Returns the flag that says if the resource can be minified (true) or not (false).
148         *
149         * @return true, if resource can be minified
150         */
151        public boolean isMinifyIt()
152        {
153                return minifyIt;
154        }
155        
156        
157        /**
158         * Gets the minified postfix we use for this resource.
159         *
160         * @return the minified postfix
161         */
162        public String getMinPostfix()
163        {
164                return minPostfix;
165        }
166}