Search in sources :

Example 41 with Context

use of org.apache.wiki.api.core.Context in project jspwiki by apache.

the class SpamFilter method insertInputFields.

/**
 * This helper method adds all the input fields to your editor that the SpamFilter requires
 * to check for spam.  This <i>must</i> be in your editor form if you intend to use the SpamFilter.
 *
 * @param pageContext The PageContext
 * @return A HTML string which contains input fields for the SpamFilter.
 */
public static String insertInputFields(final PageContext pageContext) {
    final Context ctx = Context.findContext(pageContext);
    final Engine engine = ctx.getEngine();
    final StringBuilder sb = new StringBuilder();
    if (engine.getContentEncoding().equals(StandardCharsets.UTF_8)) {
        sb.append("<input name='encodingcheck' type='hidden' value='\u3041' />\n");
    }
    return sb.toString();
}
Also used : Context(org.apache.wiki.api.core.Context) PageContext(javax.servlet.jsp.PageContext) Engine(org.apache.wiki.api.core.Engine)

Example 42 with Context

use of org.apache.wiki.api.core.Context in project jspwiki by apache.

the class DefaultTemplateManager method listTimeFormats.

/**
 * {@inheritDoc}
 */
@Override
public Map<String, String> listTimeFormats(final PageContext pageContext) {
    final Context context = Context.findContext(pageContext);
    final Properties props = m_engine.getWikiProperties();
    final ArrayList<String> tfArr = new ArrayList<>(40);
    final LinkedHashMap<String, String> resultMap = new LinkedHashMap<>();
    /* filter timeformat properties */
    for (final Enumeration<?> e = props.propertyNames(); e.hasMoreElements(); ) {
        final String name = (String) e.nextElement();
        if (name.startsWith(TIMEFORMATPROPERTIES)) {
            tfArr.add(name);
        }
    }
    /* fetch actual formats */
    if (tfArr.size() == 0) {
        /* no props found - make sure some default formats are avail */
        tfArr.add("dd-MMM-yy");
        tfArr.add("d-MMM-yyyy");
        tfArr.add("EEE, dd-MMM-yyyy, zzzz");
    } else {
        Collections.sort(tfArr);
        for (int i = 0; i < tfArr.size(); i++) {
            tfArr.set(i, props.getProperty(tfArr.get(i)));
        }
    }
    final String prefTimeZone = Preferences.getPreference(context, "TimeZone");
    final TimeZone tz = TimeZone.getTimeZone(prefTimeZone);
    // current date
    final Date d = new Date();
    try {
        // dummy format pattern
        final SimpleDateFormat fmt = Preferences.getDateFormat(context, TimeFormat.DATETIME);
        fmt.setTimeZone(tz);
        for (final String s : tfArr) {
            try {
                fmt.applyPattern(s);
                resultMap.put(s, fmt.format(d));
            } catch (final IllegalArgumentException e) {
            }
        // skip parameter
        }
    }// skip parameter
     catch (final IllegalArgumentException e) {
    }
    return resultMap;
}
Also used : PageContext(javax.servlet.jsp.PageContext) Context(org.apache.wiki.api.core.Context) ServletContext(javax.servlet.ServletContext) ArrayList(java.util.ArrayList) Properties(java.util.Properties) Date(java.util.Date) LinkedHashMap(java.util.LinkedHashMap) TimeZone(java.util.TimeZone) SimpleDateFormat(java.text.SimpleDateFormat)

Example 43 with Context

use of org.apache.wiki.api.core.Context in project jspwiki by apache.

the class TranslateTag method doAfterBody.

@Override
public final int doAfterBody() throws JspException {
    try {
        Context context = (Context) pageContext.getAttribute(Context.ATTR_CONTEXT, PageContext.REQUEST_SCOPE);
        // Because the TranslateTag should not affect any of the real page attributes we have to make a clone here.
        context = context.deepClone();
        // Get the page data.
        final BodyContent bc = getBodyContent();
        String wikiText = bc.getString();
        bc.clearBody();
        if (wikiText != null) {
            wikiText = wikiText.trim();
            final String result = context.getEngine().getManager(RenderingManager.class).textToHTML(context, wikiText);
            getPreviousOut().write(result);
        }
    } catch (final Exception e) {
        log.error("Tag failed", e);
        throw new JspException("Tag failed, check logs: " + e.getMessage());
    }
    return SKIP_BODY;
}
Also used : Context(org.apache.wiki.api.core.Context) PageContext(javax.servlet.jsp.PageContext) BodyContent(javax.servlet.jsp.tagext.BodyContent) JspException(javax.servlet.jsp.JspException) RenderingManager(org.apache.wiki.render.RenderingManager) JspException(javax.servlet.jsp.JspException)

Example 44 with Context

use of org.apache.wiki.api.core.Context in project jspwiki by apache.

the class AttachmentServlet method upload.

/**
 *  Uploads a specific mime multipart input set, intercepts exceptions.
 *
 *  @param req The servlet request
 *  @return The page to which we should go next.
 *  @throws RedirectException If there's an error and a redirection is needed
 *  @throws IOException If upload fails
 */
protected String upload(final HttpServletRequest req) throws RedirectException, IOException {
    final String msg;
    final String attName = "(unknown)";
    // If something bad happened, Upload should be able to take care of most stuff
    final String errorPage = m_engine.getURL(ContextEnum.WIKI_ERROR.getRequestContext(), "", null);
    String nextPage = errorPage;
    final String progressId = req.getParameter("progressid");
    // Check that we have a file upload request
    if (!ServletFileUpload.isMultipartContent(req)) {
        throw new RedirectException("Not a file upload", errorPage);
    }
    try {
        final FileItemFactory factory = new DiskFileItemFactory();
        // Create the context _before_ Multipart operations, otherwise strict servlet containers may fail when setting encoding.
        final Context context = Wiki.context().create(m_engine, req, ContextEnum.PAGE_ATTACH.getRequestContext());
        final UploadListener pl = new UploadListener();
        m_engine.getManager(ProgressManager.class).startProgress(pl, progressId);
        final ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setHeaderEncoding(StandardCharsets.UTF_8.name());
        if (!context.hasAdminPermissions()) {
            upload.setFileSizeMax(m_maxSize);
        }
        upload.setProgressListener(pl);
        final List<FileItem> items = upload.parseRequest(req);
        String wikipage = null;
        String changeNote = null;
        // FileItem actualFile = null;
        final List<FileItem> fileItems = new ArrayList<>();
        for (final FileItem item : items) {
            if (item.isFormField()) {
                switch(item.getFieldName()) {
                    case "page":
                        // FIXME: Kludge alert.  We must end up with the parent page name, if this is an upload of a new revision
                        wikipage = item.getString(StandardCharsets.UTF_8.name());
                        final int x = wikipage.indexOf("/");
                        if (x != -1) {
                            wikipage = wikipage.substring(0, x);
                        }
                        break;
                    case "changenote":
                        changeNote = item.getString(StandardCharsets.UTF_8.name());
                        if (changeNote != null) {
                            changeNote = TextUtil.replaceEntities(changeNote);
                        }
                        break;
                    case "nextpage":
                        nextPage = validateNextPage(item.getString(StandardCharsets.UTF_8.name()), errorPage);
                        break;
                }
            } else {
                fileItems.add(item);
            }
        }
        if (fileItems.size() == 0) {
            throw new RedirectException("Broken file upload", errorPage);
        } else {
            for (final FileItem actualFile : fileItems) {
                final String filename = actualFile.getName();
                final long fileSize = actualFile.getSize();
                try (final InputStream in = actualFile.getInputStream()) {
                    executeUpload(context, in, filename, nextPage, wikipage, changeNote, fileSize);
                }
            }
        }
    } catch (final ProviderException e) {
        msg = "Upload failed because the provider failed: " + e.getMessage();
        log.warn(msg + " (attachment: " + attName + ")", e);
        throw new IOException(msg);
    } catch (final IOException e) {
        // Show the submit page again, but with a bit more intimidating output.
        msg = "Upload failure: " + e.getMessage();
        log.warn(msg + " (attachment: " + attName + ")", e);
        throw e;
    } catch (final FileUploadException e) {
        // Show the submit page again, but with a bit more intimidating output.
        msg = "Upload failure: " + e.getMessage();
        log.warn(msg + " (attachment: " + attName + ")", e);
        throw new IOException(msg, e);
    } finally {
        m_engine.getManager(ProgressManager.class).stopProgress(progressId);
    // FIXME: In case of exceptions should absolutely remove the uploaded file.
    }
    return nextPage;
}
Also used : Context(org.apache.wiki.api.core.Context) ServletContext(javax.servlet.ServletContext) ProviderException(org.apache.wiki.api.exceptions.ProviderException) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileItemFactory(org.apache.commons.fileupload.FileItemFactory) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileItem(org.apache.commons.fileupload.FileItem) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) ProgressManager(org.apache.wiki.ui.progress.ProgressManager) FileUploadException(org.apache.commons.fileupload.FileUploadException) RedirectException(org.apache.wiki.api.exceptions.RedirectException)

Example 45 with Context

use of org.apache.wiki.api.core.Context in project jspwiki by apache.

the class RPCHandlerUTF8 method listLinks.

public Vector<Hashtable<String, String>> listLinks(String pagename) throws XmlRpcException {
    pagename = parsePageCheckCondition(pagename);
    final Page page = m_engine.getManager(PageManager.class).getPage(pagename);
    final String pagedata = m_engine.getManager(PageManager.class).getPureText(page);
    final LinkCollector localCollector = new LinkCollector();
    final LinkCollector extCollector = new LinkCollector();
    final LinkCollector attCollector = new LinkCollector();
    final Context context = Wiki.context().create(m_engine, page);
    m_engine.getManager(RenderingManager.class).textToHTML(context, pagedata, localCollector, extCollector, attCollector);
    final Vector<Hashtable<String, String>> result = new Vector<>();
    // 
    for (final String link : localCollector.getLinks()) {
        final Hashtable<String, String> ht = new Hashtable<>();
        ht.put("page", link);
        ht.put("type", LINK_LOCAL);
        if (m_engine.getManager(PageManager.class).wikiPageExists(link)) {
            ht.put("href", context.getViewURL(link));
        } else {
            ht.put("href", context.getURL(ContextEnum.PAGE_EDIT.getRequestContext(), link));
        }
        result.add(ht);
    }
    // 
    for (final String link : attCollector.getLinks()) {
        final Hashtable<String, String> ht = new Hashtable<>();
        ht.put("page", link);
        ht.put("type", LINK_LOCAL);
        ht.put("href", context.getURL(ContextEnum.PAGE_ATTACH.getRequestContext(), link));
        result.add(ht);
    }
    // 
    for (final String link : extCollector.getLinks()) {
        final Hashtable<String, String> ht = new Hashtable<>();
        ht.put("page", link);
        ht.put("type", LINK_EXTERNAL);
        ht.put("href", link);
        result.add(ht);
    }
    return result;
}
Also used : Context(org.apache.wiki.api.core.Context) PageManager(org.apache.wiki.pages.PageManager) RenderingManager(org.apache.wiki.render.RenderingManager) Hashtable(java.util.Hashtable) Page(org.apache.wiki.api.core.Page) LinkCollector(org.apache.wiki.LinkCollector) Vector(java.util.Vector)

Aggregations

Context (org.apache.wiki.api.core.Context)81 Page (org.apache.wiki.api.core.Page)46 PageManager (org.apache.wiki.pages.PageManager)42 Test (org.junit.jupiter.api.Test)40 RenderingManager (org.apache.wiki.render.RenderingManager)15 PageContext (javax.servlet.jsp.PageContext)11 Engine (org.apache.wiki.api.core.Engine)9 ReferenceManager (org.apache.wiki.references.ReferenceManager)8 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)6 Date (java.util.Date)6 ServletContext (javax.servlet.ServletContext)6 ProviderException (org.apache.wiki.api.exceptions.ProviderException)6 WikiContext (org.apache.wiki.WikiContext)5 StringReader (java.io.StringReader)4 Properties (java.util.Properties)4 MockHttpServletRequest (net.sourceforge.stripes.mock.MockHttpServletRequest)4 WikiSessionTest (org.apache.wiki.WikiSessionTest)4 Attachment (org.apache.wiki.api.core.Attachment)4 SearchResult (org.apache.wiki.api.search.SearchResult)4