Skip to content

Commit

Permalink
OAIPMH server / Sonarlint improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
josegar74 committed Dec 28, 2023
1 parent 07cd230 commit 4c4943e
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 51 deletions.
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-2023 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 @@ -65,7 +65,7 @@ public class OaiPmhDispatcher {
//--- Constructor
//---
//---------------------------------------------------------------------------
private HashMap<String, OaiPmhService> hmServices = new HashMap<String, OaiPmhService>();
private HashMap<String, OaiPmhService> hmServices = new HashMap<>();

//---------------------------------------------------------------------------

Expand Down
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-2023 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 @@ -31,12 +31,12 @@
//=============================================================================

public interface OaiPmhService {
public String getVerb();
String getVerb();

/**
* Executes the service on given input request
*/
public AbstractResponse execute(AbstractRequest request, ServiceContext context)
AbstractResponse execute(AbstractRequest request, ServiceContext context)
throws Exception;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2001-2016 Food and Agriculture Organization of the
* Copyright (C) 2001-2023 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 @@ -36,9 +36,7 @@
import java.util.TimeZone;

public class ResumptionTokenCache extends Thread {


public final static int CACHE_EXPUNGE_DELAY = 10 * 1000; // 10 seconds
public static final int CACHE_EXPUNGE_DELAY = 10 * 1000; // 10 seconds

private Map<String, GeonetworkResumptionToken> map;
private static Object stopper = new Object();
Expand Down Expand Up @@ -93,6 +91,7 @@ public int getCachemaxsize() {
return settingMan.getValueAsInt("system/oai/cachesize");
}

@Override
public void run() {
synchronized (stopper) {
while (running && !isInterrupted()) {
Expand All @@ -113,8 +112,8 @@ private synchronized void expunge() {

Date now = getUTCTime();

for (Map.Entry entry : map.entrySet()) {
if (((GeonetworkResumptionToken) entry.getValue()).getExpirDate().toDate().getTime() / 1000 < (now.getTime() / 1000)) {
for (Map.Entry<String, GeonetworkResumptionToken> entry : map.entrySet()) {
if (entry.getValue().getExpirDate().toDate().getTime() / 1000 < (now.getTime() / 1000)) {
map.remove(entry.getKey());
if (Log.isDebugEnabled(Geonet.OAI_HARVESTER))
Log.debug(Geonet.OAI_HARVESTER, "OAI cache ::expunge removing:" + entry.getKey());
Expand All @@ -131,11 +130,11 @@ private void removeLast() {
long oldest = Long.MAX_VALUE;
Object oldkey = "";

for (Map.Entry entry : map.entrySet()) {
for (Map.Entry<String, GeonetworkResumptionToken> entry : map.entrySet()) {

if (((GeonetworkResumptionToken) entry.getValue()).getExpirDate().getSeconds() < oldest) {
if (entry.getValue().getExpirDate().getSeconds() < oldest) {
oldkey = entry.getKey();
oldest = ((GeonetworkResumptionToken) entry.getValue()).getExpirDate().getSeconds();
oldest = entry.getValue().getExpirDate().getSeconds();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2001-2016 Food and Agriculture Organization of the
* Copyright (C) 2001-2023 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 @@ -57,7 +57,7 @@ public abstract class AbstractTokenLister implements OaiPmhService {
private SettingManager settingMan;
private SchemaManager schemaMan;

public AbstractTokenLister(ResumptionTokenCache cache, SettingManager sm, SchemaManager scm) {
protected AbstractTokenLister(ResumptionTokenCache cache, SettingManager sm, SchemaManager scm) {
this.cache = cache;
this.settingMan = sm;
this.schemaMan = scm;
Expand Down Expand Up @@ -114,10 +114,8 @@ public AbstractResponse execute(AbstractRequest request,

TokenListRequest req = (TokenListRequest) request;

//UserSession session = context.getUserSession();
SearchResult result;

//String token = req.getResumptionToken();
String strToken = req.getResumptionToken();
GeonetworkResumptionToken token = null;

Expand Down Expand Up @@ -165,11 +163,12 @@ public AbstractResponse execute(AbstractRequest request,
params.addContent(new Element("_schema").setText(schema));
result.addIds(Lib.search(context, (Element) params.clone()));
}
if (schemas.size() == 0) result.setIds(new ArrayList<Integer>());
if (schemas.isEmpty()) result.setIds(new ArrayList<>());
}

if (result.getIds().size() == 0)
if (result.getIds().isEmpty()) {
throw new NoRecordsMatchException("No results");
}

// we only need a new token if the result set is big enough
if (result.getIds().size() > getMaxRecords()) {
Expand All @@ -178,7 +177,6 @@ public AbstractResponse execute(AbstractRequest request,
}

} else {
//result = (SearchResult) session.getProperty(Lib.SESSION_OBJECT);
token = cache.getResumptionToken(GeonetworkResumptionToken.buildKey(req));
if (Log.isDebugEnabled(Geonet.OAI_HARVESTER))
Log.debug(Geonet.OAI_HARVESTER, "OAI ListRecords : using ResumptionToken :" + GeonetworkResumptionToken.buildKey(req));
Expand All @@ -188,7 +186,6 @@ public AbstractResponse execute(AbstractRequest request,

result = token.getRes();

//pos = result.parseToken(token);
pos = GeonetworkResumptionToken.getPos(req);
}

Expand All @@ -198,7 +195,6 @@ public AbstractResponse execute(AbstractRequest request,
if (token == null && res.getSize() == 0)
throw new NoRecordsMatchException("No results");

//result.setupToken(res, pos);
if (token != null) token.setupToken(pos);
res.setResumptionToken(token);

Expand All @@ -214,7 +210,7 @@ public AbstractResponse execute(AbstractRequest request,
*/

private List<String> getSchemasThatCanConvertTo(String prefix) {
List<String> result = new ArrayList<String>();
List<String> result = new ArrayList<>();
for (String schema : schemaMan.getSchemas()) {
if (Lib.existsConverter(schemaMan.getSchemaDir(schema), prefix)) {
result.add(schema);
Expand Down
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-2023 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 @@ -79,14 +79,13 @@ public static Record buildRecordStat(ServiceContext context, Specification<Metad

if (prefix.equals(schema)) {
Attribute schemaLocAtt = sm.getSchemaLocation(schema, context);
if (schemaLocAtt != null) {
if (md.getAttribute(schemaLocAtt.getName(), schemaLocAtt.getNamespace()) == null) {
md.setAttribute(schemaLocAtt);
// make sure namespace declaration for schemalocation is present -
// remove it first (does nothing if not there) then add it
md.removeNamespaceDeclaration(schemaLocAtt.getNamespace());
md.addNamespaceDeclaration(schemaLocAtt.getNamespace());
}
if (schemaLocAtt != null && (md.getAttribute(schemaLocAtt.getName(), schemaLocAtt.getNamespace()) == null)) {
md.setAttribute(schemaLocAtt);
// make sure namespace declaration for schemalocation is present -
// remove it first (does nothing if not there) then add it
md.removeNamespaceDeclaration(schemaLocAtt.getNamespace());
md.addNamespaceDeclaration(schemaLocAtt.getNamespace());

}
} else {
Path schemaDir = sm.getSchemaDir(schema);
Expand Down
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-2023 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 @@ -69,7 +69,7 @@ public AbstractResponse execute(AbstractRequest request, ServiceContext context)

//---------------------------------------------------------------------------

private ISODate getEarliestDS(ServiceContext context) throws Exception {
private ISODate getEarliestDS(ServiceContext context) {
final AbstractMetadata oldestByChangeDate = context.getBean(MetadataRepository.class).findOldestByChangeDate();

//--- if we don't have metadata, just return 'now'
Expand Down
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-2023 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 @@ -90,7 +90,7 @@ public ListResponse processRequest(TokenListRequest req, int pos, SearchResult r
//---
//---------------------------------------------------------------------------

private Header buildHeader(ServiceContext context, int id, String prefix) throws Exception {
private Header buildHeader(ServiceContext context, int id, String prefix) {
GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME);
SchemaManager sm = gc.getBean(SchemaManager.class);

Expand All @@ -108,10 +108,8 @@ private Header buildHeader(ServiceContext context, int id, String prefix) throws

//--- try to disseminate format if not by schema then by conversion

if (!prefix.equals(schema)) {
if (!Lib.existsConverter(sm.getSchemaDir(schema), prefix)) {
return null;
}
if (!prefix.equals(schema) && (!Lib.existsConverter(sm.getSchemaDir(schema), prefix))) {
return null;
}

//--- build header and set some infos
Expand Down
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-2023 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 @@ -76,7 +76,7 @@ public AbstractResponse execute(AbstractRequest request, ServiceContext context)
return res;
}

private MetadataFormat getSchemaInfo(ServiceContext context, SchemaManager sm, String name) throws IOException, JDOMException {
private MetadataFormat getSchemaInfo(ServiceContext context, SchemaManager sm, String name) {
MetadataFormat mf = new MetadataFormat();
mf.prefix = name;
mf.schema = "";
Expand Down Expand Up @@ -104,7 +104,7 @@ private List<MetadataFormat> getConvertFormats(ServiceContext context) throws IO
@SuppressWarnings("unchecked")
List<Element> defaultSchemas = elem.getChildren();

List<MetadataFormat> defMdfs = new ArrayList<MetadataFormat>();
List<MetadataFormat> defMdfs = new ArrayList<>();
for (Element schema : defaultSchemas) {
defMdfs.add(new MetadataFormat(schema.getAttributeValue("prefix"), schema.getAttributeValue("schemaLocation"), schema.getAttributeValue("nsUrl")));
}
Expand Down
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-2023 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 @@ -98,9 +98,7 @@ private Record buildRecord(ServiceContext context, int id, String prefix) throws
// and we do not want to stop because of one error
try {
return GetRecord.buildRecordStat(context, (Specification<Metadata>)hasMetadataId(id), prefix);
} catch (IdDoesNotExistException e) {
return null;
} catch (CannotDisseminateFormatException e2) {
} catch (IdDoesNotExistException | CannotDisseminateFormatException e) {
return null;
} catch (Exception e3) {
throw e3;
Expand Down
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-2023 Food and Agriculture Organization of the
//=== United Nations (FAO-UN), United Nations World Food Programme (WFP)
//=== and United Nations Environment Programme (UNEP)
//===
Expand All @@ -25,7 +25,6 @@

import jeeves.server.context.ServiceContext;

import org.fao.geonet.constants.Geonet;
import org.fao.geonet.domain.MetadataCategory;
import org.fao.geonet.kernel.oaipmh.OaiPmhService;
import org.fao.geonet.repository.MetadataCategoryRepository;
Expand All @@ -35,7 +34,6 @@
import org.fao.oaipmh.responses.AbstractResponse;
import org.fao.oaipmh.responses.ListSetsResponse;
import org.fao.oaipmh.responses.SetInfo;
import org.jdom.Element;

import java.util.List;

Expand Down

0 comments on commit 4c4943e

Please sign in to comment.