Coverage Report - ca.uhn.hl7v2.hoh.encoder.AbstractHl7OverHttp
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractHl7OverHttp
93%
41/44
50%
4/8
1.333
 
 1  
 package ca.uhn.hl7v2.hoh.encoder;
 2  
 
 3  
 import java.nio.charset.Charset;
 4  
 import java.util.LinkedHashMap;
 5  
 import java.util.Map;
 6  
 
 7  
 import ca.uhn.hl7v2.hoh.sign.ISigner;
 8  
 import ca.uhn.hl7v2.hoh.util.StringUtils;
 9  
 import ca.uhn.hl7v2.hoh.util.VersionLogger;
 10  
 
 11  
 abstract class AbstractHl7OverHttp {
 12  
         public static final String HTTP_HEADER_HL7_SIGNATURE = "HL7-Signature";
 13  5
         public static final String HTTP_HEADER_HL7_SIGNATURE_LC = HTTP_HEADER_HL7_SIGNATURE.toLowerCase();
 14  
 
 15  
         protected static final Charset ourDefaultCharset;
 16  
 
 17  
         static {
 18  5
                 ourDefaultCharset = Charset.forName("UTF-8");
 19  5
                 VersionLogger.init();
 20  5
         }
 21  
         
 22  
         private Charset myCharset;
 23  
         private boolean myCharsetExplicitlySet;
 24  
         private byte[] myData;
 25  
         private LinkedHashMap<String, String> myHeaders;
 26  
         private String myMessage;
 27  
         private String myPassword;
 28  
         private ISigner mySigner;
 29  
         private String myPath;
 30  
         private boolean myUsed;
 31  
         private String myUsername;
 32  
 
 33  
         /**
 34  
          * Constructor
 35  
          */
 36  900
         public AbstractHl7OverHttp() {
 37  900
                 myCharset = ourDefaultCharset;
 38  900
         }
 39  
 
 40  
         /**
 41  
          * Returns the charset associated with this message. Will not return <code>null</code>.
 42  
          */
 43  
         public Charset getCharset() {
 44  1340
                 return myCharset;
 45  
         }
 46  
 
 47  
         /**
 48  
          * @return the data
 49  
          */
 50  
         public byte[] getData() {
 51  1080
                 return myData;
 52  
         }
 53  
 
 54  
         /**
 55  
          * @return the headers
 56  
          */
 57  
         public Map<String, String> getHeaders() {
 58  5935
                 return myHeaders;
 59  
         }
 60  
 
 61  
         /**
 62  
          * @return the message
 63  
          */
 64  
         public String getMessage() {
 65  2020
                 return myMessage;
 66  
         }
 67  
 
 68  
 
 69  
         /**
 70  
          * @return the password
 71  
          */
 72  
         public String getPassword() {
 73  645
                 return myPassword;
 74  
         }
 75  
 
 76  
         /**
 77  
          * @return the signer
 78  
          */
 79  
         public ISigner getSigner() {
 80  1240
                 return mySigner;
 81  
         }
 82  
 
 83  
         /**
 84  
          * @return the path
 85  
          */
 86  
         public String getPath() {
 87  
                 // TODO: un-url encode this
 88  665
                 return myPath;
 89  
         }
 90  
 
 91  
         /**
 92  
          * @return the path
 93  
          */
 94  
         public String getPathRaw() {
 95  125
                 return myPath;
 96  
         }
 97  
 
 98  
         /**
 99  
          * @return the username
 100  
          */
 101  
         public String getUsername() {
 102  1015
                 return myUsername;
 103  
         }
 104  
 
 105  
         /**
 106  
          * @return Returns <code>true</code> if the charset was explicitly set using
 107  
          *         {@link #setCharset(Charset)}
 108  
          */
 109  
         public boolean isCharsetExplicitlySet() {
 110  60
                 return myCharsetExplicitlySet;
 111  
         }
 112  
 
 113  
         /**
 114  
          * @param theCharset
 115  
          *            The encoding charset to use (default is UTF-8)
 116  
          * @deprecated The HL7 over HTTP specification now mandates the use of UTF-8. <b>Using this method to set a value other than UTF-8 will lead to a non-conformant application</b> 
 117  
          */
 118  
         @Deprecated
 119  
         public void setCharset(Charset theCharset) {
 120  660
                 if (theCharset == null) {
 121  0
                         throw new NullPointerException("Charset can not be null");
 122  
                 }
 123  660
                 myCharsetExplicitlySet = true;
 124  660
                 myCharset = theCharset;
 125  660
         }
 126  
 
 127  
         /**
 128  
          * @param theData
 129  
          *            the data to set
 130  
          */
 131  
         protected void setData(byte[] theData) {
 132  390
                 myData = theData;
 133  390
         }
 134  
 
 135  
         /**
 136  
          * @param theHeaders
 137  
          *            the headers to set
 138  
          */
 139  
         public void setHeaders(LinkedHashMap<String, String> theHeaders) {
 140  805
                 myHeaders = theHeaders;
 141  805
         }
 142  
 
 143  
         /**
 144  
          * @param theMessage
 145  
          *            The raw message text
 146  
          */
 147  
         public void setMessage(String theMessage) {
 148  690
                 myMessage = theMessage;
 149  690
         }
 150  
 
 151  
         /**
 152  
          * @param thePassword
 153  
          *            The authorization password
 154  
          */
 155  
         public void setPassword(String thePassword) {
 156  455
                 myPassword = thePassword;
 157  455
         }
 158  
 
 159  
         /**
 160  
          * Optionally may be used to provide a signer implementation which signs HL7
 161  
          * content.
 162  
          * 
 163  
          * @see ISigner
 164  
          * @see StandardMessageSigner
 165  
          */
 166  
         public void setSigner(ISigner theSigner) {
 167  420
                 mySigner = theSigner;
 168  420
         }
 169  
 
 170  
         /**
 171  
          * @param thePath
 172  
          *            the path to set
 173  
          */
 174  
         public void setPath(String thePath) {
 175  455
                 myPath = thePath;
 176  455
         }
 177  
 
 178  
         /**
 179  
          * @param theUsername
 180  
          *            The authorization username
 181  
          */
 182  
         public void setUsername(String theUsername) {
 183  455
                 if (StringUtils.isNotBlank(theUsername)) {
 184  455
                         if (theUsername.contains(":")) {
 185  0
                                 throw new IllegalArgumentException("Username contains illegal characters");
 186  
                         }
 187  
                 }
 188  455
                 myUsername = theUsername;
 189  455
         }
 190  
 
 191  
         /**
 192  
          * Throws an {@link IllegalStateException} if called more than once 
 193  
          */
 194  
         protected void verifyNotUsed() {
 195  900
                 if (myUsed) {
 196  0
                         throw new IllegalStateException(getClass().getSimpleName() + " may not be reused");
 197  
                 }
 198  900
                 myUsed = true;
 199  900
         }
 200  
 
 201  
         /**
 202  
          * @return Returns the ISO-8859-1 charset
 203  
          */
 204  
         public static Charset getDefaultCharset() {
 205  250
                 return ourDefaultCharset;
 206  
         }
 207  
 
 208  
 }