import javax.xml.soap.*;
import java.io.ByteArrayOutputStream;
/**
Example client for the Timescales webservice of the IERS Central Bureau
*/
public class TimescalesClient{
public static void main(String args[]) throws Exception {
String result = getTimescalesValue("leapseconds", "2012-03-14 14:23:31");
//Do something with the result value ....
System.out.println(result);
}
/** Get timescales for a given datetime: Returns a single value, e.g. for param "UT1" and datetime "2012-03-14 14:23:31" you will get the equivalent time in UT1. Valid inputs for param: "leapseconds" (the number of leapseconds introduced until the given date), "mjd" (Modified Julian Date), "UT1-UTC" (in seconds), "UT1", "TAI", "TT"
@param param Valid inputs for param: "leapseconds" (the number of leapseconds introduced until the given date), "mjd" (Modified Julian Date), "UT1-UTC" (in seconds), "UT1", "TAI", "TT"
@datetime a date string with format yyyy-mm-dd HH:MM:SS
@return a single value according to the input param
*/
public static String getTimescalesValue(String param, String datetime) throws Exception{
// Create SOAP Connection
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
// Send SOAP Message to SOAP Server
String url = "https://data.iers.org/eris/webservice/timescales/timescalesServer.php";
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(param, datetime), url);
// print SOAP Response
//Convert the response into xml String
ByteArrayOutputStream out = new ByteArrayOutputStream();
soapResponse.writeTo(out);
String strMsg = new String(out.toByteArray());
//extract the result value from the xml string
int resultStart = strMsg.indexOf("", resultStart);
int resultEnd = strMsg.indexOf("");
String result = strMsg.substring(resultStart+1,resultEnd);
soapConnection.close();
return result;
}
//Create the SOAP request message
private static SOAPMessage createSOAPRequest(String param, String datetime) throws Exception {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
String serverURI = "https://data.iers.org/eris/webservice/timescales/timescalesServer.php";
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("example", serverURI);
// SOAP Body
SOAPBody soapBody = envelope.getBody();
SOAPElement soapBodyElem = soapBody.addChildElement("getTimescale", "example");
SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("param", "example");
SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("datetime", "example");
soapBodyElem1.addTextNode(param);
soapBodyElem2.addTextNode(datetime);
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", serverURI + "getTimescale");
soapMessage.saveChanges();
/*
Constructed SOAP Request Message:
leapseconds
2012-03-14 14:23:31
*/
return soapMessage;
}
}