]> source.dussan.org Git - jgit.git/commitdiff
NetscapeCookieFile: Make hash static and group overloaded write 21/143821/4
authorIvan Frade <ifrade@google.com>
Wed, 12 Jun 2019 08:23:09 +0000 (10:23 +0200)
committerMatthias Sohn <matthias.sohn@sap.com>
Sat, 15 Jun 2019 19:55:40 +0000 (21:55 +0200)
Issues reported by downstream analyzers.

The "hash" method can be static.

It is a good practice to group overloaded methods. Move the write(URL)
method with the other writes.

Change-Id: Ia42c0d7081333edcb77e58d5e627929d29672490
Signed-off-by: Ivan Frade <ifrade@google.com>
org.eclipse.jgit/src/org/eclipse/jgit/internal/transport/http/NetscapeCookieFile.java

index c8db52dd5211dd21bcc9918de770522f95353243..e16adb9bfbbce756421ed3c78625720af0c88132 100644 (file)
@@ -277,64 +277,6 @@ public final class NetscapeCookieFile {
                return cookie;
        }
 
-       /**
-        * Writes all the cookies being maintained in the set being returned by
-        * {@link #getCookies(boolean)} to the underlying file.
-        * <p>
-        * Session-cookies will not be persisted.
-        *
-        * @param url
-        *            url for which to write the cookies (important to derive
-        *            default values for non-explicitly set attributes)
-        * @throws IOException
-        *             if the underlying cookie file could not be read or written or
-        *             a problem with the lock file
-        * @throws InterruptedException
-        *             if the thread is interrupted while waiting for the lock
-        */
-       public void write(URL url) throws IOException, InterruptedException {
-               try {
-                       byte[] cookieFileContent = getFileContentIfModified();
-                       if (cookieFileContent != null) {
-                               LOG.debug("Reading the underlying cookie file '{}' " //$NON-NLS-1$
-                                               + "as it has been modified since " //$NON-NLS-1$
-                                               + "the last access", //$NON-NLS-1$
-                                               path);
-                               // reread new changes if necessary
-                               Set<HttpCookie> cookiesFromFile = NetscapeCookieFile
-                                               .parseCookieFile(cookieFileContent, creationDate);
-                               this.cookies = mergeCookies(cookiesFromFile, cookies);
-                       }
-               } catch (FileNotFoundException e) {
-                       // ignore if file previously did not exist yet!
-               }
-
-               ByteArrayOutputStream output = new ByteArrayOutputStream();
-               try (Writer writer = new OutputStreamWriter(output,
-                               StandardCharsets.US_ASCII)) {
-                       write(writer, cookies, url, creationDate);
-               }
-               LockFile lockFile = new LockFile(path.toFile());
-               for (int retryCount = 0; retryCount < LOCK_ACQUIRE_MAX_RETRY_COUNT; retryCount++) {
-                       if (lockFile.lock()) {
-                               try {
-                                       lockFile.setNeedSnapshot(true);
-                                       lockFile.write(output.toByteArray());
-                                       if (!lockFile.commit()) {
-                                               throw new IOException(MessageFormat.format(
-                                                               JGitText.get().cannotCommitWriteTo, path));
-                                       }
-                               } finally {
-                                       lockFile.unlock();
-                               }
-                               return;
-                       }
-                       Thread.sleep(LOCK_ACQUIRE_RETRY_SLEEP);
-               }
-               throw new IOException(
-                               MessageFormat.format(JGitText.get().cannotLock, lockFile));
-       }
-
        /**
         * Read the underying file and return its content but only in case it has
         * been modified since the last access.
@@ -395,10 +337,68 @@ public final class NetscapeCookieFile {
 
        }
 
-       private byte[] hash(final byte[] in) {
+       private static byte[] hash(final byte[] in) {
                return Constants.newMessageDigest().digest(in);
        }
 
+       /**
+        * Writes all the cookies being maintained in the set being returned by
+        * {@link #getCookies(boolean)} to the underlying file.
+        * <p>
+        * Session-cookies will not be persisted.
+        *
+        * @param url
+        *            url for which to write the cookies (important to derive
+        *            default values for non-explicitly set attributes)
+        * @throws IOException
+        *             if the underlying cookie file could not be read or written or
+        *             a problem with the lock file
+        * @throws InterruptedException
+        *             if the thread is interrupted while waiting for the lock
+        */
+       public void write(URL url) throws IOException, InterruptedException {
+               try {
+                       byte[] cookieFileContent = getFileContentIfModified();
+                       if (cookieFileContent != null) {
+                               LOG.debug("Reading the underlying cookie file '{}' " //$NON-NLS-1$
+                                               + "as it has been modified since " //$NON-NLS-1$
+                                               + "the last access", //$NON-NLS-1$
+                                               path);
+                               // reread new changes if necessary
+                               Set<HttpCookie> cookiesFromFile = NetscapeCookieFile
+                                               .parseCookieFile(cookieFileContent, creationDate);
+                               this.cookies = mergeCookies(cookiesFromFile, cookies);
+                       }
+               } catch (FileNotFoundException e) {
+                       // ignore if file previously did not exist yet!
+               }
+
+               ByteArrayOutputStream output = new ByteArrayOutputStream();
+               try (Writer writer = new OutputStreamWriter(output,
+                               StandardCharsets.US_ASCII)) {
+                       write(writer, cookies, url, creationDate);
+               }
+               LockFile lockFile = new LockFile(path.toFile());
+               for (int retryCount = 0; retryCount < LOCK_ACQUIRE_MAX_RETRY_COUNT; retryCount++) {
+                       if (lockFile.lock()) {
+                               try {
+                                       lockFile.setNeedSnapshot(true);
+                                       lockFile.write(output.toByteArray());
+                                       if (!lockFile.commit()) {
+                                               throw new IOException(MessageFormat.format(
+                                                               JGitText.get().cannotCommitWriteTo, path));
+                                       }
+                               } finally {
+                                       lockFile.unlock();
+                               }
+                               return;
+                       }
+                       Thread.sleep(LOCK_ACQUIRE_RETRY_SLEEP);
+               }
+               throw new IOException(
+                               MessageFormat.format(JGitText.get().cannotLock, lockFile));
+       }
+
        /**
         * Writes the given cookies to the file in the Netscape Cookie File Format
         * (also used by curl).