Search in sources :

Example 1 with WikiException

use of org.apache.wiki.api.exceptions.WikiException in project jspwiki by apache.

the class ApprovalWorkflowTest method testSaveWikiPageWithException.

@Test
public void testSaveWikiPageWithException() throws WikiException {
    // Add a PageFilter that rejects all save attempts
    FilterManager fm = m_engine.getFilterManager();
    fm.addPageFilter(new AbortFilter(), 0);
    // Create a sample test page and try to save it
    String pageName = "SaveWikiPageWorkflow-Test" + System.currentTimeMillis();
    String text = "This is a test!";
    try {
        m_engine.saveTextAsJanne(pageName, text);
    } catch (WikiException e) {
        Assert.assertTrue(e instanceof FilterException);
        Assert.assertEquals("Page save aborted.", e.getMessage());
        return;
    }
    Assert.fail("Page save should have thrown a FilterException, but didn't.");
}
Also used : WikiException(org.apache.wiki.api.exceptions.WikiException) FilterException(org.apache.wiki.api.exceptions.FilterException) FilterManager(org.apache.wiki.api.engine.FilterManager) Test(org.junit.Test)

Example 2 with WikiException

use of org.apache.wiki.api.exceptions.WikiException in project jspwiki by apache.

the class WorkflowManagerTest method testGetApprover.

@Test
public void testGetApprover() throws WikiException {
    // Test properties says workflow.saveWikiPage approver is GP Admin; workflow.foo is 'janne'
    Assert.assertEquals(new WikiPrincipal("janne", WikiPrincipal.LOGIN_NAME), wm.getApprover("workflow.foo"));
    Assert.assertEquals(new GroupPrincipal("Admin"), wm.getApprover("workflow.bar"));
    // 'saveWikiPage' workflow doesn't require approval, so we will need to catch an Exception
    try {
        Assert.assertEquals(new GroupPrincipal("Admin"), wm.getApprover("workflow.saveWikiPage"));
    } catch (WikiException e) {
        // Swallow
        return;
    }
    // We should never get here
    Assert.fail("Workflow.bar doesn't need approval!");
}
Also used : WikiException(org.apache.wiki.api.exceptions.WikiException) WikiPrincipal(org.apache.wiki.auth.WikiPrincipal) GroupPrincipal(org.apache.wiki.auth.GroupPrincipal) Test(org.junit.Test)

Example 3 with WikiException

use of org.apache.wiki.api.exceptions.WikiException in project jspwiki by apache.

the class WorkflowManager method getApprover.

/**
 * Looks up and resolves the actor who approves a Decision for a particular
 * Workflow, based on the Workflow's message key. If not found, or if
 * Principal is Unresolved, throws WikiException. This particular
 * implementation always returns the GroupPrincipal <code>Admin</code>
 *
 * @param messageKey the Decision's message key
 * @return the actor who approves Decisions
 * @throws WikiException if the message key was not found, or the
 * Principal value corresponding to the key could not be resolved
 */
public Principal getApprover(String messageKey) throws WikiException {
    Principal approver = m_approvers.get(messageKey);
    if (approver == null) {
        throw new WikiException("Workflow '" + messageKey + "' does not require approval.");
    }
    // Try to resolve UnresolvedPrincipals
    if (approver instanceof UnresolvedPrincipal) {
        String name = approver.getName();
        approver = m_engine.getAuthorizationManager().resolvePrincipal(name);
        // cache
        if (approver instanceof UnresolvedPrincipal) {
            throw new WikiException("Workflow approver '" + name + "' cannot not be resolved.");
        }
        m_approvers.put(messageKey, approver);
    }
    return approver;
}
Also used : WikiException(org.apache.wiki.api.exceptions.WikiException) Principal(java.security.Principal) UnresolvedPrincipal(org.apache.wiki.auth.acl.UnresolvedPrincipal) UnresolvedPrincipal(org.apache.wiki.auth.acl.UnresolvedPrincipal)

Example 4 with WikiException

use of org.apache.wiki.api.exceptions.WikiException in project jspwiki by apache.

the class ClassUtil method getMappedObject.

/**
 *  This method is used to locate and instantiate a mapped class.
 *  You may redefine anything in the resource file which is located in your classpath
 *  under the name <code>{@value #MAPPINGS}</code>.
 *  <p>
 *  This is an extremely powerful system, which allows you to remap many of
 *  the JSPWiki core classes to your own class.  Please read the documentation
 *  included in the default <code>{@value #MAPPINGS}</code> file to see
 *  how this method works.
 *  <p>
 *  This method takes in an object array for the constructor arguments for classes
 *  which have more than two constructors.
 *
 *  @param requestedClass The name of the class you wish to instantiate.
 *  @param initargs The parameters to be passed to the constructor. May be <code>null</code>.
 *  @return An instantiated Object.
 *  @throws WikiException If the class cannot be found or instantiated.  The error is logged.
 *  @since 2.5.40
 */
public static Object getMappedObject(String requestedClass, Object... initargs) throws WikiException {
    try {
        Class<?> cl = getMappedClass(requestedClass);
        Constructor<?>[] ctors = cl.getConstructors();
        // 
        for (int c = 0; c < ctors.length; c++) {
            Class<?>[] params = ctors[c].getParameterTypes();
            if (params.length == initargs.length) {
                for (int arg = 0; arg < initargs.length; arg++) {
                    if (params[arg].isAssignableFrom(initargs[arg].getClass())) {
                        // 
                        return ctors[c].newInstance(initargs);
                    }
                }
            }
        }
        // 
        // No arguments, so we can just call a default constructor and
        // ignore the arguments.
        // 
        Object o = cl.newInstance();
        return o;
    } catch (InstantiationException e) {
        log.info("Cannot instantiate requested class " + requestedClass, e);
        throw new WikiException("Failed to instantiate class " + requestedClass, e);
    } catch (IllegalAccessException e) {
        log.info("Cannot access requested class " + requestedClass, e);
        throw new WikiException("Failed to instantiate class " + requestedClass, e);
    } catch (IllegalArgumentException e) {
        log.info("Illegal arguments when constructing new object", e);
        throw new WikiException("Failed to instantiate class " + requestedClass, e);
    } catch (InvocationTargetException e) {
        log.info("You tried to instantiate an abstract class " + requestedClass, e);
        throw new WikiException("Failed to instantiate class " + requestedClass, e);
    }
}
Also used : WikiException(org.apache.wiki.api.exceptions.WikiException) Constructor(java.lang.reflect.Constructor) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 5 with WikiException

use of org.apache.wiki.api.exceptions.WikiException in project jspwiki by apache.

the class BugReportHandler method execute.

/**
 *  {@inheritDoc}
 */
public String execute(WikiContext context, Map<String, String> params) throws PluginException {
    String title;
    String description;
    String version;
    String submitter = null;
    SimpleDateFormat format = new SimpleDateFormat(DEFAULT_DATEFORMAT);
    ResourceBundle rb = Preferences.getBundle(context, WikiPlugin.CORE_PLUGINS_RESOURCEBUNDLE);
    title = params.get(PARAM_TITLE);
    description = params.get(PARAM_DESCRIPTION);
    version = params.get(PARAM_VERSION);
    Principal wup = context.getCurrentUser();
    if (wup != null) {
        submitter = wup.getName();
    }
    if (title == null)
        throw new PluginException(rb.getString("bugreporthandler.titlerequired"));
    if (title.length() == 0)
        return "";
    if (description == null)
        description = "";
    if (version == null)
        version = "unknown";
    Properties mappings = parseMappings(params.get(PARAM_MAPPINGS));
    try {
        StringWriter str = new StringWriter();
        PrintWriter out = new PrintWriter(str);
        Date d = new Date();
        // 
        // Outputting of basic data
        // 
        out.println("|" + mappings.getProperty(PARAM_TITLE, "Title") + "|" + title);
        out.println("|" + mappings.getProperty("date", "Date") + "|" + format.format(d));
        out.println("|" + mappings.getProperty(PARAM_VERSION, "Version") + "|" + version);
        if (submitter != null) {
            out.println("|" + mappings.getProperty("submitter", "Submitter") + "|" + submitter);
        }
        // 
        for (Iterator<Map.Entry<String, String>> i = params.entrySet().iterator(); i.hasNext(); ) {
            Map.Entry<String, String> entry = i.next();
            if (entry.getKey().equals(PARAM_TITLE) || entry.getKey().equals(PARAM_DESCRIPTION) || entry.getKey().equals(PARAM_VERSION) || entry.getKey().equals(PARAM_MAPPINGS) || entry.getKey().equals(PARAM_PAGE) || entry.getKey().startsWith("_")) {
            // Ignore this
            } else {
                // 
                // If no mapping has been defined, just ignore
                // it.
                // 
                String head = mappings.getProperty(entry.getKey(), entry.getKey());
                if (head.length() > 0) {
                    out.println("|" + head + "|" + entry.getValue());
                }
            }
        }
        out.println();
        out.println(description);
        out.close();
        // 
        // Now create a new page for this bug report
        // 
        String pageName = findNextPage(context, title, params.get(PARAM_PAGE));
        WikiPage newPage = new WikiPage(context.getEngine(), pageName);
        WikiContext newContext = (WikiContext) context.clone();
        newContext.setPage(newPage);
        context.getEngine().saveText(newContext, str.toString());
        MessageFormat formatter = new MessageFormat("");
        formatter.applyPattern(rb.getString("bugreporthandler.new"));
        String[] args = { "<a href=\"" + context.getViewURL(pageName) + "\">" + pageName + "</a>" };
        return formatter.format(args);
    } catch (RedirectException e) {
        log.info("Saving not allowed, reason: '" + e.getMessage() + "', can't redirect to " + e.getRedirect());
        throw new PluginException("Saving not allowed, reason: " + e.getMessage());
    } catch (WikiException e) {
        log.error("Unable to save page!", e);
        return rb.getString("bugreporthandler.unable");
    }
}
Also used : WikiContext(org.apache.wiki.WikiContext) WikiException(org.apache.wiki.api.exceptions.WikiException) MessageFormat(java.text.MessageFormat) PluginException(org.apache.wiki.api.exceptions.PluginException) WikiPage(org.apache.wiki.WikiPage) StringWriter(java.io.StringWriter) SimpleDateFormat(java.text.SimpleDateFormat) Principal(java.security.Principal) PrintWriter(java.io.PrintWriter) RedirectException(org.apache.wiki.api.exceptions.RedirectException)

Aggregations

WikiException (org.apache.wiki.api.exceptions.WikiException)15 Principal (java.security.Principal)5 IOException (java.io.IOException)3 WikiPage (org.apache.wiki.WikiPage)3 File (java.io.File)2 Permission (java.security.Permission)2 WikiContext (org.apache.wiki.WikiContext)2 FilterManager (org.apache.wiki.api.engine.FilterManager)2 FilterException (org.apache.wiki.api.exceptions.FilterException)2 NoRequiredPropertyException (org.apache.wiki.api.exceptions.NoRequiredPropertyException)2 ProviderException (org.apache.wiki.api.exceptions.ProviderException)2 RedirectException (org.apache.wiki.api.exceptions.RedirectException)2 GroupPrincipal (org.apache.wiki.auth.GroupPrincipal)2 WikiPrincipal (org.apache.wiki.auth.WikiPrincipal)2 UserProfile (org.apache.wiki.auth.user.UserProfile)2 DecisionRequiredException (org.apache.wiki.workflow.DecisionRequiredException)2 Test (org.junit.Test)2 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1