Search in sources :

Example 1 with EndpointDefinition

use of org.apache.synapse.endpoints.EndpointDefinition in project wso2-synapse by wso2.

the class CallMediator method handleBlockingCall.

/**
 * Send request in blocking manner
 *
 * @param synInCtx message context
 * @return  continue the mediation flow or not
 */
private boolean handleBlockingCall(MessageContext synInCtx) {
    SynapseLog synLog = getLog(synInCtx);
    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("Start : Call mediator - Blocking Call");
        if (synLog.isTraceTraceEnabled()) {
            synLog.traceTrace("Message : " + synInCtx.getEnvelope());
        }
    }
    MessageContext resultMsgCtx = null;
    // set initClientOption with blockingMsgsender
    blockingMsgSender.setInitClientOptions(initClientOptions);
    // blocking sender.
    if (endpoint == null) {
        endpoint = new DefaultEndpoint();
        EndpointDefinition endpointDefinition = new EndpointDefinition();
        ((DefaultEndpoint) endpoint).setDefinition(endpointDefinition);
        isWrappingEndpointCreated = true;
    }
    try {
        if ("true".equals(synInCtx.getProperty(SynapseConstants.OUT_ONLY))) {
            blockingMsgSender.send(endpoint, synInCtx);
        } else {
            resultMsgCtx = blockingMsgSender.send(endpoint, synInCtx);
            if ("true".equals(resultMsgCtx.getProperty(SynapseConstants.BLOCKING_SENDER_ERROR))) {
                handleFault(synInCtx, (Exception) resultMsgCtx.getProperty(SynapseConstants.ERROR_EXCEPTION));
            }
        }
    } catch (Exception ex) {
        handleFault(synInCtx, ex);
    }
    if (resultMsgCtx != null) {
        if (synLog.isTraceTraceEnabled()) {
            synLog.traceTrace("Response payload received : " + resultMsgCtx.getEnvelope());
        }
        try {
            synInCtx.setEnvelope(resultMsgCtx.getEnvelope());
            if (synLog.isTraceOrDebugEnabled()) {
                synLog.traceOrDebug("End : Call mediator - Blocking Call");
            }
            return true;
        } catch (Exception e) {
            handleFault(synInCtx, e);
        }
    } else {
        if (synLog.isTraceOrDebugEnabled()) {
            synLog.traceOrDebug("Service returned a null response");
        }
    }
    return true;
}
Also used : SynapseLog(org.apache.synapse.SynapseLog) DefaultEndpoint(org.apache.synapse.endpoints.DefaultEndpoint) EndpointDefinition(org.apache.synapse.endpoints.EndpointDefinition) MessageContext(org.apache.synapse.MessageContext) SynapseException(org.apache.synapse.SynapseException)

Example 2 with EndpointDefinition

use of org.apache.synapse.endpoints.EndpointDefinition 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;
}
Also used : AxisFault(org.apache.axis2.AxisFault) ResolvingEndpoint(org.apache.synapse.endpoints.ResolvingEndpoint) SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) AbstractEndpoint(org.apache.synapse.endpoints.AbstractEndpoint) Options(org.apache.axis2.client.Options) IndirectEndpoint(org.apache.synapse.endpoints.IndirectEndpoint) Set(java.util.Set) ServiceContext(org.apache.axis2.context.ServiceContext) AxisService(org.apache.axis2.description.AxisService) EndpointDefinition(org.apache.synapse.endpoints.EndpointDefinition) SynapseException(org.apache.synapse.SynapseException) EndpointReference(org.apache.axis2.addressing.EndpointReference) ServiceGroupContext(org.apache.axis2.context.ServiceGroupContext) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) TemplateEndpoint(org.apache.synapse.endpoints.TemplateEndpoint) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 3 with EndpointDefinition

use of org.apache.synapse.endpoints.EndpointDefinition in project wso2-synapse by wso2.

the class ProxyServiceTest method testBuildAxisServiceWithUnreachableWsdlEndpointWithPublishWSDLSafeModeDisabled.

/**
 * Tests building a proxy service with an unreachable endpoint specified as the published wsdl url with the
 * 'enablePublishWSDLSafeMode' set to true.
 *
 * @throws Exception if an error occurs when converting the URI string to a URI
 */
public void testBuildAxisServiceWithUnreachableWsdlEndpointWithPublishWSDLSafeModeDisabled() throws Exception {
    SynapseConfiguration synCfg = new SynapseConfiguration();
    AxisConfiguration axisCfg = new AxisConfiguration();
    ProxyService proxyService = new ProxyService("faultyPublishWsdlEndpointProxyWithPublishWSDLSafeModeDisabled");
    proxyService.setPublishWSDLEndpoint("wsdlEndPoint");
    AddressEndpoint wsdlEndpoint = new AddressEndpoint();
    EndpointDefinition endpointDefinition = new EndpointDefinition();
    endpointDefinition.setAddress((new URI("http://localhost/SimpleStockService.wsdl")).toString());
    wsdlEndpoint.setDefinition(endpointDefinition);
    proxyService.addParameter("enablePublishWSDLSafeMode", false);
    synCfg.addEndpoint("wsdlEndPoint", wsdlEndpoint);
    try {
        proxyService.buildAxisService(synCfg, axisCfg);
        Assert.fail("Axis service built with an unreachable wsdl endpoint should throw fault");
    } catch (SynapseException e) {
        Assert.assertTrue("Unexpected exception thrown: " + e, e.getMessage().contains("Error reading from wsdl URI"));
    }
}
Also used : AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) AddressEndpoint(org.apache.synapse.endpoints.AddressEndpoint) SynapseException(org.apache.synapse.SynapseException) EndpointDefinition(org.apache.synapse.endpoints.EndpointDefinition) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) URI(java.net.URI)

Example 4 with EndpointDefinition

use of org.apache.synapse.endpoints.EndpointDefinition in project wso2-synapse by wso2.

the class ProxyServiceTest method testBuildAxisServiceWithUnreachableWsdlEndpointWithPublishWSDLSafeModeEnabled.

/**
 * Tests building a proxy service with an unreachable endpoint specified as the published wsdl url with the
 * 'enablePublishWSDLSafeMode' set to false.
 *
 * @throws Exception if an error occurs when converting the URI string to a URI
 */
public void testBuildAxisServiceWithUnreachableWsdlEndpointWithPublishWSDLSafeModeEnabled() throws Exception {
    MessageContext synCtx = new TestMessageContext();
    SynapseConfiguration synCfg = new SynapseConfiguration();
    synCtx.setConfiguration(synCfg);
    AxisConfiguration axisCfg = new AxisConfiguration();
    ProxyService proxyService = new ProxyService("faultyPublishWsdlEndpointProxyWithPublishWSDLSafeModeEnabled");
    proxyService.setPublishWSDLEndpoint("wsdlEndPoint");
    AddressEndpoint wsdlEndpoint = new AddressEndpoint();
    EndpointDefinition endpointDefinition = new EndpointDefinition();
    endpointDefinition.setAddress((new URI("http://localhost/SimpleStockService.wsdl")).toString());
    wsdlEndpoint.setDefinition(endpointDefinition);
    proxyService.addParameter("enablePublishWSDLSafeMode", true);
    synCfg.addEndpoint("wsdlEndPoint", wsdlEndpoint);
    Assert.assertNull("Axis service built with an unreachable wsdl endpoint be null", proxyService.buildAxisService(synCfg, axisCfg));
}
Also used : AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) TestMessageContext(org.apache.synapse.TestMessageContext) AddressEndpoint(org.apache.synapse.endpoints.AddressEndpoint) EndpointDefinition(org.apache.synapse.endpoints.EndpointDefinition) MessageContext(org.apache.synapse.MessageContext) TestMessageContext(org.apache.synapse.TestMessageContext) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) URI(java.net.URI)

Example 5 with EndpointDefinition

use of org.apache.synapse.endpoints.EndpointDefinition in project wso2-synapse by wso2.

the class SynapseXMLConfigurationSerializerTest method testSerializeConfiguration6.

/**
 * Test serializeConfigurationMethod with Endpoint added for SynapseConfiguration and assert OMElement returned
 */
@Test
public void testSerializeConfiguration6() {
    SynapseXMLConfigurationSerializer serializer = new SynapseXMLConfigurationSerializer();
    SynapseConfiguration synapseConfiguration = new SynapseConfiguration();
    AddressEndpoint endpoint = new AddressEndpoint();
    EndpointDefinition definition = new EndpointDefinition();
    definition.setAddress("testUrl");
    endpoint.setName("testEndpoint");
    endpoint.setDefinition(definition);
    synapseConfiguration.addEndpoint(endpoint.getName(), endpoint);
    OMElement element = serializer.serializeConfiguration(synapseConfiguration);
    Assert.assertNotNull("OMElement is not returned", element);
    Assert.assertEquals("definitions", element.getLocalName());
    Assert.assertTrue("Endpoint added is not serialized.", element.getChildren().next().toString().contains("name=\"testEndpoint\""));
}
Also used : AddressEndpoint(org.apache.synapse.endpoints.AddressEndpoint) EndpointDefinition(org.apache.synapse.endpoints.EndpointDefinition) OMElement(org.apache.axiom.om.OMElement) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) Test(org.junit.Test)

Aggregations

EndpointDefinition (org.apache.synapse.endpoints.EndpointDefinition)24 OMElement (org.apache.axiom.om.OMElement)10 AddressEndpoint (org.apache.synapse.endpoints.AddressEndpoint)10 QName (javax.xml.namespace.QName)6 OMAttribute (org.apache.axiom.om.OMAttribute)6 SynapseException (org.apache.synapse.SynapseException)6 MessageContext (org.apache.synapse.MessageContext)4 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)4 DefaultEndpoint (org.apache.synapse.endpoints.DefaultEndpoint)4 AxisConfiguration (org.apache.axis2.engine.AxisConfiguration)3 AbstractEndpoint (org.apache.synapse.endpoints.AbstractEndpoint)3 SynapseXPath (org.apache.synapse.util.xpath.SynapseXPath)3 IOException (java.io.IOException)2 URI (java.net.URI)2 AxisFault (org.apache.axis2.AxisFault)2 TestMessageContext (org.apache.synapse.TestMessageContext)2 Endpoint (org.apache.synapse.endpoints.Endpoint)2 HTTPEndpoint (org.apache.synapse.endpoints.HTTPEndpoint)2 WSDLEndpoint (org.apache.synapse.endpoints.WSDLEndpoint)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1