Daemon Status Web Service Java Client

From Plnwiki

import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.util.List;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import org.lockss.ws.entities.AuWsResult;
import org.lockss.ws.status.DaemonStatusService;

public class DaemonStatusWsExample {
  private static final String USER_NAME = "lockss-u";
  private static final String PASSWORD = "lockss-p";
  private static final String ADDRESS_LOCATION =
      "http://localhost:8081/ws/DaemonStatusService?wsdl";
  private static final String TARGET_NAMESPACE = "http://status.ws.lockss.org/";
  private static final String SERVICE_NAME = "DaemonStatusServiceImplService";
  private static final String QUERY = "select name, publisher"
      + " where availableFromPublisher = false"
      + " and toDate(creationTime) > toDate('02/Apr/2013')";

  /**
   * The main method.
   * 
   * @param args
   *          A String[] with the command line arguments.
   * @throws Exception
   */
  public static void main(String args[]) throws Exception {
    DaemonStatusWsExample example = new DaemonStatusWsExample();

    // Authenticate.
    example.authenticate();

    // Get the service.
    Service service = Service.create(new URL(ADDRESS_LOCATION), new QName(
	TARGET_NAMESPACE, SERVICE_NAME));

    // Call the service and get the results of the query.
    List<AuWsResult> aus =
	service.getPort(DaemonStatusService.class).queryAus(QUERY);

    if (aus != null) {
      System.out.println("aus.size() = " + aus.size());

      for (AuWsResult auResult : aus) {
	System.out.println("auResult = " + auResult);
      }
    } else {
      System.out.println("aus = " + aus);
    }
  }

  /**
   * Sets the authenticator that will be used by the networking code when the
   * HTTP server asks for authentication.
   */
  private void authenticate() {
    Authenticator.setDefault(new Authenticator() {
      @Override
      protected PasswordAuthentication getPasswordAuthentication() {
	return new PasswordAuthentication(USER_NAME, PASSWORD.toCharArray());
      }
    });
  }
}