View Javadoc
1   package ca.uhn.hl7v2.hoh.util;
2   
3   import ca.uhn.hl7v2.hoh.util.repackage.Base64;
4   import jakarta.servlet.ServletOutputStream;
5   
6   import java.io.IOException;
7   import java.io.OutputStream;
8   import java.nio.charset.Charset;
9   import java.nio.charset.StandardCharsets;
10  
11  
12  public class HTTPUtils {
13  
14  	/** ISO-8859-1 */
15  	public static final Charset DEFAULT_CHARSET;
16  
17  	static {
18  		DEFAULT_CHARSET = StandardCharsets.ISO_8859_1;
19  	}
20  
21  	/**
22  	 * Non instantiable
23  	 */
24  	private HTTPUtils() {
25  		super();
26  	}
27  
28  	public static void write400BadRequest(OutputStream theOutputStream, String theMessage) throws IOException {
29  		write400BadRequest(theOutputStream, theMessage, true);
30  	}
31  
32  	public static void write400BadRequest(OutputStream theOutputStream, String theMessage, boolean theWriteHeaders) throws IOException {
33  		StringBuilder b = new StringBuilder();
34  		if (theWriteHeaders) {
35  			b.append("HTTP/1.1 400 Bad Request\r\n");
36  			b.append("Content-Type: text/html; charset=ISO-8859-1\r\n");
37  			b.append("\r\n");
38  		}
39  		b.append("<html><head><title>400 - Bad Request</title></head>");
40  		b.append("<body>");
41  		b.append("<img src=\"data:image/png;base64,");
42  		b.append(Base64.encodeBase64String(IOUtils.readClasspathIntoByteArray("/ca/uhn/hl7v2/hoh/hapi_hoh_banner.png")));
43  		b.append("\"/>");
44  		b.append("<h1>HTTP 400 - Bad Request</h1>");
45  		b.append("<p>");
46  		b.append(theMessage);
47  		b.append("</p>");
48  		b.append("<p style=\"font-size: 0.7em; color: #606060;\">HAPI (HL7 over HTTP) version ");
49  		b.append(VersionLogger.getVersion());
50  		b.append("</p>");
51  		b.append("</body>");
52  		b.append("</html>");
53  
54  		theOutputStream.write(b.toString().getBytes(DEFAULT_CHARSET));
55  		theOutputStream.flush();
56  	}
57  
58  	public static void write400SignatureVerificationFailed(OutputStream theOutputStream, boolean theWriteHeaders) throws IOException {
59  		StringBuilder b = new StringBuilder();
60  		if (theWriteHeaders) {
61  			b.append("HTTP/1.1 400 Bad Request\r\n");
62  			b.append("Content-Type: text/html; charset=ISO-8859-1\r\n");
63  			b.append("\r\n");
64  		}
65  		b.append("<html><head><title>400 - Bad Request</title></head>");
66  		b.append("<body>");
67  		b.append("<img src=\"data:image/png;base64,");
68  		b.append(Base64.encodeBase64String(IOUtils.readClasspathIntoByteArray("/ca/uhn/hl7v2/hoh/hapi_hoh_banner.png")));
69  		b.append("\"/>");
70  		b.append("<h1>HTTP 400 - Bad Request</h1>");
71  		b.append("<p>");
72  		b.append("Failed to verify message signature");
73  		b.append("</p>");
74  		b.append("<p style=\"font-size: 0.7em; color: #606060;\">HAPI (HL7 over HTTP) version ");
75  		b.append(VersionLogger.getVersion());
76  		b.append("</p>");
77  		b.append("</body>");
78  		b.append("</html>");
79  
80  		theOutputStream.write(b.toString().getBytes(DEFAULT_CHARSET));
81  		theOutputStream.flush();
82  	}
83  
84  	public static void write401Unauthorized(OutputStream theOutputStream) throws IOException {
85  		write401Unauthorized(theOutputStream, true);
86  	}
87  
88  	public static void write401Unauthorized(OutputStream theOutputStream, boolean theWriteHeaders) throws IOException {
89  		StringBuilder b = new StringBuilder();
90  		if (theWriteHeaders) {
91  			b.append("HTTP/1.1 401 Unauthorized\r\n");
92  			b.append("Content-Type: text/html; charset=ISO-8859-1\r\n");
93  			b.append("\r\n");
94  		}
95  		b.append("<html><head><title>401 - Not Authorized</title></head>");
96  		b.append("<body>");
97  		b.append("<img src=\"data:image/png;base64,");
98  		b.append(Base64.encodeBase64String(IOUtils.readClasspathIntoByteArray("/ca/uhn/hl7v2/hoh/hapi_hoh_banner.png")));
99  		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 }