1 23 24 package com.sun.enterprise.web.connector.grizzly; 25 26 import java.io.IOException ; 27 import java.nio.ByteBuffer ; 28 import java.nio.CharBuffer ; 29 import java.nio.charset.Charset ; 30 import java.nio.charset.CharsetEncoder ; 31 import java.util.Date ; 32 33 import org.apache.catalina.util.ServerInfo; 34 35 40 public class HtmlHelper{ 41 42 43 47 private static CharBuffer reponseBuffer = CharBuffer.allocate(4096); 48 49 50 53 private static CharsetEncoder encoder = 54 Charset.forName("UTF-8").newEncoder(); 55 56 59 private static String NEWLINE = "\r\n"; 60 61 62 65 public final static String OK = "HTTP/1.1 200 OK" + NEWLINE; 66 67 68 71 public final static String BAD_REQUEST 72 = "HTTP/1.1 400 Bad Request" + NEWLINE; 73 74 75 81 public synchronized static ByteBuffer 82 getErrorPage(String message, String code) throws IOException { 83 String body = prepareBody(message); 84 reponseBuffer.clear(); 85 reponseBuffer.put(code); 86 appendHeaderValue("Content-Type", "text/html"); 87 appendHeaderValue("Content-Length", body.getBytes().length + ""); 88 appendHeaderValue("Date", new Date ().toString()); 89 appendHeaderValue("Connection", "Close"); 90 appendHeaderValue("Server", ServerInfo.getServerInfo()); 91 reponseBuffer.put(NEWLINE); 92 reponseBuffer.put(body); 93 reponseBuffer.flip(); 94 return encoder.encode(reponseBuffer); 95 } 96 97 98 101 private static void appendHeaderValue(String name, String value) { 102 reponseBuffer.put(name); 103 reponseBuffer.put(": "); 104 reponseBuffer.put(value); 105 reponseBuffer.put(NEWLINE); 106 } 107 108 109 112 private static String prepareBody(String message){ 113 StringBuffer sb = new StringBuffer (); 114 115 sb.append("<html><head><title>"); 116 sb.append(ServerInfo.getServerInfo()); 117 sb.append("</title>"); 118 sb.append("<style><!--"); 119 sb.append(org.apache.catalina.util.TomcatCSS.TOMCAT_CSS); 120 sb.append("--></style> "); 121 sb.append("</head><body>"); 122 sb.append("<h1>"); 123 sb.append(message); 124 sb.append("</h1>"); 125 sb.append("<HR size=\"1\" noshade>"); 126 sb.append("<h3>").append(ServerInfo.getServerInfo()).append("</h3>"); 127 sb.append("</body></html>"); 128 return sb.toString(); 129 } 130 131 } 132 | Popular Tags |