/* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License, Version 1.0 only * (the "License"). You may not use this file except in compliance * with the License. * * You can obtain a copy of the license at /OPENSPML_V2_TOOLKIT.LICENSE * or http://www.openspml.org/v2/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at /OPENSPML_V2_TOOLKIT.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ /* * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ /* * Author(s): kent.spaulding@sun.com * Date: Sep 13, 2006 * Time: 4:22:17 PM */ /* * Modified by : Manish Verma; mverma@fnisindia.com * Modified the original class to go with the article * Date: Aug 27, 2007 * */ package org.openspml.v2.examples.nvpstore.client; import java.net.URI; import org.openspml.v2.client.Spml2Client; import org.openspml.v2.examples.nvpstore.psp.NVPObjectStoreExecutor; import org.openspml.v2.msg.Marshallable; import org.openspml.v2.msg.XMLMarshaller; import org.openspml.v2.msg.spml.AddRequest; import org.openspml.v2.msg.spml.AddResponse; import org.openspml.v2.msg.spml.ExecutionMode; import org.openspml.v2.msg.spml.Extensible; import org.openspml.v2.msg.spml.ListTargetsRequest; import org.openspml.v2.msg.spml.ListTargetsResponse; import org.openspml.v2.msg.spml.Request; import org.openspml.v2.msg.spml.Response; import org.openspml.v2.msg.spml.Target; import org.openspml.v2.profiles.DSMLProfileRegistrar; import org.openspml.v2.profiles.dsml.DSMLAttr; import org.openspml.v2.util.Spml2Exception; import org.openspml.v2.util.Spml2ExceptionWithResponse; import org.openspml.v2.util.xml.ObjectFactory; import org.openspml.v2.util.xml.ReflectiveXMLMarshaller; public class SimpleSPML2Client { public static final String DEFAULT_URL = "http://localhost:8080/nvp/spml2"; /** * This class just allows this to be tested without putting the executor in * a servlet. That's a lot easier. */ private static class LocalSpmlClient extends Spml2Client { private NVPObjectStoreExecutor mExecutor = null; public LocalSpmlClient(String schemaUrl, String baseDir) throws Spml2Exception { super(schemaUrl); mExecutor = new NVPObjectStoreExecutor(schemaUrl, baseDir); } public Response send(Request req) throws Spml2Exception { // just call the executor... we are NOT testing the Marshalling with this client Response resp; try { resp = mExecutor.execute(req); } catch (Spml2ExceptionWithResponse e) { System.out.println("There were errors in the response!"); resp = e.getResponse(); } return resp; } } /** * Turn this off if you don't want to see all the XML on the console. */ private static boolean mTrace = true; private static String mTargetName = null; private static XMLMarshaller mMarshaller; private final URI mURI; private final Spml2Client mClient; private static void printXml(Marshallable m) throws Spml2Exception { String xml = mMarshaller.marshall(m); System.out.println(xml); } private Response send(Request req) throws Spml2Exception { if (mTrace) printXml(req); Response resp = mClient.send(req); if (mTrace) printXml(resp); return resp; } private Response doListTargets() throws Spml2Exception { ListTargetsRequest ltr = new ListTargetsRequest("firstOne", ExecutionMode.SYNCHRONOUS, mURI); return send(ltr); } private AddResponse doAddAccountInfo(String eid, String fullName,String email, String description, String project)throws Spml2Exception{ AddRequest req = new AddRequest(); req.setExecutionMode(ExecutionMode.SYNCHRONOUS); req.setTargetId(mTargetName); Extensible data = new Extensible(); // set the objectclass DSMLAttr attr = new DSMLAttr(DSMLAttr.RESERVED_TYPE_ATTR_NAME, "accountInfo"); data.addOpenContentElement(attr); // set the id data.addOpenContentElement(new DSMLAttr("contactId", eid)); data.addOpenContentElement(new DSMLAttr("fullName", fullName)); data.addOpenContentElement(new DSMLAttr("email", email)); data.addOpenContentElement(new DSMLAttr("description", description)); data.addOpenContentElement(new DSMLAttr("project", project)); req.setData(data); Response resp = send(req); return (AddResponse) resp; } private SimpleSPML2Client(Spml2Client client) throws Spml2Exception { ObjectFactory.ProfileRegistrar dsmlReg = new DSMLProfileRegistrar(); ObjectFactory.getInstance().register(dsmlReg); mURI = dsmlReg.getProfileURI(); mClient = client; mMarshaller = new ReflectiveXMLMarshaller(); } /** * This will run through a series of tests. You can point it a server and execute the * SOAP functionality, or you can tell it to execute locally, running everything in * process (which does not test the marshalling.) *
* If there are 0 args, we use a default URL of DEFAULT_URL and go remote. * If there is 1 arg, that arg is the URL. * If there are 2 args, the first is a schema file URL (could be on a server) and * the second is a filename for the baseDir to use for storing files. * * @throws Spml2Exception IF there's an error, we throw one of these. */ public void execute() throws Spml2Exception { Response resp = doListTargets(); if (resp instanceof ListTargetsResponse) { ListTargetsResponse ltresp = (ListTargetsResponse) resp; Target[] targets = ltresp.getTargets(); if (targets != null && targets.length > 0) mTargetName = targets[0].getTargetID(); } String eid = "E001"; String fullName ="Manish Verma"; String email="mverma@fnisindia.com"; String description = "VP Delivery"; String project ="Chandigarh Delivery Center"; AddResponse ar = doAddAccountInfo(eid, fullName, email, description, project); } private static void print_usage() { System.out.println("This program will run a series of tests against an example server. It can"); System.out.println("be run either against a server (just give a servlet URL) or run locally."); System.out.println(); System.out.println("Usage:"); System.out.println(" If there are 0 args, we use a default URL of " + DEFAULT_URL + " and go remote."); System.out.println(" If there is 1 arg, that arg is the URL and we go remote."); System.out.println(" If there are 2 args, the first is a schema file URL (could be on a server) and"); System.out.println(" the second is a filename for the baseDir to use for storing csv files."); } /** * Get the ball rolling. * * @param args see the execute method... */ public static void main(String[] args) { try { Spml2Client client = null; boolean remote = false; if (args.length != 0) { if (args.length == 1) { if (args[0].equals("--help")) { print_usage(); return; } client = new Spml2Client(args[0]); remote = true; } else if (args.length == 2) { client = new LocalSpmlClient(args[0], args[1]); } } else { client = new Spml2Client(DEFAULT_URL); remote = true; } if (remote) mTrace = true; SimpleSPML2Client test = new SimpleSPML2Client(client); test.execute(); } catch (Spml2ExceptionWithResponse e) { try { System.out.println(e.getResponse().toXML(new ReflectiveXMLMarshaller())); } catch (Spml2Exception e1) { e1.printStackTrace(System.err); } } catch (Spml2Exception e) { System.out.println("Error: " + e.getMessage()); Throwable c = e.getCause(); if (c != null) { System.out.println(" Cause: " + c.getMessage()); } print_usage(); } catch (Throwable t) { print_usage(); } } } ############################################################################## ### This script is submitted to BigAdmin by a user of the BigAdmin community. ### Sun Microsystems, Inc. is not responsible for the ### contents or the code enclosed. ### ### ### Copyright 2007 Sun Microsystems, Inc. ALL RIGHTS RESERVED ### Use of this software is authorized pursuant to the ### terms of the license found at ### http://www.sun.com/bigadmin/common/berkeley_license.html ##############################################################################