Merge branch 'stable-2.14' into stable-2.15
* stable-2.14:
Reduce cyclomatic complexity while fixing other Sonar issues
Change-Id: I0785b57f3c5b24fa98ff00c8d0dc17abd4420070
diff --git a/src/main/java/com/ericsson/gerrit/plugins/goimport/GoImportFilter.java b/src/main/java/com/ericsson/gerrit/plugins/goimport/GoImportFilter.java
index 5911074..e728713 100644
--- a/src/main/java/com/ericsson/gerrit/plugins/goimport/GoImportFilter.java
+++ b/src/main/java/com/ericsson/gerrit/plugins/goimport/GoImportFilter.java
@@ -14,6 +14,7 @@
package com.ericsson.gerrit.plugins.goimport;
+import com.google.common.base.Strings;
import com.google.gerrit.httpd.AllRequestFilter;
import com.google.gerrit.httpd.HtmlDomUtil;
import com.google.gerrit.reviewdb.client.Project;
@@ -27,6 +28,8 @@
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
@@ -88,9 +91,7 @@
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse rsp = (HttpServletResponse) response;
String servletPath = req.getServletPath();
- String goGet = req.getParameter("go-get");
- if ("1".equals(goGet)) {
- String projectName = getProjectName(servletPath);
+ if ("1".equals(req.getParameter("go-get"))) {
// Because Gerrit allows for arbitrary-depth project names
// (that is, both "a" and "a/b/c" are both legal), we are going
// to find the most specific such project that matches the path.
@@ -103,34 +104,19 @@
// 3. If the requested path is "a/c", then project "a" would be chosen.
// 4. If the requested path is "a/b/c/d", then project "a/b" would be chosen.
// 5. If the requested path is "x/y/z", then this will fail with a 404 error.
- String[] pathParts = projectName.split("/");
-
- byte[] tosend = PAGE_404.getBytes();
+ String existent = getLongestMatch(getProjectName(servletPath));
+ byte[] toSend = PAGE_404.getBytes();
rsp.setStatus(404);
- // Start with the longest-length project name first.
- for (int length = pathParts.length; length > 0; length--) {
- // Create a new project name of the specified length; each time that we
- // go through this loop, the project name will become shorter and shorter.
- projectName = "";
- for (int i = 0; i < length; i++) {
- if (i > 0) {
- projectName += "/";
- }
- projectName += pathParts[i];
- }
-
- if (projectExists(projectName)) {
- tosend = PAGE_200.replace("${content}", getContent(projectName)).getBytes();
- rsp.setStatus(200);
- break;
- }
+ if (!Strings.isNullOrEmpty(existent)) {
+ toSend = PAGE_200.replace("${content}", getContent(existent)).getBytes();
+ rsp.setStatus(200);
}
CacheHeaders.setNotCacheable(rsp);
rsp.setContentType("text/html");
rsp.setCharacterEncoding(HtmlDomUtil.ENC.name());
- rsp.setContentLength(tosend.length);
+ rsp.setContentLength(toSend.length);
try (OutputStream out = rsp.getOutputStream()) {
- out.write(tosend);
+ out.write(toSend);
}
} else {
chain.doFilter(request, response);
@@ -140,6 +126,21 @@
}
}
+ private String getLongestMatch(String projectName) {
+ Path projectPath = Paths.get(projectName);
+ while (projectPath.getNameCount() >= 1) {
+ String asString = projectPath.toString();
+ if (projectExists(asString)) {
+ return asString;
+ }
+ projectPath = projectPath.getParent();
+ if (projectPath == null) {
+ break;
+ }
+ }
+ return null;
+ }
+
private String getProjectName(String servletPath) {
return servletPath.replaceFirst("/", "");
}