use of org.apache.synapse.endpoints.Endpoint in project wso2-synapse by wso2.
the class ForwardingService method dispatch.
/**
* Sends the message to a given endpoint.
*
* @param messageContext synapse {@link MessageContext} to be sent
*/
public void dispatch(MessageContext messageContext) {
if (log.isDebugEnabled()) {
log.debug("Sending the message to client with message processor [" + messageProcessor.getName() + "]");
}
// The below code is just for keeping the backward compatibility with the old code.
if (targetEndpoint == null) {
targetEndpoint = (String) messageContext.getProperty(ForwardingProcessorConstants.TARGET_ENDPOINT);
}
if (targetEndpoint != null) {
Endpoint endpoint = messageContext.getEndpoint(targetEndpoint);
if (endpoint == null) {
log.error("Endpoint does not exists. Deactivating the message processor");
deactivateMessageProcessor(messageContext);
return;
}
AbstractEndpoint abstractEndpoint = (AbstractEndpoint) endpoint;
EndpointDefinition endpointDefinition = abstractEndpoint.getDefinition();
String endpointReferenceValue;
if (endpointDefinition.getAddress() != null) {
endpointReferenceValue = endpointDefinition.getAddress();
// we only validate response for certain protocols (i.e HTTP/HTTPS)
isResponseValidationNotRequired = !isResponseValidationRequiredEndpoint(endpointReferenceValue);
}
try {
// Send message to the client
while (!isSuccessful && !isTerminated) {
tryToDispatchToEndpoint(messageContext, endpoint);
isTerminated = messageProcessor.isDeactivated();
if (!isSuccessful) {
prepareToRetry(messageContext);
}
}
} catch (Exception e) {
log.error("Message processor [" + messageProcessor.getName() + "] failed to send the message to" + " client", e);
}
} else {
/*
* No Target Endpoint defined for the Message So we do not have a
* place to deliver.
* Here we log a warning and remove the message todo: we can improve
* this by implementing a target inferring
* mechanism.
*/
log.error("Neither targetEndpoint defined in the MessageProcessor configuration nor " + "Property " + ForwardingProcessorConstants.TARGET_ENDPOINT + " is found in the message context, hence deactivating the MessageProcessor");
deactivateMessageProcessor(messageContext);
}
}
use of org.apache.synapse.endpoints.Endpoint in project wso2-synapse by wso2.
the class RegistryResourceFetcher method getResource.
private Object getResource(Entry entry, Properties properties) {
OMNode omNode;
RegistryEntry re = registry.getRegistryEntry(entry.getKey());
omNode = registry.lookup(entry.getKey());
if (re == null) {
return null;
}
if ((!entry.isCached() || (re.getVersion() == Long.MIN_VALUE || re.getVersion() != entry.getVersion())) || re.getLastModified() >= lastExecutionTime) {
entry.setEntryProperties(registry.getResourceProperties(entry.getKey()));
entry.setVersion(re.getVersion());
// if we get here, we have received the raw omNode from the
// registry and our previous copy (if we had one) has expired or is not valid
Object expiredValue = entry.getValue();
// resource into the appropriate object - e.g. sequence or endpoint
if (entry.getMapper() != null) {
entry.setValue(entry.getMapper().getObjectFromOMNode(omNode, properties));
if (entry.getValue() instanceof SequenceMediator) {
SequenceMediator seq = (SequenceMediator) entry.getValue();
seq.setDynamic(true);
seq.setRegistryKey(entry.getKey());
seq.init(synapseEnvironment);
} else if (entry.getValue() instanceof Endpoint) {
Endpoint ep = (Endpoint) entry.getValue();
ep.init(synapseEnvironment);
}
} else {
// if the type of the object is known to have a mapper, create the
// resultant Object using the known mapper, and cache this Object
// else cache the raw OMNode
entry.setValue(omNode);
}
if (expiredValue != null) {
// Destroy the old resource so that everything is properly cleaned up
if (expiredValue instanceof SequenceMediator) {
((SequenceMediator) expiredValue).destroy();
} else if (expiredValue instanceof Endpoint) {
((Endpoint) expiredValue).destroy();
}
}
entry.setVersion(re.getVersion());
}
// new getRegistryEntry() call
if (re.getCachableDuration() > 0) {
entry.setExpiryTime(System.currentTimeMillis() + re.getCachableDuration());
} else {
entry.setExpiryTime(-1);
}
return entry.getValue();
}
use of org.apache.synapse.endpoints.Endpoint in project wso2-synapse by wso2.
the class AbstractRegistry method getResource.
/**
* Get the resource for the given key from this registry
* @param entry The Enrty instance that contains meta-data
* @param properties bag of properties with additional information
* @return the matching resultant object
*/
public Object getResource(Entry entry, Properties properties) {
OMNode omNode = null;
RegistryEntry re = null;
// if we have an unexpired cached copy, return the cached object
if (entry.isCached() && !entry.isExpired()) {
return entry.getValue();
// if we have not cached the referenced object, fetch it and its RegistryEntry
} else if (!entry.isCached()) {
try {
omNode = lookup(entry.getKey());
entry.setEntryProperties(getResourceProperties(entry.getKey()));
} catch (OMException e) {
log.error("Error reading registry resource file : " + entry.getKey(), e);
}
if (omNode == null && entry.getEntryProperties() != null && !entry.getEntryProperties().isEmpty()) {
// Collection
re = getRegistryEntry(entry.getKey());
if (re != null) {
setExpiryTime(entry, re);
entry.setVersion(re.getVersion());
}
}
if (omNode == null) {
return null;
} else {
re = getRegistryEntry(entry.getKey());
}
// if we have cached it before, and now the cache has expired
// get its *new* registry entry and compare versions and pick new cache duration
} else if (entry.isExpired()) {
if (log.isDebugEnabled()) {
log.debug("Cached object has expired for key : " + entry.getKey());
}
re = getRegistryEntry(entry.getKey());
if (re.getVersion() != Long.MIN_VALUE && re.getVersion() == entry.getVersion()) {
if (log.isDebugEnabled()) {
log.debug("Expired version number is same as current version in registry");
}
// renew cache lease for another cachable duration (as returned by the
// new getRegistryEntry() call
setExpiryTime(entry, re);
if (log.isDebugEnabled()) {
log.debug("Renew cache lease for another " + re.getCachableDuration() / 1000 + "s");
}
// return cached object
return entry.getValue();
} else {
omNode = lookup(entry.getKey());
entry.setEntryProperties(getResourceProperties(entry.getKey()));
if (omNode == null && entry.getEntryProperties() != null && !entry.getEntryProperties().isEmpty()) {
// Collection
re = getRegistryEntry(entry.getKey());
if (re != null) {
setExpiryTime(entry, re);
entry.setVersion(re.getVersion());
}
}
if (omNode == null) {
return null;
}
}
}
// if we get here, we have received the raw omNode from the
// registry and our previous copy (if we had one) has expired or is not valid
Object expiredValue = entry.getValue();
// resource into the appropriate object - e.g. sequence or endpoint
if (entry.getMapper() != null) {
entry.setValue(entry.getMapper().getObjectFromOMNode(omNode, properties));
if (entry.getValue() instanceof SequenceMediator) {
SequenceMediator seq = (SequenceMediator) entry.getValue();
seq.setDynamic(true);
seq.setRegistryKey(entry.getKey());
} else if (entry.getValue() instanceof Endpoint) {
Endpoint ep = (Endpoint) entry.getValue();
} else if (entry.getValue() instanceof TemplateMediator) {
((TemplateMediator) entry.getValue()).setDynamic(true);
}
} else {
// else cache the raw OMNode
if (re != null && re.getType() != null) {
XMLToObjectMapper mapper = getMapper(re.getType());
if (mapper != null) {
entry.setMapper(mapper);
entry.setValue(mapper.getObjectFromOMNode(omNode, properties));
} else {
entry.setValue(omNode);
}
}
}
if (expiredValue != null) {
// Destroy the old resource so that everything is properly cleaned up
if (expiredValue instanceof SequenceMediator) {
((SequenceMediator) expiredValue).destroy();
} else if (expiredValue instanceof Endpoint) {
((Endpoint) expiredValue).destroy();
}
}
// increment cache expiry time as specified by the last getRegistryEntry() call
if (re != null) {
setExpiryTime(entry, re);
entry.setVersion(re.getVersion());
}
return entry.getValue();
}
use of org.apache.synapse.endpoints.Endpoint in project wso2-synapse by wso2.
the class SALoadbalanceEndpointSerializer method serializeEndpoint.
protected OMElement serializeEndpoint(Endpoint endpoint) {
if (!(endpoint instanceof SALoadbalanceEndpoint)) {
handleException("Invalid endpoint type for serializing. " + "Expected: SALoadbalanceEndpoint Found: " + endpoint.getClass().getName());
}
SALoadbalanceEndpoint loadbalanceEndpoint = (SALoadbalanceEndpoint) endpoint;
fac = OMAbstractFactory.getOMFactory();
OMElement endpointElement = fac.createOMElement("endpoint", SynapseConstants.SYNAPSE_OMNAMESPACE);
// serialize the parameters
serializeProperties(loadbalanceEndpoint, endpointElement);
serializeCommonAttributes(endpoint, endpointElement);
OMElement loadbalanceElement = fac.createOMElement("loadbalance", SynapseConstants.SYNAPSE_OMNAMESPACE);
endpointElement.addChild(loadbalanceElement);
Dispatcher dispatcher = loadbalanceEndpoint.getDispatcher();
if (dispatcher != null) {
OMElement sessionElement = fac.createOMElement("session", SynapseConstants.SYNAPSE_OMNAMESPACE);
if (dispatcher instanceof SoapSessionDispatcher) {
sessionElement.addAttribute("type", "soap", null);
} else if (dispatcher instanceof HttpSessionDispatcher) {
sessionElement.addAttribute("type", "http", null);
} else if (dispatcher instanceof SimpleClientSessionDispatcher) {
sessionElement.addAttribute("type", "simpleClientSession", null);
} else {
handleException("invalid session dispatcher : " + dispatcher.getClass().getName());
}
long sessionTimeout = loadbalanceEndpoint.getSessionTimeout();
if (sessionTimeout != -1) {
OMElement sessionTimeoutElement = fac.createOMElement("sessionTimeout", SynapseConstants.SYNAPSE_OMNAMESPACE);
sessionTimeoutElement.setText(String.valueOf(sessionTimeout));
sessionElement.addChild(sessionTimeoutElement);
}
endpointElement.addChild(sessionElement);
}
loadbalanceElement.addAttribute(XMLConfigConstants.LOADBALANCE_ALGORITHM, loadbalanceEndpoint.getAlgorithm().getClass().getName(), null);
if (loadbalanceEndpoint.isBuildMessageAtt()) {
loadbalanceElement.addAttribute(XMLConfigConstants.BUILD_MESSAGE, Boolean.toString(loadbalanceEndpoint.isBuildMessageAtt()), null);
}
for (Endpoint childEndpoint : loadbalanceEndpoint.getChildren()) {
loadbalanceElement.addChild(EndpointSerializer.getElementFromEndpoint(childEndpoint));
}
return endpointElement;
}
use of org.apache.synapse.endpoints.Endpoint in project wso2-synapse by wso2.
the class SendMediatorSerializer method serializeSpecificMediator.
public OMElement serializeSpecificMediator(Mediator m) {
if (!(m instanceof SendMediator)) {
handleException("Unsupported mediator passed in for serialization : " + m.getType());
}
SendMediator mediator = (SendMediator) m;
OMElement send = fac.createOMElement("send", synNS);
saveTracingState(send, mediator);
Endpoint activeEndpoint = mediator.getEndpoint();
if (activeEndpoint != null) {
send.addChild(EndpointSerializer.getElementFromEndpoint(activeEndpoint));
}
Value receive = mediator.getReceivingSequence();
if (receive != null) {
ValueSerializer serializer = new ValueSerializer();
serializer.serializeValue(receive, XMLConfigConstants.RECEIVE, send);
}
if (mediator.isBuildMessage()) {
send.addAttribute(fac.createOMAttribute("buildmessage", nullNS, "true"));
}
return send;
}
Aggregations