KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > web > connector > grizzly > HtmlHelper


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.web.connector.grizzly;
25
26 import java.io.IOException JavaDoc;
27 import java.nio.ByteBuffer JavaDoc;
28 import java.nio.CharBuffer JavaDoc;
29 import java.nio.charset.Charset JavaDoc;
30 import java.nio.charset.CharsetEncoder JavaDoc;
31 import java.util.Date JavaDoc;
32
33 import org.apache.catalina.util.ServerInfo;
34
35 /**
36  * Utility class used to generate HTML pages.
37  *
38  * @author Jean-Francois Arcand
39  */

40 public class HtmlHelper{
41
42
43     /**
44      * <code>CharBuffer</code> used to store the HTML response, containing
45      * the headers and the body of the response.
46      */

47     private static CharBuffer JavaDoc reponseBuffer = CharBuffer.allocate(4096);
48     
49
50     /**
51      * Encoder used to encode the HTML response
52      */

53     private static CharsetEncoder JavaDoc encoder =
54                                           Charset.forName("UTF-8").newEncoder();
55
56     /**
57      * HTTP end line.
58      */

59     private static String JavaDoc NEWLINE = "\r\n";
60
61
62     /**
63      * HTTP OK header
64      */

65     public final static String JavaDoc OK = "HTTP/1.1 200 OK" + NEWLINE;
66     
67
68     /**
69      * HTTP Bas Request
70      */

71     public final static String JavaDoc BAD_REQUEST
72         = "HTTP/1.1 400 Bad Request" + NEWLINE;
73  
74     
75     /**
76      * When Grizzlu has reached its connection-queue pool limits, an HTML
77      * error pages will to be returned to the clients.
78      *
79      * @return A <code>ByteBuffer</code> containings the HTTP response.
80      */

81     public synchronized static ByteBuffer JavaDoc
82             getErrorPage(String JavaDoc message, String JavaDoc code) throws IOException JavaDoc {
83         String JavaDoc 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 JavaDoc().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     /**
99      * Utility to add headers to the HTTP response.
100      */

101     private static void appendHeaderValue(String JavaDoc name, String JavaDoc value) {
102         reponseBuffer.put(name);
103         reponseBuffer.put(": ");
104         reponseBuffer.put(value);
105         reponseBuffer.put(NEWLINE);
106     }
107
108
109     /**
110      * Prepare the HTTP body containing the error messages.
111      */

112     private static String JavaDoc prepareBody(String JavaDoc message){
113         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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