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