Package wfxml

Source Code of wfxml.Basic

/*
* This file is part of the WfMOpen project.
* Copyright (C) 2001-2005 Danet GmbH (www.danet.de), GS-AN.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
* $Id: Basic.java 2330 2007-03-29 11:45:42Z schnelle $
*
* $Log$
* Revision 1.12  2007/03/27 21:59:42  mlipp
* Fixed lots of checkstyle warnings.
*
* Revision 1.11  2007/03/01 12:29:30  schnelle
* Enhanced Instance.SetProperties to process ContextData.
*
* Revision 1.10  2007/02/02 08:56:24  drmlipp
* Fixed URL.
*
* Revision 1.9  2007/02/02 08:18:29  schnelle
* Prepared tests with an Umlaut.
*
* Revision 1.8  2007/02/01 13:48:52  schnelle
* Extracted servlet URL into a constant.
*
* Revision 1.7  2007/01/31 15:13:20  schnelle
* Unified method to create the base url.
*
* Revision 1.6  2007/01/31 10:57:36  schnelle
* Adapted to change in receiver key.
*
* Revision 1.5  2007/01/30 11:56:15  drmlipp
* Merged Wf-XML branch.
*
* Revision 1.4.6.10  2007/01/10 14:17:21  schnelle
* Implemented unsubscribe.
*
* Revision 1.4.6.9  2006/12/12 09:35:19  schnelle
* Implemented ChangeState for Instance.
*
* Revision 1.4.6.8  2006/12/01 15:31:28  schnelle
* Separation of test cases dependend on the role.
*
* Revision 1.4.6.7  2006/12/01 14:18:00  schnelle
* Added support for schema type for data in create process
*
* Revision 1.4.6.6  2006/12/01 12:50:11  schnelle
* Basic import of context data for process creation.
*
* Revision 1.4.6.5  2006/11/30 12:45:20  schnelle
* Basic implementation of Factory CreateInstance.
*
* Revision 1.4.6.4  2006/11/30 10:38:21  schnelle
* Implementation of Factory ListInstance.
*
* Revision 1.4.6.3  2006/11/29 14:13:03  schnelle
* Take respect to namespaces of asap requests and responses.
*
* Revision 1.4.6.2  2006/11/29 11:05:40  schnelle
* Full implementation of the request and response headers.
*
* Revision 1.4.6.1  2006/11/28 15:32:35  schnelle
* Proper selection of the response generator.
*
* Revision 1.4  2006/09/29 12:32:11  drmlipp
* Consistently using WfMOpen as projct name now.
*
* Revision 1.3  2005/01/24 21:11:39  mlipp
* Extended.
*
* Revision 1.2  2005/01/24 20:26:01  mlipp
* Reverted saaj back to 1.1 to fit Axis version.
*
* Revision 1.1  2005/01/23 21:44:23  mlipp
* Initial version.
*
*/
package wfxml;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;

import javax.xml.soap.MessageFactory;
import javax.xml.soap.Node;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPFault;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
import javax.xml.soap.SOAPMessage;

import org.w3c.dom.NodeList;

import junit.framework.AssertionFailedError;
import de.danet.an.util.XMLUtil;

/**
* Utility methods for WfXML tests.
* @author Dirk Schnelle
*/
public class Basic{
    /** URL of the WfXML servlet. */
    public static final String WFXML_SERVLET = "http://localhost:8080/wfxml";
   
    /** The URI of the ASAP version used. */
    public static final String ASAP_NS
  = "http://www.oasis-open.org/asap/0.9/asap.xsd";
   
    /** The URI of the WfXML version used. */
    public static final String WFXML_NS
        = "http://www.wfmc.org/wfxml/2.0/wfxml20.xsd";

    /**
     * Utility method to create a base URL for the given resource.
     * @param resource the target resource.
     * @return base url.
     */
    static String getBaseUrl(String resource) {
        return WFXML_SERVLET + "?Resource=" + resource;
    }
   
    /**
     * Utility method to create an empty SOAP message.
     * @return Empty SOAP message.
     * @throws SOAPException
     */
    static SOAPMessage createMessage()
        throws SOAPException {
        MessageFactory messageFactory = MessageFactory.newInstance();
        return messageFactory.createMessage();
    }
   
    /**
     * Fills all detail data in the request header.
     * @param message the message to manipulate.
     * @param receiver receiver key
     * @param sender sender key
     * @param id user defined id
     * @throws SOAPException
     *         error manipulating the SOAP message.
     */
    static void fillRequestHeadet(SOAPMessage message, String receiver,
            String sender, String id) throws SOAPException {
        SOAPEnvelope env = message.getSOAPPart().getEnvelope();
        SOAPHeader header = env.getHeader ();
        SOAPHeaderElement req = header.addHeaderElement
            (env.createName("Request", "as", Basic.ASAP_NS));
        SOAPElement receiverkey = req.addChildElement("ReceiverKey", "as");
        receiverkey.addTextNode(receiver);
        SOAPElement senderkey = req.addChildElement("SenderKey", "as");
        senderkey.addTextNode(sender);
        if (id != null) {
            SOAPElement requestid = req.addChildElement("RequestID", "as");
            requestid.addTextNode(id);
        }
    }
   
    /**
     * Creates an action body element with the given name.
     * @param message
     * @param request
     * @return
     * @throws SOAPException
     */
    static SOAPBodyElement createActionElement(SOAPMessage message,
            String request) throws SOAPException {
        SOAPEnvelope env = message.getSOAPPart().getEnvelope();
        SOAPBody body = env.getBody();
        return body.addBodyElement(env.createName(request, "wfx",
                Basic.WFXML_NS));
    }
   
    /**
     * Calls the default endpoint with the given message.
     * @param request the SOAP request.
     * @return SOAP response.
     * @throws SOAPException
     *         Error in the SOAP message.
     */
    static SOAPMessage call(SOAPMessage request)
        throws SOAPException {
        SOAPConnectionFactory factory = SOAPConnectionFactory.newInstance();
        SOAPConnection connection = factory.createConnection();
       
        URL endpoint;
        try {
            endpoint = new URL(WFXML_SERVLET);
        } catch (MalformedURLException e) {
           throw new SOAPException(e.getMessage(), e);
        }
       
        return connection.call(request, endpoint);
    }
   
    /**
     * Retrieves the given key from the header of the given message.
     * @param message the message to inspect.
     * @param key the key to retrieve.
     * @return value of the <code>key</code>, <code>null</code> if there is no
     * value.
     * @throws SOAPException
     *         error in the SOAP message.
     */
    static String getHeaderValue(SOAPMessage message, String key)
        throws SOAPException {
        SOAPHeaderElement headerElement = null;

        SOAPHeader header = message.getSOAPHeader();
        for (Iterator i = header.getChildElements(); i.hasNext();) {
            Node node = (Node)i.next();
            if (node instanceof SOAPHeaderElement) {
                headerElement = (SOAPHeaderElement) node;
                break;
            }
        }

        for (Iterator i = headerElement.getChildElements(); i.hasNext();) {
            Node node = (Node)i.next();
            if (node instanceof SOAPElement) {
                SOAPElement element = (SOAPElement) node;
                String name = element.getElementName().getLocalName();
                if (name.equals(key)) {
                    return XMLUtil.getFirstLevelTextContent(element);
                }
            }
        }   

        return null;
    }
    /**
     * Checks, if the given message does not contain a fault message.
     * @param message the message to inspect.
     * @throws SOAPException
     *         Error in the SOAP message.
     */
    static void checkNoFault(SOAPMessage message)
        throws SOAPException {
        SOAPBody body = message.getSOAPBody();
        SOAPFault fault = body.getFault();
        if (fault != null) {
            throw new AssertionFailedError("request failed with: "
                    + fault.getFaultString());
        }
    }

    /**
     * Retrieves the ASAP specific error code.
     * @param message the response message.
     * @return ASAP specific error code.
     * @throws SOAPException
     *         Error in the SOAP message.
     */
    static String getAsapErrorCode(SOAPMessage message)
        throws SOAPException {
        SOAPBody body = message.getSOAPBody();
        SOAPFault fault = body.getFault();
        NodeList details = fault.getElementsByTagName("detail");
        SOAPElement detail = (SOAPElement) details.item(0);
      
        return getChildTextContent(detail, "ErrorCode");
    }

    /**
     * Retrieves the first element of the given request body .
     * @param message the current SOAP message.
     * @return first element
     * @throws SOAPException
     *         error evaluating the message.
     */
    static SOAPBodyElement getFirstBodyElement(SOAPMessage message)
        throws SOAPException {
        SOAPBody reqBody = message.getSOAPBody();
        for (Iterator i = reqBody.getChildElements(); i.hasNext();) {
            Node node = (Node)i.next();
            if (node instanceof SOAPBodyElement) {
                // TODO check for the correct namespace.
                return (SOAPBodyElement) node;
            }
        }   

        throw new AssertionFailedError("missing body");
    }
   
    /**
     * Retrieves the text content of the child node with the specified name.
     * @param element the parent node.
     * @param key name of the searched child node.
     * @return text content, or <code>null</code> if there is no such child
     *          node.
     * @throws SOAPException
     *         error inspecting the SOAP message.
     */
    static String getChildTextContent(SOAPElement element, String key)
        throws SOAPException {
        for (Iterator i = element.getChildElements(); i.hasNext();) {
            Node node = (Node)i.next();
            if (node instanceof SOAPElement) {
                SOAPElement child = (SOAPElement) node;
                String name = child.getElementName().getLocalName();
                if (name.equals(key)) {
                    return XMLUtil.getFirstLevelTextContent(child);
                }
            }
        }
       
        return "";
    }
}
TOP

Related Classes of wfxml.Basic

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.