Java tutorial
/** * * APDPlat - Application Product Development Platform * Copyright (c) 2013, ??, yang-shangchuan@qq.com * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ package org.apdplat.platform.util; import org.apdplat.platform.log.APDPlatLogger; import java.io.IOException; import java.io.OutputStream; import java.util.Collection; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.apache.commons.lang.StringUtils; import org.apache.struts2.ServletActionContext; import org.apdplat.platform.log.APDPlatLoggerFactory; /** *Struts2 * @author ?? */ public class Struts2Utils { private static final APDPlatLogger LOG = APDPlatLoggerFactory.getAPDPlatLogger(Struts2Utils.class); private Struts2Utils() { }; 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; private static final String TEXT_TYPE = "text/plain"; private static final String JSON_TYPE = "application/json"; private static final String XML_TYPE = "text/xml"; private static final String HTML_TYPE = "text/html"; private static final String JS_TYPE = "text/javascript"; public static HttpSession getSession() { return ServletActionContext.getRequest().getSession(); } public static HttpServletRequest getRequest() { return ServletActionContext.getRequest(); } public static HttpServletResponse getResponse() { return ServletActionContext.getResponse(); } public static String getParameter(String name) { return getRequest().getParameter(name); } 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) { LOG.error(e.getMessage(), e); } } /** * . * @see #render(String, String, String...) */ public static void renderText(final String text, final String... headers) { render(TEXT_TYPE, text, headers); } /** * HTML. * @see #render(String, String, String...) */ public static void renderHtml(final String html, final String... headers) { render(HTML_TYPE, html, headers); } /** * XML. * @see #render(String, String, String...) */ public static void renderXml(final String xml, final String... headers) { render(XML_TYPE, xml, headers); } /** * JSON. * * @param jsonString json. * @see #render(String, String, String...) */ public static void renderJson(final String jsonString, final String... headers) { render(JSON_TYPE, jsonString, headers); } /** * JSON. * * @param map Map,json. * @see #render(String, String, String...) */ @SuppressWarnings("unchecked") public static void renderJson(final Map map, final String... headers) { String jsonString = JSONObject.fromObject(map).toString(); render(JSON_TYPE, 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_TYPE, jsonString, headers); } /** * JSON. * * @param collection Java?, json. * @see #render(String, String, String...) */ public static void renderJson(final Collection<?> collection, final String... headers) { String jsonString = JSONArray.fromObject(collection).toString(); render(JSON_TYPE, jsonString, headers); } /** * JSON. * * @param array Java, json. * @see #render(String, String, String...) */ public static void renderJson(final Object[] array, final String... headers) { String jsonString = JSONArray.fromObject(array).toString(); render(JSON_TYPE, jsonString, headers); } public static void renderImage(byte[] data, String type) { try { HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType(type); try (OutputStream out = response.getOutputStream()) { out.write(data, 0, data.length); out.flush(); } } catch (IOException e) { LOG.error("?", e); } } /** * ?MashupJSONP. * * @param callbackName callback??. * @param contentMap Map,json. * @see #render(String, String, String...) */ @SuppressWarnings("unchecked") public static void renderJsonp(final String callbackName, final Map contentMap, final String... headers) { String jsonParam = JSONObject.fromObject(contentMap).toString(); StringBuilder result = new StringBuilder().append(callbackName).append("(").append(jsonParam).append(");"); //Content-Typejavascript,javascript?, callback197("{content:'Hello World!!!'}"); render(JS_TYPE, result.toString(), headers); } }