View Javadoc
1   package ca.uhn.hl7v2.hoh.llp;
2   
3   import ca.uhn.hl7v2.app.TwoPortService;
4   import ca.uhn.hl7v2.hoh.api.IAuthorizationClientCallback;
5   import ca.uhn.hl7v2.hoh.api.IAuthorizationServerCallback;
6   import ca.uhn.hl7v2.hoh.sign.ISigner;
7   import ca.uhn.hl7v2.hoh.util.ServerRoleEnum;
8   import ca.uhn.hl7v2.llp.HL7Reader;
9   import ca.uhn.hl7v2.llp.HL7Writer;
10  import ca.uhn.hl7v2.llp.LLPException;
11  import ca.uhn.hl7v2.llp.LowerLayerProtocol;
12  
13  import java.io.IOException;
14  import java.io.InputStream;
15  import java.io.OutputStream;
16  import java.nio.charset.Charset;
17  
18  /**
19   * <p>
20   * LowerLayerProtocol implementation which use the HL7 over HTTP specification
21   * as a transport layer.
22   * </p>
23   *
24   * <p>
25   * Note that this implementation has limitations:
26   * <ul>
27   * <li>It will not work on a duel socket server such as {@link TwoPortService}
28   * </ul>
29   * </p>
30   */
31  public class Hl7OverHttpLowerLayerProtocol extends LowerLayerProtocol {
32  
33      private final ServerRoleEnum myRole;
34      private IAuthorizationClientCallback myAuthorizationClientCallback;
35      private IAuthorizationServerCallback myAuthorizationServerCallback;
36      private HohLlpReader myNextReader;
37      private HohLlpWriter myNextWriter;
38      private ISigner mySigner;
39      private String myUriPath = "/";
40      private Charset myPreferredCharset;
41      private String myHost;
42  
43      public Hl7OverHttpLowerLayerProtocol(ServerRoleEnum theRole) {
44          myRole = theRole;
45  
46          if (myRole == null) {
47              throw new NullPointerException("Role can not be null");
48          }
49      }
50  
51      /**
52       * @return the authorizationClientCallback
53       */
54      IAuthorizationClientCallback getAuthorizationClientCallback() {
55          return myAuthorizationClientCallback;
56      }
57  
58      /**
59       * Provide the authorization callback, if any
60       */
61      IAuthorizationServerCallback getAuthorizationServerCallback() {
62          return myAuthorizationServerCallback;
63      }
64  
65      /**
66       * {@inheritDoc}
67       */
68      @Override
69      public HL7Reader getReader(InputStream theArg0) throws LLPException {
70          if (myNextReader == null && myNextWriter != null) {
71              myNextWriter = null;
72              throw new LLPException("Hl7OverHttpLowerLayerProtocol can not be used with a multi socket implementation");
73          }
74          prepareReadersIfNeeded();
75          HohLlpReader retVal = myNextReader;
76          try {
77              retVal.setInputStream(theArg0);
78          } catch (IOException e) {
79              throw new LLPException("Failed to set stream: " + e.getMessage(), e);
80          }
81  
82          myNextReader = null;
83          return retVal;
84      }
85  
86      /**
87       * Returns the server role this protocol implementation is being used for
88       */
89      public ServerRoleEnum getRole() {
90          return myRole;
91      }
92  
93      /**
94       * @return The signature profile signer
95       * @see #setSigner(ISigner)
96       */
97      ISigner getSigner() {
98          return mySigner;
99      }
100 
101     /**
102      * @param theSigner The signature profile signer
103      */
104     public void setSigner(ISigner theSigner) {
105         mySigner = theSigner;
106     }
107 
108     /**
109      * @return The URI to use for this LLP implementation
110      * @see #setUriPath(String)
111      */
112     public String getUriPath() {
113         return myUriPath;
114     }
115 
116     /**
117      * The URI path to use for this protocol. The URI path is the portion of the address (URL) which
118      * is being accessed which designates the location on the host (i.e. the "path"). By
119      * default this is set to "/"
120      *
121      * @param theUriPath the uri to set
122      */
123     public void setUriPath(String theUriPath) {
124         myUriPath = theUriPath;
125     }
126 
127     public String getHost() {
128         return myHost;
129     }
130 
131     public void setHost(String thehost) {
132         myHost = thehost;
133     }
134 
135     /**
136      * {@inheritDoc}
137      */
138     @Override
139     public HL7Writer getWriter(OutputStream theArg0) throws LLPException {
140         if (myNextReader != null && myNextWriter == null) {
141             myNextReader = null;
142             throw new LLPException("Hl7OverHttpLowerLayerProtocol can not be used with a multi socket implementation");
143         }
144         prepareReadersIfNeeded();
145         HohLlpWriter retVal = myNextWriter;
146         retVal.setPreferredCharset(myPreferredCharset);
147         try {
148             retVal.setOutputStream(theArg0);
149         } catch (IOException e) {
150             throw new LLPException("Failed to set stream: " + e.getMessage(), e);
151         }
152 
153         myNextWriter = null;
154         return retVal;
155     }
156 
157     private void prepareReadersIfNeeded() {
158         if (myNextReader == null && myNextWriter == null) {
159             myNextReader = new HohLlpReader(this);
160             myNextWriter = new HohLlpWriter(this);
161             myNextReader.setWriter(myNextWriter);
162         }
163     }
164 
165     /**
166      * Provides an authorization callback for authorizing incoming requests. This method
167      * must only be called of this LLP instance is in {@link ServerRoleEnum#SERVER SERVER} mode.
168      */
169     public void setAuthorizationCallback(IAuthorizationClientCallback theAuthorizationClientCallback) {
170         if (myRole == ServerRoleEnum.SERVER) {
171             throw new IllegalStateException("This LLP implementation is in CLIENT mode, so it can not use an authorization callback");
172         }
173         myAuthorizationClientCallback = theAuthorizationClientCallback;
174     }
175 
176     /**
177      * Provides an authorization callback for authorizing incoming requests. This method
178      * must only be called of this LLP instance is in {@link ServerRoleEnum#SERVER SERVER} mode.
179      */
180     public void setAuthorizationCallback(IAuthorizationServerCallback theAuthorizationCallback) {
181         if (myRole == ServerRoleEnum.CLIENT) {
182             throw new IllegalStateException("This LLP implementation is in CLIENT mode, so it can not use an authorization callback");
183         }
184         myAuthorizationServerCallback = theAuthorizationCallback;
185     }
186 
187     /**
188      * Sets the charset which will be used for any initiated outgoing messages. What this
189      * means is that if a message is sent as a response (e.g. an ACK) using this LLP,
190      * the LLP will ignore the charset provided by this method and will attempt to use
191      * the charset used in the original incoming message. On the other hand, if a new
192      * outgoing message is transmitted using this LLP (i.e. not an ACK), the charset
193      * specified here will be used.
194      */
195     public void setPreferredCharset(Charset thePreferredCharset) {
196         myPreferredCharset = thePreferredCharset;
197     }
198 }