1 package ca.uhn.hl7v2.hoh.encoder;
2
3 import static ca.uhn.hl7v2.hoh.util.StringUtils.*;
4
5 import java.io.IOException;
6 import java.io.InputStream;
7
8 import ca.uhn.hl7v2.hoh.api.DecodeException;
9 import ca.uhn.hl7v2.hoh.api.IAuthorizationServerCallback;
10 import ca.uhn.hl7v2.hoh.util.StringUtils;
11
12 public class Hl7OverHttpRequestDecoder extends AbstractHl7OverHttpDecoder {
13
14 private IAuthorizationServerCallback myAuthorizationCallback;
15 private String myActionLine;
16 private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(Hl7OverHttpRequestDecoder.class);
17
18 protected void authorize() throws AuthorizationFailureException {
19 if (myAuthorizationCallback != null) {
20 String username = getUsername();
21 if (!myAuthorizationCallback.authorize(getPath(), username, getPassword())) {
22 if (StringUtils.isBlank(username)) {
23 throw new AuthorizationFailureException("Authorization failed: No username provided");
24 } else {
25 throw new AuthorizationFailureException("Authorization failed for user: " + getUsername());
26 }
27 }
28 } else {
29 if (isNotBlank(getUsername()) || isNotBlank(getPassword())) {
30 ourLog.warn("Request contains username and/or password, but no authorization callback has been set so credentials can not be validated");
31 }
32 }
33 }
34
35 @Override
36 protected String readActionLineAndDecode(InputStream theInputStream) throws DecodeException, IOException, NoMessageReceivedException {
37 ourLog.trace("Entering readActionLineAndDecode(InputStream)");
38
39 if (myActionLine == null) {
40 String firstLine = readFirstLine(theInputStream);
41 if (firstLine == null || isBlank(firstLine)) {
42 throw new NoMessageReceivedException();
43 }
44
45 if (!firstLine.startsWith("POST ")) {
46 throw new DecodeException("HTTP request line message is not valid. Only POST action is supported. Request line was: " + firstLine);
47 }
48
49 firstLine = firstLine.substring(5);
50 int nextSpace = firstLine.indexOf(' ');
51 if (nextSpace == -1) {
52 throw new DecodeException("HTTP request line message is not valid. Not HTTP version found. Request line was: " + firstLine);
53 }
54
55 setPath(firstLine.substring(0, nextSpace));
56 if (isBlank(getPath())) {
57 throw new DecodeException("HTTP request line message is not valid. No request URI found. Request line was: " + firstLine);
58 }
59
60 String protocolVersion = firstLine.substring(nextSpace + 1);
61 if (!"HTTP/1.1".equals(protocolVersion)) {
62 throw new DecodeException("HTTP request line message is not valid. HTTP version not supported. Request line was: " + firstLine);
63 }
64
65 myActionLine = firstLine;
66 ourLog.trace("Action line is {}", myActionLine);
67
68 } else {
69 ourLog.trace("Already have an action line");
70 }
71
72 return myActionLine;
73 }
74
75
76
77
78
79 public void setAuthorizationCallback(IAuthorizationServerCallback theAuthorizationCallback) {
80 myAuthorizationCallback = theAuthorizationCallback;
81 }
82
83 }