📄 securefiltersearchable.java
字号:
/* -*- mode:java; indent-tabs-mode:nil; c-basic-offset:2 -*- * * $RCSFile$ $Revision: 1.5 $ $Date: 2006/01/25 16:56:21 $ * * Copyright (c) 2004 Autonomy Corp. All Rights Reserved. * Permission to use, copy, modify, and distribute this file is hereby * granted without fee, provided that the above copyright notice appear * in all copies. */import java.io.IOException;import java.util.ArrayList;import java.lang.Math;import com.ultraseek.xpa.search.*;/** * Silently removes disallowed <code>SearchResult</code>s from a * <code>SearchResultList</code>. * <p> * <code>GuardingSearchable</code> throws a <code>SecurityException</code> * for each disallowed <code>SearchResult</code>. * This enables your application to take appropriate action for * disallowed results. * <p> * Possible actions your application can take are: * <ul> * <li>Totally ignore the disallowed <code>SearchResult</code>. * <li>Display a message ("You must login to view all results"). * <li>Display a filtered version of the result (title only, no URL, etc.) * <li>Display the result with a "lock" icon. * </ul> * <p> * This <code>Searchable</code> ignores disallowed results from * <code>GuardingSearchable</code> by silently removing them * from the <code>SearchResultList</code>. * <p> * Note that result counts are dynamic and will change until the entire * search result list is processed. * <p> * This sample also demonstrates how to implement a <code>Searchable</code> that * performs filtering on a <code>SeachResultList</code>. * * @since XPA2.2 */public class SecureFilterSearchable extends SearchableWrapper{ public SecureFilterSearchable(Searchable base) { super(base); } public SearchResultList search(Query q) throws QueryNotSupportedException, IOException { return new ResultList(getSearchable().search(q)); } public class ResultList extends SearchResultListWrapper { private ArrayList allowedResults = new ArrayList(); // Results that are allowed private int baseResultsRead = 0; // How far we've read private int disallowedCount = 0; // how many were disallowed // NOTE: baseResultsRead == allowedResults.size() + disallowedResults public ResultList(SearchResultList base) { super(base); } public synchronized Object get(int n) { // Fetch results until we find the n'th allowed result. while ((n + 1) > allowedResults.size()) { int cursor = baseResultsRead++; try { Object res = baseResultList.get(cursor); allowedResults.add(res); } catch (SecurityException se) { disallowedCount++; } } return allowedResults.get(n); } public int size() { return (Math.max(baseResultList.size() - baseResultsRead,0) + allowedResults.size()); } public int getResultCount() throws IOException { return (Math.max(baseResultList.getResultCount() - baseResultsRead,0) + allowedResults.size()); } public int getDisallowedSize() { return disallowedCount; } public int getAllowedSize() { return allowedResults.size(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -