Binding the schema, Binding the, Schema – Brocade Network Advisor REST API Guide (Supporting Network Advisor 12.3.0) User Manual
Page 62

48
Brocade Network Advisor REST API Guide
53-1003160-01
Binding the schema
4
System.out.println("CALLING POST
http://10.24.48.103/rest/resourcegroups/All/fcfabrics/10:00:00:05:1E:40:40:00/del
etefabric");
/**
* Make the HTTP call
*/
int responseCode = con.getResponseCode();
System.out.println("Response code is " + responseCode);
if (HttpURLConnection.HTTP_NO_CONTENT != responseCode) {
PRINT_ERROR(con);
assert false : "Delete Fabric FAILED, responseCode = " + responseCode;
}
} catch (IOException ie) {
System.out.println(ie.toString());
} finally {
if (null != con) {
con.disconnect();
}
}
Binding the schema
Most of the POST operations supported by the Network Advisor REST API require input data. This
data is passed in the form of an XML payload.
To access the XML payload using Java, this example uses the Java Architecture for XML (JAXB)
binding. JAXB simplifies access to an XML document from a Java program by presenting the XML
document to the program in a Java format. As part of the JAXB framework, JAXB provides APIs for
unmarshaling and marshalling XML data.
To bind the schema, perform the following steps.
1. Unmarshal the XML data into a Java object, as shown in the following Java code example.
JAXBContext jaxbContext =
JAXBContext.newInstance(InitiatorTargetsRequest.class);
Unmarshaller u = jaxbContext.createUnmarshaller();
Object element = u.unmarshal(new File("./InitiatorTargetsRequest.xml"));
In this example, you can see that in order to unmarshal the XML data into Java objects, you
first need the Java class that represents the XML data, meaning binding the XML schema to
the Java class. That means that you need to generate the set of Java classes that represent the
REST XML schema.
2. Marshal the jaxbContext object into the HttpURLConnection output stream, as shown in the
following Java code example.
Marshaller m = jaxbContext.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(element, con.getOutputStream());
The REST schema is published in the directory
In this example, the standard JDK’s xjc application which is the XML-to-Java compiler was used to
generate the needed Java classes from the REST XML schema (xjc is included as part of JDK since
Java SE 6). Shown below is the ant target to use xjc to generate the java artifacts.