ca.uhn.hl7v2.hoh.util.HTTPUtils Maven / Gradle / Ivy
package ca.uhn.hl7v2.hoh.util;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import javax.servlet.ServletOutputStream;
import ca.uhn.hl7v2.hoh.util.repackage.Base64;
public class HTTPUtils {
/** ISO-8859-1 */
public static final Charset DEFAULT_CHARSET;
static {
DEFAULT_CHARSET = Charset.forName("ISO-8859-1");
}
/**
* Non instantiable
*/
private HTTPUtils() {
super();
}
public static void write400BadRequest(OutputStream theOutputStream, String theMessage) throws IOException {
write400BadRequest(theOutputStream, theMessage, true);
}
public static void write400BadRequest(OutputStream theOutputStream, String theMessage, boolean theWriteHeaders) throws IOException {
StringBuilder b = new StringBuilder();
if (theWriteHeaders) {
b.append("HTTP/1.1 401 Unauthorized\r\n");
b.append("Content-Type: text/html; charset=ISO-8859-1\r\n");
b.append("\r\n");
}
b.append("400 - Bad Request ");
b.append("");
b.append("");
b.append("HTTP 400 - Bad Request
");
b.append("");
b.append(theMessage);
b.append("
");
b.append("HAPI (HL7 over HTTP) version ");
b.append(VersionLogger.getVersion());
b.append("
");
b.append("");
b.append("");
theOutputStream.write(b.toString().getBytes(DEFAULT_CHARSET));
theOutputStream.flush();
}
public static void write400SignatureVerificationFailed(OutputStream theOutputStream, boolean theWriteHeaders) throws IOException {
StringBuilder b = new StringBuilder();
if (theWriteHeaders) {
b.append("HTTP/1.1 401 Unauthorized\r\n");
b.append("Content-Type: text/html; charset=ISO-8859-1\r\n");
b.append("\r\n");
}
b.append("400 - Bad Request ");
b.append("");
b.append("");
b.append("HTTP 400 - Bad Request
");
b.append("");
b.append("Failed to verify message signature");
b.append("
");
b.append("HAPI (HL7 over HTTP) version ");
b.append(VersionLogger.getVersion());
b.append("
");
b.append("");
b.append("");
theOutputStream.write(b.toString().getBytes(DEFAULT_CHARSET));
theOutputStream.flush();
}
public static void write401Unauthorized(OutputStream theOutputStream) throws IOException {
write401Unauthorized(theOutputStream, true);
}
public static void write401Unauthorized(OutputStream theOutputStream, boolean theWriteHeaders) throws IOException {
StringBuilder b = new StringBuilder();
if (theWriteHeaders) {
b.append("HTTP/1.1 401 Unauthorized\r\n");
b.append("Content-Type: text/html; charset=ISO-8859-1\r\n");
b.append("\r\n");
}
b.append("401 - Not Authorized ");
b.append("");
b.append("");
b.append("HTTP 401 - Not Authorized
");
b.append("HAPI (HL7 over HTTP) version ");
b.append(VersionLogger.getVersion());
b.append("
");
b.append("");
b.append("");
theOutputStream.write(b.toString().getBytes(DEFAULT_CHARSET));
theOutputStream.flush();
}
public static void write500InternalServerError(ServletOutputStream theOutputStream, String theMessage, boolean theWriteHeaders) throws IOException {
StringBuilder b = new StringBuilder();
if (theWriteHeaders) {
b.append("HTTP/1.1 500 Internal Server Error\r\n");
b.append("Content-Type: text/html; charset=ISO-8859-1\r\n");
b.append("\r\n");
}
b.append("HTTP 500 - Internal Server Error ");
b.append("");
b.append("");
b.append("HTTP 500 - Internal Server Error
");
b.append("");
b.append(theMessage);
b.append("
");
b.append("HAPI (HL7 over HTTP) version ");
b.append(VersionLogger.getVersion());
b.append("
");
b.append("");
b.append("");
theOutputStream.write(b.toString().getBytes(DEFAULT_CHARSET));
theOutputStream.flush();
}
}