1 package ca.uhn.hl7v2.hoh.encoder;
2
3 import static ca.uhn.hl7v2.hoh.util.StringUtils.*;
4
5 import java.io.UnsupportedEncodingException;
6
7 import ca.uhn.hl7v2.hoh.util.VersionLogger;
8 import ca.uhn.hl7v2.hoh.util.repackage.Base64;
9
10 public class Hl7OverHttpRequestEncoder extends AbstractHl7OverHttpEncoder {
11
12 private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(Hl7OverHttpRequestEncoder.class);
13 private String myHost;
14 private int myPort;
15
16 public void setHost(String theHost) {
17 myHost = theHost;
18 }
19
20 public void setPort(int thePort) {
21 myPort = thePort;
22 }
23 private boolean myAcceptGzip = false;
24
25 @Override
26 protected void addSpecificHeaders() {
27 if (isNotBlank(getUsername()) && isNotBlank(getPassword())) {
28 String authorizationUnescaped = defaultString(getUsername()) + ":" + defaultString(getPassword());
29 String encoded;
30 try {
31 encoded = Base64.encodeBase64String(authorizationUnescaped.getBytes("ISO-8859-1"));
32 } catch (UnsupportedEncodingException e) {
33 throw new Error("Could not find US-ASCII encoding. This shouldn't happen!");
34 }
35 getHeaders().put("Authorization", "Basic " + encoded);
36 }
37
38 if (myAcceptGzip) {
39 getHeaders().put("Accept-Encoding", "gzip");
40 }
41 getHeaders().put("User-Agent", "HAPI (HL7 over HTTP) Client " + VersionLogger.getVersion());
42
43 StringBuilder hostBuilder = new StringBuilder();
44 if (isNotBlank(myHost)) {
45 hostBuilder.append(myHost);
46 if (myPort > 0) {
47 hostBuilder.append(":");
48 hostBuilder.append(myPort);
49 } else {
50 ourLog.warn("Host has been set, but port has not");
51 }
52 } else {
53 ourLog.warn("Host has not been set");
54 }
55 getHeaders().put("Host", hostBuilder.toString());
56 }
57
58 @Override
59 protected void setActionLineAppropriately() {
60 setActionLine("POST " + getPath() + " HTTP/1.1");
61 }
62
63 }