com.igrow.mall.common.util.Struts2Utils.java Source code

Java tutorial

Introduction

Here is the source code for com.igrow.mall.common.util.Struts2Utils.java

Source

/**
 * Copyright (c) 2005-2009 springside.org.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * 
 * $Id: Struts2Utils.java 463 2009-09-13 14:34:14Z calvinxiu $
 */
package com.igrow.mall.common.util;

import java.io.IOException;
import java.util.Map;

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

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Struts2 Utils.
 * 
 * ?Request/Response/Sessionjsp/freemaker.
 * 
 * @author calvin
 */
public class Struts2Utils {

    //header ?//
    private static final String ENCODING_PREFIX = "encoding";
    private static final String NOCACHE_PREFIX = "no-cache";
    private static final String ENCODING_DEFAULT = "UTF-8";
    private static final boolean NOCACHE_DEFAULT = true;

    //content-type  //
    private static final String TEXT = "text/plain";
    private static final String JSON = "application/json";
    private static final String XML = "text/xml";
    private static final String HTML = "text/html";

    private static Logger logger = LoggerFactory.getLogger(Struts2Utils.class);

    // ?Request/Response/Session //

    /**
     * ?HttpSession.
     */
    public static HttpSession getSession() {
        return ServletActionContext.getRequest().getSession();
    }

    /**
     * ?HttpRequest.
     */
    public static HttpServletRequest getRequest() {
        return ServletActionContext.getRequest();
    }

    /**
     * ?HttpResponse.
     */
    public static HttpServletResponse getResponse() {
        return ServletActionContext.getResponse();
    }

    /**
     * ?Request Parameter.
     */
    public static String getParameter(String name) {
        return getRequest().getParameter(name);
    }

    // jsp/freemaker //

    /**
     * .
        
     * eg.
     * render("text/plain", "hello", "encoding:GBK");
     * render("text/plain", "hello", "no-cache:false");
     * render("text/plain", "hello", "encoding:GBK", "no-cache:false");
     * 
     * @param headers ??header??"encoding:""no-cache:",UTF-8true.
     */
    public static void render(final String contentType, final String content, final String... headers) {
        try {
            //?headers?
            String encoding = ENCODING_DEFAULT;
            boolean noCache = NOCACHE_DEFAULT;
            for (String header : headers) {
                String headerName = StringUtils.substringBefore(header, ":");
                String headerValue = StringUtils.substringAfter(header, ":");

                if (StringUtils.equalsIgnoreCase(headerName, ENCODING_PREFIX)) {
                    encoding = headerValue;
                } else if (StringUtils.equalsIgnoreCase(headerName, NOCACHE_PREFIX)) {
                    noCache = Boolean.parseBoolean(headerValue);
                } else
                    throw new IllegalArgumentException(headerName + "??header");
            }

            HttpServletResponse response = ServletActionContext.getResponse();

            //headers?
            String fullContentType = contentType + ";charset=" + encoding;
            response.setContentType(fullContentType);
            if (noCache) {
                response.setHeader("Pragma", "No-cache");
                response.setHeader("Cache-Control", "no-cache");
                response.setDateHeader("Expires", 0);
            }

            response.getWriter().write(content);
            response.getWriter().flush();

        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
    }

    /**
     * .
     * @see #render(String, String, String...)
     */
    public static void renderText(final String text, final String... headers) {
        render(TEXT, text, headers);
    }

    /**
     * HTML.
     * @see #render(String, String, String...)
     */
    public static void renderHtml(final String html, final String... headers) {
        render(HTML, html, headers);
    }

    /**
     * XML.
     * @see #render(String, String, String...)
     */
    public static void renderXml(final String xml, final String... headers) {
        render(XML, xml, headers);
    }

    /**
     * JSON.
     * 
     * @param string json.
     * @see #render(String, String, String...)
     */
    public static void renderJson(final String jsonString, final String... headers) {
        render(JSON, jsonString, headers);
    }

    /**
     * JSON.
     * 
     * @param map Map,json.
     * @see #render(String, String, String...)
     */
    public static void renderJson(@SuppressWarnings("rawtypes") final Map map, final String... headers) {
        String jsonString = JSONObject.fromObject(map).toString();
        render(JSON, jsonString, headers);
    }

    /**
     * JSON.
     * 
     * @param object Java,json.
     * @see #render(String, String, String...)
     */
    public static void renderJson(final Object object, final String... headers) {
        String jsonString = JSONObject.fromObject(object).toString();
        render(JSON, jsonString, headers);
    }
}