use of javax.jcr.ValueFactory in project sling by apache.
the class DefaultContentCreator method createUser.
/* (non-Javadoc)
* @see org.apache.sling.jcr.contentloader.ContentCreator#createUser(java.lang.String, java.lang.String, java.util.Map)
*/
public void createUser(final String name, String password, Map<String, Object> extraProperties) throws RepositoryException {
final Node parentNode = this.parentNodeStack.peek();
Session session = parentNode.getSession();
UserManager userManager = AccessControlUtil.getUserManager(session);
Authorizable authorizable = userManager.getAuthorizable(name);
if (authorizable == null) {
//principal does not exist yet, so create it
User user = userManager.createUser(name, password, new Principal() {
public String getName() {
return name;
}
}, hashPath(name));
authorizable = user;
} else {
//principal already exists, check to make sure it is the expected type
if (authorizable.isGroup()) {
throw new RepositoryException("A group already exists with the requested name: " + name);
}
//user already exists so just update it below
}
if (extraProperties != null) {
ValueFactory valueFactory = session.getValueFactory();
Set<Entry<String, Object>> entrySet = extraProperties.entrySet();
for (Entry<String, Object> entry : entrySet) {
Value value = createValue(valueFactory, entry.getValue());
authorizable.setProperty(entry.getKey(), value);
}
}
}
use of javax.jcr.ValueFactory in project sling by apache.
the class DefaultXingOauthUserManager method updateUser.
@Override
public User updateUser(Credentials credentials) {
logger.debug("update user");
final XingUser xingUser = XingOauthUtil.getXingUser(credentials);
if (xingUser == null) {
return null;
}
try {
final Session session = getSession();
final User user = getUser(credentials);
final ValueFactory valueFactory = session.getValueFactory();
final boolean firstnameUpdated = updateUserProperty(user, valueFactory, FIRSTNAME_PROPERTY, xingUser.getFirstName());
final boolean lastnameUpdated = updateUserProperty(user, valueFactory, LASTNAME_PROPERTY, xingUser.getLastName());
if (firstnameUpdated || lastnameUpdated) {
session.save();
}
return user;
} catch (Exception e) {
logger.error(e.getMessage(), e);
return null;
}
}
use of javax.jcr.ValueFactory in project sling by apache.
the class DefaultXingOauthUserManager method createUser.
@Override
public User createUser(final Credentials credentials) {
logger.debug("create user");
final XingUser xingUser = XingOauthUtil.getXingUser(credentials);
if (xingUser == null) {
return null;
}
try {
// TODO make configurable
final String userId = xingUser.getId();
final Session session = getSession();
final UserManager userManager = getUserManager(session);
final User user = userManager.createUser(userId, null);
// TODO disable user on create?
final ValueFactory valueFactory = session.getValueFactory();
final Value firstnameValue = valueFactory.createValue(xingUser.getFirstName());
final Value lastnameValue = valueFactory.createValue(xingUser.getLastName());
user.setProperty(FIRSTNAME_PROPERTY, firstnameValue);
user.setProperty(LASTNAME_PROPERTY, lastnameValue);
session.save();
return user;
} catch (Exception e) {
logger.error(e.getMessage(), e);
return null;
}
}
use of javax.jcr.ValueFactory in project sling by apache.
the class NodeTypeDefinitionScanner method registerNodeTypes.
/**
* Registers node types found in classpath in JCR repository.
* @param session Session
* @param nodeTypeResources List of classpath resource URLs pointing to node type definitions
*/
private void registerNodeTypes(Session session, List<String> nodeTypeResources) throws RepositoryException {
ClassLoader classLoader = getClass().getClassLoader();
Workspace workspace = session.getWorkspace();
NodeTypeManager nodeTypeManager = workspace.getNodeTypeManager();
NamespaceRegistry namespaceRegistry = workspace.getNamespaceRegistry();
ValueFactory valueFactory = session.getValueFactory();
// try registering node types multiple times because the exact order is not known
int iteration = 0;
List<String> remainingNodeTypeResources = new ArrayList<String>(nodeTypeResources);
while (!remainingNodeTypeResources.isEmpty()) {
registerNodeTypesAndRemoveSucceeds(remainingNodeTypeResources, classLoader, nodeTypeManager, namespaceRegistry, valueFactory, false);
iteration++;
if (iteration >= MAX_ITERATIONS) {
break;
}
}
if (!remainingNodeTypeResources.isEmpty()) {
registerNodeTypesAndRemoveSucceeds(remainingNodeTypeResources, classLoader, nodeTypeManager, namespaceRegistry, valueFactory, true);
}
}
use of javax.jcr.ValueFactory in project sling by apache.
the class ContentImportTest method importFilesWithExtraNodesUnderJcrContent.
@Test
public void importFilesWithExtraNodesUnderJcrContent() throws Exception {
// create faceted project
IProject contentProject = projectRule.getProject();
ProjectAdapter project = new ProjectAdapter(contentProject);
project.addNatures(JavaCore.NATURE_ID, "org.eclipse.wst.common.project.facet.core.nature");
// install bundle facet
project.installFacet("sling.content", "1.0");
// create filter.xml
project.createVltFilterWithRoots("/content/test-root");
// create sling:Folder at /content/test-root
project.createOrUpdateFile(Path.fromPortableString("jcr_root/content/test-root/.content.xml"), getClass().getResourceAsStream("sling-folder-nodetype.xml"));
wstServer.waitForServerToStart();
ServerAdapter server = new ServerAdapter(wstServer.getServer());
server.installModule(contentProject);
deh.clearUnexpectedEventsAfterSettling();
// create server-side content
RepositoryAccessor repo = new RepositoryAccessor(config);
repo.tryDeleteResource("/content/test-root");
repo.createNode("/content/test-root", "sling:Folder");
repo.createFile("/content/test-root/file.txt", "hello, world".getBytes());
repo.doWithSession(new SessionRunnable<Void>() {
@Override
public Void doWithSession(Session session) throws RepositoryException {
ValueFactory valueFactory = session.getValueFactory();
Node contentNode = session.getNode("/content/test-root/file.txt/jcr:content");
contentNode.addMixin("sling:chunks");
Node chunkNode = contentNode.addNode("firstChunk", "sling:chunk");
chunkNode.setProperty("sling:offset", valueFactory.createValue(0));
chunkNode.setProperty("jcr:data", valueFactory.createValue(valueFactory.createBinary(new ByteArrayInputStream("hello, world".getBytes()))));
session.save();
return null;
}
});
assertThat(repo.getNode("/content/test-root").getPrimaryNodeType().getName(), equalTo("sling:Folder"));
runImport(contentProject);
assertThat("File not properly imported", contentProject, hasFile("jcr_root/content/test-root/file.txt", "hello, world".getBytes()));
assertThat("File extra serialization dir not imported", contentProject, hasFolder("jcr_root/content/test-root/file.txt.dir"));
assertThat("File jcr:content data not serialized in .content.xml", contentProject, hasFile("jcr_root/content/test-root/file.txt.dir/.content.xml"));
assertThat("File jcr:content extra dir not serialized as _jcr_content", contentProject, hasFolder("jcr_root/content/test-root/file.txt.dir/_jcr_content"));
assertThat("First chunk dir not serialized", contentProject, hasFolder("jcr_root/content/test-root/file.txt.dir/_jcr_content/firstChunk"));
assertThat("First chunk properties not serialized", contentProject, hasFile("jcr_root/content/test-root/file.txt.dir/_jcr_content/firstChunk/.content.xml"));
assertThat("Sling folder serialization file was deleted after import", contentProject, hasFile("jcr_root/content/test-root/.content.xml"));
}
Aggregations