com.xyz.util.WebUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.xyz.util.WebUtil.java

Source

/**
 * Copyright (c) 2005-2009 springside.org.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * 
 * $Id: WebUtils.java 637 2009-11-10 14:12:49Z calvinxiu $
 */
package com.xyz.util;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.zip.GZIPOutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.util.WebUtils;

/**
 * Web Utils?.
 * 
 * @author calvin
 */
public class WebUtil {

    public static final long ONE_YEAR_SECONDS = 60 * 60 * 24 * 365;

    /**
     * ??Header.
     * 
     * @param fileName ???.
     */
    public static void setDownloadableHeader(HttpServletResponse response, String fileName) {
        response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
    }

    /**
     * LastModified Header.
     */
    public static void setLastModifiedHeader(HttpServletResponse response, long lastModifiedDate) {
        response.setDateHeader("Last-Modified", lastModifiedDate);
    }

    /**
     * Etag Header.
     */
    public static void setEtag(HttpServletResponse response, String etag) {
        response.setHeader("ETag", etag);
    }

    /**
     *  Header.
     */
    public static void setExpiresHeader(HttpServletResponse response, long expiresSeconds) {
        //Http 1.0 header
        response.setDateHeader("Expires", System.currentTimeMillis() + expiresSeconds * 1000);
        //Http 1.1 header
        response.setHeader("Cache-Control", "max-age=" + expiresSeconds);
    }

    /**
     * Header.
     */
    public static void setNoCacheHeader(HttpServletResponse response) {
        //Http 1.0 header
        response.setDateHeader("Expires", 0);
        //Http 1.1 header
        response.setHeader("Cache-Control", "max-age=0");
    }

    /**
     * ???gzip?.
     */
    public static boolean checkAccetptGzip(HttpServletRequest request) {
        //Http1.1 header
        String acceptEncoding = request.getHeader("Accept-Encoding");

        if (StringUtils.contains(acceptEncoding, "gzip")) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Gzip HeaderGZIPOutputStream.
     */
    public static OutputStream buildGzipOutputStream(HttpServletResponse response) throws IOException {
        response.setHeader("Content-Encoding", "gzip");
        return new GZIPOutputStream(response.getOutputStream());
    }

    /**
     * ??If-Modified-Since Header, ?.
     * , checkIfModifyfalse ,304 not modify status.
     */
    public static boolean checkIfModifiedSince(HttpServletRequest request, HttpServletResponse response,
            long lastModified) {
        long ifModifiedSince = request.getDateHeader("If-Modified-Since");
        if ((ifModifiedSince != -1) && (lastModified < ifModifiedSince + 1000)) {
            response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
            return false;
        }
        return true;
    }

    /**
     * ?? If-None-Match Header,Etag?.
     * 
     * Etag,checkIfNoneMatchfalse, 304 not modify status.
     */
    public static boolean checkIfNoneMatchEtag(HttpServletRequest request, HttpServletResponse response,
            String etag) {
        String headerValue = request.getHeader("If-None-Match");
        if (headerValue != null) {
            boolean conditionSatisfied = false;
            if (!headerValue.equals("*")) {
                StringTokenizer commaTokenizer = new StringTokenizer(headerValue, ",");

                while (!conditionSatisfied && commaTokenizer.hasMoreTokens()) {
                    String currentToken = commaTokenizer.nextToken();
                    if (currentToken.trim().equals(etag)) {
                        conditionSatisfied = true;
                    }
                }
            } else {
                conditionSatisfied = true;
            }

            if (conditionSatisfied) {
                response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
                response.setHeader("ETag", etag);
                return false;
            }
        }
        return true;
    }

    /**
     * ???Request Parameters.
     * 
     * Parameter???.
     */
    @SuppressWarnings("unchecked")
    public static Map<String, Object> getParametersStartingWith(HttpServletRequest request, String prefix) {
        return WebUtils.getParametersStartingWith(request, prefix);
    }
}