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("ISO-8859-1");
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 ISO-8859-1)
116 	 */
117 	public void setCharset(Charset theCharset) {
118 		if (theCharset == null) {
119 			throw new NullPointerException("Charset can not be null");
120 		}
121 		myCharsetExplicitlySet = true;
122 		myCharset = theCharset;
123 	}
124 
125 	/**
126 	 * @param theData
127 	 *            the data to set
128 	 */
129 	protected void setData(byte[] theData) {
130 		myData = theData;
131 	}
132 
133 	/**
134 	 * @param theHeaders
135 	 *            the headers to set
136 	 */
137 	public void setHeaders(LinkedHashMap<String, String> theHeaders) {
138 		myHeaders = theHeaders;
139 	}
140 
141 	/**
142 	 * @param theMessage
143 	 *            The raw message text
144 	 */
145 	public void setMessage(String theMessage) {
146 		myMessage = theMessage;
147 	}
148 
149 	/**
150 	 * @param thePassword
151 	 *            The authorization password
152 	 */
153 	public void setPassword(String thePassword) {
154 		myPassword = thePassword;
155 	}
156 
157 	/**
158 	 * Optionally may be used to provide a signer implementation which signs HL7
159 	 * content.
160 	 * 
161 	 * @see ISigner
162 	 * @see StandardMessageSigner
163 	 */
164 	public void setSigner(ISigner theSigner) {
165 		mySigner = theSigner;
166 	}
167 
168 	/**
169 	 * @param thePath
170 	 *            the path to set
171 	 */
172 	public void setPath(String thePath) {
173 		myPath = thePath;
174 	}
175 
176 	/**
177 	 * @param theUsername
178 	 *            The authorization username
179 	 */
180 	public void setUsername(String theUsername) {
181 		if (StringUtils.isNotBlank(theUsername)) {
182 			if (theUsername.contains(":")) {
183 				throw new IllegalArgumentException("Username contains illegal characters");
184 			}
185 		}
186 		myUsername = theUsername;
187 	}
188 
189 	/**
190 	 * Throws an {@link IllegalStateException} if called more than once 
191 	 */
192 	protected void verifyNotUsed() {
193 		if (myUsed) {
194 			throw new IllegalStateException(getClass().getSimpleName() + " may not be reused");
195 		}
196 		myUsed = true;
197 	}
198 
199 	/**
200 	 * @return Returns the ISO-8859-1 charset
201 	 */
202 	public static Charset getDefaultCharset() {
203 		return ourDefaultCharset;
204 	}
205 
206 }