001package ca.uhn.hl7v2.hoh.util; 002 003import java.io.IOException; 004import java.io.OutputStream; 005import java.nio.charset.Charset; 006 007import javax.servlet.ServletOutputStream; 008 009import ca.uhn.hl7v2.hoh.util.repackage.Base64; 010 011 012public class HTTPUtils { 013 014 /** ISO-8859-1 */ 015 public static final Charset DEFAULT_CHARSET; 016 017 static { 018 DEFAULT_CHARSET = Charset.forName("ISO-8859-1"); 019 } 020 021 /** 022 * Non instantiable 023 */ 024 private HTTPUtils() { 025 super(); 026 } 027 028 public static void write400BadRequest(OutputStream theOutputStream, String theMessage) throws IOException { 029 write400BadRequest(theOutputStream, theMessage, true); 030 } 031 032 public static void write400BadRequest(OutputStream theOutputStream, String theMessage, boolean theWriteHeaders) throws IOException { 033 StringBuilder b = new StringBuilder(); 034 if (theWriteHeaders) { 035 b.append("HTTP/1.1 401 Unauthorized\r\n"); 036 b.append("Content-Type: text/html; charset=ISO-8859-1\r\n"); 037 b.append("\r\n"); 038 } 039 b.append("<html><head><title>400 - Bad Request</title></head>"); 040 b.append("<body>"); 041 b.append("<img src=\"data:image/png;base64,"); 042 b.append(Base64.encodeBase64String(IOUtils.readClasspathIntoByteArray("/ca/uhn/hl7v2/hoh/hapi_hoh_banner.png"))); 043 b.append("\"/>"); 044 b.append("<h1>HTTP 400 - Bad Request</h1>"); 045 b.append("<p>"); 046 b.append(theMessage); 047 b.append("</p>"); 048 b.append("<p style=\"font-size: 0.7em; color: #606060;\">HAPI (HL7 over HTTP) version "); 049 b.append(VersionLogger.getVersion()); 050 b.append("</p>"); 051 b.append("</body>"); 052 b.append("</html>"); 053 054 theOutputStream.write(b.toString().getBytes(DEFAULT_CHARSET)); 055 theOutputStream.flush(); 056 } 057 058 public static void write400SignatureVerificationFailed(OutputStream theOutputStream, boolean theWriteHeaders) throws IOException { 059 StringBuilder b = new StringBuilder(); 060 if (theWriteHeaders) { 061 b.append("HTTP/1.1 401 Unauthorized\r\n"); 062 b.append("Content-Type: text/html; charset=ISO-8859-1\r\n"); 063 b.append("\r\n"); 064 } 065 b.append("<html><head><title>400 - Bad Request</title></head>"); 066 b.append("<body>"); 067 b.append("<img src=\"data:image/png;base64,"); 068 b.append(Base64.encodeBase64String(IOUtils.readClasspathIntoByteArray("/ca/uhn/hl7v2/hoh/hapi_hoh_banner.png"))); 069 b.append("\"/>"); 070 b.append("<h1>HTTP 400 - Bad Request</h1>"); 071 b.append("<p>"); 072 b.append("Failed to verify message signature"); 073 b.append("</p>"); 074 b.append("<p style=\"font-size: 0.7em; color: #606060;\">HAPI (HL7 over HTTP) version "); 075 b.append(VersionLogger.getVersion()); 076 b.append("</p>"); 077 b.append("</body>"); 078 b.append("</html>"); 079 080 theOutputStream.write(b.toString().getBytes(DEFAULT_CHARSET)); 081 theOutputStream.flush(); 082 } 083 084 public static void write401Unauthorized(OutputStream theOutputStream) throws IOException { 085 write401Unauthorized(theOutputStream, true); 086 } 087 088 public static void write401Unauthorized(OutputStream theOutputStream, boolean theWriteHeaders) throws IOException { 089 StringBuilder b = new StringBuilder(); 090 if (theWriteHeaders) { 091 b.append("HTTP/1.1 401 Unauthorized\r\n"); 092 b.append("Content-Type: text/html; charset=ISO-8859-1\r\n"); 093 b.append("\r\n"); 094 } 095 b.append("<html><head><title>401 - Not Authorized</title></head>"); 096 b.append("<body>"); 097 b.append("<img src=\"data:image/png;base64,"); 098 b.append(Base64.encodeBase64String(IOUtils.readClasspathIntoByteArray("/ca/uhn/hl7v2/hoh/hapi_hoh_banner.png"))); 099 b.append("\"/>"); 100 b.append("<h1>HTTP 401 - Not Authorized</h1>"); 101 b.append("<p style=\"font-size: 0.7em; color: #606060;\">HAPI (HL7 over HTTP) version "); 102 b.append(VersionLogger.getVersion()); 103 b.append("</p>"); 104 b.append("</body>"); 105 b.append("</html>"); 106 107 theOutputStream.write(b.toString().getBytes(DEFAULT_CHARSET)); 108 theOutputStream.flush(); 109 } 110 111 public static void write500InternalServerError(ServletOutputStream theOutputStream, String theMessage, boolean theWriteHeaders) throws IOException { 112 StringBuilder b = new StringBuilder(); 113 if (theWriteHeaders) { 114 b.append("HTTP/1.1 500 Internal Server Error\r\n"); 115 b.append("Content-Type: text/html; charset=ISO-8859-1\r\n"); 116 b.append("\r\n"); 117 } 118 b.append("<html><head><title>HTTP 500 - Internal Server Error</title></head>"); 119 b.append("<body>"); 120 b.append("<img src=\"data:image/png;base64,"); 121 b.append(Base64.encodeBase64String(IOUtils.readClasspathIntoByteArray("/ca/uhn/hl7v2/hoh/hapi_hoh_banner.png"))); 122 b.append("\"/>"); 123 b.append("<h1>HTTP 500 - Internal Server Error</h1>"); 124 b.append("<p>"); 125 b.append(theMessage); 126 b.append("</p>"); 127 b.append("<p style=\"font-size: 0.7em; color: #606060;\">HAPI (HL7 over HTTP) version "); 128 b.append(VersionLogger.getVersion()); 129 b.append("</p>"); 130 b.append("</body>"); 131 b.append("</html>"); 132 133 theOutputStream.write(b.toString().getBytes(DEFAULT_CHARSET)); 134 theOutputStream.flush(); 135 } 136 137}