use of org.apache.synapse.core.axis2.Axis2MessageContext in project wso2-synapse by wso2.
the class MediatorProperty method evaluate.
/**
* @param synCtx
*/
public void evaluate(MessageContext synCtx) {
String result;
if (value != null) {
result = value;
} else if (expression != null) {
result = expression.stringValueOf(synCtx);
} else {
throw new SynapseException("A value or expression must be specified");
}
if (scope == null || XMLConfigConstants.SCOPE_DEFAULT.equals(scope)) {
synCtx.setProperty(name, result);
} else if (XMLConfigConstants.SCOPE_AXIS2.equals(scope)) {
// Setting property into the Axis2 Message Context
Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
axis2MessageCtx.setProperty(name, result);
MediatorPropertyUtils.handleSpecialProperties(name, result, axis2MessageCtx);
} else if (XMLConfigConstants.SCOPE_CLIENT.equals(scope)) {
// Setting property into the Axis2 Message Context client options
Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
axis2MessageCtx.getOptions().setProperty(name, result);
} else if (XMLConfigConstants.SCOPE_TRANSPORT.equals(scope)) {
// Setting Transport Headers
Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
Object headers = axis2MessageCtx.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
if (headers != null && headers instanceof Map) {
Map headersMap = (Map) headers;
headersMap.put(name, result);
}
if (headers == null) {
Map headersMap = new HashMap();
headersMap.put(name, result);
axis2MessageCtx.setProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS, headersMap);
}
}
}
use of org.apache.synapse.core.axis2.Axis2MessageContext in project wso2-synapse by wso2.
the class MessageHelper method cloneMessageContext.
/**
* This method will simulate cloning the message context and creating an exact copy of the
* passed message. One should use this method with care; that is because, inside the new MC,
* most of the attributes of the MC like opCtx and so on are still kept as references inside
* the axis2 MessageContext for performance improvements. (Note: U dont have to worrie
* about the SOAPEnvelope, it is a cloned copy and not a reference from any other MC)
* @param synCtx - this will be cloned
* @param cloneSoapEnvelope whether to clone the soap envelope
* @return cloned Synapse MessageContext
* @throws AxisFault if there is a failure in creating the new Synapse MC or in a failure in
* clonning the underlying axis2 MessageContext
*
* @see MessageHelper#cloneAxis2MessageContext
*/
public static MessageContext cloneMessageContext(MessageContext synCtx, boolean cloneSoapEnvelope) throws AxisFault {
// creates the new MessageContext and clone the internal axis2 MessageContext
// inside the synapse message context and place that in the new one
MessageContext newCtx = synCtx.getEnvironment().createMessageContext();
Axis2MessageContext axis2MC = (Axis2MessageContext) newCtx;
axis2MC.setAxis2MessageContext(cloneAxis2MessageContext(((Axis2MessageContext) synCtx).getAxis2MessageContext(), cloneSoapEnvelope));
newCtx.setConfiguration(synCtx.getConfiguration());
newCtx.setEnvironment(synCtx.getEnvironment());
newCtx.setContextEntries(synCtx.getContextEntries());
// set the parent correlation details to the cloned MC -
// for the use of aggregation like tasks
newCtx.setProperty(EIPConstants.AGGREGATE_CORRELATION, synCtx.getMessageID());
// copying the core parameters of the synapse MC
newCtx.setTo(synCtx.getTo());
newCtx.setReplyTo(synCtx.getReplyTo());
newCtx.setSoapAction(synCtx.getSoapAction());
newCtx.setWSAAction(synCtx.getWSAAction());
newCtx.setResponse(synCtx.isResponse());
// copy all the synapse level properties to the newCtx
for (Object o : synCtx.getPropertyKeySet()) {
// If there are non String keyed properties neglect them rather than trow exception
if (o instanceof String) {
String strkey = (String) o;
Object obj = synCtx.getProperty(strkey);
if (obj instanceof String) {
// No need to do anything since Strings are immutable
} else if (obj instanceof ArrayList) {
if (log.isDebugEnabled()) {
log.debug("Deep clone Started for ArrayList property: " + strkey + ".");
}
// Call this method to deep clone ArrayList
obj = cloneArrayList((ArrayList) obj);
if (log.isDebugEnabled()) {
log.debug("Deep clone Ended for ArrayList property: " + strkey + ".");
}
} else if (obj instanceof Stack && strkey.equals(SynapseConstants.SYNAPSE__FUNCTION__STACK)) {
if (log.isDebugEnabled()) {
log.debug("Deep clone for Template function stack");
}
obj = getClonedTemplateStack((Stack<TemplateContext>) obj);
} else if (obj instanceof OMElement) {
if (log.isDebugEnabled()) {
log.debug("Deep clone for OMElement");
}
obj = (OMElement) ((OMElement) obj).cloneOMElement();
} else if (obj instanceof ResponseState) {
// do nothing and let the same reference to go to the cloned context
} else {
/**
* Need to add conditions according to type if found in
* future
*/
if (log.isDebugEnabled()) {
log.warn("Deep clone not happened for property : " + strkey + ". Class type : " + obj.getClass().getName());
}
}
newCtx.setProperty(strkey, obj);
}
}
// Make deep copy of fault stack so that parent will not be lost it's fault stack
Stack<FaultHandler> faultStack = synCtx.getFaultStack();
if (!faultStack.isEmpty()) {
List<FaultHandler> newFaultStack = new ArrayList<FaultHandler>();
newFaultStack.addAll(faultStack);
for (FaultHandler faultHandler : newFaultStack) {
if (faultHandler != null) {
newCtx.pushFaultHandler(faultHandler);
}
}
}
Stack<TemplateContext> functionStack = (Stack) synCtx.getProperty(SynapseConstants.SYNAPSE__FUNCTION__STACK);
if (functionStack != null) {
newCtx.setProperty(SynapseConstants.SYNAPSE__FUNCTION__STACK, functionStack.clone());
}
if (log.isDebugEnabled()) {
log.info("Parent's Fault Stack : " + faultStack + " : Child's Fault Stack :" + newCtx.getFaultStack());
}
// Copy ContinuationStateStack from original MC to the new MC
if (synCtx.isContinuationEnabled()) {
Stack<ContinuationState> continuationStates = synCtx.getContinuationStateStack();
newCtx.setContinuationEnabled(true);
for (ContinuationState continuationState : continuationStates) {
if (continuationState != null) {
newCtx.pushContinuationState(ContinuationStackManager.getClonedSeqContinuationState((SeqContinuationState) continuationState));
}
}
}
newCtx.setMessageFlowTracingState(synCtx.getMessageFlowTracingState());
return newCtx;
}
use of org.apache.synapse.core.axis2.Axis2MessageContext in project wso2-synapse by wso2.
the class MessageHelper method cloneMessageContextForAggregateMediator.
public static MessageContext cloneMessageContextForAggregateMediator(MessageContext synCtx) throws AxisFault {
// creates the new MessageContext and clone the internal axis2 MessageContext
// inside the synapse message context and place that in the new one
MessageContext newCtx = synCtx.getEnvironment().createMessageContext();
Axis2MessageContext axis2MC = (Axis2MessageContext) newCtx;
axis2MC.setAxis2MessageContext(cloneAxis2MessageContextForAggregate(((Axis2MessageContext) synCtx).getAxis2MessageContext()));
newCtx.setConfiguration(synCtx.getConfiguration());
newCtx.setEnvironment(synCtx.getEnvironment());
newCtx.setContextEntries(synCtx.getContextEntries());
// set the parent correlation details to the cloned MC -
// for the use of aggregation like tasks
newCtx.setProperty(EIPConstants.AGGREGATE_CORRELATION, synCtx.getMessageID());
// copying the core parameters of the synapse MC
newCtx.setTo(synCtx.getTo());
newCtx.setReplyTo(synCtx.getReplyTo());
newCtx.setSoapAction(synCtx.getSoapAction());
newCtx.setWSAAction(synCtx.getWSAAction());
newCtx.setResponse(synCtx.isResponse());
// copy all the synapse level properties to the newCtx
for (Object o : synCtx.getPropertyKeySet()) {
// throw exception
if (o instanceof String) {
/**
* Clone the properties and add to new context
* If not cloned can give errors in target configuration
*/
String strkey = (String) o;
Object obj = synCtx.getProperty(strkey);
if (obj instanceof String) {
// No need to do anything since Strings are immutable
} else if (obj instanceof ArrayList) {
if (log.isDebugEnabled()) {
log.warn("Deep clone Started for ArrayList property: " + strkey + ".");
}
// Call this method to deep clone ArrayList
obj = cloneArrayList((ArrayList) obj);
if (log.isDebugEnabled()) {
log.warn("Deep clone Ended for ArrayList property: " + strkey + ".");
}
} else {
/**
* Need to add conditions according to type if found in
* future
*/
if (log.isDebugEnabled()) {
log.warn("Deep clone not happened for property : " + strkey + ". Class type : " + obj.getClass().getName());
}
}
newCtx.setProperty(strkey, obj);
}
}
// Make deep copy of fault stack so that parent will not be lost it's fault stack
Stack<FaultHandler> faultStack = synCtx.getFaultStack();
if (!faultStack.isEmpty()) {
List<FaultHandler> newFaultStack = new ArrayList<FaultHandler>();
newFaultStack.addAll(faultStack);
for (FaultHandler faultHandler : newFaultStack) {
if (faultHandler != null) {
newCtx.pushFaultHandler(faultHandler);
}
}
}
Stack<TemplateContext> functionStack = (Stack) synCtx.getProperty(SynapseConstants.SYNAPSE__FUNCTION__STACK);
if (functionStack != null) {
newCtx.setProperty(SynapseConstants.SYNAPSE__FUNCTION__STACK, functionStack.clone());
}
if (log.isDebugEnabled()) {
log.info("Parent's Fault Stack : " + faultStack + " : Child's Fault Stack :" + newCtx.getFaultStack());
}
// Copy ContinuationStateStack from original MC to the new MC
if (synCtx.isContinuationEnabled()) {
Stack<ContinuationState> continuationStates = synCtx.getContinuationStateStack();
newCtx.setContinuationEnabled(true);
for (ContinuationState continuationState : continuationStates) {
if (continuationState != null) {
newCtx.pushContinuationState(ContinuationStackManager.getClonedSeqContinuationState((SeqContinuationState) continuationState));
}
}
}
newCtx.setMessageFlowTracingState(synCtx.getMessageFlowTracingState());
return newCtx;
}
use of org.apache.synapse.core.axis2.Axis2MessageContext in project wso2-synapse by wso2.
the class BlockingMsgSender method send.
public MessageContext send(Endpoint endpoint, MessageContext synapseInMsgCtx) throws Exception {
if (log.isDebugEnabled()) {
log.debug("Start Sending the Message ");
}
if (endpoint instanceof IndirectEndpoint) {
String endpointKey = ((IndirectEndpoint) endpoint).getKey();
endpoint = synapseInMsgCtx.getEndpoint(endpointKey);
}
if (endpoint instanceof TemplateEndpoint) {
endpoint = ((TemplateEndpoint) endpoint).getRealEndpoint();
}
// get the correct endpoint definition
if (endpoint instanceof ResolvingEndpoint) {
SynapseXPath keyExpression = ((ResolvingEndpoint) endpoint).getKeyExpression();
String key = keyExpression.stringValueOf(synapseInMsgCtx);
endpoint = ((ResolvingEndpoint) endpoint).loadAndInitEndpoint(((Axis2MessageContext) synapseInMsgCtx).getAxis2MessageContext().getConfigurationContext(), key);
}
AbstractEndpoint abstractEndpoint = (AbstractEndpoint) endpoint;
if (!abstractEndpoint.isLeafEndpoint()) {
handleException("Endpoint Type not supported");
}
// clear the message context properties related to endpoint in last service invocation
Set keySet = synapseInMsgCtx.getPropertyKeySet();
if (keySet != null) {
keySet.remove(EndpointDefinition.DYNAMIC_URL_VALUE);
}
abstractEndpoint.executeEpTypeSpecificFunctions(synapseInMsgCtx);
EndpointDefinition endpointDefinition = abstractEndpoint.getDefinition();
org.apache.axis2.context.MessageContext axisInMsgCtx = ((Axis2MessageContext) synapseInMsgCtx).getAxis2MessageContext();
org.apache.axis2.context.MessageContext axisOutMsgCtx = new org.apache.axis2.context.MessageContext();
String endpointReferenceValue = null;
if (endpointDefinition.getAddress() != null) {
endpointReferenceValue = endpointDefinition.getAddress();
} else if (axisInMsgCtx.getTo() != null) {
endpointReferenceValue = axisInMsgCtx.getTo().getAddress();
} else {
handleException("Service url, Endpoint or 'To' header is required");
}
EndpointReference epr = new EndpointReference(endpointReferenceValue);
axisOutMsgCtx.setTo(epr);
AxisService anonymousService;
if (endpointReferenceValue != null && endpointReferenceValue.startsWith(Constants.TRANSPORT_LOCAL)) {
configurationContext = axisInMsgCtx.getConfigurationContext();
anonymousService = AnonymousServiceFactory.getAnonymousService(configurationContext.getAxisConfiguration(), LOCAL_ANON_SERVICE);
} else {
anonymousService = AnonymousServiceFactory.getAnonymousService(null, configurationContext.getAxisConfiguration(), endpointDefinition.isAddressingOn() | endpointDefinition.isReliableMessagingOn(), endpointDefinition.isReliableMessagingOn(), endpointDefinition.isSecurityOn(), false);
}
axisOutMsgCtx.setConfigurationContext(configurationContext);
axisOutMsgCtx.setEnvelope(axisInMsgCtx.getEnvelope());
axisOutMsgCtx.setProperty(HTTPConstants.NON_ERROR_HTTP_STATUS_CODES, axisInMsgCtx.getProperty(HTTPConstants.NON_ERROR_HTTP_STATUS_CODES));
axisOutMsgCtx.setProperty(HTTPConstants.ERROR_HTTP_STATUS_CODES, axisInMsgCtx.getProperty(HTTPConstants.ERROR_HTTP_STATUS_CODES));
axisOutMsgCtx.setProperty(SynapseConstants.DISABLE_CHUNKING, axisInMsgCtx.getProperty(SynapseConstants.DISABLE_CHUNKING));
// Can't refer to the Axis2 constant 'NO_DEFAULT_CONTENT_TYPE' defined in 1.6.1.wso2v23-SNAPSHOT until
// an API change is done.
axisOutMsgCtx.setProperty(SynapseConstants.NO_DEFAULT_CONTENT_TYPE, axisInMsgCtx.getProperty(SynapseConstants.NO_DEFAULT_CONTENT_TYPE));
// Fill MessageContext
BlockingMsgSenderUtils.fillMessageContext(endpointDefinition, axisOutMsgCtx, synapseInMsgCtx);
if (JsonUtil.hasAJsonPayload(axisInMsgCtx)) {
JsonUtil.cloneJsonPayload(axisInMsgCtx, axisOutMsgCtx);
}
Options clientOptions;
if (initClientOptions) {
clientOptions = new Options();
} else {
clientOptions = axisInMsgCtx.getOptions();
clientOptions.setTo(epr);
}
// Fill Client options
BlockingMsgSenderUtils.fillClientOptions(endpointDefinition, clientOptions, synapseInMsgCtx);
anonymousService.getParent().addParameter(SynapseConstants.HIDDEN_SERVICE_PARAM, "true");
ServiceGroupContext serviceGroupContext = new ServiceGroupContext(configurationContext, (AxisServiceGroup) anonymousService.getParent());
ServiceContext serviceCtx = serviceGroupContext.getServiceContext(anonymousService);
axisOutMsgCtx.setServiceContext(serviceCtx);
// Invoke
boolean isOutOnly = isOutOnly(synapseInMsgCtx, axisOutMsgCtx);
try {
if (isOutOnly) {
sendRobust(axisOutMsgCtx, clientOptions, anonymousService, serviceCtx, synapseInMsgCtx);
final String httpStatusCode = String.valueOf(axisOutMsgCtx.getProperty(SynapseConstants.HTTP_SENDER_STATUSCODE)).trim();
/*
* Though this is OUT_ONLY operation, we need to set the
* response Status code so that others can make use of it.
*/
axisInMsgCtx.setProperty(SynapseConstants.HTTP_SC, httpStatusCode);
} else {
org.apache.axis2.context.MessageContext result = sendReceive(axisOutMsgCtx, clientOptions, anonymousService, serviceCtx, synapseInMsgCtx);
if (result.getEnvelope() != null) {
synapseInMsgCtx.setEnvelope(result.getEnvelope());
if (JsonUtil.hasAJsonPayload(result)) {
JsonUtil.cloneJsonPayload(result, ((Axis2MessageContext) synapseInMsgCtx).getAxis2MessageContext());
}
}
final String statusCode = String.valueOf(result.getProperty(SynapseConstants.HTTP_SENDER_STATUSCODE)).trim();
/*
* We need to set the response status code so that users can
* fetch it later.
*/
axisInMsgCtx.setProperty(SynapseConstants.HTTP_SC, statusCode);
if ("false".equals(synapseInMsgCtx.getProperty(SynapseConstants.BLOCKING_SENDER_PRESERVE_REQ_HEADERS))) {
axisInMsgCtx.setProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS, result.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS));
}
synapseInMsgCtx.setProperty(SynapseConstants.BLOCKING_SENDER_ERROR, "false");
return synapseInMsgCtx;
}
} catch (Exception ex) {
/*
* Extract the HTTP status code from the Exception message.
*/
final String errorStatusCode = extractStatusCodeFromException(ex);
axisInMsgCtx.setProperty(SynapseConstants.HTTP_SC, errorStatusCode);
if (!isOutOnly) {
// axisOutMsgCtx.getTransportOut().getSender().cleanup(axisOutMsgCtx);
synapseInMsgCtx.setProperty(SynapseConstants.BLOCKING_SENDER_ERROR, "true");
synapseInMsgCtx.setProperty(SynapseConstants.ERROR_EXCEPTION, ex);
if (ex instanceof AxisFault) {
AxisFault fault = (AxisFault) ex;
setErrorDetails(synapseInMsgCtx, fault);
org.apache.axis2.context.MessageContext faultMC = fault.getFaultMessageContext();
if (faultMC != null) {
Object statusCode = faultMC.getProperty(SynapseConstants.HTTP_SENDER_STATUSCODE);
synapseInMsgCtx.setProperty(SynapseConstants.HTTP_SC, statusCode);
axisInMsgCtx.setProperty(SynapseConstants.HTTP_SC, statusCode);
synapseInMsgCtx.setEnvelope(faultMC.getEnvelope());
}
}
return synapseInMsgCtx;
} else {
if (ex instanceof AxisFault) {
AxisFault fault = (AxisFault) ex;
setErrorDetails(synapseInMsgCtx, fault);
}
}
handleException("Error sending Message to url : " + ((AbstractEndpoint) endpoint).getDefinition().getAddress(), ex);
}
return null;
}
use of org.apache.synapse.core.axis2.Axis2MessageContext in project wso2-synapse by wso2.
the class Resource method canProcess.
@Override
boolean canProcess(MessageContext synCtx) {
if (synCtx.isResponse()) {
return true;
}
org.apache.axis2.context.MessageContext msgCtx = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
if (protocol == RESTConstants.PROTOCOL_HTTP_ONLY && !Constants.TRANSPORT_HTTP.equals(msgCtx.getIncomingTransportName())) {
if (log.isDebugEnabled()) {
log.debug("Protocol information does not match - Expected HTTP");
}
return false;
}
if (protocol == RESTConstants.PROTOCOL_HTTPS_ONLY && !Constants.TRANSPORT_HTTPS.equals(msgCtx.getIncomingTransportName())) {
if (log.isDebugEnabled()) {
log.debug("Protocol information does not match - Expected HTTPS");
}
return false;
}
String method = (String) msgCtx.getProperty(Constants.Configuration.HTTP_METHOD);
synCtx.setProperty(RESTConstants.REST_METHOD, method);
if (RESTConstants.METHOD_OPTIONS.equals(method)) {
// OPTIONS requests are always welcome
return true;
} else if (!methods.isEmpty()) {
if (!methods.contains(method)) {
if (log.isDebugEnabled()) {
log.debug("HTTP method does not match");
}
return false;
}
}
Map transportHeaders = (Map) msgCtx.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
if ((contentType != null || userAgent != null) && transportHeaders == null) {
if (log.isDebugEnabled()) {
log.debug("Transport headers not available on the message");
}
return false;
}
boolean hasPayload = !Boolean.TRUE.equals(msgCtx.getProperty(NhttpConstants.NO_ENTITY_BODY));
if (contentType != null && hasPayload) {
String type = (String) transportHeaders.get(HTTP.CONTENT_TYPE);
if (!contentType.equals(type)) {
if (log.isDebugEnabled()) {
log.debug("Content type does not match - Expected: " + contentType + ", " + "Found: " + type);
}
return false;
}
}
if (userAgent != null) {
String agent = (String) transportHeaders.get(HTTP.USER_AGENT);
if (agent == null || !agent.matches(this.userAgent)) {
if (log.isDebugEnabled()) {
log.debug("User agent does not match - Expected: " + userAgent + ", " + "Found: " + agent);
}
return false;
}
}
return true;
}
Aggregations