From 4dc093fceec9a06036635ff0dd6ec8f36230b1b7 Mon Sep 17 00:00:00 2001 From: Winston Li Date: Thu, 4 Dec 2014 21:38:41 +0000 Subject: [PATCH] Made a better postback key. --- .../application/SnapshotPushPostbackHandler.java | 8 ++++++-- .../writelatex/api/request/push/PostbackManager.java | 10 +++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/SnapshotPushPostbackHandler.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/SnapshotPushPostbackHandler.java index e903c58e43..5e598f29f1 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/SnapshotPushPostbackHandler.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/SnapshotPushPostbackHandler.java @@ -26,9 +26,13 @@ public class SnapshotPushPostbackHandler extends AbstractHandler { public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { if (request.getMethod().equals("POST") && request.getPathInfo().endsWith("postback")) { String contents = getContentsOfReader(request.getReader()); - String projectName = request.getRequestURI().split("/")[1]; + String[] parts = request.getRequestURI().split("/"); + if (parts.length < 4) { + throw new ServletException(); + } + String projectName = parts[1]; + String postbackKey = parts[2]; System.out.println("Postback received for project: " + projectName); - String postbackKey = "postback"; SnapshotPushPostbackContents postbackContents = new SnapshotPushPostbackContents(writeLatexDataSource, projectName, postbackKey, contents); try { postbackContents.processPostback(); diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/push/PostbackManager.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/push/PostbackManager.java index 33ef91454f..39bb425c43 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/push/PostbackManager.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/push/PostbackManager.java @@ -2,6 +2,8 @@ package uk.ac.ic.wlgitbridge.writelatex.api.request.push; import uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception.SnapshotPostException; +import java.math.BigInteger; +import java.security.SecureRandom; import java.util.HashMap; import java.util.Map; @@ -10,9 +12,11 @@ import java.util.Map; */ public class PostbackManager { + private final SecureRandom random; private final Map postbackContentsTable; public PostbackManager() { + random = new SecureRandom(); postbackContentsTable = new HashMap(); } @@ -39,10 +43,14 @@ public class PostbackManager { } public String makeKeyForProject(String projectName) { - String key = "postback"; + String key = System.currentTimeMillis() + randomString(); PostbackContents contents = new PostbackContents(key); postbackContentsTable.put(projectName, contents); return key; } + private String randomString() { + return new BigInteger(130, random).toString(32); + } + }