How to use flow variable in LocalTargetConnection.Path?

I'm implementing a proxy chaining. However, I need to decide which proxy to give a call to based on certain conditions. For the same, I'm setting one flow variable which gives me the proxy's path, which I will set in LocalTargetConnection.Path. Something like this,

<LocalTargetConnection>  
    <Path>/{primaryPath}</Path>
</LocalTargetConnection>

When I'm tracing the request it shows the URL to be

http://localhost:8998/{primaryPath}/<actualPath>;

And it never reaches to the actual proxy. Am I doing anything wrong over here?

Solved Solved
1 3 1,074
1 ACCEPTED SOLUTION

Unfortunately the Path element is not a Message Template, and you don't get curly-brace variable replacement there, as you have observed.

The way to do what you want is to set the target.url within the PreFlow of the Target Endpoint, in the same way you would do, if you were using an HTTPTargetConnection.

Like this:

<TargetEndpoint name="chain">
    <PreFlow name="PreFlow">
      <Request>
        <Step>
          <Name>JS-SetTargetUrl</Name>
        </Step>
      </Request>
        <Response/>
    </PreFlow>
    <PostFlow name="PostFlow">
        <Request/>
        <Response/>
    </PostFlow>
    <Flows/>
    <LocalTargetConnection>
        <Path>/required-but-not-used</Path>
    </LocalTargetConnection>
</TargetEndpoint>
 

And then in the JavaScript policy, do something like this:

<Javascript name='JS-SetTargetUrl' timeLimit='200' >
  <Properties>
    <Property name='urlpath'>/echo-1/t3</Property>
  </Properties>
  <ResourceURL>jsc://setTargetUrl.js</ResourceURL>
</Javascript>

And the JS logic looks like:

// setTargetUrl.js
var schemeAndHostForLocalConnection = 'http://localhost:8998';
context.setVariable('target.url', schemeAndHostForLocalConnection + properties.urlpath);

It just so happens that local servers listen on http://localhost:8998 . This isn't documented but it's easy to see in Trace for any proxy that uses LocalTargetConnection. so I'm just embedding that into the JavaScript and that accomplishes your goal.

View solution in original post

3 REPLIES 3

Good question!

Unfortunately the Path element is not a Message Template, and you don't get curly-brace variable replacement there, as you have observed.

The way to do what you want is to set the target.url within the PreFlow of the Target Endpoint, in the same way you would do, if you were using an HTTPTargetConnection.

Like this:

<TargetEndpoint name="chain">
    <PreFlow name="PreFlow">
      <Request>
        <Step>
          <Name>JS-SetTargetUrl</Name>
        </Step>
      </Request>
        <Response/>
    </PreFlow>
    <PostFlow name="PostFlow">
        <Request/>
        <Response/>
    </PostFlow>
    <Flows/>
    <LocalTargetConnection>
        <Path>/required-but-not-used</Path>
    </LocalTargetConnection>
</TargetEndpoint>
 

And then in the JavaScript policy, do something like this:

<Javascript name='JS-SetTargetUrl' timeLimit='200' >
  <Properties>
    <Property name='urlpath'>/echo-1/t3</Property>
  </Properties>
  <ResourceURL>jsc://setTargetUrl.js</ResourceURL>
</Javascript>

And the JS logic looks like:

// setTargetUrl.js
var schemeAndHostForLocalConnection = 'http://localhost:8998';
context.setVariable('target.url', schemeAndHostForLocalConnection + properties.urlpath);

It just so happens that local servers listen on http://localhost:8998 . This isn't documented but it's easy to see in Trace for any proxy that uses LocalTargetConnection. so I'm just embedding that into the JavaScript and that accomplishes your goal.

I was just trying to avoid the JS. I don't think so I've any other option but to use JS to set target.url explicitly. As you mentioned typically message processors listen on port 8998 (localhost), is it the case with all environments and deployments (on-prem / cloud)?