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