Wednesday, March 8, 2017

Retrieve the Properties inside ESB 4.9.0 Synapse Engine for API Invoke using Class Mediator

To retrieve the list of properties available inside the Synapse Engine, follow the below:

Create a class mediator as below:

package com.custom.urlmapping;

import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.synapse.MessageContext;
import org.apache.synapse.mediators.AbstractMediator;

public class RetrieveURLMappingMediator extends AbstractMediator {

 private static final Log log = LogFactory
   .getLog(RetrieveURLMappingMediator.class);

 public boolean mediate(MessageContext context) {

  log.info("RetrieveURLMappingMediator Invoked.");

  Set propertySet = context.getPropertyKeySet();
  for (Object propertyKey : propertySet) {

   log.info("Key: " + propertyKey.toString() + "| value: "
     + context.getProperty(propertyKey.toString()));
  }

  return true;
 }
}


Create a API as below:

<api xmlns="http://ws.apache.org/ns/synapse" name="RetriveURLMapping" context="/retrieve">
   <resource methods="GET" uri-template="/view/{symbol}">
      <inSequence>
         <log level="custom">
            <property name="STATUS" value="------------------RetriveURLMapping Invoked--------------------"></property>
         </log>
         <class name="com.custom.urlmapping.RetrieveURLMappingMediator"></class>
         <log level="custom">
            <property name="STATUS" value="------------------RetriveURLMapping Invoked END--------------------"></property>
         </log>
      </inSequence>
   </resource>
</api>

Output log will be:

[2017-03-08 15:16:14,713]  INFO - LogMediator STATUS = ------------------RetriveURLMapping Invoked--------------------
[2017-03-08 15:16:14,714]  INFO - RetrieveURLMappingMediator RetrieveURLMappingMediator Invoked.
[2017-03-08 15:16:14,714]  INFO - RetrieveURLMappingMediator Key: REST_API_CONTEXT| value: /retrieve
[2017-03-08 15:16:14,714]  INFO - RetrieveURLMappingMediator Key: REST_URL_PREFIX| value: http://172.17.0.1:8280
[2017-03-08 15:16:14,714]  INFO - RetrieveURLMappingMediator Key: SYNAPSE_REST_API_VERSION| value: 
[2017-03-08 15:16:14,714]  INFO - RetrieveURLMappingMediator Key: REST_METHOD| value: GET
[2017-03-08 15:16:14,714]  INFO - RetrieveURLMappingMediator Key: REST_SUB_REQUEST_PATH| value: /view/12345
[2017-03-08 15:16:14,714]  INFO - RetrieveURLMappingMediator Key: REST_FULL_REQUEST_PATH| value: /retrieve/view/12345
[2017-03-08 15:16:14,714]  INFO - RetrieveURLMappingMediator Key: uri.var.symbol| value: 12345
[2017-03-08 15:16:14,714]  INFO - RetrieveURLMappingMediator Key: TRANSPORT_IN_NAME| value: http
[2017-03-08 15:16:14,714]  INFO - RetrieveURLMappingMediator Key: SYNAPSE_RESOURCE| value: e056039cf63ab475f60c7bcc490e27bd50789e56c10dc453
[2017-03-08 15:16:14,715]  INFO - RetrieveURLMappingMediator Key: SYNAPSE_REST_API| value: RetriveURLMapping
[2017-03-08 15:16:14,715]  INFO - LogMediator STATUS = ------------------RetriveURLMapping Invoked END--------------------


No comments:

Post a Comment