001package ca.uhn.hl7v2.hoh.encoder; 002 003import static ca.uhn.hl7v2.hoh.util.StringUtils.*; 004 005import java.io.UnsupportedEncodingException; 006 007import ca.uhn.hl7v2.hoh.util.VersionLogger; 008import ca.uhn.hl7v2.hoh.util.repackage.Base64; 009 010public class Hl7OverHttpRequestEncoder extends AbstractHl7OverHttpEncoder { 011 012 private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(Hl7OverHttpRequestEncoder.class); 013 private String myHost; 014 private int myPort; 015 016 public void setHost(String theHost) { 017 myHost = theHost; 018 } 019 020 public void setPort(int thePort) { 021 myPort = thePort; 022 } 023 private boolean myAcceptGzip = false; 024 025 @Override 026 protected void addSpecificHeaders() { 027 if (isNotBlank(getUsername()) && isNotBlank(getPassword())) { 028 String authorizationUnescaped = defaultString(getUsername()) + ":" + defaultString(getPassword()); 029 String encoded; 030 try { 031 encoded = Base64.encodeBase64String(authorizationUnescaped.getBytes("ISO-8859-1")); 032 } catch (UnsupportedEncodingException e) { 033 throw new Error("Could not find US-ASCII encoding. This shouldn't happen!"); 034 } 035 getHeaders().put("Authorization", "Basic " + encoded); 036 } 037 038 if (myAcceptGzip) { 039 getHeaders().put("Accept-Encoding", "gzip"); 040 } 041 getHeaders().put("User-Agent", "HAPI (HL7 over HTTP) Client " + VersionLogger.getVersion()); 042 043 StringBuilder hostBuilder = new StringBuilder(); 044 if (isNotBlank(myHost)) { 045 hostBuilder.append(myHost); 046 if (myPort > 0) { 047 hostBuilder.append(":"); 048 hostBuilder.append(myPort); 049 } else { 050 ourLog.warn("Host has been set, but port has not"); 051 } 052 } else { 053 ourLog.warn("Host has not been set"); 054 } 055 getHeaders().put("Host", hostBuilder.toString()); 056 } 057 058 @Override 059 protected void setActionLineAppropriately() { 060 setActionLine("POST " + getPath() + " HTTP/1.1"); 061 } 062 063}