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 */
027package ca.uhn.hl7v2.conf.store;
028
029import java.io.*;
030import 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 */
038public abstract class URLProfileStore extends ReadOnlyProfileStore {
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;
048        try {
049            BufferedReader in = new BufferedReader(new InputStreamReader(getURL(ID).openStream()));
050            StringBuffer buf = new StringBuffer();
051            int c;
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
064    /**
065     * Returns the URL from which to read a profile given the profile ID.  For example
066     * given "123" it could return ftp://hospital_x.org/hl7/123.xml, or 
067     * http://hl7_conformance_service.com?profile=123.  
068     */
069    public abstract URL getURL(String ID) throws MalformedURLException;
070    
071
072    /** Stores profile in persistent storage with given ID.
073     */
074    /*public void persistProfile(String ID, String profile) throws IOException {
075        try {
076            URL url = getWriteURL(ID);
077            if (url == null) {
078                throw new IOException("Can't persist profile -- this profile store is read-only");
079            } else {
080                URLConnection uc = url.openConnection();
081                uc.connect();
082                uc.getOutputStream().write(profile.getBytes());
083                uc.getOutputStream().flush();
084                uc.getOutputStream().close();
085            }
086        } catch (MalformedURLException e) {
087            throw new IOException("MalformedURLException: " + e.getMessage());
088        }
089    }*/
090        
091    /**
092     * Returns the URL to which a profile should be written, given the 
093     * profile ID.  This defaults to getReadURL() but can be over-ridden.  
094     * For read-only stores, over-ride this method to return null.
095     */
096    /*public URL getWriteURL(String ID) throws MalformedURLException {
097        return getReadURL(ID);
098    }*/
099    
100}