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

fix #13115 - fix NPE, improve WMS errors handling

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.