Search in sources :

Example 16 with Axis2MessageContext

use of org.apache.synapse.core.axis2.Axis2MessageContext in project wso2-synapse by wso2.

the class GetPropertyFunction method evaluate.

/**
 * Returns the string value of the property using arg one as key and arg two as scope
 *
 * @param scopeObject scope will decide from where property will be picked up from
 *        i.e. axis2, transport, default/synapse
 * @param keyObject the key of the property
 * @param navigator object model which can be used for navigation around
 * @param dateformat The dateformat that need to convert
 * @return The String value of property using arg one as key and arg two as scope
 */
public Object evaluate(Object scopeObject, Object keyObject, Object dateformat, Navigator navigator) {
    boolean traceOn = synCtx.getTracingState() == SynapseConstants.TRACING_ON;
    boolean traceOrDebugOn = traceOn || log.isDebugEnabled();
    String scope = StringFunction.evaluate(scopeObject, navigator);
    String key = StringFunction.evaluate(keyObject, navigator);
    if (key == null || "".equals(key)) {
        if (traceOrDebugOn) {
            traceOrDebug(traceOn, "property-name should be provided when executing synapse:get-property" + "(scope,prop-name) or synapse:get-property(prop-name) Xpath function");
        }
        return NULL_STRING;
    }
    if (SynapseConstants.SYSTEM_DATE.equals(key)) {
        if (dateformat != null) {
            Format formatter = new SimpleDateFormat(dateformat.toString());
            return formatter.format(new java.util.Date());
        } else {
            Format formatter = new SimpleDateFormat();
            return formatter.format(new java.util.Date());
        }
    }
    // return the current system time as a string , don't care scope
    if (SynapseConstants.SYSTEM_TIME.equals(key)) {
        return Long.toString(System.currentTimeMillis());
    }
    if (XMLConfigConstants.SCOPE_DEFAULT.equals(scope)) {
        if (SynapseConstants.HEADER_TO.equals(key)) {
            EndpointReference toEPR = synCtx.getTo();
            if (toEPR != null) {
                return toEPR.getAddress();
            } else {
                return NULL_STRING;
            }
        } else if (SynapseConstants.HEADER_FROM.equals(key)) {
            EndpointReference fromEPR = synCtx.getFrom();
            if (fromEPR != null) {
                return fromEPR.getAddress();
            } else {
                return NULL_STRING;
            }
        } else if (SynapseConstants.HEADER_ACTION.equals(key)) {
            String wsaAction = synCtx.getWSAAction();
            if (wsaAction != null) {
                return wsaAction;
            } else {
                return NULL_STRING;
            }
        } else if (SynapseConstants.HEADER_FAULT.equals(key)) {
            EndpointReference faultEPR = synCtx.getFaultTo();
            if (faultEPR != null) {
                return faultEPR.getAddress();
            } else {
                return NULL_STRING;
            }
        } else if (SynapseConstants.HEADER_REPLY_TO.equals(key)) {
            EndpointReference replyToEPR = synCtx.getReplyTo();
            if (replyToEPR != null) {
                return replyToEPR.getAddress();
            } else {
                return NULL_STRING;
            }
        } else if (SynapseConstants.HEADER_RELATES_TO.equals(key)) {
            RelatesTo relatesTo = synCtx.getRelatesTo();
            if (relatesTo != null) {
                return relatesTo.getValue();
            } else {
                return NULL_STRING;
            }
        } else if (SynapseConstants.HEADER_MESSAGE_ID.equals(key)) {
            String messageID = synCtx.getMessageID();
            if (messageID != null) {
                return messageID;
            } else {
                return NULL_STRING;
            }
        } else if (SynapseConstants.PROPERTY_FAULT.equals(key)) {
            if (synCtx.getEnvelope().hasFault()) {
                return SynapseConstants.TRUE;
            } else if (synCtx instanceof Axis2MessageContext) {
                org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
                if (axis2MessageContext.getProperty(BaseConstants.FAULT_MESSAGE) != null && SynapseConstants.TRUE.equals(axis2MessageContext.getProperty(BaseConstants.FAULT_MESSAGE))) {
                    return SynapseConstants.TRUE;
                }
            } else {
                return NULL_STRING;
            }
        } else if (SynapseConstants.PROPERTY_MESSAGE_FORMAT.equals(key)) {
            if (synCtx.isDoingPOX())
                return SynapseConstants.FORMAT_POX;
            else if (synCtx.isDoingGET())
                return SynapseConstants.FORMAT_GET;
            else if (synCtx.isSOAP11())
                return SynapseConstants.FORMAT_SOAP11;
            else
                return SynapseConstants.FORMAT_SOAP12;
        } else if (SynapseConstants.PROPERTY_OPERATION_NAME.equals(key) || SynapseConstants.PROPERTY_OPERATION_NAMESPACE.equals(key)) {
            if (synCtx instanceof Axis2MessageContext) {
                AxisOperation axisOperation = ((Axis2MessageContext) synCtx).getAxis2MessageContext().getAxisOperation();
                if (axisOperation != null) {
                    if (SynapseConstants.PROPERTY_OPERATION_NAMESPACE.equals(key)) {
                        return axisOperation.getName().getNamespaceURI();
                    } else {
                        return axisOperation.getName().getLocalPart();
                    }
                }
            }
        } else {
            Object result = synCtx.getProperty(key);
            if (result != null) {
                return result;
            } else {
                return synCtx.getLocalEntry(key);
            }
        }
    } else if (XMLConfigConstants.SCOPE_AXIS2.equals(scope) && synCtx instanceof Axis2MessageContext) {
        org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
        return axis2MessageContext.getProperty(key);
    } else if (XMLConfigConstants.SCOPE_FUNC.equals(scope)) {
        Stack<TemplateContext> functionStack = (Stack) synCtx.getProperty(SynapseConstants.SYNAPSE__FUNCTION__STACK);
        TemplateContext topCtxt = functionStack.peek();
        if (topCtxt != null) {
            return topCtxt.getParameterValue(key);
        }
    } else if (XMLConfigConstants.SCOPE_TRANSPORT.equals(scope) && synCtx instanceof Axis2MessageContext) {
        org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
        Object headers = axis2MessageContext.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
        if (headers != null && headers instanceof Map) {
            Map headersMap = (Map) headers;
            return headersMap.get(key);
        }
    } else if (XMLConfigConstants.SCOPE_OPERATION.equals(scope) && synCtx instanceof Axis2MessageContext) {
        Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
        org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
        return axis2smc.getAxis2MessageContext().getOperationContext().getProperty(key);
    } else if (XMLConfigConstants.SCOPE_REGISTRY.equals(scope)) {
        String[] regParam = key.split("@");
        String regPath = null;
        String propName = null;
        if (regParam.length == 2) {
            regPath = regParam[0];
            propName = regParam[1];
        } else if (regParam.length == 1) {
            regPath = regParam[0];
        }
        Entry propEntry = synCtx.getConfiguration().getEntryDefinition(regPath);
        if (propEntry == null) {
            propEntry = new Entry();
            propEntry.setType(Entry.REMOTE_ENTRY);
            propEntry.setKey(key);
        }
        Registry registry = synCtx.getConfiguration().getRegistry();
        if (registry != null) {
            registry.getResource(propEntry, new Properties());
            if (propName != null) {
                Properties reqProperties = propEntry.getEntryProperties();
                if (reqProperties != null) {
                    if (reqProperties.get(propName) != null) {
                        return reqProperties.getProperty(propName);
                    }
                }
            } else if (propEntry.getValue() != null) {
                if (propEntry.getValue() instanceof OMText) {
                    return ((OMText) propEntry.getValue()).getText();
                }
                return propEntry.getValue().toString();
            }
        }
    } else if (XMLConfigConstants.SCOPE_SYSTEM.equals(scope)) {
        String propVal = System.getProperty(key);
        if (propVal != null) {
            return propVal;
        } else {
            if (traceOrDebugOn) {
                traceOrDebug(traceOn, "System property " + key + " not found");
            }
            return NULL_STRING;
        }
    } else {
        if (traceOrDebugOn) {
            traceOrDebug(traceOn, "Invalid scope : '" + scope + "' has been set for the " + "synapse:get-property(scope,prop-name) XPath function");
        }
    }
    return NULL_STRING;
}
Also used : AxisOperation(org.apache.axis2.description.AxisOperation) Properties(java.util.Properties) RelatesTo(org.apache.axis2.addressing.RelatesTo) Entry(org.apache.synapse.config.Entry) Format(java.text.Format) SimpleDateFormat(java.text.SimpleDateFormat) OMText(org.apache.axiom.om.OMText) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) Registry(org.apache.synapse.registry.Registry) TemplateContext(org.apache.synapse.mediators.template.TemplateContext) EndpointReference(org.apache.axis2.addressing.EndpointReference) Stack(java.util.Stack) SimpleDateFormat(java.text.SimpleDateFormat) Map(java.util.Map)

Example 17 with Axis2MessageContext

use of org.apache.synapse.core.axis2.Axis2MessageContext in project wso2-synapse by wso2.

the class FailoverEndpoint method sendMessage.

private void sendMessage(MessageContext synCtx) {
    logSetter();
    if (log.isDebugEnabled()) {
        log.debug("Failover Endpoint : " + getName());
    }
    if (getContext().isState(EndpointContext.ST_OFF)) {
        informFailure(synCtx, SynapseConstants.ENDPOINT_FO_NONE_READY, "Failover endpoint : " + getName() != null ? getName() : SynapseConstants.ANONYMOUS_ENDPOINT + " - is inactive");
        return;
    }
    boolean isARetry = false;
    Map<String, Integer> mEndpointLog = null;
    if (synCtx.getProperty(SynapseConstants.LAST_ENDPOINT) == null) {
        if (log.isDebugEnabled()) {
            log.debug(this + " Building the SoapEnvelope");
        }
        // If buildMessage attribute available in failover config it is honoured, else global property is considered
        if (isBuildMessageAttAvailable) {
            if (buildMessageAtt) {
                buildMessage(synCtx);
            }
        } else if (buildMessage) {
            buildMessage(synCtx);
        }
        synCtx.getEnvelope().buildWithAttachments();
        // If the endpoint failed during the sending, we need to keep the original envelope and reuse that for other endpoints
        if (Boolean.TRUE.equals(((Axis2MessageContext) synCtx).getAxis2MessageContext().getProperty(PassThroughConstants.MESSAGE_BUILDER_INVOKED))) {
            synCtx.setProperty(SynapseConstants.LB_FO_ENDPOINT_ORIGINAL_MESSAGE, synCtx.getEnvelope());
        }
        mEndpointLog = new HashMap<String, Integer>();
        synCtx.setProperty(SynapseConstants.ENDPOINT_LOG, mEndpointLog);
    } else {
        isARetry = true;
        mEndpointLog = (Map<String, Integer>) synCtx.getProperty(SynapseConstants.ENDPOINT_LOG);
    }
    if (getChildren().isEmpty()) {
        informFailure(synCtx, SynapseConstants.ENDPOINT_FO_NONE_READY, "FailoverLoadbalance endpoint : " + getName() + " - no child endpoints");
        return;
    }
    // evaluate the endpoint properties
    evaluateProperties(synCtx);
    if (dynamic) {
        // Dynamic fail-over mode - Switch to a backup endpoint when an error occurs
        // in the primary endpoint. But switch back to the primary as soon as it becomes
        // active again.
        boolean foundEndpoint = false;
        for (Endpoint endpoint : getChildren()) {
            if (endpoint.readyToSend()) {
                foundEndpoint = true;
                if (isARetry && metricsMBean != null) {
                    metricsMBean.reportSendingFault(SynapseConstants.ENDPOINT_FO_FAIL_OVER);
                }
                synCtx.pushFaultHandler(this);
                if (endpoint instanceof AbstractEndpoint) {
                    org.apache.axis2.context.MessageContext axisMC = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
                    Pipe pipe = (Pipe) axisMC.getProperty(PassThroughConstants.PASS_THROUGH_PIPE);
                    if (pipe != null) {
                        pipe.forceSetSerializationRest();
                    }
                    // allow the message to be content aware if the given message comes via PT
                    if (axisMC.getProperty(PassThroughConstants.PASS_THROUGH_PIPE) != null) {
                        ((AbstractEndpoint) endpoint).setContentAware(true);
                        ((AbstractEndpoint) endpoint).setForceBuildMC(true);
                        if (endpoint instanceof TemplateEndpoint && ((TemplateEndpoint) endpoint).getRealEndpoint() != null) {
                            if (((TemplateEndpoint) endpoint).getRealEndpoint() instanceof AbstractEndpoint) {
                                ((AbstractEndpoint) ((TemplateEndpoint) endpoint).getRealEndpoint()).setContentAware(true);
                                ((AbstractEndpoint) ((TemplateEndpoint) endpoint).getRealEndpoint()).setForceBuildMC(true);
                            }
                        }
                    }
                }
                if (endpoint.getName() != null) {
                    mEndpointLog.put(endpoint.getName(), null);
                }
                endpoint.send(synCtx);
                break;
            }
        }
        if (!foundEndpoint) {
            String msg = "Failover endpoint : " + (getName() != null ? getName() : SynapseConstants.ANONYMOUS_ENDPOINT) + " - no ready child endpoints";
            log.warn(msg);
            informFailure(synCtx, SynapseConstants.ENDPOINT_FO_NONE_READY, msg);
        }
    } else {
        if (currentEndpoint == null) {
            currentEndpoint = getChildren().get(0);
        }
        if (currentEndpoint.readyToSend()) {
            if (isARetry && metricsMBean != null) {
                metricsMBean.reportSendingFault(SynapseConstants.ENDPOINT_FO_FAIL_OVER);
            }
            synCtx.pushFaultHandler(this);
            currentEndpoint.send(synCtx);
        } else {
            boolean foundEndpoint = false;
            for (Endpoint endpoint : getChildren()) {
                if (endpoint.readyToSend()) {
                    foundEndpoint = true;
                    currentEndpoint = endpoint;
                    if (isARetry && metricsMBean != null) {
                        metricsMBean.reportSendingFault(SynapseConstants.ENDPOINT_FO_FAIL_OVER);
                    }
                    synCtx.pushFaultHandler(this);
                    currentEndpoint.send(synCtx);
                    break;
                }
            }
            if (!foundEndpoint) {
                String msg = "Failover endpoint : " + (getName() != null ? getName() : SynapseConstants.ANONYMOUS_ENDPOINT) + " - no ready child endpoints";
                log.warn(msg);
                informFailure(synCtx, SynapseConstants.ENDPOINT_FO_NONE_READY, msg);
            }
        }
    }
}
Also used : Pipe(org.apache.synapse.transport.passthru.Pipe) Integer(org.apache.axis2.databinding.types.soapencoding.Integer) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 18 with Axis2MessageContext

use of org.apache.synapse.core.axis2.Axis2MessageContext in project wso2-synapse by wso2.

the class AbstractEndpoint method prepareForEndpointStatistics.

/**
 * Process statistics for this message
 * @param synCtx the current message
 */
protected void prepareForEndpointStatistics(MessageContext synCtx) {
    // Setting Required property to reportForComponent the End Point aspects
    if (definition != null && definition.isStatisticsEnable()) {
        String opName = null;
        if (synCtx.getProperty(SynapseConstants.ENDPOINT_OPERATION) != null) {
            opName = synCtx.getProperty(SynapseConstants.ENDPOINT_OPERATION).toString();
        } else if (synCtx instanceof Axis2MessageContext) {
            AxisOperation operation = ((Axis2MessageContext) synCtx).getAxis2MessageContext().getAxisOperation();
            if (operation != null) {
                opName = operation.getName().getLocalPart();
            }
            if (opName == null || SynapseConstants.SYNAPSE_OPERATION_NAME.getLocalPart().equals(opName)) {
                String soapAction = synCtx.getSoapAction();
                opName = null;
                if (soapAction != null) {
                    int index = soapAction.indexOf("urn:");
                    if (index >= 0) {
                        opName = soapAction.substring("urn:".length());
                    } else {
                        opName = soapAction;
                    }
                }
            }
        }
        AspectConfiguration oldConfiguration = definition.getAspectConfiguration();
        if (opName != null) {
            AspectConfiguration newConfiguration = new AspectConfiguration(oldConfiguration.getId() + SynapseConstants.STATISTICS_KEY_SEPARATOR + opName);
            if (oldConfiguration.isStatisticsEnable()) {
                newConfiguration.enableStatistics();
            }
            if (oldConfiguration.isTracingEnabled()) {
                newConfiguration.enableTracing();
            }
        }
    }
}
Also used : AxisOperation(org.apache.axis2.description.AxisOperation) AspectConfiguration(org.apache.synapse.aspects.AspectConfiguration) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 19 with Axis2MessageContext

use of org.apache.synapse.core.axis2.Axis2MessageContext in project wso2-synapse by wso2.

the class DefaultEndpoint method send.

public void send(MessageContext synCtx) {
    // For setting Car name (still for Proxy)
    logSetter();
    org.apache.axis2.context.MessageContext messageContext = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
    final Pipe pipe = (Pipe) messageContext.getProperty(PassThroughConstants.PASS_THROUGH_PIPE);
    if (pipe != null && !Boolean.TRUE.equals(messageContext.getProperty(PassThroughConstants.MESSAGE_BUILDER_INVOKED)) && messageContext.getProperty("To") == null) {
        try {
            RelayUtils.buildMessage(((Axis2MessageContext) synCtx).getAxis2MessageContext(), false);
        } catch (Exception e) {
            handleException("Error while building message", e);
        }
    }
    if (getParentEndpoint() == null && !readyToSend()) {
        // if the this leaf endpoint is too a root endpoint and is in inactive
        informFailure(synCtx, SynapseConstants.ENDPOINT_ADDRESS_NONE_READY, "Currently , Default endpoint : " + getContext());
    } else {
        super.send(synCtx);
    }
}
Also used : Pipe(org.apache.synapse.transport.passthru.Pipe) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 20 with Axis2MessageContext

use of org.apache.synapse.core.axis2.Axis2MessageContext in project wso2-synapse by wso2.

the class DynamicLoadbalanceEndpoint method sendToApplicationMember.

protected void sendToApplicationMember(MessageContext synCtx, Member currentMember, DynamicLoadbalanceFaultHandler faultHandler, boolean newSession) {
    // Rewriting the URL
    org.apache.axis2.context.MessageContext axis2MsgCtx = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
    // Removing the REST_URL_POSTFIX - this is a hack.
    // In this loadbalance endpoint we create an endpoint per request by setting the complete url as the adress.
    // If a REST message comes Axis2FlexibleMEPClient append the REST_URL_POSTFIX to the adress. Hence endpoint fails
    // do send the request. e.g.  http://localhost:8080/example/index.html/example/index.html
    axis2MsgCtx.removeProperty(NhttpConstants.REST_URL_POSTFIX);
    String transport = axis2MsgCtx.getTransportIn().getName();
    String address = synCtx.getTo().getAddress();
    int incomingPort = extractPort(synCtx, transport);
    EndpointReference to = getEndpointReferenceAfterURLRewrite(currentMember, transport, address, incomingPort);
    synCtx.setTo(to);
    faultHandler.setTo(to);
    faultHandler.setCurrentMember(currentMember);
    synCtx.pushFaultHandler(faultHandler);
    if (isFailover()) {
        synCtx.getEnvelope().build();
    }
    Endpoint endpoint = getEndpoint(to, currentMember, synCtx);
    faultHandler.setCurrentEp(endpoint);
    if (isSessionAffinityBasedLB()) {
        synCtx.setProperty(SynapseConstants.PROP_SAL_ENDPOINT_DEFAULT_SESSION_TIMEOUT, getSessionTimeout());
        synCtx.setProperty(SynapseConstants.PROP_SAL_ENDPOINT_CURRENT_DISPATCHER, dispatcher);
        prepareEndPointSequence(synCtx, endpoint);
        if (newSession) {
            synCtx.setProperty(SynapseConstants.PROP_SAL_ENDPOINT_CURRENT_MEMBER, currentMember);
            // we should also indicate that this is the first message in the session. so that
            // onFault(...) method can resend only the failed attempts for the first message.
            synCtx.setProperty(SynapseConstants.PROP_SAL_ENDPOINT_FIRST_MESSAGE_IN_SESSION, Boolean.TRUE);
        }
    }
    Map<String, String> memberHosts;
    if ((memberHosts = (Map<String, String>) currentMember.getProperties().get(HttpSessionDispatcher.HOSTS)) == null) {
        currentMember.getProperties().put(HttpSessionDispatcher.HOSTS, memberHosts = new HashMap<String, String>());
    }
    memberHosts.put(extractHost(synCtx), "true");
    setupTransportHeaders(synCtx);
    try {
        endpoint.send(synCtx);
    } catch (Exception e) {
        if (e.getMessage().toLowerCase().contains("io reactor shutdown")) {
            log.fatal("System cannot continue normal operation. Restarting", e);
            // restart
            System.exit(121);
        } else {
            throw new SynapseException(e);
        }
    }
}
Also used : SynapseException(org.apache.synapse.SynapseException) MalformedURLException(java.net.MalformedURLException) SynapseException(org.apache.synapse.SynapseException) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) EndpointReference(org.apache.axis2.addressing.EndpointReference)

Aggregations

Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)92 MessageContext (org.apache.synapse.MessageContext)50 Axis2SynapseEnvironment (org.apache.synapse.core.axis2.Axis2SynapseEnvironment)24 Map (java.util.Map)23 HashMap (java.util.HashMap)20 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)19 SynapseEnvironment (org.apache.synapse.core.SynapseEnvironment)18 Test (org.junit.Test)16 OMElement (org.apache.axiom.om.OMElement)15 ConfigurationContext (org.apache.axis2.context.ConfigurationContext)14 SynapseException (org.apache.synapse.SynapseException)14 AxisConfiguration (org.apache.axis2.engine.AxisConfiguration)13 ArrayList (java.util.ArrayList)12 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)11 EndpointReference (org.apache.axis2.addressing.EndpointReference)10 SynapseLog (org.apache.synapse.SynapseLog)9 Endpoint (org.apache.synapse.endpoints.Endpoint)9 OperationContext (org.apache.axis2.context.OperationContext)8 AxisFault (org.apache.axis2.AxisFault)7 Entry (org.apache.synapse.config.Entry)7