All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.tidalwave.mobile.util.DefaultDownloadable Maven / Gradle / Ivy

/***********************************************************************************************************************
 *
 * blueBill Core - open source birding
 * Copyright (C) 2009-2011 by Tidalwave s.a.s. (http://www.tidalwave.it)
 *
 ***********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations under the License.
 *
 ***********************************************************************************************************************
 *
 * WWW: http://bluebill.tidalwave.it
 * SCM: https://java.net/hg/bluebill~core-src
 *
 **********************************************************************************************************************/
package it.tidalwave.mobile.util;

import it.tidalwave.mobile.io.FileSystem;
import java.net.MalformedURLException;
import javax.annotation.Nonnull;
import javax.inject.Provider;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import it.tidalwave.netbeans.util.Locator;
import it.tidalwave.mobile.io.MasterFileSystem;
import it.tidalwave.role.Removable;
import java.io.FileWriter;
import lombok.Cleanup;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 **********************************************************************************************************************/
public class DefaultDownloadable extends Downloadable implements Removable
  {
    private final static Logger log = LoggerFactory.getLogger(DefaultDownloadable.class);

    @Nonnull
    public final URL url; // FIXME

    @Nonnull
    public final URL proxiedUrl;
    
    /* package */ File cachedFile;
            
    /* package */ File downloadFile;
            
    /* package */ File timestampFile;
            
    /* package */ final Provider masterFileSystem = Locator.createProviderFor(MasterFileSystem.class);

    /* package */ int contentLength = 0;

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public DefaultDownloadable (final @Nonnull URL url)
      throws MalformedURLException
      {
        String x = url.toExternalForm().replaceAll(" ", "%20");
        
        this.url = new URL(x);
        this.proxiedUrl = new URL(x); // FIXME: can be eventually different, inject a proxy configurator
        
        log.info("        URL: {}", url);
        log.info("proxied URL: {}", proxiedUrl);
        computeCachedFile();
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override
    public void download()
      {
        download(false);
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override
    public void refresh()
      {
        download(true);
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    public void remove() 
      throws IOException
      {
        log.info("Deleting {}", cachedFile);
        safeDelete(cachedFile);

        setStatus(Status.NOT_DOWNLOADED);
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public File getFile()
      {
        return cachedFile;
      }

    /*******************************************************************************************************************
     *
     * Can be overridden for testing (URLs are not mockable).
     *
     ******************************************************************************************************************/
    @Nonnull
    protected InputStream createInputStream()
      throws IOException
      {
        final URLConnection connection = proxiedUrl.openConnection();
        connection.connect();
        contentLength = connection.getContentLength(); // TODO: handle the case in which it's unknown size
        return connection.getInputStream();
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    private synchronized void download (final boolean always)
      {
        log.info("download() - {}", proxiedUrl);
        computeCachedFile();
        log.info(">>>> will save to {}", cachedFile);

        if (always || (status == Status.NOT_DOWNLOADED))
          {
            setStatus(Status.DOWNLOADING);
            new Thread() // TODO: use a pool, with QUEUED state
              {
                @Override
                public void run()
                  {
                    try
                      {
                        load();
                      }
                    catch (Exception e)
                      {
                        setStatus(Status.BROKEN);
                        log.error("While loading {}: {}", url, e);
                        log.error("download()", e);
                      }
                  }
              }.start();
          }
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    private void load()
      throws IOException
      {
        log.info("load() - into {}", cachedFile);
        final byte[] buffer = new byte[64 * 1024];
        cachedFile.getParentFile().mkdirs();
        
        if (downloadFile.exists())
          {
            safeDelete(downloadFile);  
          }
        
        log.debug(">>>> writing temporary file {}", downloadFile);
        @Cleanup final InputStream is = createInputStream();
        @Cleanup final OutputStream os = new FileOutputStream(downloadFile);
//        final OutputStream os = fileSystem.get().openFileOutput(getCachedFile());
        // FIXME: first download in cache, at the end copy to the real resource.
        int loaded = 0;

        for (;;)
          {
            final int n = is.read(buffer);

            if (n < 0)
              {
                break;
              }

            os.write(buffer, 0, n);
            loaded += n;

            if (contentLength > 0)
              {
                setDownloadProgress((1.0f * loaded) / contentLength);
                log.trace("Loaded {} bytes, so far: {} bytes, progress: {}", new Object[]{ n, loaded, downloadProgress });
              }
          }

        if (cachedFile.exists())
          {
            safeDelete(cachedFile);  
          }
        
        log.debug(">>>> moving {} to {}", downloadFile, cachedFile);
        downloadFile.renameTo(cachedFile);
        final @Cleanup FileWriter w = new FileWriter(timestampFile);
        log.debug(">>>> creating {}", timestampFile);
        w.write("downloaded: " + System.currentTimeMillis() + "\n");
        
        setStatus(Status.DOWNLOADED);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    /* package */ static String normalized (final @Nonnull URL url)
      {
        return url.toExternalForm().replaceAll("://", "/").replaceAll("[:;#$?&=]", "_");
//        return url.toExternalForm().replaceAll("[:/;#@\\$\\?\\&]", "_").replaceAll("_*", "_");
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    private void computeCachedFile()
      {
        Status newStatus = null;

        synchronized (this)
          {
            if (cachedFile == null)
              {
                try
                  {
                    final FileSystem externalFileSystem = masterFileSystem.get().getExternalFileSystem();
                    final String prefix = "/Media/" + normalized(url);
                    cachedFile = externalFileSystem.getFile(prefix);
                    downloadFile = externalFileSystem.getFile(prefix + ".download");
                    timestampFile = externalFileSystem.getFile(prefix + ".timestamp");

                    if (cachedFile.exists())
                      {
                        newStatus = Status.DOWNLOADED;
                      }
                    else
                      {
                        newStatus = Status.NOT_DOWNLOADED;
                      }
                  }
                catch (IOException e)
                  {
                    newStatus = Status.BROKEN;
                    log.error("Cannot map to external storage: {}", e);
                    log.error("", e);
                  }
              }
          }

        if (newStatus != null)
          {
            setStatus(newStatus);
          }
      }
    
    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    private static void safeDelete (final @Nonnull File file)
      throws IOException 
      {
        if (!file.delete())
          {
            throw new IOException("Not deleted: " + file); // Java 5 / Android compatibility
          }
      }
  }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy