- Ensure the "BufferedReader" and the "InputStream" get always closed in the end, in "Converter.readFile()".

- Code polishing.
This commit is contained in:
Lampros Smyrnaios 2023-01-31 15:40:06 +02:00
parent c2e5af87e8
commit a239164f97
3 changed files with 13 additions and 18 deletions

View File

@ -205,13 +205,11 @@ public class RepositoryServiceImpl implements RepositoryService {
for (String repoId : ids) {
requestFilter.setId(repoId);
DatasourceResponse rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, DatasourceResponse.class);
if (rs == null) {
if ( rs == null )
logger.error("The \"DatasourceResponse\" is null!");
} else {
else
datasourceDetailsList.addAll(rs.getDatasourceInfo());
}
}
resultSet = objectMapper.readValue(objectMapper.writeValueAsString(datasourceDetailsList),
@ -547,13 +545,12 @@ public class RepositoryServiceImpl implements RepositoryService {
RepositoryInterface repositoryInterface,
String desiredCompatibilityLevel) throws Exception {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
Repository e = this.getRepositoryById(repoId);
repositoryInterface = fillInterfaceFields(e, repositoryInterface, datatype);
Repository repo = this.getRepositoryById(repoId);
repositoryInterface = fillInterfaceFields(repo, repositoryInterface, datatype);
UriComponents uriComponents = UriComponentsBuilder
.fromHttpUrl(baseAddress + "/ds/api/add/")
.build()
.encode();
.build().encode();
HttpEntity<RepositoryInterface> httpEntity = new HttpEntity<>(repositoryInterface, httpHeaders);
restTemplate.postForObject(uriComponents.toUri(), httpEntity, String.class);
@ -561,15 +558,17 @@ public class RepositoryServiceImpl implements RepositoryService {
// Explicitly update validation set (updating the interface does not allow updating the set value)
this.updateValidationSet(repoId, repositoryInterface.getId(), repositoryInterface.getAccessSet());
emailUtils.sendAdminRegisterInterfaceEmail(e, comment, repositoryInterface, desiredCompatibilityLevel, authentication);
emailUtils.sendUserRegisterInterfaceEmail(e, comment, repositoryInterface, desiredCompatibilityLevel, authentication);
emailUtils.sendAdminRegisterInterfaceEmail(repo, comment, repositoryInterface, desiredCompatibilityLevel, authentication);
emailUtils.sendUserRegisterInterfaceEmail(repo, comment, repositoryInterface, desiredCompatibilityLevel, authentication);
if (desiredCompatibilityLevel != null && (repositoryInterface.getCompatibility() == null || !repositoryInterface.getCompatibility().equals(desiredCompatibilityLevel))) {
String prevCompatibilityLevel = repositoryInterface.getCompatibility();
if ( (desiredCompatibilityLevel != null)
&& ((prevCompatibilityLevel == null) || ! prevCompatibilityLevel.equals(desiredCompatibilityLevel))) {
InterfaceComplianceRequest request = new InterfaceComplianceRequest(repoId, repositoryInterface.getId(), desiredCompatibilityLevel);
interfaceComplianceService.create(request);
}
submitInterfaceValidation(e, getAuthenticatedUser().getEmail(), repositoryInterface, false, repositoryInterface.getCompatibility());
submitInterfaceValidation(repo, getAuthenticatedUser().getEmail(), repositoryInterface, false, desiredCompatibilityLevel);
return repositoryInterface;
}

View File

@ -16,7 +16,6 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import javax.validation.constraints.NotNull;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;

View File

@ -6,7 +6,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
@ -22,13 +21,11 @@ public class Converter {
public static List<String> readFile(String filename) {
String line;
List<String> list = new ArrayList<>();
try {
InputStream in = Converter.class.getResourceAsStream("/eu/**/" + filename);
BufferedReader br = new BufferedReader(new InputStreamReader(in)); // It may throw an NPE.
try ( BufferedReader br = new BufferedReader(new InputStreamReader(Objects.requireNonNull(Converter.class.getResourceAsStream("/eu/**/" + filename)))) )
{
while ((line = br.readLine()) != null) {
list.add(line.trim());
}
br.close();
} catch (Exception e) {
logger.error("Error opening file!", e);
}