Changeset 5584 in josm for trunk/src/org/openstreetmap/josm


Ignore:
Timestamp:
2012-11-17T14:08:43+01:00 (11 years ago)
Author:
Don-vip
Message:

fix #8201 - Error handling or redacted items

Location:
trunk/src/org/openstreetmap/josm
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/ExceptionDialogUtil.java

    r5275 r5584  
    1010import java.net.SocketException;
    1111import java.net.UnknownHostException;
     12import java.util.regex.Matcher;
     13import java.util.regex.Pattern;
    1214
    1315import javax.swing.JOptionPane;
     
    242244    }
    243245
     246    private static boolean isOAuth() {
     247        return Main.pref.get("osm-server.auth-method", "basic").equals("oauth");
     248    }
     249   
    244250    /**
    245251     * Explains a {@link OsmApiException} which was thrown because the authentication at
     
    249255     */
    250256    public static void explainAuthenticationFailed(OsmApiException e) {
    251         String authMethod = Main.pref.get("osm-server.auth-method", "basic");
    252257        String msg;
    253         if (authMethod.equals("oauth")) {
     258        if (isOAuth()) {
    254259            msg = ExceptionUtil.explainFailedOAuthAuthentication(e);
    255260        } else {
     
    268273    /**
    269274     * Explains a {@link OsmApiException} which was thrown because accessing a protected
    270      * resource was forbidden.
     275     * resource was forbidden (HTTP 403).
    271276     *
    272277     * @param e the exception
    273278     */
    274279    public static void explainAuthorizationFailed(OsmApiException e) {
    275         // Fixme: add special handling that calls ExceptionUtil.explainFailedOAuthAuthorisation(e)
    276         HelpAwareOptionPane.showOptionDialog(
    277                 Main.parent,
    278                 ExceptionUtil.explainFailedAuthorisation(e),
     280       
     281        Matcher m;
     282        String msg;
     283        String url = e.getAccessedUrl();
     284        Pattern p = Pattern.compile("http://.*/api/0.6/(node|way|relation)/(\\d+)/(\\d+)");
     285       
     286        // Special case for individual access to redacted versions
     287        // See http://wiki.openstreetmap.org/wiki/Open_Database_License/Changes_in_the_API
     288        if (url != null && (m = p.matcher(url)).matches()) {
     289            String type = m.group(1);
     290            String id = m.group(2);
     291            String version = m.group(3);
     292            // {1} is the translation of "node", "way" or "relation"
     293            msg = tr("Access to redacted version ''{0}'' of {1} {2} is forbidden.",
     294                    version, tr(type), id);
     295        } else if (isOAuth()) {
     296            msg = ExceptionUtil.explainFailedOAuthAuthorisation(e);
     297        } else {
     298            msg = ExceptionUtil.explainFailedAuthorisation(e);
     299        }
     300       
     301        HelpAwareOptionPane.showOptionDialog(
     302                Main.parent,
     303                msg,
    279304                tr("Authorisation Failed"),
    280305                JOptionPane.ERROR_MESSAGE,
    281                 ht("/ErrorMessages#AuthenticationFailed")
     306                ht("/ErrorMessages#AuthorizationFailed")
    282307        );
    283308    }
  • trunk/src/org/openstreetmap/josm/io/OsmApiException.java

    r5386 r5584  
    1414    private String accessedUrl;
    1515
     16    /**
     17     * Constructs an {@code OsmApiException} with the specified response code, error header and error body
     18     * @param responseCode The HTTP response code replied by the OSM server. See {@link java.net.HttpURLConnection HttpURLConnection} for predefined HTTP response code values
     19     * @param errorHeader The error header, as transmitted in the {@code Error} field of the HTTP response header
     20     * @param errorBody The error body, as transmitted in the HTTP response body
     21     * @param accessedUrl The complete URL accessed when this error occured
     22     * @since 5584
     23     */
     24    public OsmApiException(int responseCode, String errorHeader, String errorBody, String accessedUrl) {
     25        this.responseCode = responseCode;
     26        this.errorHeader = errorHeader;
     27        this.errorBody = errorBody;
     28        this.accessedUrl = accessedUrl;
     29    }
     30   
    1631    /**
    1732     * Constructs an {@code OsmApiException} with the specified response code, error header and error body
  • trunk/src/org/openstreetmap/josm/io/OsmServerReader.java

    r5575 r5584  
    122122                    }
    123123
    124                     throw new OsmApiException(activeConnection.getResponseCode(), errorHeader, errorBody.toString());
     124                    throw new OsmApiException(activeConnection.getResponseCode(), errorHeader, errorBody.toString(), url.toString());
    125125                }
    126126
     
    131131                else
    132132                    throw new OsmTransferException(e);
    133 
    134133            }
    135134        } finally {
  • trunk/src/org/openstreetmap/josm/tools/ExceptionUtil.java

    r5525 r5584  
    1717import java.util.Date;
    1818import java.util.Locale;
    19 import java.util.Set;
    2019import java.util.TreeSet;
    2120import java.util.regex.Matcher;
     
    270269        String header = e.getErrorHeader();
    271270        String body = e.getErrorBody();
    272         if (body.equals("Your access to the API is temporarily suspended. Please log-in to the web interface to view the Contributor Terms. You do not need to agree, but you must view them.")) {
    273             return tr("<html>"
    274                     +"Your access to the API is temporarily suspended.<br>"
    275                     + "Please log-in to the web interface to view the Contributor Terms.<br>"
    276                     + "You do not need to agree, but you must view them."
    277                     + "</html>");
    278         }
    279271        String msg = null;
    280272        if (header != null) {
     
    288280        }
    289281       
    290         return tr("<html>"
    291                 + "Authorisation at the OSM server failed.<br>"
    292                 + "The server reported the following error:<br>"
    293                 + "''{0}''"
    294                 + "</html>",
    295                 msg
    296         );
     282        if (msg != null && !msg.isEmpty()) {
     283            return tr("<html>"
     284                    + "Authorisation at the OSM server failed.<br>"
     285                    + "The server reported the following error:<br>"
     286                    + "''{0}''"
     287                    + "</html>",
     288                    msg
     289            );
     290        } else {
     291            return tr("<html>"
     292                    + "Authorisation at the OSM server failed.<br>"
     293                    + "</html>"
     294            );
     295        }
    297296    }
    298297
Note: See TracChangeset for help on using the changeset viewer.