Coverage Report - ca.uhn.hl7v2.hoh.util.HTTPUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
HTTPUtils
2%
2/93
0%
0/8
1.571
 
 1  
 package ca.uhn.hl7v2.hoh.util;
 2  
 
 3  
 import java.io.IOException;
 4  
 import java.io.OutputStream;
 5  
 import java.nio.charset.Charset;
 6  
 
 7  
 import javax.servlet.ServletOutputStream;
 8  
 
 9  
 import ca.uhn.hl7v2.hoh.util.repackage.Base64;
 10  
 
 11  
 
 12  
 public class HTTPUtils {
 13  
 
 14  
         /** ISO-8859-1 */
 15  
         public static final Charset DEFAULT_CHARSET;
 16  
 
 17  
         static {
 18  5
                 DEFAULT_CHARSET = Charset.forName("ISO-8859-1");
 19  5
         }
 20  
 
 21  
         /**
 22  
          * Non instantiable
 23  
          */
 24  
         private HTTPUtils() {
 25  0
                 super();
 26  0
         }
 27  
 
 28  
         public static void write400BadRequest(OutputStream theOutputStream, String theMessage) throws IOException {
 29  0
                 write400BadRequest(theOutputStream, theMessage, true);
 30  0
         }
 31  
 
 32  
         public static void write400BadRequest(OutputStream theOutputStream, String theMessage, boolean theWriteHeaders) throws IOException {
 33  0
                 StringBuilder b = new StringBuilder();
 34  0
                 if (theWriteHeaders) {
 35  0
                         b.append("HTTP/1.1 401 Unauthorized\r\n");
 36  0
                         b.append("Content-Type: text/html; charset=ISO-8859-1\r\n");
 37  0
                         b.append("\r\n");
 38  
                 }
 39  0
                 b.append("<html><head><title>400 - Bad Request</title></head>");
 40  0
                 b.append("<body>");
 41  0
                 b.append("<img src=\"data:image/png;base64,");
 42  0
                 b.append(Base64.encodeBase64String(IOUtils.readClasspathIntoByteArray("/ca/uhn/hl7v2/hoh/hapi_hoh_banner.png")));
 43  0
                 b.append("\"/>");
 44  0
                 b.append("<h1>HTTP 400 - Bad Request</h1>");
 45  0
                 b.append("<p>");
 46  0
                 b.append(theMessage);
 47  0
                 b.append("</p>");
 48  0
                 b.append("<p style=\"font-size: 0.7em; color: #606060;\">HAPI (HL7 over HTTP) version ");
 49  0
                 b.append(VersionLogger.getVersion());
 50  0
                 b.append("</p>");
 51  0
                 b.append("</body>");
 52  0
                 b.append("</html>");
 53  
 
 54  0
                 theOutputStream.write(b.toString().getBytes(DEFAULT_CHARSET));
 55  0
                 theOutputStream.flush();
 56  0
         }
 57  
 
 58  
         public static void write400SignatureVerificationFailed(OutputStream theOutputStream, boolean theWriteHeaders) throws IOException {
 59  0
                 StringBuilder b = new StringBuilder();
 60  0
                 if (theWriteHeaders) {
 61  0
                         b.append("HTTP/1.1 401 Unauthorized\r\n");
 62  0
                         b.append("Content-Type: text/html; charset=ISO-8859-1\r\n");
 63  0
                         b.append("\r\n");
 64  
                 }
 65  0
                 b.append("<html><head><title>400 - Bad Request</title></head>");
 66  0
                 b.append("<body>");
 67  0
                 b.append("<img src=\"data:image/png;base64,");
 68  0
                 b.append(Base64.encodeBase64String(IOUtils.readClasspathIntoByteArray("/ca/uhn/hl7v2/hoh/hapi_hoh_banner.png")));
 69  0
                 b.append("\"/>");
 70  0
                 b.append("<h1>HTTP 400 - Bad Request</h1>");
 71  0
                 b.append("<p>");
 72  0
                 b.append("Failed to verify message signature");
 73  0
                 b.append("</p>");
 74  0
                 b.append("<p style=\"font-size: 0.7em; color: #606060;\">HAPI (HL7 over HTTP) version ");
 75  0
                 b.append(VersionLogger.getVersion());
 76  0
                 b.append("</p>");
 77  0
                 b.append("</body>");
 78  0
                 b.append("</html>");
 79  
 
 80  0
                 theOutputStream.write(b.toString().getBytes(DEFAULT_CHARSET));
 81  0
                 theOutputStream.flush();
 82  0
         }
 83  
 
 84  
         public static void write401Unauthorized(OutputStream theOutputStream) throws IOException {
 85  0
                 write401Unauthorized(theOutputStream, true);
 86  0
         }
 87  
 
 88  
         public static void write401Unauthorized(OutputStream theOutputStream, boolean theWriteHeaders) throws IOException {
 89  0
                 StringBuilder b = new StringBuilder();
 90  0
                 if (theWriteHeaders) {
 91  0
                         b.append("HTTP/1.1 401 Unauthorized\r\n");
 92  0
                         b.append("Content-Type: text/html; charset=ISO-8859-1\r\n");
 93  0
                         b.append("\r\n");
 94  
                 }
 95  0
                 b.append("<html><head><title>401 - Not Authorized</title></head>");
 96  0
                 b.append("<body>");
 97  0
                 b.append("<img src=\"data:image/png;base64,");
 98  0
                 b.append(Base64.encodeBase64String(IOUtils.readClasspathIntoByteArray("/ca/uhn/hl7v2/hoh/hapi_hoh_banner.png")));
 99  0
                 b.append("\"/>");
 100  0
                 b.append("<h1>HTTP 401 - Not Authorized</h1>");
 101  0
                 b.append("<p style=\"font-size: 0.7em; color: #606060;\">HAPI (HL7 over HTTP) version ");
 102  0
                 b.append(VersionLogger.getVersion());
 103  0
                 b.append("</p>");
 104  0
                 b.append("</body>");
 105  0
                 b.append("</html>");
 106  
 
 107  0
                 theOutputStream.write(b.toString().getBytes(DEFAULT_CHARSET));
 108  0
                 theOutputStream.flush();
 109  0
         }
 110  
 
 111  
         public static void write500InternalServerError(ServletOutputStream theOutputStream, String theMessage, boolean theWriteHeaders) throws IOException {
 112  0
                 StringBuilder b = new StringBuilder();
 113  0
                 if (theWriteHeaders) {
 114  0
                         b.append("HTTP/1.1 500 Internal Server Error\r\n");
 115  0
                         b.append("Content-Type: text/html; charset=ISO-8859-1\r\n");
 116  0
                         b.append("\r\n");
 117  
                 }
 118  0
                 b.append("<html><head><title>HTTP 500 - Internal Server Error</title></head>");
 119  0
                 b.append("<body>");
 120  0
                 b.append("<img src=\"data:image/png;base64,");
 121  0
                 b.append(Base64.encodeBase64String(IOUtils.readClasspathIntoByteArray("/ca/uhn/hl7v2/hoh/hapi_hoh_banner.png")));
 122  0
                 b.append("\"/>");
 123  0
                 b.append("<h1>HTTP 500 - Internal Server Error</h1>");
 124  0
                 b.append("<p>");
 125  0
                 b.append(theMessage);
 126  0
                 b.append("</p>");
 127  0
                 b.append("<p style=\"font-size: 0.7em; color: #606060;\">HAPI (HL7 over HTTP) version ");
 128  0
                 b.append(VersionLogger.getVersion());
 129  0
                 b.append("</p>");
 130  0
                 b.append("</body>");
 131  0
                 b.append("</html>");
 132  
 
 133  0
                 theOutputStream.write(b.toString().getBytes(DEFAULT_CHARSET));
 134  0
                 theOutputStream.flush();
 135  0
         }
 136  
 
 137  
 }