com.iterzp.momo.filter.EncodingConvertFilter.java Source code

Java tutorial

Introduction

Here is the source code for com.iterzp.momo.filter.EncodingConvertFilter.java

Source

/*
 * Copyright 2005-2013 iterzp.com. All rights reserved.
 * Support: http://www.iterzp.com
 * License: http://www.iterzp.com/license
 */
package com.iterzp.momo.filter;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;

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

import org.springframework.web.filter.OncePerRequestFilter;

/**
 * Filter - ???
 * 
 * @author yd14 Team
 * @version 3.0
 */
public class EncodingConvertFilter extends OncePerRequestFilter {

    /** ?? */
    private String fromEncoding = "ISO-8859-1";

    /** ?? */
    private String toEncoding = "UTF-8";

    //@SuppressWarnings("unchecked")
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
            FilterChain filterChain) throws ServletException, IOException {
        if (request.getMethod().equalsIgnoreCase("GET")) {
            for (Iterator<String[]> iterator = request.getParameterMap().values().iterator(); iterator.hasNext();) {
                String[] parames = iterator.next();
                for (int i = 0; i < parames.length; i++) {
                    try {
                        parames[i] = new String(parames[i].getBytes(fromEncoding), toEncoding);
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        filterChain.doFilter(request, response);
    }

    /**
     * ???
     * 
     * @return ??
     */
    public String getFromEncoding() {
        return fromEncoding;
    }

    /**
     * ??
     * 
     * @param fromEncoding
     *            ??
     */
    public void setFromEncoding(String fromEncoding) {
        this.fromEncoding = fromEncoding;
    }

    /**
     * ???
     * 
     * @return ??
     */
    public String getToEncoding() {
        return toEncoding;
    }

    /**
     * ??
     * 
     * @param toEncoding
     *            ??
     */
    public void setToEncoding(String toEncoding) {
        this.toEncoding = toEncoding;
    }

}