Using Jettison

This example uses a call, getResourceById, placed for GroupService. According to the Javadoc, such call is performed by a method of the GroupServiceImpl class, specifically by:

@Path("getResourceById")
@POST
@Consumes({"application/x-www-form-urlencoded"})
public
com.arcsight.product.manager.resource.service.v1.axis2.jaxws.GroupServiceGetResourceByIdResponse 
    getResourceByIdPost(@DefaultValue("") @FormParam("authToken") String authToken,
        @FormParam("resourceId") java.lang.String resourceId,
        @Context HttpHeaders hh) 
    throws com.arcsight.tools.coma.service.ServiceException, 
           com.arcsight.coma.bridge.AuthorizationException, 
           com.arcsight.coma.bridge.AuthenticationException

The example shows that the method returns an instance of the class GroupServiceGetResourceByIdResponse. It means that returned JSON/XML would be a presentation of an object of the class GroupServiceGetResourceByIdResponse.

The information is enough for Jettison to re-create an instance of that class by the returned JSON response. Once the instance is re-created, you can access members of that instance without the need to parse the JSON response.

To use Jettison:

Add the ArcSight SDK libraries (utilities/sdk/lib) to your project and add code to your client application similar to the following:

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.stream.XMLStreamReader;
import org.codehaus.jettison.json.JSONObject;
import org.codehaus.jettison.mapped.MappedNamespaceConvention;
import org.codehaus.jettison.mapped.MappedXMLStreamReader;
// ArcSight SDK classes
import com.arcsight.product.manager.resource.service.v1.model.Group;
import com.arcsight.product.manager.resource.service.v1.axis2.jaxws.GroupServiceGetResourceByIdResponse;
    String responseBody = ... // that's what you get back from HTTP Request
    Class claz = GroupServiceGetResourceByIdResponse.class;
    // 1. Provide Jettison with JAXBContext
    JSONObject obj = new JSONObject(responseBody);
    JAXBContext jaxbContext = JAXBContext.newInstance(claz);
    Unmarshaller marshaller = jaxbContext.createUnmarshaller();
    // 2. Provide information about used namespaces
    Configuration config = new Configuration();
    Map<String, String> xmlToJsonNamespaces = new HashMap<String, String>();
    // namespaceUri =
    // "http://ws.v1.service.resource.manager.product.arcsight.com/groupService/";
    // namespacePrefix = "gro";
    xmlToJsonNamespaces.put(namespaceUri, namespacePrefix);
    xmlToJsonNamespaces.put("http://www.w3.org/2001/XMLSchema-instance", "xsi");
    config.setXmlToJsonNamespaces(xmlToJsonNamespaces);
    MappedNamespaceConvention con = new MappedNamespaceConvention(config);
    // 3. Set up a reader and let it create the object of the specified class
    XMLStreamReader reader = new MappedXMLStreamReader(obj, con);
    GroupServiceGetResourceByIdResponse groupResponse
             = (GroupServiceGetResourceByIdResponse)marshaller.unmarshal(reader);
    // 4. Retrieve a member of response class Group
    Group group = groupResponse.getReturn();

From here, your client application can operate on the group object directly without any JSON parsing.

Tip: Download the required Jettison libraries from http://jettison.codehaus.org/.