Search in sources :

Example 11 with Command

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

the class DefaultCommandResolver method findCommand.

/**
 * {@inheritDoc}
 */
@Override
public Command findCommand(final HttpServletRequest request, final String defaultContext) {
    // Corner case if request is null
    if (request == null) {
        return CommandResolver.findCommand(defaultContext);
    }
    Command command = null;
    // Determine the name of the page (which may be null)
    String pageName = extractPageFromParameter(defaultContext, request);
    // Can we find a special-page command matching the extracted page?
    if (pageName != null) {
        command = m_specialPages.get(pageName);
    }
    // If we haven't found a matching command yet, extract the JSP path and compare to our list of special pages
    if (command == null) {
        command = extractCommandFromPath(request);
        // Otherwise: use the default context
        if (command == null) {
            command = CONTEXTS.get(defaultContext);
            if (command == null) {
                throw new IllegalArgumentException("Wiki context " + defaultContext + " is illegal.");
            }
        }
    }
    // For PageCommand.VIEW, default to front page if a page wasn't supplied
    if (PageCommand.VIEW.equals(command) && pageName == null) {
        pageName = m_engine.getFrontPage();
    }
    // If we were passed a page parameter, try to resolve it
    if (command instanceof PageCommand && pageName != null) {
        // If there's a matching WikiPage, "wrap" the command
        final Page page = resolvePage(request, pageName);
        return command.targetedCommand(page);
    }
    // If "create group" command, target this wiki
    final String wiki = m_engine.getApplicationName();
    if (WikiCommand.CREATE_GROUP.equals(command)) {
        return WikiCommand.CREATE_GROUP.targetedCommand(wiki);
    }
    // If group command, see if we were passed a group name
    if (command instanceof GroupCommand) {
        String groupName = request.getParameter("group");
        groupName = TextUtil.replaceEntities(groupName);
        if (groupName != null && !groupName.isEmpty()) {
            final GroupPrincipal group = new GroupPrincipal(groupName);
            return command.targetedCommand(group);
        }
    }
    // No page provided; return an "ordinary" command
    return command;
}
Also used : Command(org.apache.wiki.api.core.Command) GroupPrincipal(org.apache.wiki.auth.GroupPrincipal) Page(org.apache.wiki.api.core.Page)

Example 12 with Command

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

the class GroupCommandTest method testStaticCommand.

@Test
public void testStaticCommand() {
    Command a;
    a = GroupCommand.VIEW_GROUP;
    Assertions.assertEquals("viewGroup", a.getRequestContext());
    Assertions.assertEquals("Group.jsp", a.getJSP());
    Assertions.assertEquals("%uGroup.jsp?group=%n", a.getURLPattern());
    Assertions.assertEquals("GroupContent.jsp", a.getContentTemplate());
    Assertions.assertNull(a.getTarget());
    Assertions.assertNull(a.requiredPermission());
    Assertions.assertEquals(a, GroupCommand.VIEW_GROUP);
    a = GroupCommand.EDIT_GROUP;
    Assertions.assertEquals("editGroup", a.getRequestContext());
    Assertions.assertEquals("EditGroup.jsp", a.getJSP());
    Assertions.assertEquals("%uEditGroup.jsp?group=%n", a.getURLPattern());
    Assertions.assertEquals("EditGroupContent.jsp", a.getContentTemplate());
    Assertions.assertNull(a.getTarget());
    Assertions.assertNull(a.requiredPermission());
    Assertions.assertEquals(a, GroupCommand.EDIT_GROUP);
    a = GroupCommand.DELETE_GROUP;
    Assertions.assertEquals("deleteGroup", a.getRequestContext());
    Assertions.assertEquals("DeleteGroup.jsp", a.getJSP());
    Assertions.assertEquals("%uDeleteGroup.jsp?group=%n", a.getURLPattern());
    Assertions.assertNull(null);
    Assertions.assertNull(a.getTarget());
    Assertions.assertNull(a.requiredPermission());
    Assertions.assertEquals(a, GroupCommand.DELETE_GROUP);
}
Also used : Command(org.apache.wiki.api.core.Command) Test(org.junit.jupiter.api.Test)

Example 13 with Command

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

the class CommandResolverTest method testFindWikiActionWithPath.

@Test
public void testFindWikiActionWithPath() {
    // Passing an EDIT request with View JSP yields EDIT of the Front page
    MockHttpServletRequest request = m_engine.newHttpRequest("/Wiki.jsp");
    Command a = resolver.findCommand(request, ContextEnum.PAGE_EDIT.getRequestContext());
    Assertions.assertNotNull(a.getTarget());
    Assertions.assertEquals(((Page) a.getTarget()).getName(), m_engine.getFrontPage());
    // Passing an EDIT request with Group JSP yields VIEW_GROUP
    request = m_engine.newHttpRequest("/Group.jsp");
    a = resolver.findCommand(request, ContextEnum.PAGE_EDIT.getRequestContext());
    Assertions.assertEquals(GroupCommand.VIEW_GROUP, a);
    Assertions.assertNull(a.getTarget());
    // Passing an EDIT request with UserPreferences JSP yields PREFS
    request = m_engine.newHttpRequest("/UserPreferences.jsp");
    a = resolver.findCommand(request, ContextEnum.PAGE_EDIT.getRequestContext());
    Assertions.assertEquals(WikiCommand.PREFS, a);
    Assertions.assertNull(a.getTarget());
}
Also used : Command(org.apache.wiki.api.core.Command) MockHttpServletRequest(net.sourceforge.stripes.mock.MockHttpServletRequest) Test(org.junit.jupiter.api.Test)

Example 14 with Command

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

the class CommandResolverTest method testFindWikiActionNoParams.

@Test
public void testFindWikiActionNoParams() {
    MockHttpServletRequest request = m_engine.newHttpRequest("");
    // Passing an EDIT request with no explicit page params means the EDIT action
    Command a = resolver.findCommand(request, ContextEnum.PAGE_EDIT.getRequestContext());
    Assertions.assertEquals(PageCommand.EDIT, a);
    Assertions.assertEquals("EditContent.jsp", a.getContentTemplate());
    Assertions.assertEquals("Edit.jsp", a.getJSP());
    Assertions.assertEquals("%uEdit.jsp?page=%n", a.getURLPattern());
    Assertions.assertNull(a.getTarget());
    // Ditto for prefs context
    a = resolver.findCommand(request, ContextEnum.WIKI_PREFS.getRequestContext());
    Assertions.assertEquals(WikiCommand.PREFS, a);
    Assertions.assertNull(a.getTarget());
    // Ditto for group view context
    a = resolver.findCommand(request, ContextEnum.GROUP_VIEW.getRequestContext());
    Assertions.assertEquals(GroupCommand.VIEW_GROUP, a);
    Assertions.assertNull(a.getTarget());
    Assertions.assertThrows(IllegalArgumentException.class, () -> resolver.findCommand(m_engine.newHttpRequest(""), "nonExistentContext"));
    // Request for "UserPreference.jsp" should resolve to PREFS action
    request = m_engine.newHttpRequest("/UserPreferences.jsp");
    a = resolver.findCommand(request, ContextEnum.PAGE_EDIT.getRequestContext());
    Assertions.assertEquals(WikiCommand.PREFS, a);
    Assertions.assertNull(a.getTarget());
    // Request for "NewGroup.jsp" should resolve to CREATE_GROUP action
    // but targeted at the wiki
    request = m_engine.newHttpRequest("/NewGroup.jsp");
    a = resolver.findCommand(request, ContextEnum.PAGE_EDIT.getRequestContext());
    Assertions.assertNotSame(WikiCommand.CREATE_GROUP, a);
    Assertions.assertEquals(WikiCommand.CREATE_GROUP.getRequestContext(), a.getRequestContext());
    Assertions.assertEquals(m_engine.getApplicationName(), a.getTarget());
    // But request for JSP not mapped to action should get default
    request = m_engine.newHttpRequest("/NonExistent.jsp");
    a = resolver.findCommand(request, ContextEnum.PAGE_EDIT.getRequestContext());
    Assertions.assertEquals(PageCommand.EDIT, a);
    Assertions.assertNull(a.getTarget());
}
Also used : Command(org.apache.wiki.api.core.Command) MockHttpServletRequest(net.sourceforge.stripes.mock.MockHttpServletRequest) Test(org.junit.jupiter.api.Test)

Example 15 with Command

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

the class RedirectCommandTest method testStaticCommand.

@Test
public void testStaticCommand() {
    final Command a = RedirectCommand.REDIRECT;
    Assertions.assertEquals("", a.getRequestContext());
    Assertions.assertEquals("", a.getJSP());
    Assertions.assertEquals("%u%n", a.getURLPattern());
    Assertions.assertNull(a.getContentTemplate());
    Assertions.assertNull(a.getTarget());
    Assertions.assertEquals(a, RedirectCommand.REDIRECT);
}
Also used : Command(org.apache.wiki.api.core.Command) Test(org.junit.jupiter.api.Test)

Aggregations

Command (org.apache.wiki.api.core.Command)17 Test (org.junit.jupiter.api.Test)12 Page (org.apache.wiki.api.core.Page)4 GroupPrincipal (org.apache.wiki.auth.GroupPrincipal)4 MockHttpServletRequest (net.sourceforge.stripes.mock.MockHttpServletRequest)3 GroupPermission (org.apache.wiki.auth.permissions.GroupPermission)2 WikiPermission (org.apache.wiki.auth.permissions.WikiPermission)2 PageManager (org.apache.wiki.pages.PageManager)2 PageCommand (org.apache.wiki.ui.PageCommand)2 Permission (java.security.Permission)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 PageContext (javax.servlet.jsp.PageContext)1 TestEngine (org.apache.wiki.TestEngine)1 Context (org.apache.wiki.api.core.Context)1 Engine (org.apache.wiki.api.core.Engine)1 Session (org.apache.wiki.api.core.Session)1 SearchResult (org.apache.wiki.api.search.SearchResult)1 AuthorizationManager (org.apache.wiki.auth.AuthorizationManager)1