Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
Javascript/CSS bundling and compressing tool for java web apps.
By using jawr resources are automatically bundled together and optionally minified and gzipped.
Jawr provides tag libraries to reference a generated bundle either by id or by using the name of any of its members.
/**
* Copyright 2009-2010 Ibrahim Chaehoi
*
* 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.jawr.web.servlet;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.jawr.web.JawrConstant;
import net.jawr.web.context.ThreadLocalJawrContext;
import net.jawr.web.exception.InvalidPathException;
import net.jawr.web.exception.ResourceNotFoundException;
import net.jawr.web.resource.ImageResourcesHandler;
import net.jawr.web.resource.bundle.CheckSumUtils;
import net.jawr.web.resource.bundle.IOUtils;
import net.jawr.web.resource.bundle.factory.PropertiesBundleConstant;
import net.jawr.web.resource.bundle.factory.util.PathNormalizer;
import net.jawr.web.resource.bundle.factory.util.PropertiesConfigHelper;
import net.jawr.web.resource.bundle.generator.GeneratorRegistry;
import net.jawr.web.resource.handler.bundle.ResourceBundleHandler;
import net.jawr.web.resource.handler.reader.ResourceReaderHandler;
import net.jawr.web.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Image Request handling class. Jawr image servlet delegates to this class to
* handle requests.
*
* @author Ibrahim Chaehoi
*/
public class JawrImageRequestHandler extends JawrRequestHandler {
/** The serial version UID */
private static final long serialVersionUID = -8342090032443416738L;
/** The logger */
private static final Logger LOGGER = LoggerFactory
.getLogger(JawrImageRequestHandler.class);
/** The cache buster pattern */
private static Pattern cacheBusterPattern = Pattern.compile("("
+ "(([a-zA-Z0-9]+)_)?" + JawrConstant.CACHE_BUSTER_PREFIX
+ ")[a-zA-Z0-9]+(/.*)$");
/** The index of the generated image prefix in the cache buster pattern */
private static final int GENERATED_IMAGE_PREFIX_INDEX = 3;
/** The cache buster replace pattern for standard image */
private static final String CACHE_BUSTER_STANDARD_IMAGE_REPLACE_PATTERN = "$4";
/** The cache buster replace pattern for generated image */
private static final String CACHE_BUSTER_GENERATED_IMAGE_REPLACE_PATTERN = "$3:$4";
/** The resource handler */
private ResourceReaderHandler rsReaderHandler;
/** The resource handler */
private ResourceBundleHandler rsBundleHandler;
/** The image resource handler */
private ImageResourcesHandler imgRsHandler;
/** The bundle mapping */
private Properties bundleMapping;
/**
* Reads the properties file and initializes all configuration using the
* ServletConfig object. If aplicable, a ConfigChangeListenerThread will be
* started to listen to changes in the properties configuration.
*
* @param servletContext
* ServletContext
* @param servletConfig
* ServletConfig
* @throws ServletException
*/
public JawrImageRequestHandler(ServletContext context, ServletConfig config)
throws ServletException {
super(context, config);
resourceType = JawrConstant.IMG_TYPE;
}
/**
* Alternate constructor that does not need a ServletConfig object.
* Parameters normally read rom it are read from the initParams Map, and the
* configProps are used instead of reading a .properties file.
*
* @param servletContext
* ServletContext
* @param servletConfig
* ServletConfig
* @throws ServletException
*/
public JawrImageRequestHandler(ServletContext context,
Map initParams, Properties configProps)
throws ServletException {
super(context, initParams, configProps);
}
/**
* Initialize the Jawr config
*
* @param props
* the properties
* @throws ServletException
* if an exception occurs
*/
protected void initializeJawrConfig(Properties props)
throws ServletException {
// init registry
generatorRegistry = new GeneratorRegistry(resourceType);
// Initialize config
if (null != jawrConfig)
jawrConfig.invalidate();
jawrConfig = createJawrConfig(props);
jawrConfig.setContext(servletContext);
jawrConfig.setGeneratorRegistry(generatorRegistry);
// Set the content type to be used for every request.
contentType = JawrConstant.IMG_TYPE;
// Set mapping, to be used by the tag lib to define URLs that point to
// this servlet.
String mapping = (String) initParameters
.get(JawrConstant.SERVLET_MAPPING_PROPERTY_NAME);
if (null != mapping) {
jawrConfig.setServletMapping(mapping);
}
// Initialize the IllegalBundleRequest handler
initIllegalBundleRequestHandler();
// Initialize the resource handler
rsReaderHandler = initResourceReaderHandler();
rsBundleHandler = initResourceBundleHandler();
// Initialize custom generators
PropertiesConfigHelper propertiesHelper = new PropertiesConfigHelper(
props, resourceType);
Iterator generators = propertiesHelper.getCommonPropertyAsSet(
PropertiesBundleConstant.CUSTOM_GENERATORS).iterator();
while (generators.hasNext()) {
String generatorClass = (String) generators.next();
generatorRegistry.registerGenerator(generatorClass);
}
if (jawrConfig.getUseBundleMapping()) {
bundleMapping = rsBundleHandler.getJawrBundleMapping();
} else {
bundleMapping = new Properties();
}
imgRsHandler = new ImageResourcesHandler(jawrConfig, rsReaderHandler,
rsBundleHandler);
initImageMapping(imgRsHandler);
servletContext.setAttribute(JawrConstant.IMG_CONTEXT_ATTRIBUTE,
imgRsHandler);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Configuration read. Current config:");
LOGGER.debug(jawrConfig.toString());
}
// Warn when in debug mode
if (jawrConfig.isDebugModeOn()) {
LOGGER.warn("Jawr initialized in DEVELOPMENT MODE. Do NOT use this mode in production or integration servers. ");
}
}
/**
* Initialize the image mapping of the image resources handler
*
* @param imgRsHandler
* the image resources handler
*/
private void initImageMapping(ImageResourcesHandler imgRsHandler) {
if (jawrConfig.getUseBundleMapping()
&& rsBundleHandler.isExistingMappingFile()) {
// Initialize the image mapping
Iterator> mapIterator = bundleMapping
.entrySet().iterator();
while (mapIterator.hasNext()) {
Entry