001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.actions.downloadtasks; 003 004import java.util.ArrayList; 005import java.util.Arrays; 006import java.util.List; 007 008import org.openstreetmap.josm.data.ProjectionBounds; 009import org.openstreetmap.josm.io.UrlPattern; 010 011/** 012 * Common abstract implementation of other download tasks. 013 * @param <T> The downloaded data type 014 * @since 2322 015 */ 016public abstract class AbstractDownloadTask<T> implements DownloadTask { 017 private final List<Object> errorMessages; 018 private boolean canceled; 019 private boolean failed; 020 protected T downloadedData; 021 protected boolean zoomAfterDownload = true; 022 023 /** 024 * Constructs a new {@code AbstractDownloadTask}. 025 */ 026 protected AbstractDownloadTask() { 027 errorMessages = new ArrayList<>(); 028 } 029 030 /** 031 * Determines if the download task has been canceled. 032 * @return {@code true} if the download task has been canceled 033 */ 034 public boolean isCanceled() { 035 return canceled; 036 } 037 038 /** 039 * Marks this download task as canceled. 040 * @param canceled {@code true} to mark this download task as canceled 041 */ 042 public void setCanceled(boolean canceled) { 043 this.canceled = canceled; 044 } 045 046 /** 047 * Determines if the download task has failed. 048 * @return {@code true} if the download task has failed 049 */ 050 public boolean isFailed() { 051 return failed; 052 } 053 054 /** 055 * Marks this download task as failed. 056 * @param failed {@code true} to mark this download task as failed 057 */ 058 public void setFailed(boolean failed) { 059 this.failed = failed; 060 } 061 062 protected static <T extends Enum<T> & UrlPattern> String[] patterns(Class<T> urlPatternEnum) { 063 return Arrays.stream(urlPatternEnum.getEnumConstants()).map(UrlPattern::pattern).toArray(String[]::new); 064 } 065 066 protected final void rememberErrorMessage(String message) { 067 errorMessages.add(message); 068 } 069 070 protected final void rememberException(Exception exception) { 071 errorMessages.add(exception); 072 } 073 074 protected final void rememberDownloadedData(T data) { 075 this.downloadedData = data; 076 } 077 078 /** 079 * Replies the downloaded data. 080 * @return The downloaded data. 081 */ 082 public final T getDownloadedData() { 083 return downloadedData; 084 } 085 086 @Override 087 public final void setZoomAfterDownload(boolean zoomAfterDownload) { 088 this.zoomAfterDownload = zoomAfterDownload; 089 } 090 091 @Override 092 public List<Object> getErrorObjects() { 093 return errorMessages; 094 } 095 096 /** 097 * Determines if the given URL is accepted by {@link #getPatterns}. 098 * Can be overridden for more complex checking logic. 099 * @param url URL to download 100 * @return {@code true} if this URL is accepted 101 */ 102 public boolean acceptsUrl(String url) { 103 return url != null && Arrays.stream(getPatterns()).anyMatch(url::matches); 104 } 105 106 /** 107 * Check / decide if the task is safe for remotecontrol. 108 * <p> 109 * Keep in mind that a potential attacker has full control over the content 110 * of the file that will be downloaded. 111 * If it is possible to run arbitrary code or write to the local file 112 * system, then the task is (obviously) not save for remote execution. 113 * <p> 114 * The default value is false = unsafe. Override in a subclass to 115 * allow running the task via remotecontol. 116 * 117 * @return true if it is safe to download and open any file of the 118 * corresponding format, false otherwise 119 */ 120 public boolean isSafeForRemotecontrolRequests() { 121 return false; 122 } 123 124 @Override 125 public boolean acceptsUrl(String url, boolean isRemotecontrol) { 126 if (isRemotecontrol && !isSafeForRemotecontrolRequests()) 127 return false; 128 return acceptsUrl(url); 129 } 130 131 // Default name to keep old plugins compatible 132 @Override 133 public String getTitle() { 134 return getClass().getName(); 135 } 136 137 @Override 138 public String toString() { 139 return this.getTitle(); 140 } 141 142 // Default pattern to keep old plugins compatible 143 @Override 144 public String[] getPatterns() { 145 return new String[]{}; 146 } 147 148 /** 149 * Returns the projection bounds of downloaded data. 150 * @return the projection bounds of downloaded data or {@code null} 151 * @since 11774 152 */ 153 public ProjectionBounds getDownloadProjectionBounds() { 154 return null; 155 } 156}