Search in sources :

Example 6 with PagePermission

use of org.apache.wiki.auth.permissions.PagePermission in project jspwiki by apache.

the class RSSGenerator method generateFullWikiRSS.

/**
 *  Generates an RSS feed for the entire wiki.  Each item should be an instance of the RSSItem class.
 *
 *  @param wikiContext A WikiContext
 *  @param feed A Feed to generate the feed to.
 *  @return feed.getString().
 */
protected String generateFullWikiRSS(WikiContext wikiContext, Feed feed) {
    feed.setChannelTitle(m_engine.getApplicationName());
    feed.setFeedURL(m_engine.getBaseURL());
    feed.setChannelLanguage(m_channelLanguage);
    feed.setChannelDescription(m_channelDescription);
    Collection changed = m_engine.getRecentChanges();
    WikiSession session = WikiSession.guestSession(m_engine);
    int items = 0;
    for (Iterator i = changed.iterator(); i.hasNext() && items < 15; items++) {
        WikiPage page = (WikiPage) i.next();
        if (!m_engine.getAuthorizationManager().checkPermission(session, new PagePermission(page, PagePermission.VIEW_ACTION))) {
            // No permission, skip to the next one.
            continue;
        }
        Entry e = new Entry();
        e.setPage(page);
        String url;
        if (page instanceof Attachment) {
            url = m_engine.getURL(WikiContext.ATTACH, page.getName(), null, true);
        } else {
            url = m_engine.getURL(WikiContext.VIEW, page.getName(), null, true);
        }
        e.setURL(url);
        e.setTitle(page.getName());
        e.setContent(getEntryDescription(page));
        e.setAuthor(getAuthor(page));
        feed.addEntry(e);
    }
    return feed.getString();
}
Also used : WikiSession(org.apache.wiki.WikiSession) WikiPage(org.apache.wiki.WikiPage) Iterator(java.util.Iterator) Collection(java.util.Collection) Attachment(org.apache.wiki.attachment.Attachment) PagePermission(org.apache.wiki.auth.permissions.PagePermission)

Example 7 with PagePermission

use of org.apache.wiki.auth.permissions.PagePermission in project jspwiki by apache.

the class LuceneSearchProvider method findPages.

/**
 *  Searches pages using a particular combination of flags.
 *
 *  @param query The query to perform in Lucene query language
 *  @param flags A set of flags
 *  @return A Collection of SearchResult instances
 *  @throws ProviderException if there is a problem with the backend
 */
public Collection findPages(String query, int flags, WikiContext wikiContext) throws ProviderException {
    IndexSearcher searcher = null;
    ArrayList<SearchResult> list = null;
    Highlighter highlighter = null;
    try {
        String[] queryfields = { LUCENE_PAGE_CONTENTS, LUCENE_PAGE_NAME, LUCENE_AUTHOR, LUCENE_ATTACHMENTS };
        QueryParser qp = new MultiFieldQueryParser(Version.LUCENE_47, queryfields, getLuceneAnalyzer());
        // QueryParser qp = new QueryParser( LUCENE_PAGE_CONTENTS, getLuceneAnalyzer() );
        Query luceneQuery = qp.parse(query);
        if ((flags & FLAG_CONTEXTS) != 0) {
            highlighter = new Highlighter(new SimpleHTMLFormatter("<span class=\"searchmatch\">", "</span>"), new SimpleHTMLEncoder(), new QueryScorer(luceneQuery));
        }
        try {
            File dir = new File(m_luceneDirectory);
            Directory luceneDir = new SimpleFSDirectory(dir, null);
            IndexReader reader = DirectoryReader.open(luceneDir);
            searcher = new IndexSearcher(reader);
        } catch (Exception ex) {
            log.info("Lucene not yet ready; indexing not started", ex);
            return null;
        }
        ScoreDoc[] hits = searcher.search(luceneQuery, MAX_SEARCH_HITS).scoreDocs;
        AuthorizationManager mgr = m_engine.getAuthorizationManager();
        list = new ArrayList<SearchResult>(hits.length);
        for (int curr = 0; curr < hits.length; curr++) {
            int docID = hits[curr].doc;
            Document doc = searcher.doc(docID);
            String pageName = doc.get(LUCENE_ID);
            WikiPage page = m_engine.getPage(pageName, WikiPageProvider.LATEST_VERSION);
            if (page != null) {
                if (page instanceof Attachment) {
                // Currently attachments don't look nice on the search-results page
                // When the search-results are cleaned up this can be enabled again.
                }
                PagePermission pp = new PagePermission(page, PagePermission.VIEW_ACTION);
                if (mgr.checkPermission(wikiContext.getWikiSession(), pp)) {
                    int score = (int) (hits[curr].score * 100);
                    // Get highlighted search contexts
                    String text = doc.get(LUCENE_PAGE_CONTENTS);
                    String[] fragments = new String[0];
                    if (text != null && highlighter != null) {
                        TokenStream tokenStream = getLuceneAnalyzer().tokenStream(LUCENE_PAGE_CONTENTS, new StringReader(text));
                        fragments = highlighter.getBestFragments(tokenStream, text, MAX_FRAGMENTS);
                    }
                    SearchResult result = new SearchResultImpl(page, score, fragments);
                    list.add(result);
                }
            } else {
                log.error("Lucene found a result page '" + pageName + "' that could not be loaded, removing from Lucene cache");
                pageRemoved(new WikiPage(m_engine, pageName));
            }
        }
    } catch (IOException e) {
        log.error("Failed during lucene search", e);
    } catch (ParseException e) {
        log.info("Broken query; cannot parse query ", e);
        throw new ProviderException("You have entered a query Lucene cannot process: " + e.getMessage());
    } catch (InvalidTokenOffsetsException e) {
        log.error("Tokens are incompatible with provided text ", e);
    } finally {
        if (searcher != null) {
            try {
                searcher.getIndexReader().close();
            } catch (IOException e) {
                log.error(e);
            }
        }
    }
    return list;
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TokenStream(org.apache.lucene.analysis.TokenStream) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) ProviderException(org.apache.wiki.api.exceptions.ProviderException) WikiPage(org.apache.wiki.WikiPage) Attachment(org.apache.wiki.attachment.Attachment) Document(org.apache.lucene.document.Document) ScoreDoc(org.apache.lucene.search.ScoreDoc) InvalidTokenOffsetsException(org.apache.lucene.search.highlight.InvalidTokenOffsetsException) StringReader(java.io.StringReader) Highlighter(org.apache.lucene.search.highlight.Highlighter) Directory(org.apache.lucene.store.Directory) SimpleFSDirectory(org.apache.lucene.store.SimpleFSDirectory) SimpleHTMLEncoder(org.apache.lucene.search.highlight.SimpleHTMLEncoder) MultiFieldQueryParser(org.apache.lucene.queryparser.classic.MultiFieldQueryParser) QueryScorer(org.apache.lucene.search.highlight.QueryScorer) IOException(java.io.IOException) SimpleFSDirectory(org.apache.lucene.store.SimpleFSDirectory) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) NoRequiredPropertyException(org.apache.wiki.api.exceptions.NoRequiredPropertyException) InternalWikiException(org.apache.wiki.InternalWikiException) ParseException(org.apache.lucene.queryparser.classic.ParseException) LockObtainFailedException(org.apache.lucene.store.LockObtainFailedException) InvalidTokenOffsetsException(org.apache.lucene.search.highlight.InvalidTokenOffsetsException) IOException(java.io.IOException) ProviderException(org.apache.wiki.api.exceptions.ProviderException) MultiFieldQueryParser(org.apache.lucene.queryparser.classic.MultiFieldQueryParser) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) IndexReader(org.apache.lucene.index.IndexReader) AuthorizationManager(org.apache.wiki.auth.AuthorizationManager) ParseException(org.apache.lucene.queryparser.classic.ParseException) SimpleHTMLFormatter(org.apache.lucene.search.highlight.SimpleHTMLFormatter) File(java.io.File) PagePermission(org.apache.wiki.auth.permissions.PagePermission)

Aggregations

PagePermission (org.apache.wiki.auth.permissions.PagePermission)7 WikiPage (org.apache.wiki.WikiPage)5 WikiSession (org.apache.wiki.WikiSession)3 ProviderException (org.apache.wiki.api.exceptions.ProviderException)3 AuthorizationManager (org.apache.wiki.auth.AuthorizationManager)3 AllPermission (org.apache.wiki.auth.permissions.AllPermission)3 IOException (java.io.IOException)2 Principal (java.security.Principal)2 Collection (java.util.Collection)2 Iterator (java.util.Iterator)2 WikiSessionTest (org.apache.wiki.WikiSessionTest)2 Attachment (org.apache.wiki.attachment.Attachment)2 Test (org.junit.Test)2 File (java.io.File)1 StringReader (java.io.StringReader)1 Permission (java.security.Permission)1 DateFormat (java.text.DateFormat)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Calendar (java.util.Calendar)1