use of org.apache.wiki.api.core.Command in project jspwiki by apache.
the class DefaultCommandResolver method extractCommandFromPath.
/**
* Extracts a Command based on the JSP path of an HTTP request. If the JSP requested matches a Command's <code>getJSP()</code>
* value, that Command is returned.
*
* @param request the HTTP request
* @return the resolved Command, or <code>null</code> if not found
*/
protected Command extractCommandFromPath(final HttpServletRequest request) {
String jsp = request.getServletPath();
// Take everything to right of initial / and left of # or ?
final int hashMark = jsp.indexOf('#');
if (hashMark != -1) {
jsp = jsp.substring(0, hashMark);
}
final int questionMark = jsp.indexOf('?');
if (questionMark != -1) {
jsp = jsp.substring(0, questionMark);
}
if (jsp.startsWith("/")) {
jsp = jsp.substring(1);
}
// Find special page reference?
for (final Map.Entry<String, Command> entry : m_specialPages.entrySet()) {
final Command specialCommand = entry.getValue();
if (specialCommand.getJSP().equals(jsp)) {
return specialCommand;
}
}
// Still haven't found a matching command? Ok, see if we match against our standard list of JSPs
if (!jsp.isEmpty() && JSPS.containsKey(jsp)) {
return JSPS.get(jsp);
}
return null;
}
use of org.apache.wiki.api.core.Command in project jspwiki by apache.
the class PermissionTag method checkPermission.
/**
* Checks a single permission.
*
* @param permission permission to check for
* @return true if granted, false if not
*/
private boolean checkPermission(final String permission) {
final Session session = m_wikiContext.getWikiSession();
final Page page = m_wikiContext.getPage();
final AuthorizationManager mgr = m_wikiContext.getEngine().getManager(AuthorizationManager.class);
boolean gotPermission = false;
if (CREATE_GROUPS.equals(permission) || CREATE_PAGES.equals(permission) || EDIT_PREFERENCES.equals(permission) || EDIT_PROFILE.equals(permission) || LOGIN.equals(permission)) {
gotPermission = mgr.checkPermission(session, new WikiPermission(page.getWiki(), permission));
} else if (VIEW_GROUP.equals(permission) || EDIT_GROUP.equals(permission) || DELETE_GROUP.equals(permission)) {
final Command command = m_wikiContext.getCommand();
gotPermission = false;
if (command instanceof GroupCommand && command.getTarget() != null) {
final GroupPrincipal group = (GroupPrincipal) command.getTarget();
final String groupName = group.getName();
String action = "view";
if (EDIT_GROUP.equals(permission)) {
action = "edit";
} else if (DELETE_GROUP.equals(permission)) {
action = "delete";
}
gotPermission = mgr.checkPermission(session, new GroupPermission(groupName, action));
}
} else if (ALL_PERMISSION.equals(permission)) {
gotPermission = mgr.checkPermission(session, new AllPermission(m_wikiContext.getEngine().getApplicationName()));
} else if (page != null) {
//
if (EDIT.equals(permission)) {
final Page latest = m_wikiContext.getEngine().getManager(PageManager.class).getPage(page.getName());
if (page.getVersion() != WikiProvider.LATEST_VERSION && latest.getVersion() != page.getVersion()) {
return false;
}
}
final Permission p = PermissionFactory.getPagePermission(page, permission);
gotPermission = mgr.checkPermission(session, p);
}
return gotPermission;
}
use of org.apache.wiki.api.core.Command in project jspwiki by apache.
the class SearchResultIteratorTag method nextResult.
private int nextResult() {
if (m_iterator != null && m_iterator.hasNext() && m_count++ < m_maxItems) {
final SearchResult r = (SearchResult) m_iterator.next();
// Create a wiki context for the result
final Engine engine = m_wikiContext.getEngine();
final HttpServletRequest request = m_wikiContext.getHttpRequest();
final Command command = PageCommand.VIEW.targetedCommand(r.getPage());
final Context context = Wiki.context().create(engine, request, command);
// Stash it in the page context
pageContext.setAttribute(Context.ATTR_CONTEXT, context, PageContext.REQUEST_SCOPE);
pageContext.setAttribute(getId(), r);
return EVAL_BODY_BUFFERED;
}
return SKIP_BODY;
}
use of org.apache.wiki.api.core.Command in project jspwiki by apache.
the class GroupCommandTest method testTargetedCommand.
@Test
public void testTargetedCommand() {
// Get view command
Command a = GroupCommand.VIEW_GROUP;
final GroupPrincipal group = new GroupPrincipal("Test");
// Combine with wiki group; make sure it's not equal to old command
Command b = a.targetedCommand(group);
Assertions.assertNotSame(a, b);
Assertions.assertEquals(a.getRequestContext(), b.getRequestContext());
Assertions.assertEquals(a.getJSP(), b.getJSP());
Assertions.assertEquals(a.getURLPattern(), b.getURLPattern());
Assertions.assertEquals(a.getContentTemplate(), b.getContentTemplate());
Assertions.assertNotNull(b.getTarget());
Assertions.assertNotNull(b.requiredPermission());
Assertions.assertEquals(new GroupPermission("*:Test", "view"), b.requiredPermission());
Assertions.assertEquals(group, b.getTarget());
// Do the same with edit command
a = GroupCommand.EDIT_GROUP;
b = a.targetedCommand(group);
Assertions.assertNotSame(a, b);
Assertions.assertNotNull(b.getTarget());
Assertions.assertNotNull(b.requiredPermission());
Assertions.assertEquals(new GroupPermission("*:Test", "edit"), b.requiredPermission());
Assertions.assertEquals(group, b.getTarget());
// Do the same with delete command
a = GroupCommand.DELETE_GROUP;
b = a.targetedCommand(group);
Assertions.assertNotSame(a, b);
Assertions.assertNotNull(b.getTarget());
Assertions.assertNotNull(b.requiredPermission());
Assertions.assertEquals(new GroupPermission("*:Test", "delete"), b.requiredPermission());
Assertions.assertEquals(group, b.getTarget());
}
use of org.apache.wiki.api.core.Command in project jspwiki by apache.
the class CommandResolverTest method testFindStaticWikiAction.
@Test
public void testFindStaticWikiAction() {
// If we look for action with "edit" request context, we get EDIT action
Command a = CommandResolver.findCommand(ContextEnum.PAGE_EDIT.getRequestContext());
Assertions.assertEquals(PageCommand.EDIT, a);
Assertions.assertEquals(ContextEnum.PAGE_EDIT.getRequestContext(), a.getRequestContext());
// Ditto for prefs context
a = CommandResolver.findCommand(ContextEnum.WIKI_PREFS.getRequestContext());
Assertions.assertEquals(WikiCommand.PREFS, a);
Assertions.assertEquals(ContextEnum.WIKI_PREFS.getRequestContext(), a.getRequestContext());
// Ditto for group view context
a = CommandResolver.findCommand(ContextEnum.GROUP_VIEW.getRequestContext());
Assertions.assertEquals(GroupCommand.VIEW_GROUP, a);
Assertions.assertEquals(ContextEnum.GROUP_VIEW.getRequestContext(), a.getRequestContext());
// Looking for non-existent context; should result in exception
Assertions.assertThrows(IllegalArgumentException.class, () -> CommandResolver.findCommand("nonExistentContext"));
}
Aggregations