All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.firefly.mvc.web.servlet.SystemHtmlPage Maven / Gradle / Ivy

There is a newer version: 4.0.3.2
Show newest version
package com.firefly.mvc.web.servlet;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

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

import com.firefly.mvc.web.view.TemplateView;
import com.firefly.template.Model;
import com.firefly.template.TemplateFactory;
import com.firefly.utils.StringUtils;
import com.firefly.utils.VerifyUtils;
import com.firefly.utils.log.Log;
import com.firefly.utils.log.LogFactory;

public class SystemHtmlPage {
	private static Log log = LogFactory.getInstance().getLog("firefly-system");
	private static Map errorPage = new HashMap();
	
	public static void addErrorPage(Integer errorCode, String page) {
		errorPage.put(errorCode, page);
	}
	
	public static void addErrorPageMap(Map map) {
		errorPage = map;
	}

	public static void responseSystemPage(HttpServletRequest request,
			HttpServletResponse response, String charset, int status,
			String content) {
		response.setStatus(status);
		response.setCharacterEncoding(charset);
		response.setHeader("Content-Type", "text/html; charset=" + charset);
		try (PrintWriter writer = response.getWriter()) {
			writer.print(systemPageTemplate(status, content));
		} catch (IOException e) {
			log.error("response system page exception", e);
		} 
	}

	public static String systemPageTemplate(int status, String content) {
		if(errorPage == null)
			return getDefaultErrorPage(status, content);
		
		String page = errorPage.get(status);
		if(VerifyUtils.isEmpty(page))
			return getDefaultErrorPage(status, content);
		
		TemplateFactory templateFactory = TemplateView.getTemplateFactory();
		if(templateFactory == null)
			return getDefaultErrorPage(status, content);
		
		com.firefly.template.View v = templateFactory.getView(page);
		if(v == null)
			return getDefaultErrorPage(status, content);
		
		ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
		Model model = new Model() {
			private Map map = new HashMap();

			@Override
			public void put(String key, Object object) {
				map.put(key, object);
			}

			@Override
			public Object get(String key) {
				return map.get(key);
			}

			@Override
			public void remove(String key) {
				map.remove(key);
			}

			@Override
			public void clear() {
				map.clear();
			}
		};
		
		try {
			model.put("#systemErrorMessage", URLDecoder.decode(content, "UTF-8"));
		} catch (UnsupportedEncodingException e) {
			log.error("url decode error", e);
		}
		
		try {
			v.render(model, out);
		} finally {
			try {
				out.close();
			} catch (IOException e) {
				return getDefaultErrorPage(status, content);
			}
		}
		
		try {
			return new String(out.toByteArray(), TemplateView.getCharset());
		} catch (UnsupportedEncodingException e) {
			return getDefaultErrorPage(status, content);
		}
	}
	
	private static String getDefaultErrorPage(int status, String content) {
		StringBuilder ret = new StringBuilder();
		try {
			ret.append("firefly

HTTP ERROR ") .append(status) .append("

") .append(StringUtils.escapeXML(URLDecoder.decode(content, "UTF-8"))) .append("

firefly framework"); } catch (UnsupportedEncodingException e) { log.error("url decode error", e); } return ret.toString(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy