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