use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.
the class XSSAPIImplTest method setup.
@Before
public void setup() {
try {
XSSFilterImpl xssFilter = new XSSFilterImpl();
setDefaultHandler(xssFilter, "./src/main/resources/SLING-INF/content/config.xml");
xssAPI = new XSSAPIImpl();
Whitebox.invokeMethod(xssAPI, "activate");
Field filterField = XSSAPIImpl.class.getDeclaredField("xssFilter");
filterField.setAccessible(true);
filterField.set(xssAPI, xssFilter);
ResourceResolver mockResolver = mock(ResourceResolver.class);
when(mockResolver.map(anyString())).thenAnswer(new Answer() {
public Object answer(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
String url = (String) args[0];
return url.replaceAll("jcr:", "_jcr_");
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.
the class UpdateGroupServlet method updateGroup.
/* (non-Javadoc)
* @see org.apache.sling.jackrabbit.usermanager.UpdateGroup#updateGroup(javax.jcr.Session, java.lang.String, java.util.Map, java.util.List)
*/
public Group updateGroup(Session jcrSession, String name, Map<String, ?> properties, List<Modification> changes) throws RepositoryException {
Group group = null;
UserManager userManager = AccessControlUtil.getUserManager(jcrSession);
Authorizable authorizable = userManager.getAuthorizable(name);
if (authorizable instanceof Group) {
group = (Group) authorizable;
} else {
throw new ResourceNotFoundException("Group to update could not be determined");
}
String groupPath = AuthorizableResourceProvider.SYSTEM_USER_MANAGER_GROUP_PREFIX + group.getID();
Collection<RequestProperty> reqProperties = collectContent(properties);
try {
// cleanup any old content (@Delete parameters)
processDeletes(group, reqProperties, changes);
// write content from form
writeContent(jcrSession, group, reqProperties, changes);
// update the group memberships
ResourceResolver resourceResolver = null;
try {
//create a resource resolver to resolve the relative paths used for group membership values
final Map<String, Object> authInfo = new HashMap<String, Object>();
authInfo.put(org.apache.sling.jcr.resource.api.JcrResourceConstants.AUTHENTICATION_INFO_SESSION, jcrSession);
resourceResolver = resourceResolverFactory.getResourceResolver(authInfo);
Resource baseResource = resourceResolver.getResource(groupPath);
updateGroupMembership(baseResource, properties, group, changes);
} catch (LoginException e) {
throw new RepositoryException(e);
} finally {
if (resourceResolver != null) {
resourceResolver.close();
}
}
} catch (RepositoryException re) {
throw new RepositoryException("Failed to update group.", re);
}
return group;
}
use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.
the class AuthorizableResourceProvider method listChildren.
@Override
public Iterator<Resource> listChildren(ResolveContext<Object> ctx, Resource parent) {
try {
String path = parent.getPath();
ResourceResolver resourceResolver = parent.getResourceResolver();
// handle children of /system/userManager
if (SYSTEM_USER_MANAGER_PATH.equals(path)) {
List<Resource> resources = new ArrayList<Resource>();
if (resourceResolver != null) {
resources.add(getResource(ctx, SYSTEM_USER_MANAGER_USER_PATH, null, null));
resources.add(getResource(ctx, SYSTEM_USER_MANAGER_GROUP_PATH, null, null));
}
return resources.iterator();
}
int searchType = -1;
if (SYSTEM_USER_MANAGER_USER_PATH.equals(path)) {
searchType = PrincipalManager.SEARCH_TYPE_NOT_GROUP;
} else if (SYSTEM_USER_MANAGER_GROUP_PATH.equals(path)) {
searchType = PrincipalManager.SEARCH_TYPE_GROUP;
}
if (searchType != -1) {
PrincipalIterator principals = null;
Session session = resourceResolver.adaptTo(Session.class);
if (session != null) {
PrincipalManager principalManager = AccessControlUtil.getPrincipalManager(session);
principals = principalManager.getPrincipals(searchType);
}
if (principals != null) {
return new ChildrenIterator(parent, principals);
}
}
} catch (RepositoryException re) {
throw new SlingException("Error listing children of resource: " + parent.getPath(), re);
}
return null;
}
use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.
the class CreateGroupServlet method createGroup.
/* (non-Javadoc)
* @see org.apache.sling.jackrabbit.usermanager.CreateGroup#createGroup(javax.jcr.Session, java.lang.String, java.util.Map, java.util.List)
*/
public Group createGroup(Session jcrSession, final String name, Map<String, ?> properties, List<Modification> changes) throws RepositoryException {
// check that the parameter values have valid values.
if (jcrSession == null) {
throw new IllegalArgumentException("JCR Session not found");
}
if (name == null || name.length() == 0) {
throw new IllegalArgumentException("Group name was not supplied");
}
UserManager userManager = AccessControlUtil.getUserManager(jcrSession);
Authorizable authorizable = userManager.getAuthorizable(name);
Group group = null;
if (authorizable != null) {
// principal already exists!
throw new RepositoryException("A group already exists with the requested name: " + name);
} else {
group = userManager.createGroup(new Principal() {
public String getName() {
return name;
}
});
String groupPath = AuthorizableResourceProvider.SYSTEM_USER_MANAGER_GROUP_PREFIX + group.getID();
Collection<RequestProperty> reqProperties = collectContent(properties);
changes.add(Modification.onCreated(groupPath));
// write content from form
writeContent(jcrSession, group, reqProperties, changes);
// update the group memberships
ResourceResolver resourceResolver = null;
try {
//create a resource resolver to resolve the relative paths used for group membership values
final Map<String, Object> authInfo = new HashMap<String, Object>();
authInfo.put(org.apache.sling.jcr.resource.api.JcrResourceConstants.AUTHENTICATION_INFO_SESSION, jcrSession);
resourceResolver = resourceResolverFactory.getResourceResolver(authInfo);
Resource baseResource = resourceResolver.getResource(AuthorizableResourceProvider.SYSTEM_USER_MANAGER_GROUP_PATH);
updateGroupMembership(baseResource, properties, group, changes);
} catch (LoginException e) {
throw new RepositoryException(e);
} finally {
if (resourceResolver != null) {
resourceResolver.close();
}
}
}
return group;
}
use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.
the class ResourceTypeResolutionIT method checkResourceType.
@Test
public void checkResourceType() throws Exception {
JackrabbitSession adminSession = (JackrabbitSession) slingRepository.loginAdministrative(null);
Node contentBar = JcrUtils.getOrCreateByPath("/content/foo/bar", "nt:unstructured", adminSession);
contentBar.setProperty("sling:resourceType", "types/foo/bar");
Node appsBar = JcrUtils.getOrCreateByPath("/apps/types/foo/bar", "nt:unstructured", adminSession);
appsBar.setProperty("sling:resourceSuperType", "types/foo/parent");
JcrUtils.getOrCreateByPath("/apps/types/foo/parent", "nt:unstructured", adminSession);
adminSession.getUserManager().createUser("test-user", "test");
adminSession.save();
AccessControlUtils.allow(contentBar, "test-user", "jcr:read");
adminSession.save();
adminSession.logout();
HashMap<String, Object> authenticationInfo = new HashMap<>();
authenticationInfo.put(ResourceResolverFactory.USER, "test-user");
authenticationInfo.put(ResourceResolverFactory.PASSWORD, "test".toCharArray());
ResourceResolver testResolver = resourceResolverFactory.getResourceResolver(authenticationInfo);
try {
Resource resource = testResolver.getResource("/content/foo/bar");
assertNotNull(resource);
assertEquals("/content/foo/bar", resource.getPath());
assertTrue(resource.isResourceType("types/foo/bar"));
// this assertion causes the private ResourceResolverControl#getResourceTypeResourceResolver
// to be called, which needs to inject the resourceresolver bundle via the authenticationInfo
// see SLING-6329
assertTrue(resource.isResourceType("types/foo/parent"));
} finally {
testResolver.close();
}
}
Aggregations