read.taz.TazDownloader.java Source code

Java tutorial

Introduction

Here is the source code for read.taz.TazDownloader.java

Source

/*******************************************************************************
 * Copyright (c) 2012 Olaf Sebelin.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package read.taz;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import android.util.Log;

public class TazDownloader implements Runnable {

    private final TazFile tazFile;

    private final String uname;

    private final String passwd;

    private TazDownloadListener listener;

    public TazDownloader(TazFile tazFile, String uname, String passwd, TazDownloadListener listener) {
        super();
        this.tazFile = tazFile;
        this.uname = uname;
        this.passwd = passwd;
        this.listener = listener;
    }

    @Override
    public void run() {
        try {
            downloadFile();
            listener.success(tazFile);
        } catch (Exception e) {
            Log.e(getClass().getSimpleName(), "Cannot download taz", e);
            listener.error(tazFile, e);
        }
    }

    private void downloadFile() throws ClientProtocolException, IOException {
        if (tazFile.file.exists()) {
            Log.w(getClass().getSimpleName(), "File " + tazFile + " exists.");
            return;
        }
        HttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, 30000);
        HttpConnectionParams.setSoTimeout(httpParams, 15000);

        DefaultHttpClient client = new DefaultHttpClient(httpParams);

        Credentials defaultcreds = new UsernamePasswordCredentials(uname, passwd);

        client.getCredentialsProvider()
                .setCredentials(new AuthScope(/* FIXME DRY */"dl.taz.de", 80, AuthScope.ANY_REALM), defaultcreds);

        URI uri = tazFile.getDownloadURI();
        Log.d(getClass().getSimpleName(), "Downloading taz from " + uri);
        HttpGet get = new HttpGet(uri);
        HttpResponse response = client.execute(get);
        if (response.getStatusLine().getStatusCode() != 200) {
            throw new IOException("Download failed with HTTP Error" + response.getStatusLine().getStatusCode());
        }

        String contentType = response.getEntity().getContentType().getValue();
        if (contentType.startsWith("text/html")) {
            Log.d(getClass().getSimpleName(),
                    "Content type: " + contentType + " encountered. Assuming a non exisiting file");

            ByteArrayOutputStream htmlOut = new ByteArrayOutputStream();
            response.getEntity().writeTo(htmlOut);
            String resp = new String(htmlOut.toByteArray());
            Log.d(getClass().getSimpleName(), "Response: " + resp);
            throw new FileNotFoundException("No taz found for date " + tazFile.date.getTime());
        } else {
            FileOutputStream tazOut = new FileOutputStream(tazFile.file);
            try {
                response.getEntity().writeTo(tazOut);
            } finally {
                tazOut.close();
            }
        }

        // InputStream docStream = response.getEntity().getContent();
        // FileOutputStream out = null;
        // try {
        // out = tazOut;
        // byte[] buf = new byte[4096];
        // for (int read = docStream.read(buf); read != -1; read = docStream
        // .read(buf)) {
        // out.write(buf, 0, read);
        // }
        // } finally {
        // docStream.close();
        // if (out != null) {
        // out.close();
        // }
        // }

    }
}