001    /*
002     * Copyright (c) Andrey Kuznetsov. All Rights Reserved.
003     *
004     * http://uio.imagero.com
005     *
006     * Redistribution and use in source and binary forms, with or without
007     * modification, are permitted provided that the following conditions are met:
008     *
009     *  o Redistributions of source code must retain the above copyright notice,
010     *    this list of conditions and the following disclaimer.
011     *
012     *  o Redistributions in binary form must reproduce the above copyright notice,
013     *    this list of conditions and the following disclaimer in the documentation
014     *    and/or other materials provided with the distribution.
015     *
016     *  o Neither the name of Andrey Kuznetsov nor the names of
017     *    its contributors may be used to endorse or promote products derived
018     *    from this software without specific prior written permission.
019     *
020     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
021     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
022     * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
023     * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
024     * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
027     * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
028     * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
029     * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
030     * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031     */
032    package com.imagero.uio.bio.content;
033    
034    import com.imagero.uio.io.IOutils;
035    import com.imagero.uio.io.UnexpectedEOFException;
036    
037    import java.net.URL;
038    import java.net.HttpURLConnection;
039    import java.io.IOException;
040    import java.io.InputStream;
041    
042    /**
043     * Date: 05.01.2008
044     *
045     * @author Andrey Kuznetsov
046     */
047    public class HTTPContent extends Content {
048        URL url;
049    
050        long length;
051    
052        public HTTPContent(URL url) {
053            String protocol = url.getProtocol();
054            if (!"http".equalsIgnoreCase(protocol)) {
055                throw new IllegalArgumentException("http protokol only");
056            }
057            this.url = url;
058        }
059    
060        public int load(long offset, int bpos, byte[] buffer) throws IOException {
061            HttpURLConnection httpcon = (HttpURLConnection) url.openConnection();
062            httpcon.setAllowUserInteraction(true);
063            httpcon.setDoInput(true);
064            httpcon.setDoOutput(true);
065            httpcon.setRequestMethod("GET");
066            httpcon.setUseCaches(false);
067            httpcon.setRequestProperty("Range", "bytes=" + offset + "-" + (offset + buffer.length - bpos));
068            httpcon.connect();
069    
070            int responseCode = httpcon.getResponseCode();
071            if (responseCode != 206) {
072                httpcon.disconnect();
073                throw new IOException("byteserving not supported by server");
074            }
075            InputStream in = httpcon.getInputStream();
076    
077            int count = 0;
078            try {
079                int len = buffer.length - bpos;
080                IOutils.readFully(in, buffer, bpos, len);
081                count = len;
082            } catch (UnexpectedEOFException ex) {
083                count = ex.getCount();
084            } finally {
085                httpcon.disconnect();
086            }
087            return count;
088        }
089    
090        public void close() {
091        }
092    
093        public void save(long offset, int bpos, byte[] buffer, int length) throws IOException {
094        }
095    
096        public long length() throws IOException {
097            if (length == 0) {
098                HttpURLConnection httpcon = (HttpURLConnection) url.openConnection();
099                httpcon.setRequestMethod("HEAD");
100                httpcon.setUseCaches(false);
101                httpcon.connect();
102                length = httpcon.getContentLength();
103                httpcon.disconnect();
104            }
105            return length;
106        }
107    
108        public boolean canReload() {
109            return false;
110        }
111    
112        public boolean writable() {
113            return false;
114        }
115    }