/*
* Copyright (c) 2005-2009, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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.wso2.carbon.event.broker;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.impl.builder.StAXOMBuilder;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.MessageContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.neethi.Policy;
import org.apache.neethi.PolicyEngine;
import org.wso2.carbon.CarbonConstants;
import org.wso2.carbon.event.broker.internal.EventBrokerServiceComponent;
import org.wso2.carbon.event.broker.utils.ToJavaScriptDispatcher;
import org.wso2.carbon.event.broker.utils.XSLTbasedFormatter;
import org.wso2.event.*;
import org.wso2.event.exceptions.EventException;
import javax.xml.namespace.QName;
import java.util.HashMap;
import java.util.Map;
public class CarbonEventDispatcher extends CarbonEventBrokerConstants implements EventDispatcher {
private ConfigurationContext configContext = null;
private Policy policy = null;
protected CarbonNotificationManager notificationManager = null;
private static Log log = LogFactory.getLog(CarbonEventDispatcher.class);
private static Map<String, PreDeliveryFormatter> formatterName2ImplMap = new HashMap<String, PreDeliveryFormatter>();
static{
formatterName2ImplMap.put("XSLT", new XSLTbasedFormatter());
}
public boolean onMatchingEvent(Event event, Subscription subscription) throws EventException {
String endpoint = subscription.getEndpointUrl();
if(subscription.getEndpointUrl() != null && subscription.getEndpointUrl().equals(EventingConstants.EXECUTE_SCRIPT_URI)){
SubscriptionData subscriptionData = subscription.getSubscriptionData();
if(subscriptionData != null){
Object executableScript = subscriptionData.getProperty(EventingConstants.EVENTING_EXECUTABLE_SCRIPT_ELEMENT);
new ToJavaScriptDispatcher().onMatchingEvent(event, subscription);
return true;
}
}
if (endpoint == null){
return false;
}
String topic = event.getTopic();
OMFactory factory = OMAbstractFactory.getOMFactory();
OMNamespace topicNs = factory.createOMNamespace(
NOTIFICATION_NS_URI,
NOTIFICATION_NS_PREFIX);
OMElement topicEle = factory.createOMElement(EventingConstants.WSE_EN_TOPIC, topicNs);
topicEle.setText(topic);
OMElement payload = null;
OMElement domainHeader = null;
if(event.getMessage() instanceof MessageContext) {
MessageContext mc = (MessageContext)event.getMessage();
SOAPEnvelope envelope = mc.getEnvelope();
if (envelope.getBody() == null) {
return false;
}
payload = envelope.getBody().getFirstElement();
domainHeader = envelope.getHeader().getFirstChildWithName(
new QName(CarbonConstants.TENANT_DOMAIN_HEADER_NAMESPACE,
CarbonConstants.TENANT_DOMAIN_HEADER_NAME));
// need to clone this message otherwise soap header get lost
if (domainHeader != null) {
domainHeader = domainHeader.cloneOMElement();
}
}else if (event.getMessage() instanceof MessageContext) {
payload = (OMElement)event.getMessage();
}else{
throw new EventException("Carbon Dispatcher only support OMElement and MessageContext as EventType");
}
payload = payload.cloneOMElement();
//If there is a formatter registered, we transform the message using the formatter
PreDeliveryFormatter formatterImpl = formatterName2ImplMap.get(subscription.getFormatter());
if(formatterImpl != null){
payload = formatterImpl.format(subscription, payload);
}
try {
sendNotification(topicEle, payload, endpoint, new OMElement[]{domainHeader}, null);
return true;
} catch (Exception e) {
log.error("Unable to send message", e);
return false;
}
}
public void setNotificationManager(CarbonNotificationManager notificationManager) {
this.notificationManager = notificationManager;
}
public void init(ConfigurationContext configContext) {
if(configContext != null){
this.configContext = configContext;
}else{
this.configContext = EventBrokerServiceComponent.getServerConfigurationContext();
}
}
@SuppressWarnings("unused")
protected void sendNotification(OMElement topic, OMElement payload, String endpoint,
OMElement[] headers, Object args) throws AxisFault {
// The parameter args is used as a mechanism to pass any argument into this method, which
// is used by the implementations that extend the behavior of the default Carbon Event
// Dispatcher.
ServiceClient serviceClient;
if (configContext != null) {
serviceClient = new ServiceClient(configContext, null);
serviceClient.engageModule("addressing");
} else {
serviceClient = new ServiceClient();
}
Options options = new Options();
options.setTo(new EndpointReference(endpoint));
// Try obtaining the policy if we don't have it.
if (policy == null) {
if (this.notificationManager != null) {
String policyPath = this.notificationManager.getPropertyValue("securityPolicy");
if (policyPath != null) {
try {
StAXOMBuilder builder = new StAXOMBuilder(policyPath);
policy = PolicyEngine.getPolicy(builder.getDocumentElement());
} catch (Exception e) {
log.error("Unable to attach security policy.", e);
}
}
}
}
// If we have the policy, use it.
if (policy != null) {
options.setProperty("rampartPolicy", policy);
serviceClient.engageModule("rampart");
}
//options.setProperty(MessageContext.CLIENT_API_NON_BLOCKING, Boolean.TRUE);
options.setAction(EventingConstants.WSE_PUBLISH);
serviceClient.setOptions(options);
serviceClient.addHeader(topic);
//add the other soapheaders
if (headers != null) {
for (OMElement omElement : headers) {
if (omElement != null) {
serviceClient.addHeader(omElement);
}
}
}
serviceClient.fireAndForget(payload);
}
}