Package org.internna.iwebmvc.spring.jee

Source Code of org.internna.iwebmvc.spring.jee.IWebMvcCombinedFilter

/*
* Copyright 2002-2007 the original author or authors.
*
* 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 org.internna.iwebmvc.spring.jee;

import static org.springframework.util.StringUtils.hasText;

import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.internna.iwebmvc.performance.GZIPResponseWrapper;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.filter.DelegatingFilterProxy;

/**
* Filters all requests for security, compression and Spring context management.
*
* @author Jose Noheda
* @since 2.0
*/
public class IWebMvcCombinedFilter extends DelegatingFilterProxy {

    public static final String GZIPPED_RESPONSE = "gzip";

    public static final String cookiePath = Long.toHexString(Math.round(Math.random() * 1024 * 1024 * 1024));

    protected boolean excludedPath(String path) {
      return hasText(path) && (path.contains("/jawr") || path.contains("/resource") || path.contains("/captcha"));
    }

    protected HttpServletResponse compress(String pathInfo, HttpServletRequest request, HttpServletResponse response) {
      HttpServletResponse compressed = response;
      if (request.getAttribute(GZIPPED_RESPONSE) == null) {
            request.setAttribute(GZIPPED_RESPONSE, true);
            String acceptGzip = request.getHeader("accept-encoding");
            if ((acceptGzip != null) && (acceptGzip.indexOf("gzip") != -1) && (!GZIPResponseWrapper.class.isInstance(response)))
              if (!excludedPath(pathInfo))
                    compressed = new GZIPResponseWrapper(response);
        }
      return compressed;
    }

    protected void gzip(ServletResponse response) {
      if (GZIPResponseWrapper.class.isInstance(response)) ((GZIPResponseWrapper) response).finishResponse();
    }

    protected void setContext(HttpServletRequest request, ServletRequestAttributes attributes) {
        LocaleContextHolder.setLocale(request.getLocale(), false);
        RequestContextHolder.setRequestAttributes(attributes, false);
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        ServletRequest subSessionRequest = request;
        ServletResponse subSessionResponse = response;
        ServletRequestAttributes attributes = null;
        if (request instanceof HttpServletRequest) {
            HttpServletRequest httpRequest = (HttpServletRequest) request;
            attributes = new ServletRequestAttributes(httpRequest);
            setContext(httpRequest, attributes);
            String path = httpRequest.getPathInfo() == null ? httpRequest.getServletPath() : httpRequest.getPathInfo();
            subSessionResponse = compress(path, httpRequest, (HttpServletResponse) response);
        }
        try {
            super.doFilter(subSessionRequest, subSessionResponse, filterChain);
        } finally {
            RequestContextHolder.resetRequestAttributes();
            LocaleContextHolder.resetLocaleContext();
            if (attributes != null) attributes.requestCompleted();
        }
        gzip(subSessionResponse);
    }

}
TOP

Related Classes of org.internna.iwebmvc.spring.jee.IWebMvcCombinedFilter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.