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 static org.apache.wicket.util.resource.ResourceUtils.MIN_POSTFIX_DEFAULT_AS_EXTENSION;
020
021import java.util.Locale;
022
023import org.apache.wicket.Application;
024import org.apache.wicket.javascript.IJavaScriptCompressor;
025
026/**
027 * Package resource for javascript files.
028 */
029public class JavaScriptPackageResource extends PackageResource
030{
031        private static final long serialVersionUID = 1L;
032
033        /**
034         * Construct.
035         * 
036         * @param scope
037         *            This argument will be used to get the class loader for loading the package
038         *            resource, and to determine what package it is in
039         * @param name
040         *            The relative path to the resource
041         * @param locale
042         *            The locale of the resource
043         * @param style
044         *            The style of the resource
045         * @param variation
046         *            The component's variation (of the style)
047         */
048        public JavaScriptPackageResource(Class<?> scope, String name, Locale locale, String style,
049                String variation)
050        {
051                super(scope, name, locale, style, variation);
052
053                // JS resources can be compressed if there is configured IJavaScriptCompressor, and the
054                // resource isn't already minified (the file already has .min. in its name).
055                setCompress(!name.contains(MIN_POSTFIX_DEFAULT_AS_EXTENSION));
056        }
057
058        /**
059         * Gets the {@link IJavaScriptCompressor} to be used. By default returns the configured
060         * compressor on application level, but can be overridden by the user application to provide
061         * compressor specific to the resource.
062         * 
063         * @return the configured application level JavaScript compressor. May be {@code null}.
064         */
065        @Override
066        protected IJavaScriptCompressor getCompressor()
067        {
068                IJavaScriptCompressor compressor = null;
069                if (Application.exists())
070                {
071                        compressor = Application.get().getResourceSettings().getJavaScriptCompressor();
072                }
073                return compressor;
074        }
075
076}