View Javadoc

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  	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  		ourDefaultCharset = Charset.forName("UTF-8");
19  		VersionLogger.init();
20  	}
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  	public AbstractHl7OverHttp() {
37  		myCharset = ourDefaultCharset;
38  	}
39  
40  	/**
41  	 * Returns the charset associated with this message. Will not return <code>null</code>.
42  	 */
43  	public Charset getCharset() {
44  		return myCharset;
45  	}
46  
47  	/**
48  	 * @return the data
49  	 */
50  	public byte[] getData() {
51  		return myData;
52  	}
53  
54  	/**
55  	 * @return the headers
56  	 */
57  	public Map<String, String> getHeaders() {
58  		return myHeaders;
59  	}
60  
61  	/**
62  	 * @return the message
63  	 */
64  	public String getMessage() {
65  		return myMessage;
66  	}
67  
68  
69  	/**
70  	 * @return the password
71  	 */
72  	public String getPassword() {
73  		return myPassword;
74  	}
75  
76  	/**
77  	 * @return the signer
78  	 */
79  	public ISigner getSigner() {
80  		return mySigner;
81  	}
82  
83  	/**
84  	 * @return the path
85  	 */
86  	public String getPath() {
87  		// TODO: un-url encode this
88  		return myPath;
89  	}
90  
91  	/**
92  	 * @return the path
93  	 */
94  	public String getPathRaw() {
95  		return myPath;
96  	}
97  
98  	/**
99  	 * @return the username
100 	 */
101 	public String getUsername() {
102 		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 		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 		if (theCharset == null) {
121 			throw new NullPointerException("Charset can not be null");
122 		}
123 		myCharsetExplicitlySet = true;
124 		myCharset = theCharset;
125 	}
126 
127 	/**
128 	 * @param theData
129 	 *            the data to set
130 	 */
131 	protected void setData(byte[] theData) {
132 		myData = theData;
133 	}
134 
135 	/**
136 	 * @param theHeaders
137 	 *            the headers to set
138 	 */
139 	public void setHeaders(LinkedHashMap<String, String> theHeaders) {
140 		myHeaders = theHeaders;
141 	}
142 
143 	/**
144 	 * @param theMessage
145 	 *            The raw message text
146 	 */
147 	public void setMessage(String theMessage) {
148 		myMessage = theMessage;
149 	}
150 
151 	/**
152 	 * @param thePassword
153 	 *            The authorization password
154 	 */
155 	public void setPassword(String thePassword) {
156 		myPassword = thePassword;
157 	}
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 		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 }