Skip to content

Commit

Permalink
Remove empty class SourcesLib and deprecated/unused methods in Resour…
Browse files Browse the repository at this point in the history
…ceLib / Sonarlint improvements (geonetwork#7692)

* Remove empty class SourcesLib and deprecated/unused methods in ResourceLib

* Sonalint improvements
  • Loading branch information
josegar74 authored Feb 5, 2024
1 parent 4cbb041 commit 5123486
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 91 deletions.
10 changes: 5 additions & 5 deletions core/src/main/java/org/fao/geonet/lib/DbLib.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//=============================================================================
//=== Copyright (C) 2001-2007 Food and Agriculture Organization of the
//=== Copyright (C) 2001-2024 Food and Agriculture Organization of the
//=== United Nations (FAO-UN), United Nations World Food Programme (WFP)
//=== and United Nations Environment Programme (UNEP)
//===
Expand Down Expand Up @@ -67,7 +67,7 @@ public void insertData(ServletContext servletContext, final ServiceContext conte
runSQL(context, data);
}

static public void runSQL(final ServiceContext context, final List<String> data) {
public static void runSQL(final ServiceContext context, final List<String> data) {
TransactionManager.runInTransaction("Apply SQL statements in database", context.getApplicationContext(),
TransactionManager.TransactionRequirement.CREATE_ONLY_WHEN_NEEDED, TransactionManager.CommitBehavior.ALWAYS_COMMIT, false,
new TransactionTask<Object>() {
Expand All @@ -88,7 +88,7 @@ public void insertData(ServletContext servletContext, Statement statement, Path
runSQL(statement, data, true);
}

static private void runSQL(EntityManager entityManager, List<String> data, boolean failOnError) throws Exception {
private static void runSQL(EntityManager entityManager, List<String> data, boolean failOnError) throws Exception {
StringBuffer sb = new StringBuffer();

boolean inBlock = false;
Expand Down Expand Up @@ -244,7 +244,7 @@ private List<String> loadSqlDataFile(ServletContext servletContext, ApplicationC
Path file = checkFilePath(servletContext, appPath, filePath, filePrefix, DatabaseType.lookup(connection).toString());

// --- load the sql data
return Lib.text.load(servletContext, appPath, file, Constants.ENCODING);
return Lib.text.load(file, Constants.ENCODING);
} finally {
if (connection != null) {
connection.close();
Expand All @@ -258,7 +258,7 @@ private List<String> loadSqlDataFile(ServletContext servletContext, Statement st
Path file = checkFilePath(servletContext, appPath, filePath, filePrefix, DatabaseType.lookup(statement.getConnection()).toString());

// --- load the sql data
return Lib.text.load(servletContext, appPath, file, Constants.ENCODING);
return Lib.text.load(file, Constants.ENCODING);
}

}
6 changes: 3 additions & 3 deletions core/src/main/java/org/fao/geonet/lib/ElementLib.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//=============================================================================
//=== Copyright (C) 2001-2007 Food and Agriculture Organization of the
//=== Copyright (C) 2001-2024 Food and Agriculture Organization of the
//=== United Nations (FAO-UN), United Nations World Food Programme (WFP)
//=== and United Nations Environment Programme (UNEP)
//===
Expand Down Expand Up @@ -46,7 +46,7 @@ public class ElementLib {
//-----------------------------------------------------------------------------

public Set<String> getIds(Element elem) {
HashSet<String> hs = new HashSet<String>();
HashSet<String> hs = new HashSet<>();

for (Object child : elem.getChildren())
hs.add(((Element) child).getChildText("id"));
Expand All @@ -57,7 +57,7 @@ public Set<String> getIds(Element elem) {
//-----------------------------------------------------------------------------

public Element pruneChildren(Element elem, Set<Integer> ids) {
ArrayList<Element> alToPrune = new ArrayList<Element>();
ArrayList<Element> alToPrune = new ArrayList<>();

//--- collect elements to prune

Expand Down
21 changes: 13 additions & 8 deletions core/src/main/java/org/fao/geonet/lib/Lib.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//=============================================================================
//=== Copyright (C) 2001-2007 Food and Agriculture Organization of the
//=== Copyright (C) 2001-2024 Food and Agriculture Organization of the
//=== United Nations (FAO-UN), United Nations World Food Programme (WFP)
//=== and United Nations Environment Programme (UNEP)
//===
Expand All @@ -24,11 +24,16 @@
package org.fao.geonet.lib;

public class Lib {
public final static ElementLib element = new ElementLib();
public final static DbLib db = new DbLib();
public final static ResourceLib resource = new ResourceLib();
public final static TypeLib type = new TypeLib();
public final static NetLib net = new NetLib();
public final static SourcesLib sources = new SourcesLib();
public final static TextLib text = new TextLib();
public static final ElementLib element = new ElementLib();
public static final DbLib db = new DbLib();
public static final ResourceLib resource = new ResourceLib();
public static final TypeLib type = new TypeLib();
public static final NetLib net = new NetLib();
public static final TextLib text = new TextLib();

/**
* Private constructor to avoid instantiate the class.
*/
private Lib() {
}
}
6 changes: 3 additions & 3 deletions core/src/main/java/org/fao/geonet/lib/NetLib.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//=============================================================================
//=== Copyright (C) 2001-2023 Food and Agriculture Organization of the
//=== Copyright (C) 2001-2024 Food and Agriculture Organization of the
//=== United Nations (FAO-UN), United Nations World Food Programme (WFP)
//=== and United Nations Environment Programme (UNEP)
//===
Expand Down Expand Up @@ -43,7 +43,7 @@
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Properties;
import java.util.regex.PatternSyntaxException;

Expand Down Expand Up @@ -229,7 +229,7 @@ public URLConnection setupProxy(ServiceContext context, URL url) throws IOExcept
conn = url.openConnection(proxy);

if (username.trim().length() != 0) {
String encodedUserPwd = new Base64().encodeAsString((username + ":" + password).getBytes(Charset.forName("UTF-8")));
String encodedUserPwd = new Base64().encodeAsString((username + ":" + password).getBytes(StandardCharsets.UTF_8));
conn.setRequestProperty("Accept-Charset", "UTF-8");
conn.setRequestProperty("Proxy-Authorization", "Basic " + encodedUserPwd);
}
Expand Down
24 changes: 4 additions & 20 deletions core/src/main/java/org/fao/geonet/lib/ResourceLib.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//=============================================================================
//=== Copyright (C) 2001-2007 Food and Agriculture Organization of the
//=== Copyright (C) 2001-2024 Food and Agriculture Organization of the
//=== United Nations (FAO-UN), United Nations World Food Programme (WFP)
//=== and United Nations Environment Programme (UNEP)
//===
Expand Down Expand Up @@ -34,7 +34,6 @@
import org.fao.geonet.exceptions.OperationNotAllowedEx;
import org.fao.geonet.kernel.AccessManager;
import org.fao.geonet.kernel.GeonetworkDataDirectory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -113,7 +112,7 @@ public void checkPrivilege(ServiceContext context, String id,
denyAccess(context);
}

public void denyAccess(ServiceContext context) throws Exception {
public void denyAccess(ServiceContext context) throws AccessDeniedException, OperationNotAllowedEx {
if (context.getUserSession().isAuthenticated()) {
throw new AccessDeniedException("User is not permitted to access this resource");
} else {
Expand All @@ -134,21 +133,6 @@ public void checkEditPrivilege(ServiceContext context, String id)
/**
* @return the absolute path of the folder choosen to store all deleted metadata
*/
@Deprecated
public Path getRemovedDir(ServiceContext context) {
GeonetContext gc = (GeonetContext) context
.getHandlerContext(Geonet.CONTEXT_NAME);
return gc.getBean(GeonetworkDataDirectory.class).getBackupDir();
}

/**
* See {@link #getRemovedDir(Path, String)}
*/
@Deprecated
public Path getRemovedDir(ServiceContext context, String id) {
return getRemovedDir(getRemovedDir(context), id);
}

public Path getRemovedDir(int id) {
ApplicationContext appContext = ApplicationContextHolder.get();
GeonetworkDataDirectory dataDirectory = appContext.getBean(GeonetworkDataDirectory.class);
Expand All @@ -172,10 +156,10 @@ public Path getRemovedDir(Path removedDir, String id) {
// ---
// -----------------------------------------------------------------------------

private String pad(int group, int lenght) {
private String pad(int group, int length) {
String text = Integer.toString(group);

while (text.length() < lenght)
while (text.length() < length)
text = "0" + text;

return text;
Expand Down
40 changes: 0 additions & 40 deletions core/src/main/java/org/fao/geonet/lib/SourcesLib.java

This file was deleted.

22 changes: 10 additions & 12 deletions core/src/main/java/org/fao/geonet/lib/TextLib.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//=============================================================================
//=== Copyright (C) 2001-2007 Food and Agriculture Organization of the
//=== Copyright (C) 2001-2024 Food and Agriculture Organization of the
//=== United Nations (FAO-UN), United Nations World Food Programme (WFP)
//=== and United Nations Environment Programme (UNEP)
//===
Expand All @@ -26,11 +26,11 @@
import org.fao.geonet.Util;
import org.fao.geonet.utils.IO;

import javax.servlet.ServletContext;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
Expand All @@ -41,27 +41,25 @@
public class TextLib {
private static final Random RANDOM = new Random();

public List<String> load(ServletContext servletContext, Path appPath, Path file) throws IOException {
return load(servletContext, appPath, file, "ISO-8859-1");
public List<String> load(Path file) throws IOException {
return load(file, "ISO-8859-1");
}

public List<String> load(ServletContext servletContext, Path appPath, Path file, String encoding) throws IOException {
BufferedReader reader = IO.newBufferedReader(file, Charset.forName(encoding));
List<String> al = new ArrayList<String>();
String line = reader.readLine();
try {
public List<String> load(Path file, String encoding) throws IOException {
try(BufferedReader reader = IO.newBufferedReader(file, Charset.forName(encoding))) {
List<String> al = new ArrayList<>();
String line = reader.readLine();

while (line != null) {
al.add(line);
line = reader.readLine();
}
return al;
} finally {
reader.close();
}
}

public void save(Path file, List<String> lines) throws IOException {
try (BufferedWriter ow = Files.newBufferedWriter(file, Charset.forName("ISO-8859-1"))) {
try (BufferedWriter ow = Files.newBufferedWriter(file, StandardCharsets.ISO_8859_1)) {
for (String line : lines) {
ow.write(line);
ow.newLine();
Expand Down

0 comments on commit 5123486

Please sign in to comment.