Commits

Sebastian Sdorra committed 67a37e6

added userAgentStartsWith method to HttpUtil

  • Participants
  • Parent commits 1b74a4c
  • Branches 1.x

Comments (0)

Files changed (2)

File scm-core/src/main/java/sonia/scm/util/HttpUtil.java

 import java.net.URLDecoder;
 import java.net.URLEncoder;
 
+import java.util.Locale;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
    */
   public static final String HEADER_SCM_CLIENT = "X-SCM-Client";
 
+  /** Field description */
+  public static final String HEADER_USERAGENT = "User-Agent";
+
   /** authentication header */
   public static final String HEADER_WWW_AUTHENTICATE = "WWW-Authenticate";
 
       STATUS_UNAUTHORIZED_MESSAGE);
   }
 
+  /**
+   * Returns true if the User-Agent header of the current request starts with
+   * the given string.
+   *
+   *
+   * @param request http request
+   * @param userAgent string to test against the header
+   *
+   * @return true if the header starts with the given string
+   *
+   * @since 1.37
+   */
+  public static boolean userAgentStartsWith(HttpServletRequest request,
+    String userAgent)
+  {
+    return Strings.nullToEmpty(request.getHeader(HEADER_USERAGENT)).toLowerCase(
+      Locale.ENGLISH).startsWith(
+      Strings.nullToEmpty(userAgent).toLowerCase(Locale.ENGLISH));
+  }
+
   //~--- get methods ----------------------------------------------------------
 
   /**

File scm-core/src/test/java/sonia/scm/util/HttpUtilTest.java

 
   }
 
+  /**
+   * Method description
+   *
+   */
+  @Test
+  public void userAgentStartsWithTest()
+  {
+    HttpServletRequest request = mock(HttpServletRequest.class);
+
+    when(request.getHeader(HttpUtil.HEADER_USERAGENT)).thenReturn(
+      "git/1.7.10.5997.gaa4aa");
+    assertTrue(HttpUtil.userAgentStartsWith(request, "git/"));
+    assertTrue(HttpUtil.userAgentStartsWith(request, "GIT/"));
+    assertFalse(HttpUtil.userAgentStartsWith(request, "git/a"));
+    assertFalse(HttpUtil.userAgentStartsWith(request, "sobbo/"));
+  }
+
   //~--- get methods ----------------------------------------------------------
 
   /**