| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.gui.preferences.server;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 5 |
|
|---|
| 6 | import java.awt.Component;
|
|---|
| 7 | import java.io.IOException;
|
|---|
| 8 | import java.net.HttpURLConnection;
|
|---|
| 9 | import java.net.MalformedURLException;
|
|---|
| 10 | import java.net.URL;
|
|---|
| 11 |
|
|---|
| 12 | import javax.swing.JOptionPane;
|
|---|
| 13 | import javax.xml.parsers.ParserConfigurationException;
|
|---|
| 14 |
|
|---|
| 15 | import org.openstreetmap.josm.gui.HelpAwareOptionPane;
|
|---|
| 16 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
|---|
| 17 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
|---|
| 18 | import org.openstreetmap.josm.io.Capabilities;
|
|---|
| 19 | import org.openstreetmap.josm.io.OsmTransferException;
|
|---|
| 20 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
|---|
| 21 | import org.openstreetmap.josm.tools.HttpClient;
|
|---|
| 22 | import org.openstreetmap.josm.tools.Logging;
|
|---|
| 23 | import org.xml.sax.InputSource;
|
|---|
| 24 | import org.xml.sax.SAXException;
|
|---|
| 25 |
|
|---|
| 26 | /**
|
|---|
| 27 | * This is an asynchronous task for testing whether an URL points to an OSM API server.
|
|---|
| 28 | * It tries to retrieve capabilities from the given URL. If it succeeds, the method
|
|---|
| 29 | * {@link #isSuccess()} replies true, otherwise false.
|
|---|
| 30 | * @since 2745
|
|---|
| 31 | */
|
|---|
| 32 | public class ApiUrlTestTask extends PleaseWaitRunnable {
|
|---|
| 33 |
|
|---|
| 34 | private final String url;
|
|---|
| 35 | private boolean canceled;
|
|---|
| 36 | private boolean success;
|
|---|
| 37 | private final Component parent;
|
|---|
| 38 | private HttpClient connection;
|
|---|
| 39 |
|
|---|
| 40 | /**
|
|---|
| 41 | * Constructs a new {@code ApiUrlTestTask}.
|
|---|
| 42 | *
|
|---|
| 43 | * @param parent the parent component relative to which the {@link PleaseWaitRunnable}-Dialog is displayed
|
|---|
| 44 | * @param url the url. Must not be null.
|
|---|
| 45 | * @throws IllegalArgumentException if url is null.
|
|---|
| 46 | */
|
|---|
| 47 | public ApiUrlTestTask(Component parent, String url) {
|
|---|
| 48 | super(parent, tr("Testing OSM API URL ''{0}''", url), false /* don't ignore exceptions */);
|
|---|
| 49 | CheckParameterUtil.ensureParameterNotNull(url, "url");
|
|---|
| 50 | this.parent = parent;
|
|---|
| 51 | this.url = url;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | protected void alertInvalidUrl(String url) {
|
|---|
| 55 | HelpAwareOptionPane.showMessageDialogInEDT(
|
|---|
| 56 | parent,
|
|---|
| 57 | tr("<html>"
|
|---|
| 58 | + "''{0}'' is not a valid OSM API URL.<br>"
|
|---|
| 59 | + "Please check the spelling and validate again."
|
|---|
| 60 | + "</html>",
|
|---|
| 61 | url
|
|---|
| 62 | ),
|
|---|
| 63 | tr("Invalid API URL"),
|
|---|
| 64 | JOptionPane.ERROR_MESSAGE,
|
|---|
| 65 | HelpUtil.ht("/Preferences/Connection#InvalidAPIUrl")
|
|---|
| 66 | );
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | protected void alertInvalidCapabilitiesUrl(String url) {
|
|---|
| 70 | HelpAwareOptionPane.showMessageDialogInEDT(
|
|---|
| 71 | parent,
|
|---|
| 72 | tr("<html>"
|
|---|
| 73 | + "Failed to build URL ''{0}'' for validating the OSM API server.<br>"
|
|---|
| 74 | + "Please check the spelling of ''{1}'' and validate again."
|
|---|
| 75 | +"</html>",
|
|---|
| 76 | url,
|
|---|
| 77 | getNormalizedApiUrl()
|
|---|
| 78 | ),
|
|---|
| 79 | tr("Invalid API URL"),
|
|---|
| 80 | JOptionPane.ERROR_MESSAGE,
|
|---|
| 81 | HelpUtil.ht("/Preferences/Connection#InvalidAPIGetChangesetsUrl")
|
|---|
| 82 | );
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | protected void alertConnectionFailed() {
|
|---|
| 86 | HelpAwareOptionPane.showMessageDialogInEDT(
|
|---|
| 87 | parent,
|
|---|
| 88 | tr("<html>"
|
|---|
| 89 | + "Failed to connect to the URL ''{0}''.<br>"
|
|---|
| 90 | + "Please check the spelling of ''{1}'' and your Internet connection and validate again."
|
|---|
| 91 | +"</html>",
|
|---|
| 92 | url,
|
|---|
| 93 | getNormalizedApiUrl()
|
|---|
| 94 | ),
|
|---|
| 95 | tr("Connection to API failed"),
|
|---|
| 96 | JOptionPane.ERROR_MESSAGE,
|
|---|
| 97 | HelpUtil.ht("/Preferences/Connection#ConnectionToAPIFailed")
|
|---|
| 98 | );
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | protected void alertInvalidServerResult(int retCode) {
|
|---|
| 102 | HelpAwareOptionPane.showMessageDialogInEDT(
|
|---|
| 103 | parent,
|
|---|
| 104 | tr("<html>"
|
|---|
| 105 | + "Failed to retrieve a list of changesets from the OSM API server at<br>"
|
|---|
| 106 | + "''{1}''. The server responded with the return code {0} instead of 200.<br>"
|
|---|
| 107 | + "Please check the spelling of ''{1}'' and validate again."
|
|---|
| 108 | + "</html>",
|
|---|
| 109 | retCode,
|
|---|
| 110 | getNormalizedApiUrl()
|
|---|
| 111 | ),
|
|---|
| 112 | tr("Connection to API failed"),
|
|---|
| 113 | JOptionPane.ERROR_MESSAGE,
|
|---|
| 114 | HelpUtil.ht("/Preferences/Connection#InvalidServerResult")
|
|---|
| 115 | );
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | protected void alertInvalidCapabilities() {
|
|---|
| 119 | HelpAwareOptionPane.showMessageDialogInEDT(
|
|---|
| 120 | parent,
|
|---|
| 121 | tr("<html>"
|
|---|
| 122 | + "The OSM API server at ''{0}'' did not return a valid response.<br>"
|
|---|
| 123 | + "It is likely that ''{0}'' is not an OSM API server.<br>"
|
|---|
| 124 | + "Please check the spelling of ''{0}'' and validate again."
|
|---|
| 125 | + "</html>",
|
|---|
| 126 | getNormalizedApiUrl()
|
|---|
| 127 | ),
|
|---|
| 128 | tr("Connection to API failed"),
|
|---|
| 129 | JOptionPane.ERROR_MESSAGE,
|
|---|
| 130 | HelpUtil.ht("/Preferences/Connection#InvalidSettings")
|
|---|
| 131 | );
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | @Override
|
|---|
| 135 | protected void cancel() {
|
|---|
| 136 | canceled = true;
|
|---|
| 137 | synchronized (this) {
|
|---|
| 138 | if (connection != null) {
|
|---|
| 139 | connection.disconnect();
|
|---|
| 140 | }
|
|---|
| 141 | }
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | @Override
|
|---|
| 145 | protected void finish() {
|
|---|
| 146 | // Do nothing
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | /**
|
|---|
| 150 | * Removes leading and trailing whitespace from the API URL and removes trailing '/'.
|
|---|
| 151 | *
|
|---|
| 152 | * @return the normalized API URL
|
|---|
| 153 | */
|
|---|
| 154 | protected String getNormalizedApiUrl() {
|
|---|
| 155 | String apiUrl = url.trim();
|
|---|
| 156 | while (apiUrl.endsWith("/")) {
|
|---|
| 157 | apiUrl = apiUrl.substring(0, apiUrl.lastIndexOf('/'));
|
|---|
| 158 | }
|
|---|
| 159 | return apiUrl;
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | @Override
|
|---|
| 163 | protected void realRun() throws SAXException, IOException, OsmTransferException {
|
|---|
| 164 | try {
|
|---|
| 165 | try {
|
|---|
| 166 | new URL(getNormalizedApiUrl());
|
|---|
| 167 | } catch (MalformedURLException e) {
|
|---|
| 168 | alertInvalidUrl(getNormalizedApiUrl());
|
|---|
| 169 | return;
|
|---|
| 170 | }
|
|---|
| 171 | URL capabilitiesUrl;
|
|---|
| 172 | String getCapabilitiesUrl = getNormalizedApiUrl() + "/0.6/capabilities";
|
|---|
| 173 | try {
|
|---|
| 174 | capabilitiesUrl = new URL(getCapabilitiesUrl);
|
|---|
| 175 | } catch (MalformedURLException e) {
|
|---|
| 176 | alertInvalidCapabilitiesUrl(getCapabilitiesUrl);
|
|---|
| 177 | return;
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | synchronized (this) {
|
|---|
| 181 | connection = HttpClient.create(capabilitiesUrl);
|
|---|
| 182 | connection.connect();
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | if (connection.getResponse().getResponseCode() != HttpURLConnection.HTTP_OK) {
|
|---|
| 186 | alertInvalidServerResult(connection.getResponse().getResponseCode());
|
|---|
| 187 | return;
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | try {
|
|---|
| 191 | Capabilities.CapabilitiesParser.parse(new InputSource(connection.getResponse().getContent()));
|
|---|
| 192 | } catch (SAXException | ParserConfigurationException e) {
|
|---|
| 193 | Logging.warn(e);
|
|---|
| 194 | alertInvalidCapabilities();
|
|---|
| 195 | return;
|
|---|
| 196 | }
|
|---|
| 197 | success = true;
|
|---|
| 198 | } catch (IOException e) {
|
|---|
| 199 | if (canceled)
|
|---|
| 200 | // ignore exceptions
|
|---|
| 201 | return;
|
|---|
| 202 | Logging.error(e);
|
|---|
| 203 | alertConnectionFailed();
|
|---|
| 204 | }
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | /**
|
|---|
| 208 | * Determines if the test has been canceled.
|
|---|
| 209 | * @return {@code true} if canceled, {@code false} otherwise
|
|---|
| 210 | */
|
|---|
| 211 | public boolean isCanceled() {
|
|---|
| 212 | return canceled;
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | /**
|
|---|
| 216 | * Determines if the test has succeeded.
|
|---|
| 217 | * @return {@code true} if success, {@code false} otherwise
|
|---|
| 218 | */
|
|---|
| 219 | public boolean isSuccess() {
|
|---|
| 220 | return success;
|
|---|
| 221 | }
|
|---|
| 222 | }
|
|---|