Created
April 2, 2014 09:25
-
-
Save nrdsrfr/9930809 to your computer and use it in GitHub Desktop.
Hacky Zuul filter which validates the request endpoint, sets host depending on API version and shows how to override static HttpServletRequest paths using an HttpServletRequestWrapper (routing really should be done in separate route filter)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package scripts.preProcess | |
import com.netflix.zuul.ZuulFilter | |
import com.netflix.zuul.context.Debug | |
import com.netflix.zuul.context.RequestContext | |
import org.apache.commons.lang.StringUtils | |
import org.apache.http.MethodNotSupportedException | |
import org.codehaus.groovy.util.StringUtil | |
import javax.servlet.http.HttpServletRequest | |
import javax.servlet.http.HttpServletRequestWrapper | |
import java.util.regex.Pattern | |
/** | |
* Created by jesse on 20/03/2014. | |
*/ | |
class SpineApiFilter extends ZuulFilter{ | |
def apis = new HashSet(['bonus','player']) | |
private String api | |
@Override | |
String filterType() { | |
return 'pre' | |
} | |
@Override | |
int filterOrder() { | |
return 1 | |
} | |
@Override | |
boolean shouldFilter() { | |
return true | |
} | |
@Override | |
Object run() { | |
try{ | |
String[] uriBits = RequestContext.getCurrentContext().getRequest().getRequestURI().split("/") | |
api = uriBits.getAt(1) | |
Debug.addRequestDebug("VALIDATING:: " + uriBits.toString()) | |
}catch (IndexOutOfBoundsException e){ | |
Debug.addRequestDebug("SPINE API:: No Endpoint Specified") | |
return null | |
} | |
if(apis.contains(api)){ | |
Debug.addRequestDebug("SPINE API:: Valid API Requested") | |
}else{ | |
Debug.addRequestDebug("SPINE API:: Invalid API Requested") | |
RequestContext.getCurrentContext().setRouteHost(new URL("http://www.google.co.uk/404")) | |
return null | |
} | |
Debug.addRequestDebug("API REQUEST:: URI: " + RequestContext.getCurrentContext().getRequest().getRequestURI()) | |
Debug.addRequestDebug("API REQUEST:: Path: " + RequestContext.getCurrentContext().getRequest().getPathInfo()) | |
try{ | |
"$api"(RequestContext.getCurrentContext().getRequest()) | |
}catch (Exception e){ | |
Debug.addRequestDebug("couldn't find dynamic routing method for " + api) | |
} | |
return null | |
} | |
void bonus(HttpServletRequest req) { | |
def path = req.getPathInfo().split("/") | |
Debug.addRequestDebug("BONUS API REQUEST:: Path - " + path.toString()) | |
if(path[1]=="v3"){ | |
RequestContext.getCurrentContext().setRouteHost(new URL("https://a1-qa01-btv3.cloudapp.net")) | |
Debug.addRequestDebug("BONUS API REQUEST:: API Version 3 - Changing Origin Route") | |
}else if(path[1]=="v2"){ | |
RequestContext.getCurrentContext().setRouteHost(new URL("https://a1-qa01-btv2.cloudapp.net")) | |
Debug.addRequestDebug("BONUS API REQUEST:: API Version 2 - Changing Origin Route") | |
}else{ | |
Debug.addRequestDebug("BONUS API REQUEST:: API Version Unknown") | |
} | |
HttpServletRequestWrapper wrappedRequest = new HttpServletRequestWrapper(req){ | |
private HttpServletRequest request = req; | |
public String getPathInfo(){ | |
return new String( | |
request.getPathInfo().replaceFirst(new Pattern(/\/bonus\/v\d\//,0),'/help') | |
) | |
} | |
public String getRequestURI(){ | |
return new String( | |
request.getRequestURI().replaceFirst(new Pattern(/\/bonus\/v\d\//,0),'/help') | |
) | |
} | |
public StringBuffer getRequestURL(){ | |
return new StringBuffer( | |
request.getRequestURL().toString().replaceFirst(new Pattern(/\/bonus\/v\d\//,0),'/help') | |
) | |
} | |
} | |
RequestContext.getCurrentContext().setRequest(wrappedRequest) | |
} | |
void player(HttpServletRequest req){ | |
RequestContext.getCurrentContext().setRouteHost(new URL("https://qa01-sgp.bedegaming.net:18201")) | |
HttpServletRequestWrapper wrappedRequest = new HttpServletRequestWrapper(req){ | |
private HttpServletRequest request = req; | |
public String getPathInfo(){ | |
return new String( | |
request.getPathInfo().replaceFirst('/player','/Player/help') | |
) | |
} | |
public String getRequestURI(){ | |
return new String( | |
request.getRequestURI().replaceFirst('/player','/Player/help') | |
) | |
} | |
public StringBuffer getRequestURL(){ | |
return new StringBuffer( | |
request.getRequestURL().toString().replaceFirst('/player','/Player/help') | |
) | |
} | |
} | |
RequestContext.getCurrentContext().setRequest(wrappedRequest) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment