mirror of
				https://github.com/scm-manager/scm-manager.git
				synced 2025-10-31 10:35:56 +01:00 
			
		
		
		
	Reduce code smells (#2089)
Reduce code smells found by deepsource.io. We focused on the low-hanging fruits and not breaking any api.
This commit is contained in:
		| @@ -973,36 +973,6 @@ public class SCMSvnDiffGenerator implements ISvnDiffGenerator { | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   private String getGitDiffLabel1(SvnDiffCallback.OperationKind operationKind, String path1, String path2, String copyFromPath, String revision) { | ||||
|     if (operationKind == SvnDiffCallback.OperationKind.Deleted) { | ||||
|       return getLabel("a/" + path1, revision); | ||||
|     } else if (operationKind == SvnDiffCallback.OperationKind.Copied) { | ||||
|       return getLabel("a/" + copyFromPath, revision); | ||||
|     } else if (operationKind == SvnDiffCallback.OperationKind.Added) { | ||||
|       return getLabel("/dev/null", revision); | ||||
|     } else if (operationKind == SvnDiffCallback.OperationKind.Modified) { | ||||
|       return getLabel("a/" + path1, revision); | ||||
|     } else if (operationKind == SvnDiffCallback.OperationKind.Moved) { | ||||
|       return getLabel("a/" + copyFromPath, revision); | ||||
|     } | ||||
|     throw new IllegalArgumentException("Unsupported operation: " + operationKind); | ||||
|   } | ||||
|  | ||||
|   private String getGitDiffLabel2(SvnDiffCallback.OperationKind operationKind, String path1, String path2, String copyFromPath, String revision) { | ||||
|     if (operationKind == SvnDiffCallback.OperationKind.Deleted) { | ||||
|       return getLabel("/dev/null", revision); | ||||
|     } else if (operationKind == SvnDiffCallback.OperationKind.Copied) { | ||||
|       return getLabel("b/" + path2, revision); | ||||
|     } else if (operationKind == SvnDiffCallback.OperationKind.Added) { | ||||
|       return getLabel("b/" + path2, revision); | ||||
|     } else if (operationKind == SvnDiffCallback.OperationKind.Modified) { | ||||
|       return getLabel("b/" + path2, revision); | ||||
|     } else if (operationKind == SvnDiffCallback.OperationKind.Moved) { | ||||
|       return getLabel("b/" + path2, revision); | ||||
|     } | ||||
|     throw new IllegalArgumentException("Unsupported operation: " + operationKind); | ||||
|   } | ||||
|  | ||||
|   private void displayGitDiffHeader(OutputStream outputStream, SvnDiffCallback.OperationKind operationKind, String path1, String path2, String copyFromPath) throws SVNException { | ||||
|     if (operationKind == SvnDiffCallback.OperationKind.Deleted) { | ||||
|       displayGitDiffHeaderDeleted(outputStream, path1, path2, copyFromPath); | ||||
| @@ -1126,11 +1096,6 @@ public class SCMSvnDiffGenerator implements ISvnDiffGenerator { | ||||
|     displayString(outputStream, path1); | ||||
|   } | ||||
|  | ||||
|   private String getAdjustedPathWithLabel(String displayPath, String path, String revision, String commonAncestor) { | ||||
|     String adjustedPath = getAdjustedPath(displayPath, path, commonAncestor); | ||||
|     return getLabel(adjustedPath, revision); | ||||
|   } | ||||
|  | ||||
|   private String getAdjustedPath(String displayPath, String path1, String commonAncestor) { | ||||
|     String adjustedPath = getRelativePath(path1, commonAncestor); | ||||
|  | ||||
|   | ||||
| @@ -21,20 +21,15 @@ | ||||
|  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||||
|  * SOFTWARE. | ||||
|  */ | ||||
|      | ||||
| package sonia.scm.web; | ||||
|  | ||||
| //~--- non-JDK imports -------------------------------------------------------- | ||||
| package sonia.scm.web; | ||||
|  | ||||
| import com.google.common.collect.ImmutableList; | ||||
| import com.google.common.collect.ImmutableList.Builder; | ||||
| import com.google.common.collect.Ordering; | ||||
| import com.google.inject.Inject; | ||||
| import com.google.inject.Provider; | ||||
|  | ||||
| import org.slf4j.Logger; | ||||
| import org.slf4j.LoggerFactory; | ||||
|  | ||||
| import org.tmatesoft.svn.core.SVNDirEntry; | ||||
| import org.tmatesoft.svn.core.SVNErrorMessage; | ||||
| import org.tmatesoft.svn.core.SVNException; | ||||
| @@ -43,8 +38,6 @@ import org.tmatesoft.svn.core.internal.server.dav.CollectionRenderer; | ||||
| import org.tmatesoft.svn.core.internal.server.dav.DAVPathUtil; | ||||
| import org.tmatesoft.svn.core.internal.server.dav.DAVResource; | ||||
| import org.tmatesoft.svn.core.internal.server.dav.DAVResourceURI; | ||||
|  | ||||
| import sonia.scm.config.ScmConfiguration; | ||||
| import sonia.scm.repository.Repository; | ||||
| import sonia.scm.repository.RepositoryProvider; | ||||
| import sonia.scm.template.Template; | ||||
| @@ -52,399 +45,177 @@ import sonia.scm.template.TemplateEngine; | ||||
| import sonia.scm.template.TemplateEngineFactory; | ||||
| import sonia.scm.util.HttpUtil; | ||||
|  | ||||
| //~--- JDK imports ------------------------------------------------------------ | ||||
|  | ||||
| import java.io.IOException; | ||||
| import java.io.StringWriter; | ||||
|  | ||||
| import java.util.Iterator; | ||||
| import java.util.List; | ||||
| import javax.servlet.http.HttpServletRequest; | ||||
|  | ||||
| /** | ||||
|  * | ||||
|  * @author Sebastian Sdorra | ||||
|  */ | ||||
| public class SvnCollectionRenderer implements CollectionRenderer | ||||
| { | ||||
| public class SvnCollectionRenderer implements CollectionRenderer { | ||||
|  | ||||
|   /** Field description */ | ||||
|   private static final String RESOURCE_SVNINDEX = | ||||
|     "/sonia/scm/svn.index.mustache"; | ||||
|   private final TemplateEngineFactory templateEngineFactory; | ||||
|   private final RepositoryProvider repositoryProvider; | ||||
|   private static final String RESOURCE_SVNINDEX = "/sonia/scm/svn.index.mustache"; | ||||
|  | ||||
|   /** | ||||
|    * the logger for SvnCollectionRenderer | ||||
|    */ | ||||
|   private static final Logger logger = | ||||
|     LoggerFactory.getLogger(SvnCollectionRenderer.class); | ||||
|   private static final Logger LOG = LoggerFactory.getLogger(SvnCollectionRenderer.class); | ||||
|  | ||||
|   //~--- constructors --------------------------------------------------------- | ||||
|  | ||||
|   /** | ||||
|    * Constructs ... | ||||
|    * | ||||
|    * | ||||
|    * | ||||
|    * @param configuration | ||||
|    * @param templateEngineFactory | ||||
|    * @param repositoryProvider | ||||
|    * @param requestProvider | ||||
|    */ | ||||
|   @Inject | ||||
|   public SvnCollectionRenderer(ScmConfiguration configuration, | ||||
|     TemplateEngineFactory templateEngineFactory, | ||||
|     RepositoryProvider repositoryProvider, | ||||
|     Provider<HttpServletRequest> requestProvider) | ||||
|   { | ||||
|     this.configuration = configuration; | ||||
|   public SvnCollectionRenderer(TemplateEngineFactory templateEngineFactory, | ||||
|                                RepositoryProvider repositoryProvider) { | ||||
|     this.templateEngineFactory = templateEngineFactory; | ||||
|     this.repositoryProvider = repositoryProvider; | ||||
|     this.requestProvider = requestProvider; | ||||
|   } | ||||
|  | ||||
|   //~--- methods -------------------------------------------------------------- | ||||
|  | ||||
|   /** | ||||
|    * Method description | ||||
|    * | ||||
|    * | ||||
|    * @param buffer | ||||
|    * @param resource | ||||
|    * | ||||
|    * @throws SVNException | ||||
|    */ | ||||
|   @Override | ||||
|   @SuppressWarnings("java:S2139") | ||||
|   public void renderCollection(StringBuilder buffer, DAVResource resource) | ||||
|     throws SVNException | ||||
|   { | ||||
|     throws SVNException { | ||||
|     TemplateEngine engine = templateEngineFactory.getDefaultEngine(); | ||||
|  | ||||
|     StringWriter writer = new StringWriter(); | ||||
|  | ||||
|     try | ||||
|     { | ||||
|     try { | ||||
|       Template template = engine.getTemplate(RESOURCE_SVNINDEX); | ||||
|  | ||||
|       template.execute(writer, createRepositoryWrapper(resource)); | ||||
|     } | ||||
|     catch (IOException ex) | ||||
|     { | ||||
|       logger.error("could not render directory", ex); | ||||
|     } catch (IOException ex) { | ||||
|       LOG.error("could not render directory", ex); | ||||
|  | ||||
|       throw new SVNException(SVNErrorMessage.UNKNOWN_ERROR_MESSAGE, ex); | ||||
|     } | ||||
|  | ||||
|     writer.flush(); | ||||
|     buffer.append(writer.toString()); | ||||
|     buffer.append(writer); | ||||
|   } | ||||
|  | ||||
|   /** | ||||
|    * Method description | ||||
|    * | ||||
|    * | ||||
|    * @param resource | ||||
|    * | ||||
|    * @return | ||||
|    * | ||||
|    * @throws SVNException | ||||
|    */ | ||||
|   private RepositoryWrapper createRepositoryWrapper(DAVResource resource) | ||||
|     throws SVNException | ||||
|   { | ||||
|     throws SVNException { | ||||
|     Builder<DirectoryEntry> entries = ImmutableList.builder(); | ||||
|  | ||||
|     DAVResourceURI uri = resource.getResourceURI(); | ||||
|     String path = uri.getPath(); | ||||
|  | ||||
|     if (!HttpUtil.SEPARATOR_PATH.equals(path)) | ||||
|     { | ||||
|     if (!HttpUtil.SEPARATOR_PATH.equals(path)) { | ||||
|       String completePath = HttpUtil.append(uri.getContext(), path); | ||||
|       String parent = DAVPathUtil.removeTail(completePath, true); | ||||
|  | ||||
|       entries.add(new DirectoryEntry("..", parent, true)); | ||||
|     } | ||||
|  | ||||
|     for (Iterator iterator = resource.getEntries().iterator(); | ||||
|       iterator.hasNext(); ) | ||||
|     { | ||||
|       SVNDirEntry entry = (SVNDirEntry) iterator.next(); | ||||
|     for (Object o : resource.getEntries()) { | ||||
|       SVNDirEntry entry = (SVNDirEntry) o; | ||||
|  | ||||
|       entries.add(new DirectoryEntry(resource, entry)); | ||||
|     } | ||||
|  | ||||
|     //J- | ||||
|     return new RepositoryWrapper( | ||||
|       repositoryProvider.get(),  | ||||
|       resource, | ||||
|       repositoryProvider.get(), | ||||
|       new DirectoryOrdering().immutableSortedCopy(entries.build()) | ||||
|     ); | ||||
|     //J+ | ||||
|   } | ||||
|   private static class DirectoryEntry { | ||||
|     private final boolean directory; | ||||
|     private final String name; | ||||
|     private final String url; | ||||
|  | ||||
|   private String getBaseUrl() { | ||||
|     return HttpUtil.getCompleteUrl(requestProvider.get()); | ||||
|   } | ||||
|  | ||||
|   //~--- inner classes -------------------------------------------------------- | ||||
|  | ||||
|   /** | ||||
|    * Class description | ||||
|    * | ||||
|    * | ||||
|    * @version        Enter version here..., 13/11/10 | ||||
|    * @author         Enter your name here... | ||||
|    */ | ||||
|   private static class DirectoryEntry | ||||
|   { | ||||
|  | ||||
|     /** | ||||
|      * Constructs ... | ||||
|      * | ||||
|      * | ||||
|      * @param resource | ||||
|      * @param entry | ||||
|      */ | ||||
|     public DirectoryEntry(DAVResource resource, SVNDirEntry entry) | ||||
|     { | ||||
|     public DirectoryEntry(DAVResource resource, SVNDirEntry entry) { | ||||
|       this.name = entry.getName(); | ||||
|       this.url = createUrl(resource, entry); | ||||
|       this.directory = entry.getKind() == SVNNodeKind.DIR; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Constructs ... | ||||
|      * | ||||
|      * | ||||
|      * @param name | ||||
|      * @param url | ||||
|      * @param directory | ||||
|      */ | ||||
|     public DirectoryEntry(String name, String url, boolean directory) | ||||
|     { | ||||
|     public DirectoryEntry(String name, String url, boolean directory) { | ||||
|       this.name = name; | ||||
|       this.url = url; | ||||
|       this.directory = directory; | ||||
|     } | ||||
|  | ||||
|     //~--- get methods -------------------------------------------------------- | ||||
|  | ||||
|     /** | ||||
|      * Method description | ||||
|      * | ||||
|      * | ||||
|      * @return | ||||
|      */ | ||||
|     public String getName() | ||||
|     { | ||||
|     public String getName() { | ||||
|       return name; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Method description | ||||
|      * | ||||
|      * | ||||
|      * @return | ||||
|      */ | ||||
|     public String getUrl() | ||||
|     { | ||||
|     public String getUrl() { | ||||
|       return url; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Method description | ||||
|      * | ||||
|      * | ||||
|      * @return | ||||
|      */ | ||||
|     public boolean isDirectory() | ||||
|     { | ||||
|     public boolean isDirectory() { | ||||
|       return directory; | ||||
|     } | ||||
|  | ||||
|     //~--- methods ------------------------------------------------------------ | ||||
|  | ||||
|     /** | ||||
|      * Method description | ||||
|      * | ||||
|      * | ||||
|      * @param resource | ||||
|      * @param entry | ||||
|      * | ||||
|      * @return | ||||
|      */ | ||||
|     private String createUrl(DAVResource resource, SVNDirEntry entry) | ||||
|     { | ||||
|     private String createUrl(DAVResource resource, SVNDirEntry entry) { | ||||
|       StringBuilder buffer = new StringBuilder(); | ||||
|  | ||||
|       buffer.append(resource.getResourceURI().getContext()); | ||||
|  | ||||
|       String path = resource.getResourceURI().getPath(); | ||||
|  | ||||
|       if (!HttpUtil.SEPARATOR_PATH.equals(path)) | ||||
|       { | ||||
|       if (!HttpUtil.SEPARATOR_PATH.equals(path)) { | ||||
|         buffer.append(path); | ||||
|       } | ||||
|  | ||||
|       buffer.append(DAVPathUtil.standardize(entry.getName())); | ||||
|  | ||||
|       if (isDirectory()) | ||||
|       { | ||||
|       if (isDirectory()) { | ||||
|         buffer.append(HttpUtil.SEPARATOR_PATH); | ||||
|       } | ||||
|  | ||||
|       return buffer.toString(); | ||||
|     } | ||||
|  | ||||
|     //~--- fields ------------------------------------------------------------- | ||||
|  | ||||
|     /** Field description */ | ||||
|     private final boolean directory; | ||||
|  | ||||
|     /** Field description */ | ||||
|     private final String name; | ||||
|  | ||||
|     /** Field description */ | ||||
|     private final String url; | ||||
|   } | ||||
|   private static class DirectoryOrdering extends Ordering<DirectoryEntry> { | ||||
|  | ||||
|  | ||||
|   /** | ||||
|    * Class description | ||||
|    * | ||||
|    * | ||||
|    * @version        Enter version here..., 13/11/10 | ||||
|    * @author         Enter your name here... | ||||
|    */ | ||||
|   private static class DirectoryOrdering extends Ordering<DirectoryEntry> | ||||
|   { | ||||
|  | ||||
|     /** | ||||
|      * Method description | ||||
|      * | ||||
|      * | ||||
|      * @param left | ||||
|      * @param right | ||||
|      * | ||||
|      * @return | ||||
|      */ | ||||
|     @Override | ||||
|     public int compare(DirectoryEntry left, DirectoryEntry right) | ||||
|     { | ||||
|       int result; | ||||
|     public int compare(DirectoryEntry left, DirectoryEntry right) { | ||||
|       int result = 0; | ||||
|  | ||||
|       if (left.isDirectory() &&!right.isDirectory()) | ||||
|       { | ||||
|       if (left == null || right == null) { | ||||
|         return result; | ||||
|       } | ||||
|  | ||||
|       if (left.isDirectory() && !right.isDirectory()) { | ||||
|         result = -1; | ||||
|       } | ||||
|       else if (!left.isDirectory() && right.isDirectory()) | ||||
|       { | ||||
|       } else if (!left.isDirectory() && right.isDirectory()) { | ||||
|         result = 1; | ||||
|       } | ||||
|       else | ||||
|       { | ||||
|         if ("..".equals(left.getName())) | ||||
|         { | ||||
|       } else { | ||||
|         if ("..".equals(left.getName())) { | ||||
|           result = -1; | ||||
|         } | ||||
|         else if ("..".equals(right.getName())) | ||||
|         { | ||||
|         } else if ("..".equals(right.getName())) { | ||||
|           result = 1; | ||||
|         } | ||||
|         else | ||||
|         { | ||||
|         } else { | ||||
|           result = left.getName().compareTo(right.getName()); | ||||
|         } | ||||
|       } | ||||
|  | ||||
|       return result; | ||||
|     } | ||||
|  | ||||
|   } | ||||
|  | ||||
|   private static class RepositoryWrapper { | ||||
|     private final List<DirectoryEntry> entries; | ||||
|  | ||||
|   /** | ||||
|    * Class description | ||||
|    * | ||||
|    * | ||||
|    * @version        Enter version here..., 13/11/10 | ||||
|    * @author         Enter your name here... | ||||
|    */ | ||||
|   private static class RepositoryWrapper | ||||
|   { | ||||
|     private final Repository repository; | ||||
|  | ||||
|     /** | ||||
|      * Constructs ... | ||||
|      * | ||||
|      * | ||||
|      * | ||||
|      * @param repository | ||||
|      * @param resource | ||||
|      * @param entries | ||||
|      */ | ||||
|     public RepositoryWrapper(Repository repository, DAVResource resource, List<DirectoryEntry> entries) | ||||
|     { | ||||
|  | ||||
|     public RepositoryWrapper(Repository repository, List<DirectoryEntry> entries) { | ||||
|       this.repository = repository; | ||||
|       this.resource = resource; | ||||
|       this.entries = entries; | ||||
|     } | ||||
|  | ||||
|     //~--- get methods -------------------------------------------------------- | ||||
|  | ||||
|     /** | ||||
|      * Method description | ||||
|      * | ||||
|      * | ||||
|      * @return | ||||
|      */ | ||||
|     public List<DirectoryEntry> getEntries() | ||||
|     { | ||||
|     public List<DirectoryEntry> getEntries() { | ||||
|       return entries; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Method description | ||||
|      * | ||||
|      * | ||||
|      * @return | ||||
|      */ | ||||
|     public String getName() | ||||
|     { | ||||
|     public String getName() { | ||||
|       return repository.getName(); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Method description | ||||
|      * | ||||
|      * | ||||
|      * @return | ||||
|      */ | ||||
|     public Repository getRepository() | ||||
|     { | ||||
|     public Repository getRepository() { | ||||
|       return repository; | ||||
|     } | ||||
|  | ||||
|     //~--- fields ------------------------------------------------------------- | ||||
|  | ||||
|     /** Field description */ | ||||
|     private final List<DirectoryEntry> entries; | ||||
|  | ||||
|     /** Field description */ | ||||
|     private final Repository repository; | ||||
|  | ||||
|     /** Field description */ | ||||
|     private final DAVResource resource; | ||||
|   } | ||||
|  | ||||
|  | ||||
|   //~--- fields --------------------------------------------------------------- | ||||
|  | ||||
|   private final Provider<HttpServletRequest> requestProvider; | ||||
|    | ||||
|   /** Field description */ | ||||
|   private final ScmConfiguration configuration; | ||||
|  | ||||
|   /** Field description */ | ||||
|   private final RepositoryProvider repositoryProvider; | ||||
|  | ||||
|   /** Field description */ | ||||
|   private final TemplateEngineFactory templateEngineFactory; | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user