001package ca.uhn.hl7v2.hoh.encoder;
002
003import static ca.uhn.hl7v2.hoh.util.StringUtils.*;
004
005import java.io.IOException;
006import java.io.InputStream;
007
008import ca.uhn.hl7v2.hoh.api.DecodeException;
009import ca.uhn.hl7v2.hoh.api.IAuthorizationServerCallback;
010import ca.uhn.hl7v2.hoh.util.StringUtils;
011
012public class Hl7OverHttpRequestDecoder extends AbstractHl7OverHttpDecoder {
013
014        private IAuthorizationServerCallback myAuthorizationCallback;
015        private String myActionLine;
016        private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(Hl7OverHttpRequestDecoder.class);
017
018        protected void authorize() throws AuthorizationFailureException {
019                if (myAuthorizationCallback != null) {
020                        String username = getUsername();
021                        if (!myAuthorizationCallback.authorize(getPath(), username, getPassword())) {
022                                if (StringUtils.isBlank(username)) {
023                                        throw new AuthorizationFailureException("Authorization failed: No username provided");
024                                } else {
025                                        throw new AuthorizationFailureException("Authorization failed for user: " + getUsername());
026                                }
027                        }
028                } else {
029                        if (isNotBlank(getUsername()) || isNotBlank(getPassword())) {
030                                ourLog.warn("Request contains username and/or password, but no authorization callback has been set so credentials can not be validated");
031                        }
032                }
033        }
034
035        @Override
036        protected String readActionLineAndDecode(InputStream theInputStream) throws DecodeException, IOException, NoMessageReceivedException {
037                ourLog.trace("Entering readActionLineAndDecode(InputStream)");
038                
039                if (myActionLine == null) {
040                        String firstLine = readFirstLine(theInputStream);
041                        if (firstLine == null || isBlank(firstLine)) {
042                                throw new NoMessageReceivedException();
043                        }
044
045                        if (!firstLine.startsWith("POST ")) {
046                                throw new DecodeException("HTTP request line message is not valid. Only POST action is supported. Request line was: " + firstLine);
047                        }
048
049                        firstLine = firstLine.substring(5);
050                        int nextSpace = firstLine.indexOf(' ');
051                        if (nextSpace == -1) {
052                                throw new DecodeException("HTTP request line message is not valid. Not HTTP version found. Request line was: " + firstLine);
053                        }
054
055                        setPath(firstLine.substring(0, nextSpace));
056                        if (isBlank(getPath())) {
057                                throw new DecodeException("HTTP request line message is not valid. No request URI found. Request line was: " + firstLine);
058                        }
059
060                        String protocolVersion = firstLine.substring(nextSpace + 1);
061                        if (!"HTTP/1.1".equals(protocolVersion)) {
062                                throw new DecodeException("HTTP request line message is not valid. HTTP version not supported. Request line was: " + firstLine);
063                        }
064
065                        myActionLine = firstLine;
066                        ourLog.trace("Action line is {}", myActionLine);
067
068                } else {
069                        ourLog.trace("Already have an action line");
070                }
071                
072                return myActionLine;
073        }
074
075        /**
076         * @param theAuthorizationCallback
077         *            the authorizationCallback to set
078         */
079        public void setAuthorizationCallback(IAuthorizationServerCallback theAuthorizationCallback) {
080                myAuthorizationCallback = theAuthorizationCallback;
081        }
082
083}