一、前言
关于grizzly-module源码包中com.sun.grizzly.http.HtmlHelper辅助处理类,对访问请求成功200、400的情况统一从后台返回固定html模板进行显示,详情参见源码说明。
二、源码说明
package com.sun.grizzly.http;@b@@b@import java.io.IOException;@b@import java.nio.ByteBuffer;@b@import java.nio.CharBuffer;@b@import java.nio.charset.Charset;@b@import java.nio.charset.CharsetEncoder;@b@import java.util.Date;@b@@b@public class HtmlHelper@b@{@b@ private static final String CSS = "H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}HR {color : #525D76;}";@b@ private static CharBuffer reponseBuffer = CharBuffer.allocate(4096);@b@ private static CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();@b@ private static String NEWLINE = "\r\n";@b@ public static final String OK = "HTTP/1.1 200 OK" + NEWLINE;@b@ public static final String BAD_REQUEST = "HTTP/1.1 400 Bad Request" + NEWLINE;@b@@b@ public static synchronized ByteBuffer getErrorPage(String message, String code)@b@ throws IOException@b@ {@b@ String body = prepareBody(message);@b@ reponseBuffer.clear();@b@ reponseBuffer.put(code);@b@ appendHeaderValue("Content-Type", "text/html");@b@ appendHeaderValue("Content-Length", body.getBytes().length + "");@b@ appendHeaderValue("Date", new Date().toString());@b@ appendHeaderValue("Connection", "Close");@b@ appendHeaderValue("Server", SelectorThread.SERVER_NAME);@b@ reponseBuffer.put(NEWLINE);@b@ reponseBuffer.put(body);@b@ reponseBuffer.flip();@b@ return encoder.encode(reponseBuffer);@b@ }@b@@b@ private static void appendHeaderValue(String name, String value)@b@ {@b@ reponseBuffer.put(name);@b@ reponseBuffer.put(": ");@b@ reponseBuffer.put(value);@b@ reponseBuffer.put(NEWLINE);@b@ }@b@@b@ private static String prepareBody(String message)@b@ {@b@ StringBuffer sb = new StringBuffer();@b@@b@ sb.append("<html><head><title>");@b@ sb.append(SelectorThread.SERVER_NAME);@b@ sb.append("</title>");@b@ sb.append("<style><!--");@b@ sb.append("H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}HR {color : #525D76;}");@b@ sb.append("--></style> ");@b@ sb.append("</head><body>");@b@ sb.append("<h1>");@b@ sb.append(message);@b@ sb.append("</h1>");@b@ sb.append("<HR size=\"1\" noshade>");@b@ sb.append("<h3>").append(SelectorThread.SERVER_NAME).append("</h3>");@b@@b@ sb.append("</body></html>");@b@ return sb.toString();@b@ }@b@}