Search in sources :

Example 16 with WikiContext

use of org.apache.wiki.WikiContext in project jspwiki by apache.

the class SearchResultIteratorTag method nextResult.

private int nextResult() {
    if (m_iterator != null && m_iterator.hasNext() && m_count++ < m_maxItems) {
        SearchResult r = (SearchResult) m_iterator.next();
        // Create a wiki context for the result
        WikiEngine engine = m_wikiContext.getEngine();
        HttpServletRequest request = m_wikiContext.getHttpRequest();
        Command command = PageCommand.VIEW.targetedCommand(r.getPage());
        WikiContext context = new WikiContext(engine, request, command);
        // Stash it in the page context
        pageContext.setAttribute(WikiTagBase.ATTR_CONTEXT, context, PageContext.REQUEST_SCOPE);
        pageContext.setAttribute(getId(), r);
        return EVAL_BODY_BUFFERED;
    }
    return SKIP_BODY;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) WikiContext(org.apache.wiki.WikiContext) Command(org.apache.wiki.ui.Command) PageCommand(org.apache.wiki.ui.PageCommand) SearchResult(org.apache.wiki.search.SearchResult) WikiEngine(org.apache.wiki.WikiEngine)

Example 17 with WikiContext

use of org.apache.wiki.WikiContext in project jspwiki by apache.

the class AttachmentServlet method doGet.

/**
 *  Serves a GET with two parameters: 'wikiname' specifying the wikiname
 *  of the attachment, 'version' specifying the version indicator.
 */
// FIXME: Messages would need to be localized somehow.
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
    WikiContext context = m_engine.createContext(req, WikiContext.ATTACH);
    String version = req.getParameter(HDR_VERSION);
    String nextPage = req.getParameter("nextpage");
    String msg = "An error occurred. Ouch.";
    int ver = WikiProvider.LATEST_VERSION;
    AttachmentManager mgr = m_engine.getAttachmentManager();
    AuthorizationManager authmgr = m_engine.getAuthorizationManager();
    String page = context.getPage().getName();
    if (page == null) {
        log.info("Invalid attachment name.");
        res.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return;
    }
    OutputStream out = null;
    InputStream in = null;
    try {
        log.debug("Attempting to download att " + page + ", version " + version);
        if (version != null) {
            ver = Integer.parseInt(version);
        }
        Attachment att = mgr.getAttachmentInfo(page, ver);
        if (att != null) {
            // 
            // Check if the user has permission for this attachment
            // 
            Permission permission = PermissionFactory.getPagePermission(att, "view");
            if (!authmgr.checkPermission(context.getWikiSession(), permission)) {
                log.debug("User does not have permission for this");
                res.sendError(HttpServletResponse.SC_FORBIDDEN);
                return;
            }
            // 
            if (HttpUtil.checkFor304(req, att.getName(), att.getLastModified())) {
                log.debug("Client has latest version already, sending 304...");
                res.sendError(HttpServletResponse.SC_NOT_MODIFIED);
                return;
            }
            String mimetype = getMimeType(context, att.getFileName());
            res.setContentType(mimetype);
            // 
            // We use 'inline' instead of 'attachment' so that user agents
            // can try to automatically open the file.
            // 
            res.addHeader("Content-Disposition", "inline; filename=\"" + att.getFileName() + "\";");
            res.addDateHeader("Last-Modified", att.getLastModified().getTime());
            if (!att.isCacheable()) {
                res.addHeader("Pragma", "no-cache");
                res.addHeader("Cache-control", "no-cache");
            }
            // If a size is provided by the provider, report it.
            if (att.getSize() >= 0) {
                // log.info("size:"+att.getSize());
                res.setContentLength((int) att.getSize());
            }
            out = res.getOutputStream();
            in = mgr.getAttachmentStream(context, att);
            int read = 0;
            byte[] buffer = new byte[BUFFER_SIZE];
            while ((read = in.read(buffer)) > -1) {
                out.write(buffer, 0, read);
            }
            if (log.isDebugEnabled()) {
                msg = "Attachment " + att.getFileName() + " sent to " + req.getRemoteUser() + " on " + HttpUtil.getRemoteAddress(req);
                log.debug(msg);
            }
            if (nextPage != null) {
                res.sendRedirect(validateNextPage(nextPage, m_engine.getURL(WikiContext.ERROR, "", null, false)));
            }
        } else {
            msg = "Attachment '" + page + "', version " + ver + " does not exist.";
            log.info(msg);
            res.sendError(HttpServletResponse.SC_NOT_FOUND, msg);
        }
    } catch (ProviderException pe) {
        msg = "Provider error: " + pe.getMessage();
        log.debug("Provider failed while reading", pe);
        // 
        try {
            res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg);
        } catch (IllegalStateException e) {
        }
    } catch (NumberFormatException nfe) {
        log.warn("Invalid version number: " + version);
        res.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid version number");
    } catch (SocketException se) {
        // 
        // These are very common in download situations due to aggressive
        // clients.  No need to try and send an error.
        // 
        log.debug("I/O exception during download", se);
    } catch (IOException ioe) {
        // 
        // Client dropped the connection or something else happened.
        // We don't know where the error came from, so we'll at least
        // try to send an error and catch it quietly if it doesn't quite work.
        // 
        msg = "Error: " + ioe.getMessage();
        log.debug("I/O exception during download", ioe);
        try {
            res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg);
        } catch (IllegalStateException e) {
        }
    } finally {
        IOUtils.closeQuietly(in);
        // 
        // Quite often, aggressive clients close the connection when they have
        // received the last bits.  Therefore, we close the output, but ignore
        // any exception that might come out of it.
        // 
        IOUtils.closeQuietly(out);
    }
}
Also used : SocketException(java.net.SocketException) WikiContext(org.apache.wiki.WikiContext) ProviderException(org.apache.wiki.api.exceptions.ProviderException) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) Permission(java.security.Permission) AuthorizationManager(org.apache.wiki.auth.AuthorizationManager)

Example 18 with WikiContext

use of org.apache.wiki.WikiContext in project jspwiki by apache.

the class MarkdownRendererTest method translate.

String translate(final WikiEngine e, final WikiPage p, final String src) throws Exception {
    WikiContext context = new WikiContext(e, testEngine.newHttpRequest(), p);
    MarkdownParser tr = new MarkdownParser(context, new BufferedReader(new StringReader(src)));
    MarkdownRenderer conv = new MarkdownRenderer(context, tr.parse());
    newPage(p.getName(), src);
    return conv.getString();
}
Also used : WikiContext(org.apache.wiki.WikiContext) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) MarkdownParser(org.apache.wiki.parser.markdown.MarkdownParser) MarkdownRenderer(org.apache.wiki.render.markdown.MarkdownRenderer)

Example 19 with WikiContext

use of org.apache.wiki.WikiContext in project jspwiki by apache.

the class JSPWikiMarkupParserTest method testCollectingLinksAttachment.

@Test
public void testCollectingLinksAttachment() throws Exception {
    try {
        testEngine.saveText(PAGE_NAME, "content");
        Attachment att = new Attachment(testEngine, PAGE_NAME, "TestAtt.txt");
        att.setAuthor("FirstPost");
        testEngine.getAttachmentManager().storeAttachment(att, testEngine.makeAttachmentFile());
        LinkCollector coll = new LinkCollector();
        LinkCollector coll_others = new LinkCollector();
        String src = "[TestAtt.txt]";
        WikiContext context = new WikiContext(testEngine, new WikiPage(testEngine, PAGE_NAME));
        MarkupParser p = new JSPWikiMarkupParser(context, new BufferedReader(new StringReader(src)));
        p.addLocalLinkHook(coll_others);
        p.addExternalLinkHook(coll_others);
        p.addAttachmentLinkHook(coll);
        p.parse();
        Collection<String> links = coll.getLinks();
        Assert.assertEquals("no links found", 1, links.size());
        Assert.assertEquals("wrong link", PAGE_NAME + "/TestAtt.txt", links.iterator().next());
        Assert.assertEquals("wrong links found", 0, coll_others.getLinks().size());
    } finally {
        String files = testEngine.getWikiProperties().getProperty(BasicAttachmentProvider.PROP_STORAGEDIR);
        File storagedir = new File(files, PAGE_NAME + BasicAttachmentProvider.DIR_EXTENSION);
        if (storagedir.exists() && storagedir.isDirectory())
            TestEngine.deleteAll(storagedir);
    }
}
Also used : WikiContext(org.apache.wiki.WikiContext) WikiPage(org.apache.wiki.WikiPage) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) Attachment(org.apache.wiki.attachment.Attachment) LinkCollector(org.apache.wiki.LinkCollector) File(java.io.File) Test(org.junit.Test)

Example 20 with WikiContext

use of org.apache.wiki.WikiContext in project jspwiki by apache.

the class JSPWikiMarkupParserTest method translate_nofollow.

private String translate_nofollow(String src) throws IOException, NoRequiredPropertyException, ServletException, WikiException {
    props = TestEngine.getTestProperties();
    props.setProperty("jspwiki.translatorReader.useRelNofollow", "true");
    TestEngine testEngine2 = new TestEngine(props);
    WikiContext context = new WikiContext(testEngine2, new WikiPage(testEngine2, PAGE_NAME));
    JSPWikiMarkupParser r = new JSPWikiMarkupParser(context, new BufferedReader(new StringReader(src)));
    XHTMLRenderer conv = new XHTMLRenderer(context, r.parse());
    return conv.getString();
}
Also used : WikiContext(org.apache.wiki.WikiContext) WikiPage(org.apache.wiki.WikiPage) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) XHTMLRenderer(org.apache.wiki.render.XHTMLRenderer) TestEngine(org.apache.wiki.TestEngine)

Aggregations

WikiContext (org.apache.wiki.WikiContext)90 WikiPage (org.apache.wiki.WikiPage)63 Test (org.junit.Test)40 TestEngine (org.apache.wiki.TestEngine)11 StringReader (java.io.StringReader)9 WikiEngine (org.apache.wiki.WikiEngine)9 ProviderException (org.apache.wiki.api.exceptions.ProviderException)8 IOException (java.io.IOException)7 InputStream (java.io.InputStream)7 Before (org.junit.Before)7 BufferedReader (java.io.BufferedReader)6 StringWriter (java.io.StringWriter)6 Collection (java.util.Collection)6 InputStreamReader (java.io.InputStreamReader)5 Date (java.util.Date)5 LinkCollector (org.apache.wiki.LinkCollector)5 Attachment (org.apache.wiki.attachment.Attachment)5 Properties (java.util.Properties)4 MockHttpServletRequest (net.sourceforge.stripes.mock.MockHttpServletRequest)4 WikiDocument (org.apache.wiki.parser.WikiDocument)4