From 6091428fc30f08478fb2bd46cdfec4f34c403260 Mon Sep 17 00:00:00 2001 From: ChaseHsbrk Date: Tue, 10 Dec 2019 13:42:22 +0200 Subject: [PATCH 1/2] Allow for using the Basic Auth or Bearer Schemes So because the Basic Authentication and Bearer Authentication schemes are near identical in how they are formatted when being added to the Authorization Http Request header, I thought, why not just use a token attribute instead that can then be used to set the Authorization header directly due to already being in the needed format. --- .../org/kamranzafar/jddl/Authentication.java | 101 ++++++++---------- 1 file changed, 47 insertions(+), 54 deletions(-) diff --git a/src/main/java/org/kamranzafar/jddl/Authentication.java b/src/main/java/org/kamranzafar/jddl/Authentication.java index 464dfdf..5902ef6 100644 --- a/src/main/java/org/kamranzafar/jddl/Authentication.java +++ b/src/main/java/org/kamranzafar/jddl/Authentication.java @@ -1,61 +1,54 @@ /** - * Copyright 2012 Kamran Zafar - * - * 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. - * + * Copyright 2012 Kamran Zafar + *

+ * 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. */ package org.kamranzafar.jddl; +import org.kamranzafar.jddl.util.Base64; + public class Authentication { - public static enum AuthType { - BASIC - } - - private AuthType authType = AuthType.BASIC; - private String username; - private String password; - - public Authentication() { - } - - public Authentication(String username, String password) { - super(); - this.username = username; - this.password = password; - } - - public AuthType getAuthType() { - return authType; - } - - public void setAuthType(AuthType authType) { - this.authType = authType; - } - - public String getUsername() { - return username; - } - - public void setUsername(String username) { - this.username = username; - } - - public String getPassword() { - return password; - } - - public void setPassword(String password) { - this.password = password; - } + + public enum AuthType { + BASIC, BEARER + } + + private AuthType authType = AuthType.BASIC; + + private String token; + + private Authentication() { + } + + public static Authentication withBasicAuthentication(final String username, final String password) { + Authentication auth = new Authentication(); + auth.authType = AuthType.BASIC; + auth.token = String.format("Basic %s", Base64.encodeBytes(String.format("%s:%s", username, password).getBytes())); + + return auth; + } + + public static Authentication withBearerAuthentication(final String token) { + Authentication auth = new Authentication(); + auth.authType = AuthType.BEARER; + auth.token = String.format("Bearer %s", token); + + return auth; + } + + public String getRequestHeader() { + return this.token; + } + } From 506ef28cdd62b15deb8c8b9b882b53fd9c05a86b Mon Sep 17 00:00:00 2001 From: ChaseHsbrk Date: Tue, 10 Dec 2019 13:46:13 +0200 Subject: [PATCH 2/2] Update DirectDownloader.java Updating to match with the new Authorization class --- .../kamranzafar/jddl/DirectDownloader.java | 398 +++++++++--------- 1 file changed, 197 insertions(+), 201 deletions(-) diff --git a/src/main/java/org/kamranzafar/jddl/DirectDownloader.java b/src/main/java/org/kamranzafar/jddl/DirectDownloader.java index 100147d..1818c1a 100644 --- a/src/main/java/org/kamranzafar/jddl/DirectDownloader.java +++ b/src/main/java/org/kamranzafar/jddl/DirectDownloader.java @@ -33,209 +33,205 @@ /** * @author kamran - * */ public class DirectDownloader extends HttpConnector implements Runnable { - private int poolSize = 3; - private int bufferSize = 2048; - - private DirectDownloadThread[] dts; - private Proxy proxy; - private final BlockingQueue tasks = new LinkedBlockingQueue(); + private int poolSize = 3; + private int bufferSize = 2048; - private static Logger logger = Logger.getLogger(DirectDownloader.class.getName()); + private DirectDownloadThread[] dts; + private Proxy proxy; + private final BlockingQueue tasks = new LinkedBlockingQueue<>(); - public DirectDownloader() { - } - - public DirectDownloader(int poolSize) { - this.poolSize = poolSize; - } - - public DirectDownloader(Proxy proxy) { - this.proxy = proxy; - } - - public DirectDownloader(Proxy proxy, int poolSize) { - this.poolSize = poolSize; - this.proxy = proxy; - } - - protected class DirectDownloadThread extends Thread { - private static final String CD_FNAME = "fname="; - private static final String CONTENT_DISPOSITION = "Content-Disposition"; - - private boolean cancel = false; - private boolean stop = false; - - private final BlockingQueue tasks; - - public DirectDownloadThread(BlockingQueue tasks) { - this.tasks = tasks; - } - - protected void download(DownloadTask dt) throws IOException, InterruptedException, KeyManagementException, - NoSuchAlgorithmException { - HttpURLConnection conn = (HttpURLConnection) getConnection(dt.getUrl(), proxy); - - if (dt.getAuthentication() != null) { - Authentication auth = dt.getAuthentication(); - String authString = auth.getUsername() + ":" + auth.getPassword(); - - conn.setRequestProperty("Authorization", "Basic " + Base64.encodeBytes(authString.getBytes())); - } - - conn.setReadTimeout(dt.getTimeout()); - conn.setDoOutput(true); - conn.connect(); - - int fsize = conn.getContentLength(); - String fname; - - String cd = conn.getHeaderField(CONTENT_DISPOSITION); - - if (cd != null) { - fname = cd.substring(cd.indexOf(CD_FNAME) + 1, cd.length() - 1); - } else { - String url = dt.getUrl().toString(); - fname = url.substring(url.lastIndexOf('/') + 1); - } - - InputStream is = conn.getInputStream(); - - OutputStream os = dt.getOutputStream(); - List listeners = dt.getListeners(); - - byte[] buff = new byte[bufferSize]; - int res; - - for (DownloadListener listener : listeners) { - listener.onStart(fname, fsize); - } - - int total = 0; - while ((res = is.read(buff)) != -1) { - os.write(buff, 0, res); - total += res; - for (DownloadListener listener : listeners) { - listener.onUpdate(res, total); - } - - synchronized (dt) { - // cancel download - if (cancel || dt.isCancelled()) { - close(is, os); - for (DownloadListener listener : listeners) { - listener.onCancel(); - } - - throw new RuntimeException("Cancelled download"); - } - - // stop thread - if (stop) { - close(is, os); - for (DownloadListener listener : listeners) { - listener.onCancel(); - } - - throw new InterruptedException("Shutdown"); - } - - // pause thread - while (dt.isPaused()) { - try { - wait(); - } catch (Exception e) { - } - } - } - } - - for (DownloadListener listener : listeners) { - listener.onComplete(); - } - - close(is, os); - } - - private void close(InputStream is, OutputStream os) { - try { - is.close(); - os.close(); - } catch (IOException e) { - } - } - - @Override - public void run() { - while (true) { - try { - download(tasks.take()); - } catch (InterruptedException e) { - logger.info("Stopping download thread"); - break; - } catch (Exception e) { - e.printStackTrace(); - } - } - } - - public void cancel() { - cancel = true; - } - - public void shutdown() { - stop = true; - } - } - - public void download(DownloadTask dt) { - tasks.add(dt); - } - - public void run() { - logger.info("Initializing downloader..."); - - dts = new DirectDownloadThread[poolSize]; - - for (int i = 0; i < dts.length; i++) { - dts[i] = new DirectDownloadThread(tasks); - dts[i].start(); - } - - logger.info("Downloader started, waiting for tasks."); - } - - public void shutdown() { - for (int i = 0; i < dts.length; i++) { - if (dts[i] != null) { - dts[i].shutdown(); - } - } - } - - public void cancelAll() { - for (int i = 0; i < dts.length; i++) { - if (dts[i] != null) { - dts[i].cancel(); - } - } - } - - public int getPoolSize() { - return poolSize; - } - - public void setPoolSize(int poolSize) { - this.poolSize = poolSize; - } - - public int getBufferSize() { - return bufferSize; - } - - public void setBufferSize(int bufferSize) { - this.bufferSize = bufferSize; - } + private static Logger logger = Logger.getLogger(DirectDownloader.class.getName()); + + public DirectDownloader() { + } + + public DirectDownloader(int poolSize) { + this.poolSize = poolSize; + } + + public DirectDownloader(Proxy proxy) { + this.proxy = proxy; + } + + public DirectDownloader(Proxy proxy, int poolSize) { + this.poolSize = poolSize; + this.proxy = proxy; + } + + protected class DirectDownloadThread extends Thread { + private static final String CD_FNAME = "fname="; + private static final String CONTENT_DISPOSITION = "Content-Disposition"; + + private boolean cancel = false; + private boolean stop = false; + + private final BlockingQueue tasks; + + public DirectDownloadThread(BlockingQueue tasks) { + this.tasks = tasks; + } + + protected void download(DownloadTask dt) throws IOException, InterruptedException, KeyManagementException, + NoSuchAlgorithmException { + HttpURLConnection conn = (HttpURLConnection) getConnection(dt.getUrl(), proxy); + + if (dt.getAuthentication() != null) { + conn.setRequestProperty("Authorization", dt.getAuthentication().getRequestHeader()); + } + + conn.setReadTimeout(dt.getTimeout()); + conn.setDoOutput(true); + conn.connect(); + + int fsize = conn.getContentLength(); + String fname; + + String cd = conn.getHeaderField(CONTENT_DISPOSITION); + + if (cd != null) { + fname = cd.substring(cd.indexOf(CD_FNAME) + 1, cd.length() - 1); + } else { + String url = dt.getUrl().toString(); + fname = url.substring(url.lastIndexOf('/') + 1); + } + + InputStream is = conn.getInputStream(); + + OutputStream os = dt.getOutputStream(); + List listeners = dt.getListeners(); + + byte[] buff = new byte[bufferSize]; + int res; + + for (DownloadListener listener : listeners) { + listener.onStart(fname, fsize); + } + + int total = 0; + while ((res = is.read(buff)) != -1) { + os.write(buff, 0, res); + total += res; + for (DownloadListener listener : listeners) { + listener.onUpdate(res, total); + } + + synchronized (dt) { + // cancel download + if (cancel || dt.isCancelled()) { + close(is, os); + for (DownloadListener listener : listeners) { + listener.onCancel(); + } + + throw new RuntimeException("Cancelled download"); + } + + // stop thread + if (stop) { + close(is, os); + for (DownloadListener listener : listeners) { + listener.onCancel(); + } + + throw new InterruptedException("Shutdown"); + } + + // pause thread + while (dt.isPaused()) { + try { + wait(); + } catch (Exception e) { + } + } + } + } + + for (DownloadListener listener : listeners) { + listener.onComplete(); + } + + close(is, os); + } + + private void close(InputStream is, OutputStream os) { + try { + is.close(); + os.close(); + } catch (IOException e) { + } + } + + @Override + public void run() { + while (true) { + try { + download(tasks.take()); + } catch (InterruptedException e) { + logger.info("Stopping download thread"); + break; + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + public void cancel() { + cancel = true; + } + + public void shutdown() { + stop = true; + } + } + + public void download(DownloadTask dt) { + tasks.add(dt); + } + + public void run() { + logger.info("Initializing downloader..."); + + dts = new DirectDownloadThread[poolSize]; + + for (int i = 0; i < dts.length; i++) { + dts[i] = new DirectDownloadThread(tasks); + dts[i].start(); + } + + logger.info("Downloader started, waiting for tasks."); + } + + public void shutdown() { + for (final DirectDownloadThread dt : dts) { + if (dt != null) { + dt.shutdown(); + } + } + } + + public void cancelAll() { + for (final DirectDownloadThread dt : dts) { + if (dt != null) { + dt.cancel(); + } + } + } + + public int getPoolSize() { + return poolSize; + } + + public void setPoolSize(int poolSize) { + this.poolSize = poolSize; + } + + public int getBufferSize() { + return bufferSize; + } + + public void setBufferSize(int bufferSize) { + this.bufferSize = bufferSize; + } }