Search in sources :

Example 6 with InternalWikiException

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

the class SpamFilter method initialize.

/**
 *  {@inheritDoc}
 */
@Override
public void initialize(WikiEngine engine, Properties properties) {
    m_forbiddenWordsPage = properties.getProperty(PROP_WORDLIST, m_forbiddenWordsPage);
    m_forbiddenIPsPage = properties.getProperty(PROP_IPLIST, m_forbiddenIPsPage);
    m_pageNameMaxLength = properties.getProperty(PROP_MAX_PAGENAME_LENGTH, m_pageNameMaxLength);
    m_errorPage = properties.getProperty(PROP_ERRORPAGE, m_errorPage);
    m_limitSinglePageChanges = TextUtil.getIntegerProperty(properties, PROP_PAGECHANGES, m_limitSinglePageChanges);
    m_limitSimilarChanges = TextUtil.getIntegerProperty(properties, PROP_SIMILARCHANGES, m_limitSimilarChanges);
    m_maxUrls = TextUtil.getIntegerProperty(properties, PROP_MAXURLS, m_maxUrls);
    m_banTime = TextUtil.getIntegerProperty(properties, PROP_BANTIME, m_banTime);
    m_blacklist = properties.getProperty(PROP_BLACKLIST, m_blacklist);
    m_ignoreAuthenticated = TextUtil.getBooleanProperty(properties, PROP_IGNORE_AUTHENTICATED, m_ignoreAuthenticated);
    m_useCaptcha = properties.getProperty(PROP_CAPTCHA, "").equals("asirra");
    try {
        m_urlPattern = m_compiler.compile(URL_REGEXP);
    } catch (MalformedPatternException e) {
        log.fatal("Internal error: Someone put in a faulty pattern.", e);
        throw new InternalWikiException("Faulty pattern.", e);
    }
    m_akismetAPIKey = TextUtil.getStringProperty(properties, PROP_AKISMET_API_KEY, m_akismetAPIKey);
    m_stopAtFirstMatch = TextUtil.getStringProperty(properties, PROP_FILTERSTRATEGY, STRATEGY_EAGER).equals(STRATEGY_EAGER);
    log.info("# Spam filter initialized.  Temporary ban time " + m_banTime + " mins, max page changes/minute: " + m_limitSinglePageChanges);
}
Also used : MalformedPatternException(org.apache.oro.text.regex.MalformedPatternException) InternalWikiException(org.apache.wiki.InternalWikiException)

Example 7 with InternalWikiException

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

the class PluginContent method parsePluginLine.

/**
 * Parses a plugin invocation and returns a DOM element.
 *
 * @param context     The WikiContext
 * @param commandline The line to parse
 * @param pos         The position in the stream parsing.
 * @return A DOM element
 * @throws PluginException If plugin invocation is faulty
 * @since 2.10.0
 */
public static PluginContent parsePluginLine(WikiContext context, String commandline, int pos) throws PluginException {
    PatternMatcher matcher = new Perl5Matcher();
    try {
        PluginManager pm = context.getEngine().getPluginManager();
        if (matcher.contains(commandline, pm.getPluginPattern())) {
            MatchResult res = matcher.getMatch();
            String plugin = res.group(2);
            String args = commandline.substring(res.endOffset(0), commandline.length() - (commandline.charAt(commandline.length() - 1) == '}' ? 1 : 0));
            Map<String, String> arglist = pm.parseArgs(args);
            // set wikitext bounds of plugin as '_bounds' parameter, e.g., [345,396]
            if (pos != -1) {
                int end = pos + commandline.length() + 2;
                String bounds = pos + "|" + end;
                arglist.put(PluginManager.PARAM_BOUNDS, bounds);
            }
            PluginContent result = new PluginContent(plugin, arglist);
            return result;
        }
    } catch (ClassCastException e) {
        log.error("Invalid type offered in parsing plugin arguments.", e);
        throw new InternalWikiException("Oops, someone offered !String!", e);
    } catch (NoSuchElementException e) {
        String msg = "Missing parameter in plugin definition: " + commandline;
        log.warn(msg, e);
        throw new PluginException(msg);
    } catch (IOException e) {
        String msg = "Zyrf.  Problems with parsing arguments: " + commandline;
        log.warn(msg, e);
        throw new PluginException(msg);
    }
    return null;
}
Also used : PluginException(org.apache.wiki.api.exceptions.PluginException) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) IOException(java.io.IOException) MatchResult(org.apache.oro.text.regex.MatchResult) InternalWikiException(org.apache.wiki.InternalWikiException) PluginManager(org.apache.wiki.api.engine.PluginManager) PatternMatcher(org.apache.oro.text.regex.PatternMatcher) NoSuchElementException(java.util.NoSuchElementException)

Example 8 with InternalWikiException

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

the class Preferences method getDateFormat.

/**
 *  Get SimpleTimeFormat according to user browser locale and preferred time
 *  formats. If not found, it will revert to whichever format is set for the
 *  default
 *
 *  @param context WikiContext to use for rendering.
 *  @param tf Which version of the dateformat you are looking for?
 *  @return A SimpleTimeFormat object which you can use to render
 *  @since 2.8
 */
public static SimpleDateFormat getDateFormat(WikiContext context, TimeFormat tf) {
    InternationalizationManager imgr = context.getEngine().getInternationalizationManager();
    Locale clientLocale = getLocale(context);
    String prefTimeZone = getPreference(context, "TimeZone");
    String prefDateFormat;
    log.debug("Checking for preferences...");
    switch(tf) {
        case DATETIME:
            prefDateFormat = getPreference(context, "DateFormat");
            log.debug("Preferences fmt = " + prefDateFormat);
            if (prefDateFormat == null) {
                prefDateFormat = imgr.get(InternationalizationManager.CORE_BUNDLE, clientLocale, "common.datetimeformat");
                log.debug("Using locale-format = " + prefDateFormat);
            }
            break;
        case TIME:
            prefDateFormat = imgr.get("common.timeformat");
            break;
        case DATE:
            prefDateFormat = imgr.get("common.dateformat");
            break;
        default:
            throw new InternalWikiException("Got a TimeFormat for which we have no value!");
    }
    try {
        SimpleDateFormat fmt = new SimpleDateFormat(prefDateFormat, clientLocale);
        if (prefTimeZone != null) {
            TimeZone tz = TimeZone.getTimeZone(prefTimeZone);
            // TimeZone tz = TimeZone.getDefault();
            // tz.setRawOffset(Integer.parseInt(prefTimeZone));
            fmt.setTimeZone(tz);
        }
        return fmt;
    } catch (Exception e) {
        return null;
    }
}
Also used : Locale(java.util.Locale) TimeZone(java.util.TimeZone) InternationalizationManager(org.apache.wiki.i18n.InternationalizationManager) SimpleDateFormat(java.text.SimpleDateFormat) MissingResourceException(java.util.MissingResourceException) InternalWikiException(org.apache.wiki.InternalWikiException) InternalWikiException(org.apache.wiki.InternalWikiException)

Example 9 with InternalWikiException

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

the class AbstractFileProvider method getAllPages.

/**
 *  {@inheritDoc}
 */
public Collection getAllPages() throws ProviderException {
    log.debug("Getting all pages...");
    ArrayList<WikiPage> set = new ArrayList<WikiPage>();
    File wikipagedir = new File(m_pageDirectory);
    File[] wikipages = wikipagedir.listFiles(new WikiFileFilter());
    if (wikipages == null) {
        log.error("Wikipages directory '" + m_pageDirectory + "' does not exist! Please check " + PROP_PAGEDIR + " in jspwiki.properties.");
        throw new InternalWikiException("Page directory does not exist");
    }
    for (int i = 0; i < wikipages.length; i++) {
        String wikiname = wikipages[i].getName();
        int cutpoint = wikiname.lastIndexOf(FILE_EXT);
        WikiPage page = getPageInfo(unmangleName(wikiname.substring(0, cutpoint)), WikiPageProvider.LATEST_VERSION);
        if (page == null) {
            // This should not really happen.
            // FIXME: Should we throw an exception here?
            log.error("Page " + wikiname + " was found in directory listing, but could not be located individually.");
            continue;
        }
        set.add(page);
    }
    return set;
}
Also used : WikiPage(org.apache.wiki.WikiPage) ArrayList(java.util.ArrayList) File(java.io.File) InternalWikiException(org.apache.wiki.InternalWikiException)

Example 10 with InternalWikiException

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

the class CheckVersionTag method doWikiStartTag.

/**
 *  {@inheritDoc}
 */
@Override
public final int doWikiStartTag() throws IOException, ProviderException {
    WikiEngine engine = m_wikiContext.getEngine();
    WikiPage page = m_wikiContext.getPage();
    if (page != null && engine.pageExists(page.getName())) {
        int version = page.getVersion();
        boolean include = false;
        WikiPage latest = engine.getPage(page.getName());
        switch(m_mode) {
            case LATEST:
                include = (version < 0) || (latest.getVersion() == version);
                break;
            case NOTLATEST:
                include = (version > 0) && (latest.getVersion() != version);
                break;
            case FIRST:
                include = (version == 1) || (version < 0 && latest.getVersion() == 1);
                break;
            case NOTFIRST:
                include = version > 1;
                break;
            default:
                throw new InternalWikiException("Mode which is not available!");
        }
        if (include) {
            // log.debug("INCLD");
            return EVAL_BODY_INCLUDE;
        }
    }
    return SKIP_BODY;
}
Also used : WikiPage(org.apache.wiki.WikiPage) WikiEngine(org.apache.wiki.WikiEngine) InternalWikiException(org.apache.wiki.InternalWikiException)

Aggregations

InternalWikiException (org.apache.wiki.InternalWikiException)12 WikiPage (org.apache.wiki.WikiPage)4 IOException (java.io.IOException)3 MalformedPatternException (org.apache.oro.text.regex.MalformedPatternException)2 WikiEngine (org.apache.wiki.WikiEngine)2 JDOMException (org.jdom2.JDOMException)2 File (java.io.File)1 StringReader (java.io.StringReader)1 Principal (java.security.Principal)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Locale (java.util.Locale)1 MissingResourceException (java.util.MissingResourceException)1 NoSuchElementException (java.util.NoSuchElementException)1 Properties (java.util.Properties)1 TimeZone (java.util.TimeZone)1 JspWriter (javax.servlet.jsp.JspWriter)1 MatchResult (org.apache.oro.text.regex.MatchResult)1 PatternMatcher (org.apache.oro.text.regex.PatternMatcher)1 Perl5Matcher (org.apache.oro.text.regex.Perl5Matcher)1