Replacing a URL in my output

Not applicable

HI,

I need help finding the right way to replace a URL that I do not want to pass in my output.

I would like to replace the <link> value (http://feed.google.com) with (http://feed.placeholder.com) how do i do this in a policy? or should i do this with a script?

<?xml version="1.0" encoding="UTF-8"?> <rss version="2.0">

<channel>

<title>New E...</title> <link>http://feed.google.com</link> <description>Syndicated Feed of New...</description>

Solved Solved
0 6 2,317
1 ACCEPTED SOLUTION

Not applicable

Thanks guys!

Here's what I ended up going with. Hope this helps someone else here!

var env = context.getVariable('environment.name'); 

if (env == "production") {

	targetUrl = "http://production.apigee.net/"; 
} 
else if (env == "staging") {   

	targetUrl = "http://staging.apigee.net/"; 
} 

else if (env == "development") {
	targetUrl = "http://development.apigee.net/"; 
} 
str = targetUrl + "syndication/"; 
var rc = context.getVariable("response.content");
var newstr = rc.replace("http://feed.test.com/folder", str);
context.setVariable("response.content", newstr);

View solution in original post

6 REPLIES 6

script is easy - a regex to replace url in your js policy

Thanks Mukundha!

Does anyone have any script examples of a simple string find and replace or of the regex example Mukundha suggested?

I am new to this and could use the help to make short work if it.

Thanks everyone!

am not a regex expert - but i had a sample like this -

var re = /http\:\/\/54\.144\.216\.0\:9000\/temp/g;
str = 'http://the-replace-url';
var rc = context.getVariable("response.content");
var newstr = rc.replace(re, str);
context.setVariable("response.content", newstr);

@clatimer1, @Carlos Eberhardt - do you have any other samples?

I think script is the way to go. Allows you great flexibility and performs well. For simple static types of replacements I think Mukundha's answer is just fine. I often run into the need to do something more dynamic, so I'll try to parameterize the replacement a bit more and make use of a URL library like URI.js to make things less error-prone. For example, here's a script I've used to make sure that any URL referencing the target is replaced with the proxy endpoint. This example only tries to rewrite target basepath + optional uuid. You could make this more or less generic as needed. I'm setting variables earlier in the flow to ensure my replacement URLs match precisely how the caller hit the endpoint. The URI library is probably overkill in this simple example but it makes it easier if I later need to go in and start mucking about with paths in interesting ways. Also this was sort of quick and dirty, so some verification still needs to be done to ensure all URLs would be handled correctly. 😉

// Used to rewrite URLs in the response to route through same proxy.
// URLRewrite policy.

var pscheme = context.getVariable('pscheme')
var phost = context.getVariable('phost')
var pbase = context.getVariable('pbase')
var tscheme = context.getVariable('target.scheme')
var thost = context.getVariable('target.host')
var tbase = context.getVariable('target.basepath')
var uuid = context.getVariable("UUID")
if (uuid) {
    pbase += '/' + uuid
}

var p = new URI({
    protocol: pscheme,
    hostname: phost,
    path: pbase
})

var t = new URI({
    protocol: tscheme,
    hostname: thost,
    path: tbase
})

var r = new RegExp(t.href(), "ig")

response.content = response.content.replace(r, p.href())

The policy includes the URI.js library like so:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Javascript async="false" continueOnError="false" enabled="true" timeLimit="200" name="URLRewrite">
    <DisplayName>URLRewrite</DisplayName>
    <Properties/>
    <IncludeURL>jsc://URI.js</IncludeURL>
    <ResourceURL>jsc://URLRewrite.js</ResourceURL>
</Javascript>

URI.js can be found here: http://medialize.github.io/URI.js/

Not applicable

Thanks guys!

Here's what I ended up going with. Hope this helps someone else here!

var env = context.getVariable('environment.name'); 

if (env == "production") {

	targetUrl = "http://production.apigee.net/"; 
} 
else if (env == "staging") {   

	targetUrl = "http://staging.apigee.net/"; 
} 

else if (env == "development") {
	targetUrl = "http://development.apigee.net/"; 
} 
str = targetUrl + "syndication/"; 
var rc = context.getVariable("response.content");
var newstr = rc.replace("http://feed.test.com/folder", str);
context.setVariable("response.content", newstr);

Thanks for sharing @jtolerico !