001    /**
002     * The contents of this file are subject to the Mozilla Public License Version 1.1
003     * (the "License"); you may not use this file except in compliance with the License.
004     * You may obtain a copy of the License at http://www.mozilla.org/MPL/
005     * Software distributed under the License is distributed on an "AS IS" basis,
006     * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
007     * specific language governing rights and limitations under the License.
008     *
009     * The Original Code is "URLProfileStore.java".  Description:
010     * "A read-only profile store that loads profiles from URLs."
011     *
012     * The Initial Developer of the Original Code is University Health Network. Copyright (C)
013     * 2003.  All Rights Reserved.
014     *
015     * Contributor(s): ______________________________________.
016     *
017     * Alternatively, the contents of this file may be used under the terms of the
018     * GNU General Public License (the  ?GPL?), in which case the provisions of the GPL are
019     * applicable instead of those above.  If you wish to allow use of your version of this
020     * file only under the terms of the GPL and not to allow others to use your version
021     * of this file under the MPL, indicate your decision by deleting  the provisions above
022     * and replace  them with the notice and other provisions required by the GPL License.
023     * If you do not delete the provisions above, a recipient may use your version of
024     * this file under either the MPL or the GPL.
025     *
026     */
027    package ca.uhn.hl7v2.conf.store;
028    
029    import java.io.*;
030    import java.net.*;
031    
032    /**
033     * A read-only profile store that loads profiles from URLs.  The URL 
034     * for a profile is determined by the method getURL().  An 
035     * attempt is also made to write 
036     * @author Bryan Tripp
037     */
038    public abstract class URLProfileStore implements ProfileStore {
039        
040        /** Creates a new instance of URLProfileStore */
041        public URLProfileStore() {
042        }
043        
044        /** Retrieves profile from persistent storage (by ID).
045         */
046        public String getProfile(String ID) throws IOException {
047            String profile = null;
048            try {
049                BufferedReader in = new BufferedReader(new InputStreamReader(getURL(ID).openStream()));
050                StringBuffer buf = new StringBuffer();
051                int c = -1;
052                while ( (c = in.read()) != -1) {
053                    buf.append( (char) c );
054                } 
055                in.close();
056                profile = buf.toString();
057            } catch (MalformedURLException e) {
058                throw new IOException("MalformedURLException: " + e.getMessage());
059            }
060            return profile;
061        }
062        
063        /** Stores profile in persistent storage with given ID.
064         */
065        public void persistProfile(String ID, String profile) throws IOException {
066            throw new IOException("Can't persist profile -- this profile store is read-only");
067        }
068    
069        /**
070         * Returns the URL from which to read a profile given the profile ID.  For example
071         * given "123" it could return ftp://hospital_x.org/hl7/123.xml, or 
072         * http://hl7_conformance_service.com?profile=123.  
073         */
074        public abstract URL getURL(String ID) throws MalformedURLException;
075        
076    
077        /** Stores profile in persistent storage with given ID.
078         */
079        /*public void persistProfile(String ID, String profile) throws IOException {
080            try {
081                URL url = getWriteURL(ID);
082                if (url == null) {
083                    throw new IOException("Can't persist profile -- this profile store is read-only");
084                } else {
085                    URLConnection uc = url.openConnection();
086                    uc.connect();
087                    uc.getOutputStream().write(profile.getBytes());
088                    uc.getOutputStream().flush();
089                    uc.getOutputStream().close();
090                }
091            } catch (MalformedURLException e) {
092                throw new IOException("MalformedURLException: " + e.getMessage());
093            }
094        }*/
095            
096        /**
097         * Returns the URL to which a profile should be written, given the 
098         * profile ID.  This defaults to getReadURL() but can be over-ridden.  
099         * For read-only stores, over-ride this method to return null.
100         */
101        /*public URL getWriteURL(String ID) throws MalformedURLException {
102            return getReadURL(ID);
103        }*/
104        
105    }