View Javadoc

1   /**
2    * The contents of this file are subject to the Mozilla Public License Version 1.1
3    * (the "License"); you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at http://www.mozilla.org/MPL/
5    * Software distributed under the License is distributed on an "AS IS" basis,
6    * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
7    * specific language governing rights and limitations under the License.
8    *
9    * The Original Code is "URLProfileStore.java".  Description:
10   * "A read-only profile store that loads profiles from URLs."
11   *
12   * The Initial Developer of the Original Code is University Health Network. Copyright (C)
13   * 2003.  All Rights Reserved.
14   *
15   * Contributor(s): ______________________________________.
16   *
17   * Alternatively, the contents of this file may be used under the terms of the
18   * GNU General Public License (the "GPL"), in which case the provisions of the GPL are
19   * applicable instead of those above.  If you wish to allow use of your version of this
20   * file only under the terms of the GPL and not to allow others to use your version
21   * of this file under the MPL, indicate your decision by deleting  the provisions above
22   * and replace  them with the notice and other provisions required by the GPL License.
23   * If you do not delete the provisions above, a recipient may use your version of
24   * this file under either the MPL or the GPL.
25   *
26   */
27  package ca.uhn.hl7v2.conf.store;
28  
29  import java.io.*;
30  import java.net.*;
31  
32  /**
33   * A read-only profile store that loads profiles from URLs.  The URL 
34   * for a profile is determined by the method getURL().  An 
35   * attempt is also made to write 
36   * @author Bryan Tripp
37   */
38  public abstract class URLProfileStore extends ReadOnlyProfileStore {
39      
40      /** Creates a new instance of URLProfileStore */
41      public URLProfileStore() {
42      }
43      
44      /** Retrieves profile from persistent storage (by ID).
45       */
46      public String getProfile(String ID) throws IOException {
47          String profile;
48          try {
49              BufferedReader in = new BufferedReader(new InputStreamReader(getURL(ID).openStream()));
50              StringBuffer buf = new StringBuffer();
51              int c;
52              while ( (c = in.read()) != -1) {
53                  buf.append( (char) c );
54              } 
55              in.close();
56              profile = buf.toString();
57          } catch (MalformedURLException e) {
58              throw new IOException("MalformedURLException: " + e.getMessage());
59          }
60          return profile;
61      }
62  
63  
64      /**
65       * Returns the URL from which to read a profile given the profile ID.  For example
66       * given "123" it could return ftp://hospital_x.org/hl7/123.xml, or 
67       * http://hl7_conformance_service.com?profile=123.  
68       */
69      public abstract URL getURL(String ID) throws MalformedURLException;
70      
71  
72      /** Stores profile in persistent storage with given ID.
73       */
74      /*public void persistProfile(String ID, String profile) throws IOException {
75          try {
76              URL url = getWriteURL(ID);
77              if (url == null) {
78                  throw new IOException("Can't persist profile -- this profile store is read-only");
79              } else {
80                  URLConnection uc = url.openConnection();
81                  uc.connect();
82                  uc.getOutputStream().write(profile.getBytes());
83                  uc.getOutputStream().flush();
84                  uc.getOutputStream().close();
85              }
86          } catch (MalformedURLException e) {
87              throw new IOException("MalformedURLException: " + e.getMessage());
88          }
89      }*/
90          
91      /**
92       * Returns the URL to which a profile should be written, given the 
93       * profile ID.  This defaults to getReadURL() but can be over-ridden.  
94       * For read-only stores, over-ride this method to return null.
95       */
96      /*public URL getWriteURL(String ID) throws MalformedURLException {
97          return getReadURL(ID);
98      }*/
99      
100 }