Java tutorial
/* * 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/>. */ /* * DownloadFile.java * Copyright (C) 2011-2016 University of Waikato, Hamilton, New Zealand */ package adams.flow.sink; import adams.core.BufferSupporter; import adams.core.License; import adams.core.QuickInfoHelper; import adams.core.annotation.MixedCopyright; import adams.core.io.FileUtils; import org.apache.commons.codec.binary.Base64; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.net.URL; import java.net.URLConnection; /** <!-- globalinfo-start --> * Downloads a file from a URL and saves it locally.<br> * Also handles basic authentication when using URLs like this:<br> * http://user:pass@domain.com/url * <br><br> <!-- globalinfo-end --> * <!-- flow-summary-start --> * Input/output:<br> * - accepts:<br> * java.lang.String<br> * java.net.URL<br> * <br><br> <!-- flow-summary-end --> * <!-- options-start --> * <pre>-logging-level <OFF|SEVERE|WARNING|INFO|CONFIG|FINE|FINER|FINEST> (property: loggingLevel) * The logging level for outputting errors and debugging output. * default: WARNING * </pre> * * <pre>-name <java.lang.String> (property: name) * The name of the actor. * default: DownloadFile * </pre> * * <pre>-annotation <adams.core.base.BaseAnnotation> (property: annotations) * The annotations to attach to this actor. * default: * </pre> * * <pre>-skip <boolean> (property: skip) * If set to true, transformation is skipped and the input token is just forwarded * as it is. * default: false * </pre> * * <pre>-stop-flow-on-error <boolean> (property: stopFlowOnError) * If set to true, the flow gets stopped in case this actor encounters an error; * useful for critical actors. * default: false * </pre> * * <pre>-silent <boolean> (property: silent) * If enabled, then no errors are output in the console. * default: false * </pre> * * <pre>-output <adams.core.io.PlaceholderFile> (property: outputFile) * The file to save the downloaded content to. * default: ${CWD} * </pre> * * <pre>-buffer-size <int> (property: bufferSize) * The size of byte-buffer used for reading/writing the content. * default: 1024 * minimum: 1 * </pre> * <!-- options-end --> * * @author fracpete (fracpete at waikato dot ac dot nz) * @version $Revision$ */ public class DownloadFile extends AbstractFileWriter implements BufferSupporter { /** for serialization. */ private static final long serialVersionUID = 8688918591152139449L; /** the buffer size to use. */ protected int m_BufferSize; /** * Returns a string describing the object. * * @return a description suitable for displaying in the gui */ @Override public String globalInfo() { return "Downloads a file from a URL and saves it locally.\n" + "Also handles basic authentication when using URLs like this:\n" + "http://user:pass@domain.com/url"; } /** * Adds options to the internal list of options. */ @Override public void defineOptions() { super.defineOptions(); m_OptionManager.add("buffer-size", "bufferSize", 1024, 1, null); } /** * Returns a quick info about the actor, which will be displayed in the GUI. * * @return null if no info available, otherwise short string */ @Override public String getQuickInfo() { String result; result = QuickInfoHelper.toString(this, "bufferSize", m_BufferSize, "Buffer: "); result += QuickInfoHelper.toString(this, "outputFile", m_OutputFile, ", Output: "); return result; } /** * Sets the size of the buffer. * * @param value the size */ public void setBufferSize(int value) { if (getOptionManager().isValid("bufferSize", value)) { m_BufferSize = value; reset(); } } /** * Get output file. * * @return file */ public int getBufferSize() { return m_BufferSize; } /** * Returns the tip text for this property. * * @return tip text for this property suitable for * displaying in the GUI or for listing the options. */ public String bufferSizeTipText() { return "The size of byte-buffer used for reading/writing the content."; } /** * Returns the tip text for this property. * * @return tip text for this property suitable for * displaying in the GUI or for listing the options. */ @Override public String outputFileTipText() { return "The file to save the downloaded content to."; } /** * Returns the class that the consumer accepts. * * @return <!-- flow-accepts-start -->java.lang.String.class, java.net.URL.class<!-- flow-accepts-end --> */ public Class[] accepts() { return new Class[] { String.class, URL.class }; } /** * Executes the flow item. * * @return null if everything is fine, otherwise error message */ @Override @MixedCopyright(author = "http://stackoverflow.com/users/2920131/lboix", license = License.CC_BY_SA_3, url = "http://stackoverflow.com/a/13122190", note = "handling basic authentication") protected String doExecute() { String result; URL url; BufferedInputStream input; BufferedOutputStream output; FileOutputStream fos; byte[] buffer; int len; int count; URLConnection conn; String basicAuth; input = null; output = null; fos = null; try { if (m_InputToken.getPayload() instanceof String) url = new URL((String) m_InputToken.getPayload()); else url = (URL) m_InputToken.getPayload(); conn = url.openConnection(); if (url.getUserInfo() != null) { basicAuth = "Basic " + new String(new Base64().encode(url.getUserInfo().getBytes())); conn.setRequestProperty("Authorization", basicAuth); } input = new BufferedInputStream(conn.getInputStream()); fos = new FileOutputStream(m_OutputFile.getAbsoluteFile()); output = new BufferedOutputStream(fos); buffer = new byte[m_BufferSize]; count = 0; while ((len = input.read(buffer)) > 0) { count++; output.write(buffer, 0, len); if (count % 100 == 0) output.flush(); } output.flush(); result = null; } catch (Exception e) { result = handleException("Problem downloading '" + m_InputToken.getPayload() + "': ", e); } finally { FileUtils.closeQuietly(input); FileUtils.closeQuietly(output); FileUtils.closeQuietly(fos); } return result; } }