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    * <p>
9    * The Original Code is "URLProfileStore.java".  Description:
10   * "A read-only profile store that loads profiles from URLs."
11   * <p>
12   * The Initial Developer of the Original Code is University Health Network. Copyright (C)
13   * 2003.  All Rights Reserved.
14   * <p>
15   * Contributor(s): ______________________________________.
16   * <p>
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  package ca.uhn.hl7v2.conf.store;
27  
28  import java.io.BufferedReader;
29  import java.io.IOException;
30  import java.io.InputStreamReader;
31  import java.net.MalformedURLException;
32  import java.net.URL;
33  
34  /**
35   * A read-only profile store that loads profiles from URLs.  The URL 
36   * for a profile is determined by the method getURL().  An 
37   * attempt is also made to write 
38   * @author Bryan Tripp
39   */
40  public abstract class URLProfileStore extends ReadOnlyProfileStore {
41  
42      /** Creates a new instance of URLProfileStore */
43      public URLProfileStore() {
44      }
45  
46      /**
47       * Retrieves profile from persistent storage (by ID).
48       *
49       * @param id profile id
50       * @return profile content or null if profile could not be found
51       */
52      public String getProfile(String id) throws IOException {
53          String profile = null;
54          BufferedReader in = null;
55          try {
56              URL url = getURL(id);
57              if (url != null) {
58                  in = new BufferedReader(new InputStreamReader(url.openStream()));
59                  StringBuilder buf = new StringBuilder();
60                  int c;
61                  while ((c = in.read()) != -1) {
62                      buf.append((char) c);
63                  }
64                  profile = buf.toString();
65              }
66          } catch (MalformedURLException e) {
67              throw new IOException("MalformedURLException: " + e.getMessage());
68          } finally {
69              if (in != null) in.close();
70          }
71          return profile;
72      }
73  
74  
75      /**
76       * Returns the URL from which to read a profile given the profile ID.  For example
77       * given "123" it could return ftp://hospital_x.org/hl7/123.xml, or 
78       * http://hl7_conformance_service.com?profile=123.  
79       */
80      public abstract URL getURL(String ID) throws MalformedURLException;
81  
82  
83      /** Stores profile in persistent storage with given ID.
84       */
85      /*public void persistProfile(String ID, String profile) throws IOException {
86          try {
87              URL url = getWriteURL(ID);
88              if (url == null) {
89                  throw new IOException("Can't persist profile -- this profile store is read-only");
90              } else {
91                  URLConnection uc = url.openConnection();
92                  uc.connect();
93                  uc.getOutputStream().write(profile.getBytes());
94                  uc.getOutputStream().flush();
95                  uc.getOutputStream().close();
96              }
97          } catch (MalformedURLException e) {
98              throw new IOException("MalformedURLException: " + e.getMessage());
99          }
100     }*/
101 
102     /**
103      * Returns the URL to which a profile should be written, given the 
104      * profile ID.  This defaults to getReadURL() but can be over-ridden.  
105      * For read-only stores, over-ride this method to return null.
106      */
107     /*public URL getWriteURL(String ID) throws MalformedURLException {
108         return getReadURL(ID);
109     }*/
110 
111 }