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

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

/*=============================================================================*
*  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 org.apache.ws.addressing.EndpointReference;
import org.apache.ws.addressing.XmlBeansEndpointReference;
import org.apache.ws.notification.topics.v2004_06.TopicsConstants;
import org.apache.ws.util.XmlBeanUtils;
import org.apache.ws.util.soap.SoapClient;
import org.apache.ws.XmlObjectWrapper;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlOptions;
import org.apache.axis.message.addressing.Constants;
import org.oasisOpen.docs.wsn.x2004.x06.wsnWSBaseNotification12Draft01.SubscribeDocument;
import org.oasisOpen.docs.wsn.x2004.x06.wsnWSBaseNotification12Draft01.SubscribeResponseDocument;
import org.oasisOpen.docs.wsn.x2004.x06.wsnWSBaseNotification12Draft01.TopicExpressionType;
import org.oasisOpen.docs.wsrf.x2004.x06.wsrfWSResourceProperties12Draft01.GetResourcePropertyDocument;
import org.oasisOpen.docs.wsrf.x2004.x06.wsrfWSResourceProperties12Draft01.GetResourcePropertyResponseDocument;
import org.oasisOpen.docs.wsrf.x2004.x06.wsrfWSResourceProperties12Draft01.SetResourcePropertiesDocument;
import org.oasisOpen.docs.wsrf.x2004.x06.wsrfWSResourceProperties12Draft01.SetResourcePropertiesResponseDocument;
import org.oasisOpen.docs.wsrf.x2004.x06.wsrfWSResourceProperties12Draft01.UpdateType;
import org.xmlsoap.schemas.soap.envelope.Envelope;
import org.xmlsoap.schemas.soap.envelope.EnvelopeDocument;
import org.xmlsoap.schemas.soap.envelope.Header;

import javax.xml.namespace.QName;
import java.net.URI;
import java.net.URL;
import java.util.Calendar;
import java.util.Observable;

/**
* TODO
*/
public class ResourceStub extends Observable
{
    static final boolean DEBUG = false;//Boolean.getBoolean( "debug" );

    private EndpointReference m_epr;

    public static boolean USE_WRAPPED_NOTIFICATIONS = Boolean.valueOf(System.getProperty("wrapped_notifications", "true")).booleanValue();

    public ResourceStub( EndpointReference epr )
    {
       m_epr = epr;
    }

    public XmlObject[] getResourceProperty( QName propName ) throws FaultException
    {
        GetResourcePropertyDocument requestDoc = GetResourcePropertyDocument.Factory.newInstance();
      
        requestDoc.setGetResourceProperty( propName );
        XmlObject response = sendRequest( requestDoc, "http://xyz.com/action/GetResourceProperty","P" );
        if ( ! ( response instanceof GetResourcePropertyResponseDocument.GetResourcePropertyResponse ) )
        {
            throw new FaultException( response.toString() );
        }
        return XmlBeanUtils.getChildElements( response );
    }

    public void updateResourceProperty( XmlObject[] propElems ) throws FaultException
    {
        SetResourcePropertiesDocument requestDoc = SetResourcePropertiesDocument.Factory.newInstance();
        SetResourcePropertiesDocument.SetResourceProperties setResourceProperties = requestDoc.addNewSetResourceProperties();
        UpdateType updateType = setResourceProperties.addNewUpdate();
        for ( int i = 0; i < propElems.length; i++ )
        {
            XmlBeanUtils.addChildElement( updateType, propElems[i] );
        }
        XmlObject response = sendRequest( requestDoc, "http://xyz.com/action/SetResourceProperties","U" );
        if ( ! ( response instanceof SetResourcePropertiesResponseDocument.SetResourcePropertiesResponse ) )
        {
            throw new FaultException( response.toString() );
        }
    }

    public EndpointReference subscribe( String consumerURL, QName topic ) throws FaultException
    {
        SubscribeDocument requestDoc = SubscribeDocument.Factory.newInstance();
        SubscribeDocument.Subscribe subscribe = requestDoc.addNewSubscribe();
        subscribe.setUseNotify(USE_WRAPPED_NOTIFICATIONS);
        Calendar instance = Calendar.getInstance();
        instance.setTimeInMillis(instance.getTimeInMillis() + 7200000);
        subscribe.setInitialTerminationTime( instance );
        org.xmlsoap.schemas.ws.x2003.x03.addressing.EndpointReferenceType consumerRef = subscribe.addNewConsumerReference();
        org.xmlsoap.schemas.ws.x2003.x03.addressing.AttributedURI address = consumerRef.addNewAddress();
        address.setStringValue( consumerURL );
        TopicExpressionType topicExpr = subscribe.addNewTopicExpression();
        topicExpr.setDialect( TopicsConstants.TOPIC_EXPR_DIALECT_SIMPLE );
        XmlBeanUtils.setValueAsQName( topicExpr, topic );
        XmlObject response = sendRequest( requestDoc, "http://xyz.com/action/Subscribe","S" );
        if ( ! ( response instanceof SubscribeResponseDocument.SubscribeResponse ) )
        {
            throw new FaultException( response.toString() );
        }
        SubscribeResponseDocument.SubscribeResponse subscribeResponse = (SubscribeResponseDocument.SubscribeResponse) response;
        return new XmlBeansEndpointReference( subscribeResponse.getSubscriptionReference() );
    }

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

    public XmlObject sendRequest( XmlObject requestDoc, String action ,String type )
    {
        EnvelopeDocument requestEnvelopeDoc = createEnvelope();
        Envelope requestEnvelope = requestEnvelopeDoc.getEnvelope();
        addAddressingHeaders( requestEnvelope.getHeader(), action );
        XmlBeanUtils.addChildElement( requestEnvelope.getBody(), requestDoc );
        try
        {
            URL endpointURL = new URL( m_epr.getAddress() );
            URI actionURI = new URI( action );
            if ( DEBUG ) { System.out.println( "Sending request: \n" + requestEnvelopeDoc ); }
            String response = SoapClient.sendRequest( endpointURL, requestEnvelopeDoc.newInputStream(), actionURI );           
            setChanged();
            XmlOptions xmlOpts = new XmlOptions().setSaveOuter();
            xmlOpts.setSavePrettyPrint();
        notifyObservers(new WcmMessage(requestDoc.xmlText(xmlOpts), response,type));           
            EnvelopeDocument responseEnvelopeDoc = (EnvelopeDocument) XmlObject.Factory.parse( response );
            if (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 );
        }
    }

    private void addAddressingHeaders( Header header, String action )
    {
        XmlObject eprXBean = ((XmlObjectWrapper)m_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(m_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(m_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 (m_epr.getReferenceProperties() != null)
        {
            XmlObject[] refPropElems = (XmlObject[]) m_epr.getReferenceProperties();
            for (int i = 0; i < refPropElems.length; i++)
            {
                XmlBeanUtils.addChildElement(header, refPropElems[i]);
            }
        }
    }

}
TOP

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

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.