Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

improvement: added cookie value replacement

  • Loading branch information...
commit b360611f0c770ce466d533341a3a407c7d3cf0d0 1 parent 46e402c
Robbie authored
View
21 app/assets/javascripts/transformer.coffee
@@ -10,6 +10,10 @@ class Transformer
if !(@transformerJson.headerSubstitutions?)
@transformerJson.headerSubstitutions = []
@transformerJson.headerSubstitutions = [{token : token, paramName: headerParamName}]
+ putCookieRule: (token, cookieName) ->
+ if !(@transformerJson.cookieSubstitutions?)
+ @transformerJson.cookieSubstitutions = []
+ @transformerJson.cookieSubstitutions = [{token : token, paramName: cookieName}]
getQueryParamRule: () ->
if @transformerJson.queryParamSubstitutions? && @transformerJson.queryParamSubstitutions.length == 1
return @transformerJson.queryParamSubstitutions[0]
@@ -20,6 +24,11 @@ class Transformer
return @transformerJson.headerSubstitutions[0]
else
return {}
+ getCookieRule: () ->
+ if @transformerJson.cookieSubstitutions? && @transformerJson.cookieSubstitutions.length == 1
+ return @transformerJson.cookieSubstitutions[0]
+ else
+ return {}
getJsonData: ()->
JSON.stringify(self.transformerJson)
@@ -28,10 +37,11 @@ class View
controller = null
constructor: (@model) ->
self = this
- $("#set").click((event)->controller.putQueryParamRule($("#token").val(), $("#queryParamName").val()))
$("#update").click((event)->controller.save())
$(".close-button").click((event)-> $("#success").hide();$("#error").hide())
+ $("#set").click((event)->controller.putQueryParamRule($("#token").val(), $("#queryParamName").val()))
$("#setHeader").click((event)->controller.putHeaderParamRule($("#headerToken").val(), $("#headerParamName").val()))
+ $("#setCookie").click((event)->controller.putCookieRule($("#cookieToken").val(), $("#cookieName").val()))
setController: (newController) ->
controller = newController
self.update()
@@ -50,6 +60,11 @@ class View
$("#headerRule").empty().text(headerRule.token.toString() + " -> " + headerRule.paramName.toString())
else
$("#headerRule").empty()
+ cookieRule = @model.getCookieRule()
+ if(cookieRule.token? && cookieRule.paramName?)
+ $("#cookieRule").empty().text(cookieRule.token.toString() + " -> " + cookieRule.paramName.toString())
+ else
+ $("#cookieRule").empty()
class Controller
@@ -64,6 +79,10 @@ class Controller
if token? && headerParamName? && token.length > 1 && headerParamName.length > 1
@model.putHeaderParamRule(token, headerParamName)
@view.update()
+ putCookieRule: (token, cookieName) ->
+ if token? && cookieName? && token.length > 1 && cookieName.length > 1
+ @model.putCookieRule(token, cookieName)
+ @view.update()
save: ()->
$.ajax({type:"PUT",contentType:"application/json", data:@model.getJsonData(),error:me.onError,success:me.onSuccess})
onError: ()->
View
16 app/controllers/Responders.scala
@@ -104,16 +104,26 @@ object Responders extends Controller{
}.getOrElse(body)
}
- def applyRulesToBody(queryRules: scala.Seq[SubstitutionRule],headerRules: scala.Seq[SubstitutionRule]): String = {
+ def doCookieSubstitution(): (String, SubstitutionRule) => String = {
+ (body: String, rule: SubstitutionRule) =>
+ request.cookies.get(rule.paramName).map{param=>
+ body.replaceAll(rule.token, param.value)
+ }.getOrElse(body)
+ }
+
+ def applyRulesToBody(queryRules: scala.Seq[SubstitutionRule],headerRules: scala.Seq[SubstitutionRule],cookieRules: scala.Seq[SubstitutionRule]): String = {
val querySubbed = queryRules.foldLeft[String](responder.body) {
doQueryParamSubstitution()
}
- headerRules.foldLeft[String](querySubbed) {
+ val headerSubbed = headerRules.foldLeft[String](querySubbed) {
doHeaderSubstitution()
}
+ cookieRules.foldLeft[String](headerSubbed) {
+ doCookieSubstitution()
+ }
}
(Actors.transformers ? GetTransformer(responder.name)).map{
- case Some(Transformer(queryParam,headerParam))=> applyRulesToBody(queryParam,headerParam)
+ case Some(Transformer(queryParam,headerParam,cookieRules))=> applyRulesToBody(queryParam,headerParam,cookieRules)
case _ => responder.body
}.map(body => Ok(body).withHeaders(responder.headers: _*)).asPromise
}
View
12 app/controllers/Transformers.scala
@@ -6,7 +6,7 @@ import play.api.libs.concurrent._
import play.api.mvc.{Result, Action, Controller}
import akka.util.Timeout
-case class Transformer(queryParamSubstitutions : Seq[SubstitutionRule],headerSubstitutions : Seq[SubstitutionRule])
+case class Transformer(queryParamSubstitutions : Seq[SubstitutionRule],headerSubstitutions : Seq[SubstitutionRule], cookieSubstitutions : Seq[SubstitutionRule])
case class SubstitutionRule(token:String, paramName:String)
object Transformers extends Controller{
@@ -32,13 +32,17 @@ object Transformers extends Controller{
implicit object TransformerFormat extends Format[Transformer] {
def reads(json: JsValue) = {
Transformer((json \ "queryParamSubstitutions").asOpt[List[SubstitutionRule]].getOrElse(List()),
- (json \ "headerSubstitutions").asOpt[List[SubstitutionRule]].getOrElse(List()))
+ (json \ "headerSubstitutions").asOpt[List[SubstitutionRule]].getOrElse(List()),
+ (json \ "cookieSubstitutions").asOpt[List[SubstitutionRule]].getOrElse(List())
+ )
}
def writes(transformer: Transformer) = {
JsObject(Seq("queryParamSubstitutions" -> JsArray(transformer.queryParamSubstitutions.map(q => Json.toJson(q))),
- "headerSubstitutions" -> JsArray(transformer.headerSubstitutions.map(q => Json.toJson(q)))
- ))
+ "headerSubstitutions" -> JsArray(transformer.headerSubstitutions.map(q => Json.toJson(q))),
+ "cookieSubstitutions" -> JsArray(transformer.cookieSubstitutions.map(q => Json.toJson(q)))
+ )
+ )
}
}
View
13 app/views/transformer.scala.html
@@ -8,18 +8,27 @@
<form class="form-inline">
<input id="token" type="text" value placeholder="token name">
<input id="queryParamName" type="text" value placeholder="query parameter name">
- <a class="btn" id="set">Set Rule</a>
+ <a class="btn" id="set">Put Rule</a>
</form>
<form class="form-inline">
<input id="headerToken" type="text" value placeholder="token name">
<input id="headerParamName" type="text" value placeholder="header name">
- <a class="btn" id="setHeader">Set Rule</a>
+ <a class="btn" id="setHeader">Put Rule</a>
</form>
+
+ <form class="form-inline">
+ <input id="cookieToken" type="text" value placeholder="token name">
+ <input id="cookieName" type="text" value placeholder="cookie name">
+ <a class="btn" id="setCookie">Put Rule</a>
+ </form>
+
<h3>Query Parameter Substitution Rule</h3>
<p id="rule"></p>
<h3>Header Substitution Rule</h3>
<p id="headerRule"></p>
+ <h3>Cookie Substitution Rule</h3>
+ <p id="cookieRule"></p>
<a class="btn btn-primary" id="update">Save/Update</a>
<div class="alert alert-success close-button" id="success" style="display:none;">Save/Update Successful
Please sign in to comment.
Something went wrong with that request. Please try again.