1 /* 2 * $Id$ 3 * 4 * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved. 5 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 6 * 7 * This code is free software; you can redistribute it and/or modify it 8 * under the terms of the GNU General Public License version 2 only, as 9 * published by the Free Software Foundation. Oracle designates this 10 * particular file as subject to the "Classpath" exception as provided 11 * by Oracle in the LICENSE file that accompanied this code. 12 * 13 * This code is distributed in the hope that it will be useful, but WITHOUT 14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 16 * version 2 for more details (a copy is included in the LICENSE file that 17 * accompanied this code). 18 * 19 * You should have received a copy of the GNU General Public License version 20 * 2 along with this work; if not, write to the Free Software Foundation, 21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 22 * 23 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 24 * or visit www.oracle.com if you need additional information or have any 25 * questions. 26 */ 27 package com.sun.javatest.httpd; 28 29 import java.io.PrintWriter; 30 import java.text.DateFormat; 31 import java.util.Date; 32 import java.util.Dictionary; 33 import java.util.Enumeration; 34 import java.util.TimeZone; 35 36 import com.sun.javatest.util.I18NResourceBundle; 37 38 /** 39 * Utility methods to make it easier to generate HTTP/HTML. 40 */ 41 42 public class PageGenerator { 43 /** 44 * Generate the correct HTTP header for a sucessful request (200) 45 */ 46 public static void generateOkHttp(PrintWriter out) { 47 out.println(HTTP_OK); 48 genServerHdr(out); 49 out.println(HTTP_CONTENT_HTML); 50 out.println(); 51 } 52 53 /** 54 * Generate the correct HTTP header for a bad request (400) 55 */ 56 public static void generateBadHttp(PrintWriter out) { 57 out.println(HTTP_BAD); 58 genServerHdr(out); 59 out.println(HTTP_CONTENT_HTML); 60 } 61 62 /** 63 * Generate the content type header line. 64 * 65 * @param version The HTML spec. version you wish to use. 66 */ 67 public static void generateDocType(PrintWriter out, int version) { 68 switch (version) { 69 case HTML32: out.println(HTML32_ID); break; 70 case HTML40_TRANS: out.println(HTML40_TRANS_ID); break; 71 } 72 } 73 74 /** 75 * Creates a plain header, with only a title. 76 */ 77 public static void writeHeader(PrintWriter out, String title) { 78 out.println ("<Head>"); 79 out.print ("<Title>"); 80 out.print (title); 81 out.println ("</Title>"); 82 out.println ("</Head>"); 83 } 84 85 public static void writeBeginDoc(PrintWriter out) { 86 out.println("<html>"); 87 } 88 89 /** 90 * Writes the address info 91 */ 92 public static void writeEndDoc(PrintWriter out) { 93 out.println("</html>"); 94 } 95 96 public static void startBody(PrintWriter out) { 97 out.println("<Body>"); 98 } 99 100 public static void endBody(PrintWriter out) { 101 out.println("</Body>"); 102 } 103 104 /** 105 * Writes the address block and other info at the end of the body. 106 */ 107 public static void writeFooter(PrintWriter out) { 108 out.println(dateFormat.format(new Date())); 109 out.println("<Address>"); 110 111 out.print(i18n.getString("generator.produced.txt")); 112 out.print(swName); 113 out.print(" "); 114 out.println(swVersion); 115 116 out.print(i18n.getString("generator.built.txt")); 117 out.println(swBuildDate); 118 out.println("</Address>"); 119 } 120 121 /** 122 * Prints the contents of any dictionary in a two column table. 123 */ 124 public static void writeDictionary(PrintWriter out, Dictionary<String, String> dict, 125 String keyHeader, String valHeader) { 126 // XXX should include HTML filtering of strings 127 128 if (keyHeader == null) keyHeader = "Key"; 129 if (valHeader == null) valHeader = "Value"; 130 131 out.println("<Table Border>"); 132 133 StringBuffer buf = new StringBuffer(50); 134 135 // write the table header 136 buf.append("<tr><th>"); 137 buf.append(keyHeader); 138 buf.append("<th>"); 139 buf.append(valHeader); 140 buf.append("</tr>"); 141 out.println(buf.toString()); 142 143 if (dict == null || dict.size() == 0) { 144 // no values to write, fill the space 145 buf.setLength(0); 146 buf.append("<tr><td colspan=2>"); 147 buf.append("-EMPTY-"); 148 buf.append("</tr>"); 149 } 150 else { 151 Enumeration<?> keys = dict.keys(); 152 while (keys.hasMoreElements()) { 153 Object key = keys.nextElement(); 154 out.println("<tr>"); 155 buf.setLength(0); 156 buf.append("<td>"); 157 buf.append(key.toString()); 158 buf.append("<td>"); 159 buf.append((dict.get(key)).toString()); 160 out.println(buf.toString()); 161 out.println("</tr>"); 162 } // while 163 } 164 165 out.println("</Table>"); 166 } 167 168 public static void startTable(PrintWriter out, boolean borders) { 169 out.print("<Table"); 170 if (borders) out.print(" Border"); 171 172 out.println(">"); 173 } 174 175 public static void endTable(PrintWriter out) { 176 out.println("</Table>"); 177 } 178 179 public static String getSWBuildDate() { 180 return swBuildDate; 181 } 182 183 public static String getSWName() { 184 return swName; 185 } 186 187 public static String getSWVersion() { 188 return swVersion; 189 } 190 191 // --- set environment info --- 192 public static void setSWBuildDate(String date) { 193 swBuildDate = date; 194 } 195 196 public static void setSWName(String name) { 197 swName = name; 198 } 199 200 public static void setSWVersion(String ver) { 201 swVersion = ver; 202 } 203 204 // *********** PRIVATE *************** 205 206 private static void genServerHdr(PrintWriter out) { 207 if (swName != null) { 208 out.print("Server: "); 209 out.print(swName); 210 if (swVersion != null) { 211 out.print("/"); 212 out.print(swVersion); 213 } 214 215 if (swBuildDate != null) { 216 out.print(" built "); 217 out.print(swBuildDate); 218 } 219 } 220 221 out.println(); 222 out.print("Date: "); 223 out.println(dateFormat.format(new Date())); 224 } 225 226 private static DateFormat dateFormat; 227 private static String swBuildDate; 228 private static String swName; 229 private static String swVersion; 230 private static I18NResourceBundle i18n = I18NResourceBundle.getBundleForClass(PageGenerator.class); 231 232 private static final String TIMEZONE = "UTC"; 233 234 public static final int HTML32 = 0; 235 public static final int HTML40_TRANS = 1; 236 237 private static final String HTML32_ID = "<!DOCTYPE HTML>"; 238 private static final String HTML40_TRANS_ID = "<!DOCTYPE HTML>"; 239 240 private static final String HTTP_CONTENT_TYPE = "Content-Type: "; 241 private static final String HTTP_OK = "HTTP/1.1 200 OK"; 242 private static final String HTTP_BAD = "HTTP/1.1 400 Bad Request"; 243 private static final String HTTP_CONTENT_HTML = HTTP_CONTENT_TYPE + "text/html"; 244 245 static { 246 dateFormat = DateFormat.getDateTimeInstance(DateFormat.FULL, 247 DateFormat.LONG); 248 dateFormat.setTimeZone(TimeZone.getTimeZone(TIMEZONE)); 249 } 250 251 } 252