/* * NOSA HEADER START * * The contents of this file are subject to the terms of the NASA Open * Source Agreement (NOSA), Version 1.3 only (the "Agreement"). You may * not use this file except in compliance with the Agreement. * * You can obtain a copy of the agreement at * docs/NASA_Open_Source_Agreement_1.3.txt * or * http://cdaweb.gsfc.nasa.gov/WebServices/NASA_Open_Source_Agreement_1.3.txt. * * See the Agreement for the specific language governing permissions * and limitations under the Agreement. * * When distributing Covered Code, include this NOSA HEADER in each * file and include the Agreement file at * docs/NASA_Open_Source_Agreement_1.3.txt. If applicable, add the * following below this NOSA HEADER, with the fields enclosed by * brackets "[]" replaced with your own identifying information: * Portions Copyright [yyyy] [name of copyright owner] * * NOSA HEADER END * * Copyright (c) 2008-2010 United States Government as represented by * the National Aeronautics and Space Administration. No copyright is * claimed in the United States under Title 17, U.S.Code. All Other * Rights Reserved. * */ package gov.nasa.gsfc.spdf.helm.test; import java.io.InputStream; import java.io.OutputStream; import java.net.Authenticator; import java.net.URL; import java.net.HttpURLConnection; import java.util.List; import java.util.Map; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.UniformInterfaceException; import com.sun.jersey.api.client.WebResource; import org.w3._1999.xhtml.Html; import gov.nasa.gsfc.helm.data.schema.Eventlist; /** * This class represents an example client of Heliophysics Event List * Manager (HELM) Web services. It utilizes the client API of the * Jersey JAX-RS (JSR 311) * reference implementation. A similar example could be implemented * using any of the other competing APIs. * * @version $Revision: $ * @author B. Harris */ public class JaxRsExample { /** * Client of HELM RESTful Web services. */ private Client client = Client.create(); /** * JAXB context for HELM XML data. */ private JAXBContext helmJaxbContext = JAXBContext.newInstance("gov.nasa.gsfc.helm.data.schema"); /** * JAXB context for XHTML data. */ private JAXBContext xhtmlJaxbContext = JAXBContext.newInstance("org.w3._1999.xhtml"); /** * This example's user-agent value. */ private static final String EXAMPLE_USER_AGENT = "JaxRsExample"; public JaxRsExample() throws JAXBException { } /** * Executes this example. * * @param endpoint base URL of HELM Web services * @param listname name of an eventlist */ public void execute(String endpoint, String listname) throws Exception { /* execute OPTIONS, HEAD? System.out.println("Invoking HTTP OPTIONS " + url); Wadl wadl = helm.options(Wadl.class); */ Html eventlists = getEventlists(endpoint); System.out.println("eventlists:"); marshal(System.out, eventlists); Eventlist eventlist = getEventlist(endpoint, listname); System.out.println("eventlist:"); marshal(System.out, eventlist); String jsonEventlist = getJsonEventlist(endpoint, listname); System.out.println("JSON eventlist:"); System.out.println(" " + jsonEventlist); } private Html getEventlists(String endpoint) { String url = endpoint + "/eventlists/"; WebResource helm = client.resource(url); WebResource.Builder request = helm.accept(MediaType.APPLICATION_XHTML_XML); request.header(HttpHeaders.USER_AGENT, EXAMPLE_USER_AGENT); System.out.println("Invoking HTTP GET " + url); return request.get(Html.class); } private Eventlist getEventlist(String endpoint, String listname) { String url = endpoint + "/eventlists/" + listname; WebResource helm = client.resource(url); WebResource.Builder request = helm.accept(MediaType.APPLICATION_XML); request.header(HttpHeaders.USER_AGENT, EXAMPLE_USER_AGENT); System.out.println("Invoking HTTP GET " + url); return request.get(Eventlist.class); } private String getJsonEventlist(String endpoint, String listname) { String url = endpoint + "/eventlists/" + listname; WebResource helm = client.resource(url); WebResource.Builder request = helm.accept(MediaType.APPLICATION_JSON); request.header(HttpHeaders.USER_AGENT, EXAMPLE_USER_AGENT); System.out.println("Invoking JSON HTTP GET " + url); return request.get(String.class); } private void marshal(OutputStream out, Object value) throws JAXBException { Marshaller marshaller = null; if (value instanceof gov.nasa.gsfc.helm.data.schema.Eventlist) { marshaller = helmJaxbContext.createMarshaller(); } else if (value instanceof org.w3._1999.xhtml.Html) { marshaller = xhtmlJaxbContext.createMarshaller(); } else { System.err.println("Don't know how to marshall " + value.getClass().getName()); } marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.marshal(value, out); } /** * Instantiates an instance of this class and then invokes the * {@link #execute(String)} method. * * @param args command line arguments. * args[0] is the base URL of HELM Web services * args[1] is the name of the eventlist to get * @throws Exception if an Exception occurs */ public static void main(String[] args) throws Exception { if (args.length < 2) { System.out.println("ERROR: missing argument(s)"); System.out.println("USAGE: JaxRsExample endpoint eventlist-name"); return; } JaxRsExample example = new JaxRsExample(); example.execute(args[0], args[1]); } }