0

I'm working on a project that needs to pull current and historical weather data, and so far, I've been attempting to use the library https://github.com/sudohippie/weather-noaa, modifying it to work with Java 8 (as the last commit was in 2013), but I can't seem to make its SOAP client work. The relevant code and error logs are below.

package sudoHippieNOAA.service.soap;

import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Raghav Sidhanti
 * 9/12/13
 */
public class SOAPClientTest {
    public static void main(String[] args) throws IOException, SOAPException {
        String endpoint = " https://graphical.weather.gov:443/xml/SOAP_server/ndfdXMLserver.php";
        String methodName = "GmlTimeSeries";

        Map<String, String> params = new LinkedHashMap<String, String>();
        params.put("listLatLon", "38.99,-77.02 39.70,-104.80");
        params.put("startTime", "2004-01-01T00:00:00");
        params.put("endTime", "2017-09-12T00:00:00");
        params.put("compType", "Between");
        params.put("featureType", "Forecast_Gml2Point");
        params.put("propertyName", "maxt,mint,wx");

        SOAPClient client = new SOAPClient(endpoint);
        SOAPMessage message = client.getDataAsSOAPMessage(methodName, params);

        System.out.println(NOAASOAPUtil.getSOAPMessageContentAsString(message));
    }
}
package sudoHippieNOAA.service.soap;

import javax.xml.soap.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;

/**
 * Raghav Sidhanti
 * 9/11/13
 */
public class SOAPClient {
    private final String endpoint;

    public SOAPClient(String endpoint) {
        this.endpoint = endpoint;
    }

    public SOAPMessage getDataAsSOAPMessage(String methodName, Map<String, String> args) throws MalformedURLException, SOAPException {
        // construct message
        SOAPMessage requestMessage = buildMessage(methodName, args);

        // connect to server and retrieve information
        SOAPMessage responseMessage = requestForData(requestMessage);

        return responseMessage;
    }

    private SOAPMessage requestForData(SOAPMessage message) throws SOAPException, MalformedURLException {
        SOAPConnectionFactory connectionFactory = SOAPConnectionFactory.newInstance();
        SOAPConnection connection = connectionFactory.createConnection();

        try{
            return connection.call(message, new URL(endpoint));
        }finally {
            connection.close();
        }
    }

    private SOAPMessage buildMessage(String methodName, Map<String, String> args) throws SOAPException {
        // construct a message
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage message = messageFactory.createMessage();

        // getData message envelope and body
        SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
        SOAPBody body = envelope.getBody();

        // create body element
        Name bodyName = envelope.createName(methodName, "m", endpoint);
        SOAPBodyElement bodyElement = body.addBodyElement(bodyName);

        // add parameters as children of body element
        for(String param : args.keySet()){
            Name name = envelope.createName(param);
            bodyElement.addChildElement(name).addTextNode(args.get(param));
        }

        message.saveChanges();

        return message;
    }

    String getEndpoint() {
        return endpoint;
    }
}
Exception in thread "main" com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Invalid Content-Type:text/html. Is this an error message instead of a SOAP response?
    at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(Unknown Source)
    at sudoHippieNOAA.service.soap.SOAPClient.requestForData(SOAPClient.java:34)
    at sudoHippieNOAA.service.soap.SOAPClient.getDataAsSOAPMessage(SOAPClient.java:24)
    at sudoHippieNOAA.service.soap.SOAPClientTest.main(SOAPClientTest.java:27)
Caused by: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Invalid Content-Type:text/html. Is this an error message instead of a SOAP response?
    at com.sun.xml.internal.messaging.saaj.soap.MessageImpl.identifyContentType(Unknown Source)
    at com.sun.xml.internal.messaging.saaj.soap.MessageFactoryImpl.createMessage(Unknown Source)
    at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(Unknown Source)
    ... 4 more

CAUSE:

com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Invalid Content-Type:text/html. Is this an error message instead of a SOAP response?
    at com.sun.xml.internal.messaging.saaj.soap.MessageImpl.identifyContentType(Unknown Source)
    at com.sun.xml.internal.messaging.saaj.soap.MessageFactoryImpl.createMessage(Unknown Source)
    at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(Unknown Source)
    at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(Unknown Source)
    at sudoHippieNOAA.service.soap.SOAPClient.requestForData(SOAPClient.java:34)
    at sudoHippieNOAA.service.soap.SOAPClient.getDataAsSOAPMessage(SOAPClient.java:24)
    at sudoHippieNOAA.service.soap.SOAPClientTest.main(SOAPClientTest.java:27)

Does anybody know what the problem is, how to fix it, or any libraries that are better maintained and serve the same purpose (note: as long as I can get radar reflectivity data and ideally humidity and temperature as well, it works)? It doesn't need to pull data from NOAA; WeatherUnderground or a similar service would also work.

Z. Urq.
  • 33
  • 1
  • 6
  • By default for HTTP requests is used content type 'text' (you can see it in error log). You need to set it to correct type (I guess, it should be text/xml or application/xml) – Bor Laze Jan 28 '19 at 20:18
  • As I am completely new to working with web services, I don't know how to do that. How do I set it to text/xml or application/xml? Is it something to do with "addTextNode()" near the end of "buildMessage()" in the second piece of code? – Z. Urq. Jan 29 '19 at 14:40

0 Answers0