Move temporary cookie-based auth code into separate function

Change-Id: I2e49dcd10e3edfaa885e5f34d104c98faf7e5170
diff --git a/plugin/src/main/java/de/ids_mannheim/korap/plkexport/IdsExportService.java b/plugin/src/main/java/de/ids_mannheim/korap/plkexport/IdsExportService.java
index b975d1b..2db47cf 100644
--- a/plugin/src/main/java/de/ids_mannheim/korap/plkexport/IdsExportService.java
+++ b/plugin/src/main/java/de/ids_mannheim/korap/plkexport/IdsExportService.java
@@ -63,6 +63,7 @@
  * - Get variable cutoff from URL
  * - Right now, the web service returns one page (cutoff=1) or
  *   all pages.
+ * - Handle timeout results (with minimum total results).
  */
 
 @Path("/")
@@ -197,40 +198,7 @@
                 xff = req.getRemoteAddr();
             };
 
-            // This is a temporary solution using session riding - only
-            // valid for the time being
-            Cookie[] cookies = req.getCookies();
-            String cookiePath = properties.getProperty("cookie.path", "");
-
-            // Iterate through all cookies for a Kalamar session
-            for (int i = 0; i < cookies.length; i++) {
-                
-                // Check the valid path
-                if (cookiePath != "" && cookies[i].getPath() != cookiePath) {
-                    continue;
-                };
-
-                // Ignore irrelevant cookies
-                if (!cookies[i].getName().matches("^kalamar(-.+?)?$")) {
-                    continue;
-                };
-
-                // Get the value
-                String b64 = cookies[i].getValue();
-                String[] b64Parts = b64.split("--", 2);
-                if (b64Parts.length == 2) {
-                    // Read the payload
-                    String payload = new String(b64Dec.decode(b64Parts[0]));
-                    if (payload != "") {
-                        Matcher m = authrep.matcher(payload);
-                        if (m.find()) {
-                            auth = m.group(1);
-                            break;
-                        };
-                    };
-                };
-                continue;
-            };
+            auth = authFromCookie(req);
         };
     
         String resp;
@@ -247,7 +215,7 @@
                 );
         }
 
-        // set filename to query
+        // set filename based on query (if not already set)
         if (fname == null) {
             fname = q;
         }
@@ -366,7 +334,9 @@
     
 
     // Decorate request with auth headers
-    private Invocation.Builder authBuilder (Invocation.Builder reqBuilder, String xff, String auth) {
+    private Invocation.Builder authBuilder (Invocation.Builder reqBuilder,
+                                            String xff,
+                                            String auth) {
         if (xff != "") {
             reqBuilder = reqBuilder.header("X-Forwarded-For", xff);
         };
@@ -378,12 +348,52 @@
     };
 
 
+    // Get authorization token from cookie
+    private String authFromCookie (HttpServletRequest r) {
+
+        // This is a temporary solution using session riding - only
+        // valid for the time being
+        Cookie[] cookies = r.getCookies();
+        String cookiePath = properties.getProperty("cookie.path", "");
+
+        // Iterate through all cookies for a Kalamar session
+        for (int i = 0; i < cookies.length; i++) {
+                
+            // Check the valid path
+            if (cookiePath != "" && cookies[i].getPath() != cookiePath) {
+                continue;
+            };
+
+            // Ignore irrelevant cookies
+            if (!cookies[i].getName().matches("^kalamar(-.+?)?$")) {
+                continue;
+            };
+
+            // Get the value
+            String b64 = cookies[i].getValue();
+            String[] b64Parts = b64.split("--", 2);
+            if (b64Parts.length == 2) {
+                // Read the payload
+                String payload = new String(b64Dec.decode(b64Parts[0]));
+                if (payload != "") {
+                    Matcher m = authrep.matcher(payload);
+                    if (m.find()) {
+                        return m.group(1);
+                    };
+                };
+            };
+        };
+
+        return "";
+    };
+
+    
     /*
      * Response with form template.
      */
     private Response responseForm () {
         return responseForm(null, null);
-    }
+    };
 
 
     /*
@@ -429,7 +439,7 @@
                 .ok(new String("Template not found"))
                 .status(Status.INTERNAL_SERVER_ERROR)
                 .build();
-        }
+        };
 
         ResponseBuilder resp = Response.ok(out.toString(), "text/html");