In Apigee how do I route all content with a specific prefix to another service - apigee

I want to configure my proxy in the following fashion.
All requests by default get passed through to http://main.api.com
Any request that has has a URI path that start /admin gets routed to http://admin.api.com
<RouteRule name="mocker">
<Condition>(proxy.pathsuffix JavaRegex "^/admin/?.*$")</Condition>
<TargetEndpoint>MockTarget</TargetEndpoint>
</RouteRule>
<RouteRule name="default">
<TargetEndpoint>default</TargetEndpoint>
</RouteRule>
This partially works in that it does indeed route to a different host. However, it maintains the path; so requesting /admin/foo/bar gets redirected to http://admin.api.com/admin/foo/bar, but I wanted it to go to http://admin.api.com/foo/bar
How can I achieve that?

You will have to set target.copy.pathsuffix to false in the Target PreFlow. This will ensure that the path from Proxy is not copied to the Target.
You can easily do this by adding a Javascript policy on the Target Preflow with the code
context.setVariable("target.copy.pathsuffix", false);

Related

How to route requests to desired endpoint using Environment Variables in APIGEE

I've a situation where I need to route requests to desired endpoint based on Environment the request hits. for example QA - QA, Prod to Prod
I've configured a proxy and defined a default target host during initial config.
Then I'm using a javascript to decide target host based on the env the request comes in.
var env = context.getVariable('environment.name');
if(env=="prod") {
var host = 'https://prod.com';
}
if(env=="test") {
var host = 'https://qa.com';
}
I've used this JS file in target endpoint(default) preflow as a step.
I see that all requests are sent to the default host that I configured during initial process.
Am I missing something here please help.
Also I've seen about using Target Server Env config. I've configured the hosts but how do I reference/use it in my proxy.
I usually set the target endpoint (it is the same to host of yours) in Key Value Mapping of 'Environment Configuration' of Apigee.
And then assign it to variable (example assign it to variable name endpointUrl) in Key Value Maps Operation policy
Finally, use it in your Target Request Message like below.
<AssignVariable>
<Name>target.url</Name>
<Ref>endpointUrl</Ref>
</AssignVariable>
Adventage of this method is if your host changed, you just edit the value in Key Value Mapping not edit in your code and do not need to re-deploy your API.
However, I answer you from my work experience only.
Maybe you have try to go Apigee Community, you may found the solution that suits you.

Relative paths for assets in Play Framework

I'm running two Play 2.3.x applications behind nginx. In nginx, application A is configured to be accessed at "/". Application B is configured to be accessed at "/appB/".
I'm having some problems resolving assets for application B when using the built in routes/assets functionality (<script src="#routes.Assets.at("someScriptfile.js")") type="text/javascript"></script>. The problem here is that the URL will be absolute, for example /assets/file.png. This will result in that the proxy forwards the request to application A.
I want to use relatives path instead. That would allow me to run the application B both with and without the proxy. I want the asset url to be assets/file.png.
What are my options? I know I can solve it by using content rewrite in the proxy, but we would
Assets.at returns a Call, which is converted to a String implicitly in Scala. The String it produces is just the URL the reverse router resolves to. If all you want to do is remove the leading slash, you can drop the first character.
#routes.Assets.at("someScriptfile.js")").drop(1)
Or to be super sure that what's removed is only a leading slash, you could use stripPrefix.
#routes.Assets.at("someScriptfile.js")").stripPrefix("/")
Try something like this:
For your Application B use absolute URLs but prepend them with the "/appB/"
package misc;
import controllers.routes;
public class BAssets {
public static String at(String path) {
return "/appB" + routes.Assets.at(path).toString();
}
}
Then every time you want to serve some asset for application B, you will write it like this:
<script src="#misc.BAssets.at("javascripts/hello.js")"></script>
If you want, you can even include more logic in the method which calculates the path to the asset - for example based on if your application is in dev mode or in prod. Or even based on the proxy configuration (you can decide to change the path to appC tomorrow).
I just found a solution that works.
Edit application.conf and add the following: application.context="/appB/".
This will translate <script src="#routes.Assets.at("someScriptfile.js")" type="text/javascript"></script> to url /appB/assets/someScriptfile.js
I just created a PR to have relative paths added to Play. Looks like it will be released with 2.6.7
https://github.com/playframework/playframework/pull/7839

Creating a url in controller in asp.net mvc 4

I am trying to send activation mail to the currently registered user.In mail body,I need to send a link like http://example.com/account/activation?username=d&email=g.Now, for debugging on local machine, I manually write it as localhost:30995/account/activation?username=d&email=g. But, when my port number changes, I need to rewrite it.
I tried another question
on this website,but, compiler gives error like url.action doesnot exist.
Please give me fresh solution as I am confused with that solution.
Use a Url.Action overload that takes a protocol parameter to generate your URLs:
Url.Action("Activation", "Account", new { username = "d", email = "g" }, "http")
This generates an absolute URL rather than a relative one. The protocol can be either "http" or "https". So this will return http://localhost:XXXXX/account/activation?username=d&email=g on your local machine, and http://example.com/account/activation?username=d&email=g on production.
In short, this will stick whatever domain you're hosting your app on in front of your URL; you can then change your hostname/port number/domain name as many times as you want. Your links will always point to the host they originated from. That should solve the problem you're facing.
Try using IIS / IIS-Express instead of Casinni web server that comes with visual studio.
You could add bindings to have the right URL (with host entries of course).
This will avoid the port numbers in your links.

Grails - SSL and Spring security core

I would like to have my application running exclusively with SSL turned on.
I am using the Spring Security core plugin.
This is how I attempt to do it in Config.groovy:
grails.plugins.springsecurity.portMapper.httpPort = 8080
grails.plugins.springsecurity.portMapper.httpsPort = 8443
grails.plugins.springsecurity.secureChannel.definition = [ '/**' : 'REQUIRES_SECURE_CHANNEL']
I was expecting this to cause redirects every time I would try to access a Url using HTTP.
However, I am never redirected, and can navigate through both HTTP and HTTPS. I may add I am starting my application using grails run-app -https
Am I getting this all wrong ?
Any suggestion is most welcome.
Do you have a custom filterchain declared in your config?
you might need to add 'channelProcessingFilter' to your chain in that case
http://static.springsource.org/spring-security/site/docs/3.0.x/reference/security-filter-chain.html
You can also try using the forceHttps option
grails.plugins.springsecurity.auth.forceHttps = true
You don't have any wildcards, so the definition is literally matching the root URL (/), but nothing below it (/foo). What you want is:
grails.plugins.springsecurity.secureChannel.definition = [ '/**' : 'REQUIRES_SECURE_CHANNEL']
^^
(You can clearly see the wildcards in the documentation :-)
Finally, if your server is behind a load balancer or other firewall that hides the protocol, check that same page for instructions on checking the header.

HttpContext.Current.RewritePath - to another domain - is this possible?

Using ASP.Net is it possible to rewrite to a different domain using HttpContext.Current.RewritePath? Eg.
HttpContext.Current.RewritePath("http://www.otherdomain.com");
When I try this I get the following exception:
'http://www.otherdomain.com' is not a valid virtual path
HttpContext.Current.RewritePath is rewriting of the url on the server side.
It means that if you request the url /products you can tell to asp.net to rednder /Products.aspx by using:
if( HttpContext.Current.Request.RawUrl == "/products")
HttpContext.Current.RewritePath("/Products.aspx");
if you want to send the user to another domain you need to do:
HttpContext.Current.Response.Redirect("http://www.otherdomain.com/page");

Resources