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 | 230 | public class Hl7OverHttpRequestEncoder extends AbstractHl7OverHttpEncoder { |
11 | |
|
12 | 5 | 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 | 115 | myHost = theHost; |
18 | 115 | } |
19 | |
|
20 | |
public void setPort(int thePort) { |
21 | 115 | myPort = thePort; |
22 | 115 | } |
23 | 230 | private boolean myAcceptGzip = false; |
24 | |
|
25 | |
@Override |
26 | |
protected void addSpecificHeaders() { |
27 | 230 | if (isNotBlank(getUsername()) && isNotBlank(getPassword())) { |
28 | 205 | String authorizationUnescaped = defaultString(getUsername()) + ":" + defaultString(getPassword()); |
29 | |
String encoded; |
30 | |
try { |
31 | 205 | encoded = Base64.encodeBase64String(authorizationUnescaped.getBytes("ISO-8859-1")); |
32 | 0 | } catch (UnsupportedEncodingException e) { |
33 | 0 | throw new Error("Could not find US-ASCII encoding. This shouldn't happen!"); |
34 | 205 | } |
35 | 205 | getHeaders().put("Authorization", "Basic " + encoded); |
36 | |
} |
37 | |
|
38 | 230 | if (myAcceptGzip) { |
39 | 0 | getHeaders().put("Accept-Encoding", "gzip"); |
40 | |
} |
41 | 230 | getHeaders().put("User-Agent", "HAPI (HL7 over HTTP) Client " + VersionLogger.getVersion()); |
42 | |
|
43 | 230 | StringBuilder hostBuilder = new StringBuilder(); |
44 | 230 | if (isNotBlank(myHost)) { |
45 | 115 | hostBuilder.append(myHost); |
46 | 115 | if (myPort > 0) { |
47 | 115 | hostBuilder.append(":"); |
48 | 115 | hostBuilder.append(myPort); |
49 | |
} else { |
50 | 0 | ourLog.warn("Host has been set, but port has not"); |
51 | |
} |
52 | |
} else { |
53 | 115 | ourLog.warn("Host has not been set"); |
54 | |
} |
55 | 230 | getHeaders().put("Host", hostBuilder.toString()); |
56 | 230 | } |
57 | |
|
58 | |
@Override |
59 | |
protected void setActionLineAppropriately() { |
60 | 230 | setActionLine("POST " + getPath() + " HTTP/1.1"); |
61 | 230 | } |
62 | |
|
63 | |
} |