final String actualLang = Languages.forRequestParameter(lang);
final String sha1 = Hash.of(body, actualLang);
Logger.debug("Got some code, sha1 is " + sha1);
final Paste paste = Paste.findByHash(sha1);
if (paste != null)
{
Logger.info("Found id " + paste.key + " for code with sha1 " + sha1);
return ok(pastebinMacro.render(paste.key, body));
}
Logger.debug("Need to generate an id for code with sha1 " + sha1);
return Api.post(body, actualLang, new Api.Call<Result>()
{
public Result call(String url, ImmutableMap<String, String> parameters)
{
return async(WsUtils.post(url, parameters)
.map(new F.Function<WS.Response, Result>()
{
public Result apply(WS.Response response) throws Throwable
{
final String responseBody = response.getBody();
if (PastebinUrl.isUrl(responseBody))
{
final String pasteId = PastebinUrl.id(responseBody);
final Paste p = new Paste();
p.key = pasteId;
p.hash = sha1;
Paste.create(p);
Logger.info("Created id " + pasteId + " for code with sha1 " + sha1);