001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.actions.downloadtasks;
003
004import java.util.ArrayList;
005import java.util.List;
006import org.openstreetmap.josm.io.XmlWriter;
007
008/**
009 * Common abstract implementation of other download tasks
010 * @since 2322
011 */
012public abstract class AbstractDownloadTask implements DownloadTask {
013    private List<Object> errorMessages;
014    private boolean canceled = false;
015    private boolean failed = false;
016
017    public AbstractDownloadTask() {
018        errorMessages = new ArrayList<>();
019    }
020
021    public boolean isCanceled() {
022        return canceled;
023    }
024
025    public void setCanceled(boolean canceled) {
026        this.canceled = canceled;
027    }
028
029    public boolean isFailed() {
030        return failed;
031    }
032
033    public void setFailed(boolean failed) {
034        this.failed = failed;
035    }
036
037    protected void rememberErrorMessage(String message) {
038        errorMessages.add(message);
039    }
040
041    protected void rememberException(Exception exception) {
042        errorMessages.add(exception);
043    }
044
045    @Override
046    public List<Object> getErrorObjects() {
047        return errorMessages;
048    }
049
050    @Override
051    public String acceptsDocumentationSummary() {
052        StringBuilder buf = new StringBuilder("<tr><td>");
053        buf.append(getTitle());
054        buf.append(":</td><td>");
055        String[] patterns = getPatterns();
056        if (patterns.length>0) {
057            buf.append("<ul>");
058            for (String pattern: patterns) {
059                buf.append("<li>");
060                buf.append(XmlWriter.encode(pattern));
061                buf.append("</li>");
062            }
063            buf.append("</ul>");
064        }
065        buf.append("</td></tr>");
066        return buf.toString();
067    }
068
069    // Can be overridden for more complex checking logic
070    public boolean acceptsUrl(String url) {
071        if (url==null) return false;
072        for (String p: getPatterns()) {
073            if (url.matches(p)) {
074                return true;
075            }
076        }
077        return false;
078    }
079
080    /**
081     * Check / decide if the task is safe for remotecontrol.
082     * 
083     * Keep in mind that a potential attacker has full control over the content
084     * of the file that will be downloaded.
085     * If it is possible to run arbitrary code or write to the local file
086     * system, then the task is (obviously) not save for remote execution.
087     * 
088     * The default value is false = unsafe. Override in a subclass to
089     * allow running the task via remotecontol.
090     * 
091     * @return true if it is safe to download and open any file of the
092     * corresponding format, false otherwise
093     */
094    public boolean isSafeForRemotecontrolRequests() {
095        return false;
096    }
097
098    @Override
099    public boolean acceptsUrl(String url, boolean isRemotecontrol) {
100        if (isRemotecontrol && !isSafeForRemotecontrolRequests()) return false;
101        return acceptsUrl(url);
102    }
103
104    // Default name to keep old plugins compatible
105    @Override
106    public String getTitle() {
107        return getClass().getName();
108    }
109
110    // Default pattern to keep old plugins compatible
111    @Override
112    public String[] getPatterns() {
113        return new String[]{};
114    }
115
116}