- Add error-checking and improve exceptions handling.

- Code polishing.
This commit is contained in:
Lampros Smyrnaios 2022-12-07 13:37:55 +02:00
parent f720446bdf
commit deae3b8b54
9 changed files with 51 additions and 42 deletions

View File

@ -22,8 +22,7 @@ public class FrontEndLinkURIAuthenticationSuccessHandler implements Authenticati
private String frontEndURI;
private static final Logger LOGGER = Logger
.getLogger(FrontEndLinkURIAuthenticationSuccessHandler.class);
private static final Logger LOGGER = Logger.getLogger(FrontEndLinkURIAuthenticationSuccessHandler.class);
public void init() {
LOGGER.debug("Front end uri : " + frontEndURI);

View File

@ -33,8 +33,7 @@ import java.util.List;
@Api(description = "Piwik API", tags = {"piwik"})
public class PiWikController {
private static final Logger LOGGER = Logger
.getLogger(PiWikController.class);
private static final Logger LOGGER = Logger.getLogger(PiWikController.class);
@Autowired
private PiWikServiceImpl piWikService;

View File

@ -19,8 +19,7 @@ public class StatsController {
@Autowired
private StatsServiceImpl statsService;
@RequestMapping(value = "/getStatistics" , method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@RequestMapping(value = "/getStatistics" , method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Map getStatistics(){
return statsService.getStatistics();

View File

@ -426,8 +426,7 @@ public class RepositoryServiceImpl implements RepositoryService {
public List<AggregationInfo> getRepositoryAggregations(String id, int from, int size) {
List<AggregationInfo> res = getRepositoryAggregations(id);
return res.subList(from, Math.min(from + size, res.size()));
return (res != null) ? res.subList(from, Math.min(from + size, res.size())) : null;
}
@Override
@ -435,9 +434,11 @@ public class RepositoryServiceImpl implements RepositoryService {
LOGGER.debug("Retrieving aggregations (by year) for repository with id : " + id);
List<AggregationInfo> aggregationHistory = getRepositoryAggregations(id);
Map<String, List<AggregationInfo>> aggregationByYear = new HashMap<>();
return aggregationHistory.size() == 0 ? aggregationByYear : createYearMap(aggregationHistory);
if ( aggregationHistory != null ) {
Map<String, List<AggregationInfo>> aggregationByYear = new HashMap<>();
return aggregationHistory.size() == 0 ? aggregationByYear : createYearMap(aggregationHistory);
} else
return null;
}
private Map<String, List<AggregationInfo>> createYearMap(List<AggregationInfo> aggregationHistory) {
@ -479,15 +480,19 @@ public class RepositoryServiceImpl implements RepositoryService {
// String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
ApiDetailsResponse rs = restTemplate.getForObject(uriComponents.toUri(), ApiDetailsResponse.class);
if ( rs == null ) {
LOGGER.error("The ApiDetailsResponse was null!");
return null;
}
// TODO STOP FILTERING OUT "sword", "rest" AND FIX UI!
List<ApiDetails> res = new ArrayList<>();
for (ApiDetails det: rs.getApi()) {
if (!det.getProtocol().equals("sword") &&
!det.getProtocol().equals("rest") &&
!det.getProtocol().equals("ftp")) {
String protocol = det.getProtocol();
if ( !protocol.equals("sword") &&
!protocol.equals("rest") &&
!protocol.equals("ftp")) {
res.add(det);
}
}
@ -560,7 +565,6 @@ public class RepositoryServiceImpl implements RepositoryService {
}
return repository;
}
@ -585,8 +589,11 @@ public class RepositoryServiceImpl implements RepositoryService {
} catch (Exception e) {
LOGGER.error("Error sending email", e);
}
} else
LOGGER.error("Error storing repository: " + responseEntity.getBody().toString());
} else {
Object responseBody = responseEntity.getBody();
if ( responseBody != null )
LOGGER.error("Error updating repository: " + responseBody);
}
return repository;
}
@ -603,8 +610,7 @@ public class RepositoryServiceImpl implements RepositoryService {
// LOGGER.debug("JSON to update -> " + json_repository);
HttpEntity<Repository> httpEntity = new HttpEntity<>(repository, httpHeaders);
ResponseEntity responseEntity = restTemplate.exchange(uriComponents.toUri(), HttpMethod.POST, httpEntity
, ResponseEntity.class);
ResponseEntity responseEntity = restTemplate.exchange(uriComponents.toUri(), HttpMethod.POST, httpEntity, ResponseEntity.class);
if (responseEntity.getStatusCode().equals(HttpStatus.OK)) {
try {
@ -613,8 +619,11 @@ public class RepositoryServiceImpl implements RepositoryService {
} catch (Exception e) {
LOGGER.error("Error sending emails: " + e);
}
} else
LOGGER.debug(responseEntity.getBody().toString());
} else {
Object responseBody = responseEntity.getBody();
if ( responseBody != null )
LOGGER.error("Error updating repository: " + responseBody);
}
return repository;
}
@ -643,7 +652,9 @@ public class RepositoryServiceImpl implements RepositoryService {
LOGGER.error("Error sending emails: " + e);
}
} else {
LOGGER.debug(responseEntity.getBody().toString());
Object responseBody = responseEntity.getBody();
if ( responseBody != null )
LOGGER.error("Error storing repository: " + responseBody);
}
}
@ -804,7 +815,8 @@ public class RepositoryServiceImpl implements RepositoryService {
RequestFilter requestFilter = new RequestFilter();
requestFilter.setRegisteredby(userEmail);
return Arrays.asList(restTemplate.postForObject(uriComponents.toUri(), requestFilter, String[].class));
Object result = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String[].class);
return (result != null) ? Collections.singletonList(result.toString()) : null;
}
private Vocabulary getVocabulary(String vocName) {

View File

@ -1,7 +1,7 @@
package eu.dnetlib.repo.manager.service;
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
import org.springframework.beans.factory.annotation.Value;
import org.mitre.openid.connect.model.UserInfo;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.GrantedAuthority;
@ -25,12 +25,16 @@ public class UserServiceImpl implements UserService {
LOGGER.debug("User authentication : " + authentication);
Map<String,Object> body = new HashMap<>();
body.put("sub",authentication.getSub());
if(authentication.getUserInfo().getName() == null || authentication.getUserInfo().getName().equals(""))
body.put("name",authentication.getUserInfo().getGivenName() + " " + authentication.getUserInfo().getFamilyName());
else
body.put("name",authentication.getUserInfo().getName());
body.put("email",authentication.getUserInfo().getEmail());
UserInfo userInfo = authentication.getUserInfo();
String userName = userInfo.getName();
if ( userName == null || userName.isEmpty() )
body.put("name", userInfo.getGivenName() + " " + userInfo.getFamilyName());
else
body.put("name", userName);
body.put("email",userInfo.getEmail());
List<String> roles = authentication.getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList());
body.put("role",roles);

View File

@ -57,8 +57,7 @@ public class ValidatorServiceImpl implements ValidatorService {
private Map<String, List<RuleSet>> rulesetMap = new ConcurrentHashMap<String, List<RuleSet>>();
private static final Logger LOGGER = Logger
.getLogger(ValidatorServiceImpl.class);
private static final Logger LOGGER = Logger.getLogger(ValidatorServiceImpl.class);
@Autowired
private EmailUtils emailUtils;

View File

@ -12,7 +12,6 @@ import org.json.JSONObject;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
@ -87,17 +86,17 @@ public class Converter {
List<String> list = new ArrayList<>();
try {
//InputStream in = Converter.class.getResourceAsStream("resources/eu/dnetlib/repo/manager/service/utils/"+filename);
InputStream in = Converter.class.getClass().getResourceAsStream("/eu/**/" + filename);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
InputStream in = Converter.class.getResourceAsStream("/eu/**/" + filename);
BufferedReader br = new BufferedReader(new InputStreamReader(in)); // It may throw an NPE.
while ((line = br.readLine()) != null) {
list.add(line.trim());
}
br.close();
} catch (IOException e) {
} catch (Exception e) {
LOGGER.debug("Error opening file!");
LOGGER.error(e);
}
return list;
return list; // It may be empty.
}
public List<AggregationInfo> toAggregationHistory(String aggregationHistoryResponse) throws JSONException {

View File

@ -10,7 +10,7 @@ import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Map;
@Component
@ -78,8 +78,7 @@ public class HttpUtils {
private HttpHeaders createHeaders(String username, String password) {
return new HttpHeaders() {{
String auth = username + ":" + password;
byte[] encodedAuth = Base64.encodeBase64(
auth.getBytes(Charset.forName("US-ASCII")));
byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(StandardCharsets.US_ASCII));
String authHeader = "Basic " + new String(encodedAuth);
set("Authorization", authHeader);
}};

View File

@ -32,8 +32,7 @@ public class OaiTools {
OaiPmhServer harvester = new OaiPmhServer(baseUrl);
SetsList setList = harvester.listSets();
ResumptionToken token = setList.getResumptionToken();
List<Set> sets = new ArrayList<Set>();
sets.addAll(setList.asList());
List<Set> sets = new ArrayList<>(setList.asList());
while (token != null) {
setList = harvester.listSets(token);
token = setList.getResumptionToken();