001package ball.util.ant.taskdefs;
002/*-
003 * ##########################################################################
004 * Utilities
005 * $Id: DownloadTask.java 5285 2020-02-05 04:23:21Z ball $
006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/ball-util/trunk/src/main/java/ball/util/ant/taskdefs/DownloadTask.java $
007 * %%
008 * Copyright (C) 2008 - 2020 Allen D. Ball
009 * %%
010 * Licensed under the Apache License, Version 2.0 (the "License");
011 * you may not use this file except in compliance with the License.
012 * You may obtain a copy of the License at
013 *
014 *      http://www.apache.org/licenses/LICENSE-2.0
015 *
016 * Unless required by applicable law or agreed to in writing, software
017 * distributed under the License is distributed on an "AS IS" BASIS,
018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019 * See the License for the specific language governing permissions and
020 * limitations under the License.
021 * ##########################################################################
022 */
023import ball.net.ResponseCacheImpl;
024import java.io.File;
025import java.io.FileOutputStream;
026import java.io.InputStream;
027import java.net.ResponseCache;
028import java.net.URI;
029import java.net.URLConnection;
030import lombok.Getter;
031import lombok.NoArgsConstructor;
032import lombok.Setter;
033import lombok.ToString;
034import lombok.experimental.Accessors;
035import org.apache.commons.io.IOUtils;
036import org.apache.tools.ant.BuildException;
037import org.apache.tools.ant.Task;
038import org.apache.tools.ant.util.ClasspathUtils;
039
040/**
041 * {@link.uri http://ant.apache.org/ Ant} {@link Task} to download resources
042 * using {@link URLConnection} and {@link ResponseCacheImpl}.
043 *
044 * {@ant.task}
045 *
046 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
047 * @version $Revision: 5285 $
048 */
049@AntTask("download")
050@NoArgsConstructor @ToString
051public class DownloadTask extends Task implements AnnotatedAntTask,
052                                                  ClasspathDelegateAntTask,
053                                                  ConfigurableAntTask {
054    static {
055        if (ResponseCache.getDefault() == null) {
056            ResponseCache.setDefault(ResponseCacheImpl.DEFAULT);
057        }
058    }
059
060    @Getter @Setter @Accessors(chain = true, fluent = true)
061    private ClasspathUtils.Delegate delegate = null;
062    @NotNull @Getter @Setter
063    private URI uri = null;
064    @NotNull @Getter @Setter
065    private File toFile = null;
066
067    @Override
068    public void init() throws BuildException {
069        super.init();
070        ClasspathDelegateAntTask.super.init();
071        ConfigurableAntTask.super.init();
072    }
073
074    @Override
075    public void execute() throws BuildException {
076        super.execute();
077        AnnotatedAntTask.super.execute();
078
079        try {
080            URLConnection connection = getUri().toURL().openConnection();
081
082            try (InputStream in = connection.getInputStream();
083                 FileOutputStream out = new FileOutputStream(getToFile())) {
084                IOUtils.copy(in, out);
085                log(getUri() + " --> " + getToFile());
086            }
087        } catch (BuildException exception) {
088            throw exception;
089        } catch (RuntimeException exception) {
090            throw exception;
091        } catch (Exception exception) {
092            throw new BuildException(exception);
093        }
094    }
095}