use of javax.jcr.ValueFactory in project sling by apache.
the class RepositoryDistributionPackageImporterTest method testImportPackageWithRequiredPrivileges.
@Test
public void testImportPackageWithRequiredPrivileges() throws Exception {
SlingRepository repository = mock(SlingRepository.class);
String serviceName = "admin";
Session session = mock(Session.class);
ValueFactory vf = mock(ValueFactory.class);
when(session.getValueFactory()).thenReturn(vf);
AccessControlManager acm = mock(AccessControlManager.class);
String privilegeName = "jcr:read";
Privilege privilege = mock(Privilege.class);
String path = "/var/something";
Node rootNode = mock(Node.class);
Node createdNode = mock(Node.class);
Node jcrContentNode = mock(Node.class);
when(createdNode.addNode(JcrConstants.JCR_CONTENT, NodeType.NT_RESOURCE)).thenReturn(jcrContentNode);
when(rootNode.addNode(any(String.class), any(String.class))).thenReturn(createdNode);
when(session.getNode(path)).thenReturn(rootNode);
when(acm.hasPrivileges(path, new Privilege[] { privilege })).thenReturn(true);
when(acm.privilegeFromName(privilegeName)).thenReturn(privilege);
when(session.getAccessControlManager()).thenReturn(acm);
when(repository.loginService(serviceName, null)).thenReturn(session);
RepositoryDistributionPackageImporter repositoryDistributionPackageImporter = new RepositoryDistributionPackageImporter(repository, serviceName, path, privilegeName);
ResourceResolver resourceResolver = mock(ResourceResolver.class);
DistributionPackage distributionPackage = mock(DistributionPackage.class);
when(distributionPackage.getId()).thenReturn("someid");
InputStream stream = mock(InputStream.class);
when(distributionPackage.createInputStream()).thenReturn(stream);
repositoryDistributionPackageImporter.importPackage(resourceResolver, distributionPackage);
}
use of javax.jcr.ValueFactory in project camel by apache.
the class JcrNodePathCreationTest method setUp.
@Override
@Before
public void setUp() throws Exception {
super.setUp();
Session session = openSession();
ValueFactory valFact = session.getValueFactory();
multiValued = new Value[] { valFact.createValue("value-1"), valFact.createValue("value-2") };
session.logout();
}
use of javax.jcr.ValueFactory in project jackrabbit by apache.
the class ACLProvider method init.
//----------------------------------------------< AccessControlProvider >---
/**
* @see org.apache.jackrabbit.core.security.authorization.AccessControlProvider#init(javax.jcr.Session, java.util.Map)
*/
@Override
public void init(Session systemSession, Map configuration) throws RepositoryException {
super.init(systemSession, configuration);
NodeImpl root = (NodeImpl) session.getRootNode();
if (root.hasNode(N_ACCESSCONTROL)) {
acRoot = root.getNode(N_ACCESSCONTROL);
if (!acRoot.isNodeType(NT_REP_ACCESS_CONTROL)) {
throw new RepositoryException("Error while initializing Access Control Provider: Found ac-root to be wrong node type " + acRoot.getPrimaryNodeType().getName());
}
} else {
acRoot = root.addNode(N_ACCESSCONTROL, NT_REP_ACCESS_CONTROL, null);
}
editor = new ACLEditor(session, session.getQPath(acRoot.getPath()));
entriesCache = new EntriesCache(session, editor, acRoot.getPath());
// TODO: replace by configurable default policy (see JCR-2331)
if (!configuration.containsKey(PARAM_OMIT_DEFAULT_PERMISSIONS)) {
try {
log.debug("Install initial permissions: ...");
ValueFactory vf = session.getValueFactory();
Map<String, Value> restrictions = new HashMap<String, Value>();
restrictions.put(session.getJCRName(ACLTemplate.P_NODE_PATH), vf.createValue(root.getPath(), PropertyType.PATH));
PrincipalManager pMgr = session.getPrincipalManager();
AccessControlManager acMgr = session.getAccessControlManager();
// initial default permissions for the administrators group
String pName = SecurityConstants.ADMINISTRATORS_NAME;
if (pMgr.hasPrincipal(pName)) {
Principal administrators = pMgr.getPrincipal(pName);
installDefaultPermissions(administrators, new Privilege[] { acMgr.privilegeFromName(Privilege.JCR_ALL) }, restrictions, editor);
} else {
log.info("Administrators principal group is missing -> Not adding default permissions.");
}
// initialize default permissions for the everyone group
installDefaultPermissions(pMgr.getEveryone(), new Privilege[] { acMgr.privilegeFromName(Privilege.JCR_READ) }, restrictions, editor);
session.save();
} catch (RepositoryException e) {
log.error("Failed to set-up minimal access control for root node of workspace " + session.getWorkspace().getName());
session.getRootNode().refresh(false);
}
}
}
use of javax.jcr.ValueFactory in project jackrabbit by apache.
the class GarbageCollectorTest method testGC.
public void testGC() throws Exception {
Node root = testRootNode;
Session session = root.getSession();
deleteMyNodes();
runGC(session, true);
root.addNode("node1");
Node node2 = root.addNode("node2");
Node n = node2.addNode("nodeWithBlob").addNode("sub");
ValueFactory vf = session.getValueFactory();
Binary b = vf.createBinary(new RandomInputStream(20, 1000));
n.setProperty("test", b);
session.save();
n = node2.addNode("nodeWithTemporaryBlob");
n.setProperty("test", vf.createBinary(new RandomInputStream(11, 1000)));
session.save();
n.remove();
session.save();
GarbageCollector gc = ((SessionImpl) session).createDataStoreGarbageCollector();
gc.getDataStore().clearInUse();
gc.setPersistenceManagerScan(false);
gc.setMarkEventListener(this);
if (gc.getDataStore() instanceof FileDataStore) {
// make sure the file is old (access time resolution is 2 seconds)
Thread.sleep(2000);
}
LOG.debug("scanning...");
gc.mark();
int count = listIdentifiers(gc);
LOG.debug("stop scanning; currently " + count + " identifiers");
LOG.debug("deleting...");
gc.getDataStore().clearInUse();
assertTrue(gc.sweep() > 0);
int count2 = listIdentifiers(gc);
assertEquals(count - 1, count2);
// verify the node was moved, and that the binary is still there
n = root.getNode("node1").getNode("nodeWithBlob").getNode("sub");
b = n.getProperty("test").getValue().getBinary();
InputStream in = b.getStream();
InputStream in2 = new RandomInputStream(20, 1000);
verifyInputStream(in, in2);
deleteMyNodes();
gc.close();
}
use of javax.jcr.ValueFactory in project jackrabbit by apache.
the class OpenFilesTest method testStreams.
/**
* Test opening a large number of streams.
*/
public void testStreams() throws RepositoryException {
Session session = getHelper().getReadWriteSession();
try {
Node test = session.getRootNode().addNode("test");
ValueFactory vf = session.getValueFactory();
test.setProperty("data", vf.createBinary(new ByteArrayInputStream(new byte[10 * 1024])));
session.save();
for (int i = 0; i < 10000; i++) {
test.getProperty("data").getValue();
}
} catch (Exception e) {
e.printStackTrace();
assertFalse(e.getMessage(), true);
} finally {
session.logout();
}
}
Aggregations