Search in sources :

Example 36 with ValueFactory

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);
        }
    }
}
Also used : User(org.apache.jackrabbit.api.security.user.User) Node(javax.jcr.Node) RepositoryException(javax.jcr.RepositoryException) ValueFactory(javax.jcr.ValueFactory) Entry(java.util.Map.Entry) UserManager(org.apache.jackrabbit.api.security.user.UserManager) Value(javax.jcr.Value) Authorizable(org.apache.jackrabbit.api.security.user.Authorizable) Principal(java.security.Principal) Session(javax.jcr.Session)

Example 37 with ValueFactory

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;
    }
}
Also used : User(org.apache.jackrabbit.api.security.user.User) XingUser(org.apache.sling.auth.xing.api.XingUser) XingUser(org.apache.sling.auth.xing.api.XingUser) ValueFactory(javax.jcr.ValueFactory) RepositoryException(javax.jcr.RepositoryException) Session(javax.jcr.Session)

Example 38 with ValueFactory

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;
    }
}
Also used : User(org.apache.jackrabbit.api.security.user.User) XingUser(org.apache.sling.auth.xing.api.XingUser) UserManager(org.apache.jackrabbit.api.security.user.UserManager) AbstractXingUserManager(org.apache.sling.auth.xing.api.AbstractXingUserManager) XingOauthUserManager(org.apache.sling.auth.xing.oauth.XingOauthUserManager) Value(javax.jcr.Value) XingUser(org.apache.sling.auth.xing.api.XingUser) ValueFactory(javax.jcr.ValueFactory) RepositoryException(javax.jcr.RepositoryException) Session(javax.jcr.Session)

Example 39 with ValueFactory

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);
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NamespaceRegistry(javax.jcr.NamespaceRegistry) ArrayList(java.util.ArrayList) ValueFactory(javax.jcr.ValueFactory) Workspace(javax.jcr.Workspace)

Example 40 with ValueFactory

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"));
}
Also used : ServerAdapter(org.apache.sling.ide.test.impl.helpers.ServerAdapter) RepositoryAccessor(org.apache.sling.ide.test.impl.helpers.RepositoryAccessor) ByteArrayInputStream(java.io.ByteArrayInputStream) Node(javax.jcr.Node) ProjectAdapter(org.apache.sling.ide.test.impl.helpers.ProjectAdapter) RepositoryException(javax.jcr.RepositoryException) ValueFactory(javax.jcr.ValueFactory) IProject(org.eclipse.core.resources.IProject) Session(javax.jcr.Session) Test(org.junit.Test)

Aggregations

ValueFactory (javax.jcr.ValueFactory)105 Value (javax.jcr.Value)51 Node (javax.jcr.Node)50 Session (javax.jcr.Session)40 Test (org.junit.Test)17 RepositoryException (javax.jcr.RepositoryException)16 InputStream (java.io.InputStream)13 AccessControlManager (javax.jcr.security.AccessControlManager)13 HashMap (java.util.HashMap)12 Privilege (javax.jcr.security.Privilege)12 Property (javax.jcr.Property)11 ByteArrayInputStream (java.io.ByteArrayInputStream)10 Query (javax.jcr.query.Query)8 Calendar (java.util.Calendar)7 QueryManager (javax.jcr.query.QueryManager)7 RowIterator (javax.jcr.query.RowIterator)7 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)7 NodeIterator (javax.jcr.NodeIterator)6 JackrabbitNode (org.apache.jackrabbit.api.JackrabbitNode)6 IOException (java.io.IOException)5