org.apache.synapse.endpoints.Endpoint Java Examples

The following examples show how to use org.apache.synapse.endpoints.Endpoint. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EndpointResource.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
/**
 * Changes the endpoint state based on json payload.
 *
 * @param axis2MessageContext Axis2Message context
 * @param configuration       Synapse configuration
 * @param payload             Request json payload
 */
private void changeEndpointStatus(org.apache.axis2.context.MessageContext axis2MessageContext,
                                  SynapseConfiguration configuration, JsonObject payload) {

    String endpointName = payload.get(NAME).getAsString();
    String status = payload.get(STATUS).getAsString();
    Endpoint ep = configuration.getEndpoint(endpointName);
    if (ep != null) {
        JSONObject jsonResponse = new JSONObject();
        if (INACTIVE_STATUS.equalsIgnoreCase(status)) {
            ep.getContext().switchOff();
            jsonResponse.put(Constants.MESSAGE_JSON_ATTRIBUTE, endpointName + " is switched Off");
        } else if (ACTIVE_STATUS.equalsIgnoreCase(status)) {
            ep.getContext().switchOn();
            jsonResponse.put(Constants.MESSAGE_JSON_ATTRIBUTE, endpointName + " is switched On");
        } else {
            jsonResponse = Utils.createJsonError("Provided state is not valid", axis2MessageContext, Constants.BAD_REQUEST);
        }
        Utils.setJsonPayLoad(axis2MessageContext, jsonResponse);
    } else {
        Utils.setJsonPayLoad(axis2MessageContext,  Utils.createJsonError("Endpoint does not exist",
                axis2MessageContext, Constants.NOT_FOUND));
    }
}
 
Example #2
Source File: TenantAwareLoadBalanceEndpoint.java    From attic-stratos with Apache License 2.0 6 votes vote down vote up
private void prepareEndPointSequence(MessageContext synCtx, Endpoint endpoint) {

        Object o = synCtx.getProperty(SynapseConstants.PROP_SAL_ENDPOINT_ENDPOINT_LIST);
        List<Endpoint> endpointList;
        if (o instanceof List) {
            endpointList = (List<Endpoint>) o;
            endpointList.add(this);

        } else {
            // this is the first endpoint in the hierarchy. so create the queue and
            // insert this as the first element.
            endpointList = new ArrayList<Endpoint>();
            endpointList.add(this);
            synCtx.setProperty(SynapseConstants.PROP_SAL_ENDPOINT_ENDPOINT_LIST, endpointList);
        }

        // if the next endpoint is not a session affinity one, endpoint sequence ends
        // here. but we have to add the next endpoint to the list.
        if (!(endpoint instanceof TenantAwareLoadBalanceEndpoint)) {
            endpointList.add(endpoint);
            // Clearing out if there any any session information with current message
            if (dispatcher.isServerInitiatedSession()) {
                dispatcher.removeSessionID(synCtx);
            }
        }
    }
 
Example #3
Source File: TenantAwareLoadBalanceEndpoint.java    From attic-stratos with Apache License 2.0 6 votes vote down vote up
/**
 * @param to     get an endpoint to send the information
 * @param member The member to which an EP has to be created
 * @param synCtx synapse context
 * @return the created endpoint
 */
private Endpoint getEndpoint(EndpointReference to, org.apache.axis2.clustering.Member member, MessageContext synCtx) {
    AddressEndpoint endpoint = new AddressEndpoint();
    endpoint.setEnableMBeanStats(false);
    endpoint.setName("DLB:" + member.getHostName() +
            ":" + member.getPort() + ":" + UUID.randomUUID());

    EndpointDefinition definition = new EndpointDefinition();
    definition.setTimeoutAction(SynapseConstants.DISCARD_AND_FAULT);
    definition.setTimeoutDuration(LoadBalancerConfiguration.getInstance().getEndpointTimeout());
    definition.setReplicationDisabled(true);
    definition.setAddress(to.getAddress());

    endpoint.setDefinition(definition);
    endpoint.init((SynapseEnvironment)
            ((Axis2MessageContext) synCtx).getAxis2MessageContext().
                    getConfigurationContext().getAxisConfiguration().
                    getParameterValue(SynapseConstants.SYNAPSE_ENV));
    return endpoint;
}
 
Example #4
Source File: LoadBalancerServiceComponent.java    From attic-stratos with Apache License 2.0 6 votes vote down vote up
/**
 * Registers the Endpoint deployer.
 *
 * @param axisConfig         AxisConfiguration to which this deployer belongs
 * @param synapseEnvironment SynapseEnvironment to which this deployer belongs
 */
private void registerDeployer(AxisConfiguration axisConfig,
                              SynapseEnvironment synapseEnvironment)
        throws TenantAwareLoadBalanceEndpointException {
    SynapseConfiguration synCfg = synapseEnvironment
            .getSynapseConfiguration();
    DeploymentEngine deploymentEngine = (DeploymentEngine) axisConfig
            .getConfigurator();
    SynapseArtifactDeploymentStore deploymentStore = synCfg
            .getArtifactDeploymentStore();

    String synapseConfigPath = ServiceBusUtils
            .getSynapseConfigAbsPath(synapseEnvironment
                    .getServerContextInformation());
    String endpointDirPath = synapseConfigPath + File.separator
            + MultiXMLConfigurationBuilder.ENDPOINTS_DIR;

    for (Endpoint ep : synCfg.getDefinedEndpoints().values()) {
        if (ep.getFileName() != null) {
            deploymentStore.addRestoredArtifact(endpointDirPath
                    + File.separator + ep.getFileName());
        }
    }
    deploymentEngine.addDeployer(new EndpointDeployer(), endpointDirPath,
            ServiceBusConstants.ARTIFACT_EXTENSION);
}
 
Example #5
Source File: MediationPersistenceTest.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
protected void assertEquals(Endpoint expected, Endpoint actual) {
    if (expected != null && actual != null) {
        if (expected.getName() == null) {
            assertNull(actual.getName());
        } else {
            assertEquals(expected.getName(), actual.getName());
        }

        if (expected instanceof AddressEndpoint && actual instanceof AddressEndpoint) {
            AddressEndpoint original = (AddressEndpoint) expected;
            AddressEndpoint copy = (AddressEndpoint) actual;
            assertEquals(original.getDefinition().getAddress(), copy.getDefinition().getAddress());
            return;
        }

        // TODO: Implement support for comparing other types of endpoints
        // TODO: For the moment we can live with only address endpoints

    } else if (expected == null) {
        assertNull(actual);
        return;
    }
    fail("Endpoints do not match");
}
 
Example #6
Source File: EndpointPersistenceTest.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
private void checkSynapseXMLPersistence() {
    InputStream in = getClass().getClassLoader().getResourceAsStream("epr2.xml");
    Endpoint endpoint2 = createEndpoint(in);
    synapseConfigSvc.getSynapseConfiguration().addEndpoint(endpoint2.getName(), endpoint2);
    getMediationPersistenceManager().saveItem(endpoint2.getName(),
            ServiceBusConstants.ITEM_TYPE_ENDPOINT);
    System.out.println("Added new endpoint : " + endpoint2.getName());
    hold();
    Endpoint copy = getConfigurationFromSynapseXML().getDefinedEndpoints().
            get(endpoint2.getName());
    assertEquals(endpoint2, copy);
    System.out.println("Endpoint : " + endpoint2.getName() + " saved successfully");

    synapseConfigSvc.getSynapseConfiguration().removeEndpoint(endpoint2.getName());
    getMediationPersistenceManager().deleteItem(endpoint2.getName(), null,
            ServiceBusConstants.ITEM_TYPE_ENDPOINT);
    System.out.println("Endpoint : " + endpoint2.getName() + " removed");
    hold();

    Endpoint removedSeq = getConfigurationFromSynapseXML().getDefinedEndpoints().
            get(endpoint2.getName());
    assertNull(removedSeq);
    System.out.println("Endpoint : " + endpoint2.getName() + " deleted from synapse.xml successfully");
}
 
Example #7
Source File: EndpointResource.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
private JSONObject getEndpointByName(MessageContext messageContext, String endpointName) {

        SynapseConfiguration configuration = messageContext.getConfiguration();
        Endpoint ep = configuration.getEndpoint(endpointName);
        if (Objects.nonNull(ep)) {
            return getEndpointAsJson(ep);
        } else {
            return null;
        }
    }
 
Example #8
Source File: EndpointResource.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the json representation of the endpoint based on its type.
 *
 * @param endpoint endpoint
 * @return json-object with endpoint details
 */
private JSONObject getEndpointAsJson(Endpoint endpoint) {

    JSONObject endpointObject = endpoint.getJsonRepresentation();
    OMElement synapseConfiguration = EndpointSerializer.getElementFromEndpoint(endpoint);
    endpointObject.put(Constants.SYNAPSE_CONFIGURATION, synapseConfiguration);
    endpointObject.put(IS_ACTIVE, isEndpointActive(endpoint));

    return endpointObject;
}
 
Example #9
Source File: EndpointResource.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
private void populateEndpointList(MessageContext messageContext, SynapseConfiguration configuration) {

        org.apache.axis2.context.MessageContext axis2MessageContext =
                ((Axis2MessageContext) messageContext).getAxis2MessageContext();

        Map<String, Endpoint> namedEndpointMap = configuration.getDefinedEndpoints();
        Collection<Endpoint> namedEndpointCollection = namedEndpointMap.values();

        JSONObject jsonBody = Utils.createJSONList(namedEndpointCollection.size());

        for (Endpoint ep : namedEndpointCollection) {

            JSONObject endpointObject = new JSONObject();

            String epName = ep.getName();
            endpointObject.put(Constants.NAME, epName);

            OMElement element = EndpointSerializer.getElementFromEndpoint(ep);
            OMElement firstElement = element.getFirstElement();
            String type;
            // For template endpoints the endpoint type can not be retrieved from firstElement
            if (firstElement == null) {
                type = element.getAttribute(new QName("template")).getLocalName();
            } else {
                type = firstElement.getLocalName();
            }
            endpointObject.put(Constants.TYPE, type);
            endpointObject.put(IS_ACTIVE, isEndpointActive(ep));

            jsonBody.getJSONArray(Constants.LIST).put(endpointObject);
        }
        Utils.setJsonPayLoad(axis2MessageContext, jsonBody);
    }
 
Example #10
Source File: TestMessageContext.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
public Endpoint getEndpoint(String key) {
    Object o = localEntries.get(key);
    if (o != null && o instanceof Endpoint) {
        return (Endpoint) o;
    } else {
        Endpoint e = getConfiguration().getEndpoint(key);
        synchronized (e) {
            if (!e.isInitialized()) {
                e.init(synEnv);
            }
        }
        localEntries.put(key, e);
        return e;
    }
}
 
Example #11
Source File: EndpointStore.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
protected OMElement saveToFile(Endpoint endpoint, SynapseConfiguration synapseConfig) {
    try {
        return serializer.serializeEndpoint(endpoint, synapseConfig, null);
    } catch (Exception e) {
        handleException("Error while saving the endpoint: " + endpoint.getName() + " to the " +
                "file system", e);
    }
    return null;
}
 
Example #12
Source File: EndpointPersistenceTest.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
public void testEndpointPersistence() throws IOException {
    System.out.println("Starting endpoint persistence test...");

    String fileName = "epr1.xml";
    InputStream in = getClass().getClassLoader().getResourceAsStream(fileName);
    Endpoint endpoint = createEndpoint(in);
    in.close();
    endpoint.setFileName(fileName);
    synapseConfigSvc.getSynapseConfiguration().addEndpoint(endpoint.getName(), endpoint);
    getMediationPersistenceManager().saveItem(endpoint.getName(),
            ServiceBusConstants.ITEM_TYPE_ENDPOINT);
    System.out.println("Added new endpoint : " + endpoint.getName());
    checkSavedEndpoint(endpoint);

    if (endpoint instanceof AddressEndpoint) {
        ((AddressEndpoint) endpoint).getDefinition().setAddress("http://wso2.org/services/Foo");
        getMediationPersistenceManager().saveItem(endpoint.getName(),
                ServiceBusConstants.ITEM_TYPE_ENDPOINT);
        System.out.println("Updated endpoint : " + endpoint.getName());
        checkSavedEndpoint(endpoint);
    }

    synapseConfigSvc.getSynapseConfiguration().removeEndpoint(endpoint.getName());
    getMediationPersistenceManager().deleteItem(endpoint.getName(), fileName,
            ServiceBusConstants.ITEM_TYPE_ENDPOINT);
    System.out.println("Endpoint : " + endpoint.getName() + " removed");
    hold();

    File file = new File(path + File.separator +
            MultiXMLConfigurationBuilder.ENDPOINTS_DIR, fileName);
    if (file.exists()) {
        fail("The file : " + fileName + " has not been deleted");
    }
    System.out.println("Endpoint file : " + fileName + " deleted successfully");

    checkSynapseXMLPersistence();

    System.out.println("Endpoint persistence test completed successfully...");
}
 
Example #13
Source File: TenantAwareLoadBalanceEndpoint.java    From attic-stratos with Apache License 2.0 4 votes vote down vote up
@Override
public void setCurrentEp(Endpoint currentEp) {
    this.currentEp = currentEp;
}
 
Example #14
Source File: TenantAwareLoadBalanceEndpoint.java    From attic-stratos with Apache License 2.0 4 votes vote down vote up
protected void sendToApplicationMember(MessageContext synCtx,
                                       org.apache.axis2.clustering.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 load balance endpoint we create an endpoint per request by setting the complete url as the address.
    //If a REST message comes Axis2FlexibleMEPClient append the REST_URL_POSTFIX to the address. 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();
    EndpointReference to = getEndpointReferenceAfterURLRewrite(synCtx, currentMember, transport);
    synCtx.setTo(to);

    Endpoint endpoint = getEndpoint(to, currentMember, synCtx);

    // Push fault handler to manage statistics and fail-over logic
    faultHandler.setTo(to);
    faultHandler.setCurrentMember(currentMember);
    faultHandler.setCurrentEp(endpoint);
    synCtx.pushFaultHandler(faultHandler);
    synCtx.getEnvelope().build();

    if (isSessionAffinityBasedLB()) {
        synCtx.setProperty(SynapseConstants.PROP_SAL_ENDPOINT_DEFAULT_SESSION_TIMEOUT, getSessionTimeout());
        synCtx.setProperty(SynapseConstants.PROP_SAL_ENDPOINT_CURRENT_DISPATCHER, dispatcher);

        if (newSession) {
            prepareEndPointSequence(synCtx, endpoint);
            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(extractTargetHost(synCtx), "true");
    setupTransportHeaders(synCtx);
    setupLoadBalancerContextProperties(synCtx, currentMember);

    try {
        if (log.isDebugEnabled()) {
            log.debug(String.format("Sending request %s to endpoint: %s", synCtx.getMessageID(), to.getAddress()));
        }
        endpoint.send(synCtx);

        // Increment in-flight request count
        incrementInFlightRequestCount(synCtx);
    } catch (Exception e) {
        if (e.getMessage().toLowerCase().contains("io reactor shutdown")) {
            log.fatal("System cannot continue normal operation. Restarting", e);
            System.exit(121); // restart
        } else {
            throw new SynapseException(e);
        }
    }
}
 
Example #15
Source File: MediationPersistenceTest.java    From micro-integrator with Apache License 2.0 4 votes vote down vote up
protected Endpoint createEndpoint(InputStream in) {
    return EndpointFactory.getEndpointFromElement(parse(in), false, new Properties());
}
 
Example #16
Source File: EndpointStore.java    From micro-integrator with Apache License 2.0 4 votes vote down vote up
protected OMElement serialize(Endpoint endpoint) {
    return EndpointSerializer.getElementFromEndpoint(endpoint);
}
 
Example #17
Source File: EndpointStore.java    From micro-integrator with Apache License 2.0 4 votes vote down vote up
protected Endpoint getObjectToPersist(String name, SynapseConfiguration config) {
    return config.getDefinedEndpoints().get(name);
}
 
Example #18
Source File: EndpointStore.java    From micro-integrator with Apache License 2.0 4 votes vote down vote up
protected String getFileName(Endpoint endpoint) {
    if (endpoint instanceof AbstractEndpoint) {
        return endpoint.getFileName();
    }
    return null;
}
 
Example #19
Source File: SynapseAppDeployer.java    From micro-integrator with Apache License 2.0 4 votes vote down vote up
/**
 * Register synapse deployers to the deployment engine.
 *
 * @param axisConfig   - axisConfiguration to which this deployer belongs
 * @param artifactType - type of the artifact
 * @param deployer     - related deployer
 */
private void registerSynapseDeployer(AxisConfiguration axisConfig, String artifactType, Deployer deployer) {

    DeploymentEngine deploymentEngine = (DeploymentEngine) axisConfig.getConfigurator();
    String artifactDirName = getArtifactDirName(artifactType);

    if (artifactDirName != null) {
        SynapseArtifactDeploymentStore deploymentStore = getSynapseConfiguration(axisConfig).getArtifactDeploymentStore();
        SynapseConfiguration synCfg = getSynapseConfiguration(axisConfig);
        String artifactDir = getArtifactDirPath(axisConfig, artifactDirName);

        switch (artifactType) {
            case SynapseAppDeployerConstants.SEQUENCE_TYPE:
                for (SequenceMediator seq : synCfg.getDefinedSequences().values()) {
                    if (seq.getFileName() != null) {
                        deploymentStore.addRestoredArtifact(artifactDir + File.separator + seq.getFileName());
                    }
                }
                break;
            case SynapseAppDeployerConstants.ENDPOINT_TYPE:
                for (Endpoint ep : synCfg.getDefinedEndpoints().values()) {
                    if (ep.getFileName() != null) {
                        deploymentStore.addRestoredArtifact(artifactDir + File.separator + ep.getFileName());
                    }
                }
                break;
            case SynapseAppDeployerConstants.PROXY_SERVICE_TYPE:
                for (ProxyService proxyService : synCfg.getProxyServices()) {
                    if (proxyService.getFileName() != null) {
                        deploymentStore.addRestoredArtifact(artifactDir + File.separator + proxyService.getFileName());
                    }
                }
                break;
            case SynapseAppDeployerConstants.LOCAL_ENTRY_TYPE:
                for (Entry entry : synCfg.getDefinedEntries().values()) {
                    if (entry.getFileName() != null) {
                        deploymentStore.addRestoredArtifact(artifactDir + File.separator + entry.getFileName());
                    }
                }
                break;
            case SynapseAppDeployerConstants.TASK_TYPE:
                for (Startup stp : synCfg.getStartups()) {
                    if (stp.getFileName() != null) {
                        deploymentStore.addRestoredArtifact(artifactDir + File.separator + stp.getFileName());
                    }
                }
                break;
            case SynapseAppDeployerConstants.MESSAGE_STORE_TYPE:
                for (MessageStore messageStore : synCfg.getMessageStores().values()) {
                    if (messageStore.getFileName() != null) {
                        deploymentStore.addRestoredArtifact(artifactDir + File.separator + messageStore.getFileName());
                    }
                }
                break;
            case SynapseAppDeployerConstants.MESSAGE_PROCESSOR_TYPE:
                for (MessageProcessor processor : synCfg.getMessageProcessors().values()) {
                    if (processor.getFileName() != null) {
                        deploymentStore.addRestoredArtifact(artifactDir + File.separator + processor.getFileName());
                    }
                }
                break;
            case SynapseAppDeployerConstants.API_TYPE:
                for (API api : synCfg.getAPIs()) {
                    if (api.getFileName() != null) {
                        deploymentStore.addRestoredArtifact(artifactDir + File.separator + api.getFileName());
                    }
                }
                break;
            case SynapseAppDeployerConstants.TEMPLATE_TYPE:
                for (TemplateMediator seqTempl : synCfg.getSequenceTemplates().values()) {
                    if (seqTempl.getFileName() != null) {
                        deploymentStore.addRestoredArtifact(artifactDir + File.separator + seqTempl.getFileName());
                    }
                }
                for (Template epTempl : synCfg.getEndpointTemplates().values()) {
                    if (epTempl.getFileName() != null) {
                        deploymentStore.addRestoredArtifact(artifactDir + File.separator + epTempl.getFileName());
                    }
                }
                break;
            case SynapseAppDeployerConstants.INBOUND_ENDPOINT_TYPE:
                for (InboundEndpoint inboundEndpoint : synCfg.getInboundEndpoints()) {
                    if (inboundEndpoint.getFileName() != null) {
                        deploymentStore.addRestoredArtifact(artifactDir + File.separator + inboundEndpoint.getFileName());
                    }
                }
                break;
            default:
                // do nothing

        }
        deploymentEngine.addDeployer(deployer, artifactDir, ServiceBusConstants.ARTIFACT_EXTENSION);
    }
}
 
Example #20
Source File: EndpointResource.java    From micro-integrator with Apache License 2.0 4 votes vote down vote up
private Boolean isEndpointActive(Endpoint endpoint) {
    // 1 represents the endpoint active state
    return endpoint.getContext().isState(1);
}