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 * <p>
009 * The Original Code is "URLProfileStore.java".  Description:
010 * "A read-only profile store that loads profiles from URLs."
011 * <p>
012 * The Initial Developer of the Original Code is University Health Network. Copyright (C)
013 * 2003.  All Rights Reserved.
014 * <p>
015 * Contributor(s): ______________________________________.
016 * <p>
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 */
026package ca.uhn.hl7v2.conf.store;
027
028import java.io.BufferedReader;
029import java.io.IOException;
030import java.io.InputStreamReader;
031import java.net.MalformedURLException;
032import java.net.URL;
033
034/**
035 * A read-only profile store that loads profiles from URLs.  The URL 
036 * for a profile is determined by the method getURL().  An 
037 * attempt is also made to write 
038 * @author Bryan Tripp
039 */
040public abstract class URLProfileStore extends ReadOnlyProfileStore {
041
042    /** Creates a new instance of URLProfileStore */
043    public URLProfileStore() {
044    }
045
046    /**
047     * Retrieves profile from persistent storage (by ID).
048     *
049     * @param id profile id
050     * @return profile content or null if profile could not be found
051     */
052    public String getProfile(String id) throws IOException {
053        String profile = null;
054        BufferedReader in = null;
055        try {
056            URL url = getURL(id);
057            if (url != null) {
058                in = new BufferedReader(new InputStreamReader(url.openStream()));
059                StringBuilder buf = new StringBuilder();
060                int c;
061                while ((c = in.read()) != -1) {
062                    buf.append((char) c);
063                }
064                profile = buf.toString();
065            }
066        } catch (MalformedURLException e) {
067            throw new IOException("MalformedURLException: " + e.getMessage());
068        } finally {
069            if (in != null) in.close();
070        }
071        return profile;
072    }
073
074
075    /**
076     * Returns the URL from which to read a profile given the profile ID.  For example
077     * given "123" it could return ftp://hospital_x.org/hl7/123.xml, or 
078     * http://hl7_conformance_service.com?profile=123.  
079     */
080    public abstract URL getURL(String ID) throws MalformedURLException;
081
082
083    /** Stores profile in persistent storage with given ID.
084     */
085    /*public void persistProfile(String ID, String profile) throws IOException {
086        try {
087            URL url = getWriteURL(ID);
088            if (url == null) {
089                throw new IOException("Can't persist profile -- this profile store is read-only");
090            } else {
091                URLConnection uc = url.openConnection();
092                uc.connect();
093                uc.getOutputStream().write(profile.getBytes());
094                uc.getOutputStream().flush();
095                uc.getOutputStream().close();
096            }
097        } catch (MalformedURLException e) {
098            throw new IOException("MalformedURLException: " + e.getMessage());
099        }
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}