# COSMOS Developer's Guide

## Student-Teacher Client Proxy

COSMOS provides a remote client proxy for invoking the CMDBf query service. Since this example implements the query service, we can use the query service client to invoke the query operation.

Here is a code snippet for invoking the query service using the query service client. This example loads a query from a file, invokes the GraphQuery operation of the Example MDR query service, and prints out the query response in the console.

    // EPR of the query service
    String queryServieEpr = "http://localhost:8080/axis2/services/ExampleMdrQueryService";

    // instantiate the query service client
    QueryServiceClient client = new QueryServiceClient(queryServieEpr);

    // path to a file that contains a CMDBf query
    String filepath = "c:\cosmos-demo\cosmos-client\cmdbfQuery\ExampleMDR";

    try {
        // loads the query into a DOM tree
        Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(queryFile));
        Element elem = doc.getDocumentElement();

        // invokes the query
        Element response = client.graphQuery(elem);

        // Writes the response using a StringWriter
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer trans = tf.newTransformer();
        StringWriter sw = new StringWriter();
        trans.transform(new DOMSource(response), new StreamResult(sw));

        // prints out the response
        System.out.println("Query response:");
        System.out.println(sw.toString());

    } catch (CMDBfException cmdbfex) {
        // In case the query operation returns a SOAP fault,
        // here is how you can catch the fault and inspect the fault message
        if (cmdbfex.getCause() instanceof AxisFault) {
            AxisFault axisfault = (AxisFault) cmdbfex.getCause();
            System.out.println("Fault code: " + axisfault.getFaultCode());
            System.out.println("Fault subcode: " + axisfault.getFaultSubCodes());
            System.out.println("Fault message: " + axisfault.getMessage());
            System.out.println("Fault details: " + axisfault.getDetail());
        }
    }

[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]