Package org.apache.ws.muws.interop.client

Source Code of org.apache.ws.muws.interop.client.ServiceStub

/*=============================================================================*
*  Copyright 2005 The Apache Software Foundation
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*=============================================================================*/
package org.apache.ws.muws.interop.client;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.Observable;

import javax.xml.namespace.QName;

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.XmlOptions;
import org.oasisOpen.docs.wsdm.x2004.x12.mows.wsdmMows.GetManageabilityReferencesDocument;
import org.oasisOpen.docs.wsdm.x2004.x12.mows.wsdmMows.GetManageabilityReferencesResponseDocument;
import org.oasisOpen.docs.wsdm.x2004.x12.muws.wsdmMuwsPart2.QueryRelationshipsByTypeDocument;
import org.oasisOpen.docs.wsdm.x2004.x12.muws.wsdmMuwsPart2.QueryRelationshipsByTypeResponseDocument;
import org.oasisOpen.docs.wsdm.x2004.x12.muws.wsdmMuwsPart2.RelationshipType;
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.EndpointReferenceType;

/**
* TODO
*/
public class ServiceStub extends Observable
{

    protected URL m_url;

    public ServiceStub( URL url )
    {
        m_url = url;
    }

    protected EndpointReferenceType[] getManageabilityReferences() throws FaultException
    {
        GetManageabilityReferencesDocument requestDoc = GetManageabilityReferencesDocument.Factory.newInstance();
        requestDoc.addNewGetManageabilityReferences();
        XmlObject response = sendRequest( requestDoc, "http://xyz.com/action/GetManageabilityReferences", m_url);
        if ( ! ( response instanceof GetManageabilityReferencesResponseDocument.GetManageabilityReferencesResponse ) )
        {
            throw new FaultException( response.toString() );
        }
        return ((GetManageabilityReferencesResponseDocument.GetManageabilityReferencesResponse)response).getManageabilityEndpointReferenceArray();
    }



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

    public XmlObject sendRequest(XmlObject requestDoc, String action, URL url)
    {
        EnvelopeDocument requestEnvelopeDoc = createEnvelope();
        Envelope requestEnvelope = requestEnvelopeDoc.getEnvelope();
        XmlBeanUtils.addChildElement( requestEnvelope.getBody(), requestDoc );
        try
        {
            URI actionURI = new URI( action );
            if ( ResourceStub.DEBUG ) { System.out.println( "Sending request: \n" + requestEnvelopeDoc ); }
           
           
            setChanged();
            String response = SoapClient.sendRequest( url, requestEnvelopeDoc.newInputStream(), actionURI );
            XmlOptions xmlOpts = new XmlOptions().setSaveOuter();
            xmlOpts.setSavePrettyPrint();           
            EnvelopeDocument responseEnvelopeDoc = (EnvelopeDocument) XmlObject.Factory.parse( response );
        notifyObservers(new WcmMessage(requestDoc.xmlText(xmlOpts), responseEnvelopeDoc.xmlText(xmlOpts)));

            if ( ResourceStub.DEBUG ) { System.out.println( "Received response: \n" + responseEnvelopeDoc ); }
            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 );
        }
    }
    protected XmlObject sendRequest(EnvelopeDocument requestEnvelope, String action, URL url)
    {

        try
        {
            URI actionURI = new URI( action );
            if ( ResourceStub.DEBUG ) { System.out.println( "Sending request: \n" + requestEnvelope ); }
            String response = SoapClient.sendRequest( url, requestEnvelope.newInputStream(), actionURI );
            EnvelopeDocument responseEnvelopeDoc = (EnvelopeDocument) XmlObject.Factory.parse( response );
            if ( ResourceStub.DEBUG ) { System.out.println( "Received response: \n" + responseEnvelopeDoc ); }
            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 );
        }
    }

    protected void addAddressingHeader(Header header, String action, String address, QName keyHeaderName, String weatherstationKey1)
    {
        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);


        XmlObject resourceKeyHeader = XmlBeanUtils.addChildElement(header, keyHeaderName);
        XmlBeanUtils.setValue(resourceKeyHeader, weatherstationKey1);
    }
}
TOP

Related Classes of org.apache.ws.muws.interop.client.ServiceStub

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.