Rails compatibility + authenticityToken + working with JSON

64 views
Skip to first unread message

Dominik Dorn

unread,
Feb 21, 2011, 6:27:00 PM2/21/11
to play-fr...@googlegroups.com
Hi,

I'm integrating an app that was build for a rails website into our
Play! Application.
Almost everything works smooth so far, however two things came to my attention:

#1:
Rails also has the principle of our AuthenticityToken, however the
param is called
"authenticity_token" instead of our "authenticityToken".

I wanted to suggest, that we change the param name in Play to match the one
already used in Rails because
- they are already more established and therefor;
- there are already a lot of apps able to send authenticityTokens,
but the rails way;
- there aren't that many places needed to change;
and most importantly
- it would remove some integration issues for me and probably some
other developers in the future


and here my issue #2:
Rails seems to be able to do direct parameter binding from a json
string, so imagine this Request:


Host: hostname.com
User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13)
Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/2.0.0.1

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

Accept-Language: de-at,de;q=0.8,en-us;q=0.5,en;q=0.3

Accept-Encoding: gzip,deflate

Accept-Charset: ISO-8859-2,utf-8;q=0.7,*;q=0.7

Keep-Alive: 115

Connection: keep-alive

Content-Type: application/json; charset=utf-8

'Accept'-Type: application/json; charset=utf-8

Referer: http://hostname.com/public/viewer/default/41D1D6E5724E862CC746B4084DFD8A6F.cache.html

Content-Length: 306

Cookie: km_ai=j0pUTV6AHVjl6Ilz-_Lc8fz-kqY; km_lv=x; km_uq=; km_uq=;
PLAY_FLASH=; PLAY_ERRORS=;
PLAY_SESSION=4af0fe1ce37afb544638b20fefbf087e979eecd4-%00___ID%3A62b79d75-37b4-44b0-9001-f362fca4d5ab%00

Pragma: no-cache

Cache-Control: no-cache

{ "authenticity_token":
"007c6d694ebd17ed938342c5adff74467884e81b","metrics":
{"documentId":"2a219788-0daf-449e-8ab2-ff2ee5a46d9a","sessionId":"2a219788-0daf-449e-8ab2-ff2ee5a46d9a714415","metrics":[{"id":40,"action":5,"time":2147507275,"attributes":{"left":"-65","top":"277","width":"1354","height":"690"}}]}}


In the original rails app, it was possible to access this through:

def track_metrics
metrics = params['metrics']
metrics = metrics['metrics']
metrics.each do |metric|
# do stuff
end

so the authenticity_token gets automatically bound and the "metrics"
parameter is available as array/list.
While I found out that its possible to access the direct request body
through request.body, I'm wondering
if its possible to specify the binding somehow?

My current approach is this one, but it isn't that easy as it could be:

public static void trackMetrics(
String body
)
{
JsonObject jsonData = null;
if(body != null)
{
jsonData = new JsonParser().parse(body).getAsJsonObject();
if(jsonData.get("authenticity_token") != null)
{
Scope.Params.current().put("authenticityToken",
jsonData.get("authenticity_token").getAsString());
}
}
checkAuthenticity();

JsonArray metrics =
jsonData.get("metrics").getAsJsonObject().get("metrics").getAsJsonArray();

for (JsonElement metric : metrics) {
System.out.println("metric = " + metric);
}
// other stuff
}

I'm thankful for any suggestions

Thanks,
Dominik

Guillaume Bort

unread,
Feb 26, 2011, 3:19:56 PM2/26/11
to play-fr...@googlegroups.com
Yes why not. I think we could support both for compatibility reasons.
Have you reported the ticket?

> --
> You received this message because you are subscribed to the Google Groups "play-framework" group.
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.
>
>

--
Guillaume Bort, http://guillaume.bort.fr

For anything work-related, use g...@zenexity.fr; for everything else,
write guillau...@gmail.com

Dominik Dorn

unread,
Mar 3, 2011, 10:42:12 AM3/3/11
to play-fr...@googlegroups.com, Guillaume Bort
Reply all
Reply to author
Forward
0 new messages