Package org.wsdmdemo.service

Source Code of org.wsdmdemo.service.InteropRequestUtils

package org.wsdmdemo.service;

import org.apache.axis.message.addressing.Constants;
import org.apache.ws.XmlObjectWrapper;
import org.apache.ws.addressing.EndpointReference;
import org.apache.ws.addressing.XmlBeansEndpointReference;
import org.apache.ws.muws.v1_0.capability.IdentityCapability;
import org.apache.ws.util.XmlBeanUtils;
import org.apache.ws.util.soap.SoapClient;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.impl.values.XmlAnyUriImpl;
import org.oasisOpen.docs.wsdm.x2004.x12.muws.wsdmMuwsPart2.RelationshipType;
import org.oasisOpen.docs.wsdm.x2004.x12.muws.wsdmMuwsPart2.RelationshipTypeType;
import org.oasisOpen.docs.wsrf.x2004.x06.wsrfWSResourceProperties12Draft01.GetResourcePropertyDocument;
import org.oasisOpen.docs.wsrf.x2004.x06.wsrfWSResourceProperties12Draft01.GetResourcePropertyResponseDocument;
import org.xmlsoap.schemas.soap.envelope.Envelope;
import org.xmlsoap.schemas.soap.envelope.EnvelopeDocument;
import org.xmlsoap.schemas.soap.envelope.Header;
import org.xmlsoap.schemas.ws.x2004.x08.addressing.EndpointReferenceDocument;
import org.xmlsoap.schemas.ws.x2004.x08.addressing.EndpointReferenceType;

import java.net.URI;
import java.net.URL;


/**
* Utility methods for building/sending requests...
*
* @author Sal Campana
*/
public class InteropRequestUtils
{


    public static EnvelopeDocument createEnvelope()
    {
        EnvelopeDocument envelopeDoc = EnvelopeDocument.Factory.newInstance();
        Envelope envelope = envelopeDoc.addNewEnvelope();
        envelope.addNewHeader();
        envelope.addNewBody();
        return envelopeDoc;
    }

    public static XmlObject sendRequest(XmlObject requestDoc, String action, EndpointReference epr)
    {
        EnvelopeDocument requestEnvelopeDoc = createEnvelope();
        Envelope requestEnvelope = requestEnvelopeDoc.getEnvelope();
        addAddressingHeaders(requestEnvelope.getHeader(), action, epr);
        XmlBeanUtils.addChildElement(requestEnvelope.getBody(), requestDoc);
        try
        {
            URL endpointURL = new URL(epr.getAddress());
            System.out.println("Sending Request to " + endpointURL.toString());
            URI actionURI = new URI(action);
            String response = SoapClient.sendRequest(endpointURL, requestEnvelopeDoc.newInputStream(), actionURI);
            EnvelopeDocument responseEnvelopeDoc = (EnvelopeDocument) XmlObject.Factory.parse(response);
            Envelope responseEnvelope = responseEnvelopeDoc.getEnvelope();
            XmlObject[] responseBodyElems = XmlBeanUtils.getChildElements(responseEnvelope.getBody());
            System.out.println("Recieved response from " + endpointURL.toString());
            if (responseBodyElems.length == 0)
            {
                return null;
            }
            else
            {
                return responseBodyElems[0];
            }

        }
        catch (Exception e)
        {
            System.out.println("An Exception Occurred!");
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    public static void addAddressingHeaders(Header header, String action, EndpointReference epr)
    {
        XmlObject eprXBean = ((XmlObjectWrapper) epr).getXmlObject();
        XmlObject toElem;
        XmlObject actionElem;
        if (eprXBean.schemaType().getName().getNamespaceURI().equals(Constants.NS_URI_ADDRESSING_2003_03))
        {
            org.xmlsoap.schemas.ws.x2003.x03.addressing.ToDocument toDoc = org.xmlsoap.schemas.ws.x2003.x03.addressing.ToDocument.Factory.newInstance();
            org.xmlsoap.schemas.ws.x2003.x03.addressing.AttributedURI attributedURI = toDoc.addNewTo();
            attributedURI.setStringValue(epr.getAddress());
            toElem = toDoc;
            org.xmlsoap.schemas.ws.x2003.x03.addressing.ActionDocument actionDoc = org.xmlsoap.schemas.ws.x2003.x03.addressing.ActionDocument.Factory.newInstance();
            org.xmlsoap.schemas.ws.x2003.x03.addressing.AttributedURI actionType = actionDoc.addNewAction();
            actionType.setStringValue(action);
            actionElem = actionDoc;
        }
        else
        {
            org.xmlsoap.schemas.ws.x2004.x08.addressing.ToDocument toDoc = org.xmlsoap.schemas.ws.x2004.x08.addressing.ToDocument.Factory.newInstance();
            org.xmlsoap.schemas.ws.x2004.x08.addressing.AttributedURI attributedURI = toDoc.addNewTo();
            attributedURI.setStringValue(epr.getAddress());
            toElem = toDoc;
            org.xmlsoap.schemas.ws.x2004.x08.addressing.ActionDocument actionDoc = org.xmlsoap.schemas.ws.x2004.x08.addressing.ActionDocument.Factory.newInstance();
            org.xmlsoap.schemas.ws.x2004.x08.addressing.AttributedURI actionType = actionDoc.addNewAction();
            actionType.setStringValue(action);
            actionElem = actionDoc;
        }
        XmlBeanUtils.addChildElement(header, toElem);
        XmlBeanUtils.addChildElement(header, actionElem);
        if (epr.getReferenceProperties() != null)
        {
            XmlObject[] refPropElems = (XmlObject[]) epr.getReferenceProperties();
            for (int i = 0; i < refPropElems.length; i++)
            {
                XmlBeanUtils.addChildElement(header, refPropElems[i]);
            }
        }
    }

    public static XmlObject sendRequest(XmlObject requestDoc, String action, String address)
    {
        EnvelopeDocument requestEnvelopeDoc = createEnvelope();
        Envelope requestEnvelope = requestEnvelopeDoc.getEnvelope();
        addAddressingHeaders(requestEnvelope.getHeader(), action, address);
        XmlBeanUtils.addChildElement(requestEnvelope.getBody(), requestDoc);
        try
        {
            System.out.println("Sending request to: " + address);
            URL endpointURL = new URL(address);
            URI actionURI = new URI(action);
            String response = SoapClient.sendRequest(endpointURL, requestEnvelopeDoc.newInputStream(), actionURI);
            EnvelopeDocument responseEnvelopeDoc = (EnvelopeDocument) XmlObject.Factory.parse(response);
            Envelope responseEnvelope = responseEnvelopeDoc.getEnvelope();
            XmlObject[] responseBodyElems = XmlBeanUtils.getChildElements(responseEnvelope.getBody());
            if (responseBodyElems.length == 0)
            {
                return null;
            }
            else
            {
                return responseBodyElems[0];
            }
        }
        catch (Exception e)
        {
            throw new RuntimeException(e);
        }
    }

    private static void addAddressingHeaders(Header header, String action, String address)
    {
        XmlObject toElem;
        XmlObject actionElem;
        org.xmlsoap.schemas.ws.x2004.x08.addressing.ToDocument toDoc = org.xmlsoap.schemas.ws.x2004.x08.addressing.ToDocument.Factory.newInstance();
        org.xmlsoap.schemas.ws.x2004.x08.addressing.AttributedURI attributedURI = toDoc.addNewTo();
        attributedURI.setStringValue(address);
        toElem = toDoc;
        org.xmlsoap.schemas.ws.x2004.x08.addressing.ActionDocument actionDoc = org.xmlsoap.schemas.ws.x2004.x08.addressing.ActionDocument.Factory.newInstance();
        org.xmlsoap.schemas.ws.x2004.x08.addressing.AttributedURI actionType = actionDoc.addNewAction();
        actionType.setStringValue(action);
        actionElem = actionDoc;

        XmlBeanUtils.addChildElement(header, toElem);
        XmlBeanUtils.addChildElement(header, actionElem);
    }

    public static EndpointReferenceDocument getEndpointReference(URL url) throws Exception
    {
        System.out.println("Connecting to URL for EPR: " + url.toString());
        return (EndpointReferenceDocument) XmlObject.Factory.parse(url);
    }

    public static String getResourceId(EndpointReferenceType endpointReferenceType)
    {
        System.out.println("Getting resourceid for epr: " + endpointReferenceType.getAddress().getStringValue());
        GetResourcePropertyDocument getResourcePropertyDocument = GetResourcePropertyDocument.Factory.newInstance();
        getResourcePropertyDocument.setGetResourceProperty(IdentityCapability.PROP_NAME_RESOURCE_ID);
        XmlObject xmlObject = sendRequest(getResourcePropertyDocument, "http://docs.oasis-open.org/wsrf/2004/06/wsrf-WS-ResourceProperties-1.2-draft-01.wsdl/GetResourceProperty", new XmlBeansEndpointReference(endpointReferenceType));
        String resourceID = null;
        if (xmlObject instanceof GetResourcePropertyResponseDocument.GetResourcePropertyResponse)
        {
            GetResourcePropertyResponseDocument.GetResourcePropertyResponse getResourcePropertyResponse = (GetResourcePropertyResponseDocument.GetResourcePropertyResponse) xmlObject;
            XmlObject[] childElements = XmlBeanUtils.getChildElements(getResourcePropertyResponse);
            if (childElements.length == 1)
            {
                XmlAnyUriImpl childElement = (XmlAnyUriImpl) childElements[0];
                resourceID = childElement.getStringValue();
            }
        }
        return resourceID;

    }

    public static RelationshipType createRelationshipType(RelationshipType relationshipType)
    {
        try
        {
            //define the relationship type
            RelationshipTypeType relationshipTypeType = relationshipType.addNewType();
            XmlObject relationType = XmlObject.Factory.parse("<" + InteropConstants.RELATIONSHIP_RELATION.getPrefix() + ":" + InteropConstants.RELATIONSHIP_RELATION.getLocalPart() + " xmlns:" + InteropConstants.RELATIONSHIP_RELATION.getPrefix() + "=" + "\"" + InteropConstants.RELATIONSHIP_RELATION.getNamespaceURI() + "\" />");
            XmlBeanUtils.addChildElement(relationshipTypeType, relationType);
        }
        catch (XmlException e)
        {
            e.printStackTrace();
        }
        return relationshipType;
    }

}
TOP

Related Classes of org.wsdmdemo.service.InteropRequestUtils

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.