package org.apache.ws.resource.example;
import org.apache.ws.XmlObjectWrapper;
import org.apache.ws.addressing.EndpointReference;
import org.apache.ws.addressing.XmlBeansEndpointReference;
import org.apache.ws.addressing.v2003_03.AddressingConstants;
import org.apache.ws.notification.topics.v2004_06.TopicsConstants;
import org.apache.ws.resource.example.notifConsumer.ConsumerPropertyQNames;
import org.apache.ws.resource.example.notifConsumer.LastMessageType;
import org.apache.ws.util.XmlBeanUtils;
import org.apache.ws.util.soap.SoapClient;
import org.apache.xmlbeans.XmlObject;
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.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 javax.xml.namespace.QName;
import java.net.URI;
import java.net.URL;
import java.util.Calendar;
/**
* @author Sal Campana
*/
public class RequestUtils
{
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("Received response from " + endpointURL.toString());
System.out.println(responseBodyElems[0].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 boolean invokeSubscribe(String consumerURL, String serviceEPRURL, String topicName)
{
QName topic = QName.valueOf(topicName);
SubscribeDocument requestDoc = SubscribeDocument.Factory.newInstance();
SubscribeDocument.Subscribe subscribe = requestDoc.addNewSubscribe();
subscribe.setUseNotify(true);
Calendar instance = Calendar.getInstance();
instance.setTimeInMillis(instance.getTimeInMillis() + 2000000);
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(createEpr(consumerURL).getAddress());
TopicExpressionType topicExpr = subscribe.addNewTopicExpression();
topicExpr.setDialect(TopicsConstants.TOPIC_EXPR_DIALECT_SIMPLE.toString());
XmlBeanUtils.setValueAsQName(topicExpr, topic);
XmlObject response = sendRequest(requestDoc, "http://xyz.com/action/Subscribe", createEpr(serviceEPRURL));
if (!(response instanceof SubscribeResponseDocument.SubscribeResponse))
{
return false;
}
else
{
return true;
}
}
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 LastMessageType getLastMessage(String serviceURL)
{
GetResourcePropertyDocument getResourcePropertyDocument = GetResourcePropertyDocument.Factory.newInstance();
getResourcePropertyDocument.setGetResourceProperty(ConsumerPropertyQNames.LASTMESSAGE);
XmlObject xmlObject = sendRequest(getResourcePropertyDocument, "http://getResourceProp", createEpr(serviceURL));
if (xmlObject instanceof GetResourcePropertyResponseDocument.GetResourcePropertyResponse)
{
GetResourcePropertyResponseDocument.GetResourcePropertyResponse getResourcePropertyDocumentResponse = (GetResourcePropertyResponseDocument.GetResourcePropertyResponse) xmlObject;
XmlObject[] childElements = XmlBeanUtils.getChildElements(getResourcePropertyDocumentResponse);
if (childElements.length > 0)
{
return (LastMessageType) childElements[0];
}
else
{
return null;
}
}
else
{
return null;
}
}
private static EndpointReference createEpr(String filesystemUrl)
{
XmlBeansEndpointReference epr = null;
try
{
EndpointReferenceDocument eprdoc = (EndpointReferenceDocument) XmlObject.Factory.parse(new URL(filesystemUrl));
epr = new XmlBeansEndpointReference(eprdoc.getEndpointReference());
}
catch (Exception e)
{
e.printStackTrace();
}
return epr;
}
private static EnvelopeDocument createEnvelope()
{
EnvelopeDocument envelopeDoc = EnvelopeDocument.Factory.newInstance();
Envelope envelope = envelopeDoc.addNewEnvelope();
envelope.addNewHeader();
envelope.addNewBody();
return envelopeDoc;
}
private static void addAddressingHeaders(Header header, String action, EndpointReference epr)
{
XmlObject eprXBean = ((XmlObjectWrapper) epr).getXmlObject();
XmlObject toElem;
XmlObject actionElem;
if (eprXBean.schemaType().getName().getNamespaceURI().equals(AddressingConstants.NSURI_ADDRESSING_SCHEMA))
{
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);
XmlBeansEndpointReference newepr = null;
if (eprXBean.schemaType().getName().getNamespaceURI().equals(AddressingConstants.NSURI_ADDRESSING_SCHEMA))
{
newepr = new XmlBeansEndpointReference((org.xmlsoap.schemas.ws.x2003.x03.addressing.EndpointReferenceType) eprXBean);
}
else
{
newepr = new XmlBeansEndpointReference((org.xmlsoap.schemas.ws.x2004.x08.addressing.EndpointReferenceType) eprXBean);
}
if (newepr.getReferenceProperties() != null)
{
XmlObject[] refPropElems = (XmlObject[]) newepr.getReferenceProperties();
for (int i = 0; i < refPropElems.length; i++)
{
XmlBeanUtils.addChildElement(header, refPropElems[i]);
}
}
}
}