Skip to content

Commit

Permalink
Merge pull request DSpace#9747 from saschaszott/patch-45
Browse files Browse the repository at this point in the history
Refactoring of OpenSearchController / SyndicationFeed: remove Map dependency
  • Loading branch information
tdonohue authored Nov 14, 2024
2 parents eb07d45 + 58c9bfd commit 3ff173a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.rometools.modules.opensearch.OpenSearchModule;
import com.rometools.modules.opensearch.entity.OSQuery;
Expand Down Expand Up @@ -58,12 +57,12 @@ public class OpenSearchServiceImpl implements OpenSearchService {
private static final Logger log = org.apache.logging.log4j.LogManager.getLogger(OpenSearchServiceImpl.class);

// Namespaces used
protected final String osNs = "http://a9.com/-/spec/opensearch/1.1/";
protected final static String osNs = "http://a9.com/-/spec/opensearch/1.1/";

@Autowired(required = true)
@Autowired
protected ConfigurationService configurationService;

@Autowired(required = true)
@Autowired
protected HandleService handleService;

protected OpenSearchServiceImpl() {
Expand Down Expand Up @@ -119,11 +118,10 @@ public String getDescription(String scope) {

@Override
public String getResultsString(Context context, String format, String query, int totalResults, int start,
int pageSize,
IndexableObject scope, List<IndexableObject> results,
Map<String, String> labels) throws IOException {
int pageSize, IndexableObject scope, List<IndexableObject> results)
throws IOException {
try {
return getResults(context, format, query, totalResults, start, pageSize, scope, results, labels)
return getResults(context, format, query, totalResults, start, pageSize, scope, results)
.outputString();
} catch (FeedException e) {
log.error(e.toString(), e);
Expand All @@ -133,11 +131,10 @@ public String getResultsString(Context context, String format, String query, int

@Override
public Document getResultsDoc(Context context, String format, String query, int totalResults, int start,
int pageSize,
IndexableObject scope, List<IndexableObject> results, Map<String, String> labels)
int pageSize, IndexableObject scope, List<IndexableObject> results)
throws IOException {
try {
return getResults(context, format, query, totalResults, start, pageSize, scope, results, labels)
return getResults(context, format, query, totalResults, start, pageSize, scope, results)
.outputW3CDom();
} catch (FeedException e) {
log.error(e.toString(), e);
Expand All @@ -146,8 +143,7 @@ public Document getResultsDoc(Context context, String format, String query, int
}

protected SyndicationFeed getResults(Context context, String format, String query, int totalResults, int start,
int pageSize, IndexableObject scope,
List<IndexableObject> results, Map<String, String> labels) {
int pageSize, IndexableObject scope, List<IndexableObject> results) {
// Encode results in requested format
if ("rss".equals(format)) {
format = "rss_2.0";
Expand All @@ -156,7 +152,7 @@ protected SyndicationFeed getResults(Context context, String format, String quer
}

SyndicationFeed feed = new SyndicationFeed();
feed.populate(null, context, scope, results, labels);
feed.populate(null, context, scope, results);
feed.setType(format);
feed.addModule(openSearchMarkup(query, totalResults, start, pageSize));
return feed;
Expand Down
38 changes: 20 additions & 18 deletions dspace-api/src/main/java/org/dspace/app/util/SyndicationFeed.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -135,8 +136,6 @@ public class SyndicationFeed {
protected String[] podcastableMIMETypes =
configurationService.getArrayProperty("webui.feed.podcast.mimetypes", new String[] {"audio/x-mpeg"});

// -------- Instance variables:

// the feed object we are building
protected SyndFeed feed = null;

Expand All @@ -146,9 +145,6 @@ public class SyndicationFeed {
protected CommunityService communityService;
protected ItemService itemService;

/**
* Constructor.
*/
public SyndicationFeed() {
feed = new SyndFeedImpl();
ContentServiceFactory contentServiceFactory = ContentServiceFactory.getInstance();
Expand All @@ -157,32 +153,24 @@ public SyndicationFeed() {
communityService = contentServiceFactory.getCommunityService();
}

/**
* Returns list of metadata selectors used to compose the description element
*
* @return selector list - format 'schema.element[.qualifier]'
*/
public static String[] getDescriptionSelectors() {
return (String[]) ArrayUtils.clone(descriptionFields);
}


/**
* Fills in the feed and entry-level metadata from DSpace objects.
*
* @param request request
* @param context context
* @param dso the scope
* @param items array of objects
* @param labels label map
*/
public void populate(HttpServletRequest request, Context context, IndexableObject dso,
List<IndexableObject> items, Map<String, String> labels) {
List<IndexableObject> items) {
String logoURL = null;
String objectURL = null;
String defaultTitle = null;
boolean podcastFeed = false;
this.request = request;

Map<String, String> labels = getLabels();

// dso is null for the whole site, or a search without scope
if (dso == null) {
defaultTitle = configurationService.getProperty("dspace.name");
Expand Down Expand Up @@ -553,5 +541,19 @@ protected String getOneDC(Item item, String field) {
List<MetadataValue> dcv = itemService.getMetadataByMetadataString(item, field);
return (dcv.size() > 0) ? dcv.get(0).getValue() : null;
}
}

/**
* Internal method to get labels for the returned document
*/
private Map<String, String> getLabels() {
// TODO: get strings from translation file or configuration
Map<String, String> labelMap = new HashMap<>();
labelMap.put(SyndicationFeed.MSG_UNTITLED, "notitle");
labelMap.put(SyndicationFeed.MSG_LOGO_TITLE, "logo.title");
labelMap.put(SyndicationFeed.MSG_FEED_DESCRIPTION, "general-feed.description");
for (String selector : descriptionFields) {
labelMap.put("metadata." + selector, selector);
}
return labelMap;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.dspace.content.DSpaceObject;
import org.dspace.core.Context;
Expand Down Expand Up @@ -86,14 +85,12 @@ public interface OpenSearchService {
* @param pageSize - page size
* @param scope - search scope, null or the community/collection
* @param results the retrieved DSpace objects satisfying search
* @param labels labels to apply - format specific
* @return formatted search results
* @throws IOException if IO error
*/
public String getResultsString(Context context, String format, String query, int totalResults, int start,
int pageSize,
IndexableObject scope, List<IndexableObject> results,
Map<String, String> labels) throws IOException;
int pageSize, IndexableObject scope, List<IndexableObject> results)
throws IOException;

/**
* Returns a formatted set of search results as a document
Expand All @@ -106,13 +103,11 @@ public String getResultsString(Context context, String format, String query, int
* @param pageSize - page size
* @param scope - search scope, null or the community/collection
* @param results the retrieved DSpace objects satisfying search
* @param labels labels to apply - format specific
* @return formatted search results
* @throws IOException if IO error
*/
public Document getResultsDoc(Context context, String format, String query, int totalResults, int start,
int pageSize,
IndexableObject scope, List<IndexableObject> results, Map<String, String> labels)
int pageSize, IndexableObject scope, List<IndexableObject> results)
throws IOException;

public DSpaceObject resolveScope(Context context, String scope) throws SQLException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
Expand All @@ -24,7 +22,6 @@
import org.apache.logging.log4j.Logger;
import org.dspace.app.rest.utils.ContextUtil;
import org.dspace.app.rest.utils.ScopeResolver;
import org.dspace.app.util.SyndicationFeed;
import org.dspace.app.util.factory.UtilServiceFactory;
import org.dspace.app.util.service.OpenSearchService;
import org.dspace.authorize.factory.AuthorizeServiceFactory;
Expand Down Expand Up @@ -200,12 +197,10 @@ public void search(HttpServletRequest request,
log.info("opensearch done, query=\"" + query + "\",results="
+ qResults.getTotalSearchResults());

// format and return results
Map<String, String> labelMap = getLabels(request);
List<IndexableObject> dsoResults = qResults.getIndexableObjects();
Document resultsDoc = openSearchService.getResultsDoc(context, format, query,
(int) qResults.getTotalSearchResults(), qResults.getStart(),
qResults.getMaxResults(), container, dsoResults, labelMap);
qResults.getMaxResults(), container, dsoResults);
try {
Transformer xf = TransformerFactory.newInstance().newTransformer();
response.setContentType(openSearchService.getContentType(format));
Expand Down Expand Up @@ -274,20 +269,4 @@ private void init() {
public void setOpenSearchService(OpenSearchService oSS) {
openSearchService = oSS;
}


/**
* Internal method to get labels for the returned document
*/
private Map<String, String> getLabels(HttpServletRequest request) {
// TODO: get strings from translation file or configuration
Map<String, String> labelMap = new HashMap<String, String>();
labelMap.put(SyndicationFeed.MSG_UNTITLED, "notitle");
labelMap.put(SyndicationFeed.MSG_LOGO_TITLE, "logo.title");
labelMap.put(SyndicationFeed.MSG_FEED_DESCRIPTION, "general-feed.description");
for (String selector : SyndicationFeed.getDescriptionSelectors()) {
labelMap.put("metadata." + selector, selector);
}
return labelMap;
}
}

0 comments on commit 3ff173a

Please sign in to comment.