protected void printEndTag(PageRequest request, PageResponse response,
Document hit, int hitIndex)
throws RegainException
{
// Get the search results
SearchResults results = SearchToolkit.getSearchResults(request);
boolean shouldHighlight = results.getShouldHighlight(hitIndex);
String url = results.getHitUrl(hitIndex);
String fieldName = (shouldHighlight) ? "highlightedTitle" : "title";
String title = hit.get(fieldName);
if (shouldHighlight && (title == null || title.length() == 0)) {
title = hit.get("title");
}
boolean openInNewWindow = results.getOpenHitInNewWindow(hitIndex);
// Trim the title
if (title != null) {
title = title.trim();
}
// Use the URL as title if there is no title.
if ((title == null) || (title.length() == 0)) {
int lastSlash = url.lastIndexOf("/");
if (lastSlash == -1) {
title = url;
} else {
title = RegainToolkit.urlDecode(url.substring(lastSlash + 1), RegainToolkit.INDEX_ENCODING);
}
}
// Pass file URLs to the file servlet
String href = url;
String encoding = response.getEncoding();
boolean useFileToHttpBridge = results.getUseFileToHttpBridgeForHit(hitIndex);
if (url.startsWith("file://") && useFileToHttpBridge) {
// Create a URL that targets the file-to-http-bridge
// NOTE: This is the counterpart to SearchToolkit.extractFileUrl
// Get the file name
String fileName = RegainToolkit.urlToFileName(url);
// Workaround: Double slashes have to be prevented, because tomcat
// merges two slashes to one (even if one of them is URL-encoded and even
// if one of them is a backslash or an encoded backslash)
// -> We escape the second slashe with "$/$" and normal "$" with "$$"
// (This should work in all cases: "a//b" -> "a/$/$b",
// "a///b" -> "a/$/$/b", "a$b" -> "a$$b", "a$/$b" -> "a$$/$$b")
String decodedHref = RegainToolkit.replace("file/" + fileName,
new String[] {"//", "$"},
new String[] {"/$/$", "$$"});
// Create a URL (encoded with the page encoding)
href = RegainToolkit.urlEncode(decodedHref, encoding);
// Now decode the forward slashes
// NOTE: This step is only for beautifing the URL, the above workaround is
// also nessesary without this step
href = RegainToolkit.replace(href, "%2F", "/");
// Add the index name
// NOTE: This is needed to ensure that only documents can be loaded that
// are in the indexes
String indexName = results.getHitIndexName(hitIndex);
String encodedIndexName = RegainToolkit.urlEncode(indexName, encoding);
// @todo: refactor this
//href += "?index=" + encodedIndexName;
} else {
href = RegainToolkit.urlDecode(url, RegainToolkit.INDEX_ENCODING);