Changeset 10520 in josm for trunk/src/org


Ignore:
Timestamp:
2016-07-06T22:51:51+02:00 (8 years ago)
Author:
Don-vip
Message:

fix #13115 - fix NPE, improve WMS errors handling

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

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/preferences/imagery/AddWMSLayerPanel.java

    r10179 r10520  
    8484                    formats.setSelectedItem(wms.getPreferredFormats());
    8585                } catch (MalformedURLException ex) {
     86                    Main.error(ex, false);
    8687                    JOptionPane.showMessageDialog(getParent(), tr("Invalid service URL."),
    8788                            tr("WMS Error"), JOptionPane.ERROR_MESSAGE);
    8889                } catch (IOException ex) {
     90                    Main.error(ex, false);
    8991                    JOptionPane.showMessageDialog(getParent(), tr("Could not retrieve WMS layer list."),
    9092                            tr("WMS Error"), JOptionPane.ERROR_MESSAGE);
     
    9395                    String title = tr("WMS Error");
    9496                    String message = tr("Could not parse WMS layer list.");
    95                     Main.error("Could not parse WMS layer list. Incoming data:\n"+incomingData);
    96                     if (incomingData != null
    97                             && (incomingData.startsWith("<html>") || incomingData.startsWith("<HTML>"))
    98                             && (incomingData.endsWith("</html>") || incomingData.endsWith("</HTML>"))) {
     97                    Main.error(ex, "Could not parse WMS layer list. Incoming data:\n"+incomingData);
     98                    if ((incomingData.startsWith("<html>") || incomingData.startsWith("<HTML>"))
     99                      && (incomingData.endsWith("</html>") || incomingData.endsWith("</HTML>"))) {
    99100                        GuiHelper.notifyUserHtmlError(AddWMSLayerPanel.this, title, message, incomingData);
    100101                    } else {
     102                        if (ex.getMessage() != null) {
     103                            message += '\n' + ex.getMessage();
     104                        }
    101105                        JOptionPane.showMessageDialog(getParent(), message, title, JOptionPane.ERROR_MESSAGE);
    102106                    }
  • trunk/src/org/openstreetmap/josm/io/imagery/WMSImagery.java

    r10404 r10520  
    4040        private final String incomingData;
    4141
     42        /**
     43         * Constructs a new {@code WMSGetCapabilitiesException}
     44         * @param cause the cause (which is saved for later retrieval by the {@link #getCause()} method)
     45         * @param incomingData the answer from WMS server
     46         */
    4247        public WMSGetCapabilitiesException(Throwable cause, String incomingData) {
    4348            super(cause);
     
    4550        }
    4651
     52        /**
     53         * Constructs a new {@code WMSGetCapabilitiesException}
     54         * @param message   the detail message. The detail message is saved for later retrieval by the {@link #getMessage()} method
     55         * @param incomingData the answer from the server
     56         * @since 10520
     57         */
     58        public WMSGetCapabilitiesException(String message, String incomingData) {
     59            super(message);
     60            this.incomingData = incomingData;
     61        }
     62
     63        /**
     64         * Returns the answer from WMS server.
     65         * @return the answer from WMS server
     66         */
    4767        public String getIncomingData() {
    4868            return incomingData;
     
    160180            });
    161181            Document document = builder.parse(new InputSource(new StringReader(incomingData)));
     182            Element root = document.getDocumentElement();
     183
     184            // Check if the request resulted in ServiceException
     185            if ("ServiceException".equals(root.getTagName())) {
     186                throw new WMSGetCapabilitiesException(root.getTextContent(), incomingData);
     187            }
    162188
    163189            // Some WMS service URLs specify a different base URL for their GetMap service
    164             Element child = getChild(document.getDocumentElement(), "Capability");
     190            Element child = getChild(root, "Capability");
    165191            child = getChild(child, "Request");
    166192            child = getChild(child, "GetMap");
     
    197223            }
    198224
    199             Element capabilityElem = getChild(document.getDocumentElement(), "Capability");
     225            Element capabilityElem = getChild(root, "Capability");
    200226            List<Element> children = getChildren(capabilityElem, "Layer");
    201227            layers = parseLayers(children, new HashSet<String>());
     
    327353    private static List<Element> getChildren(Element parent, String name) {
    328354        List<Element> retVal = new ArrayList<>();
    329         for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {
    330             if (child instanceof Element && name.equals(child.getNodeName())) {
    331                 retVal.add((Element) child);
     355        if (parent != null) {
     356            for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {
     357                if (child instanceof Element && name.equals(child.getNodeName())) {
     358                    retVal.add((Element) child);
     359                }
    332360            }
    333361        }
Note: See TracChangeset for help on using the changeset viewer.