Package javax.jcr

Examples of javax.jcr.Session



        try {
            ResourceResolver resourceResolver = resourceResolverFactory.getAdministrativeResourceResolver(null);

            Session session = resourceResolver.adaptTo(Session.class);

            JackrabbitSession jackrabbitSession = (JackrabbitSession) session;

            UserManager userManager =  jackrabbitSession.getUserManager();


            if (userManager.getAuthorizable("testUser") == null) {
                userManager.createUser("testUser", "password");
            }


            final Principal testUserPrincipal = new Principal() {
                public String getName() {
                    return "testUser";
                }};
               
            session.save();

        } catch (Exception e) {
            e.printStackTrace()//To change body of catch statement use File | Settings | File Templates.
        }
    }
View Full Code Here


        public Resource next() {
            Principal nextPrincipal = principals.nextPrincipal();
            try {
                ResourceResolver resourceResolver = parent.getResourceResolver();
                if (resourceResolver != null) {
                    Session session = resourceResolver.adaptTo(Session.class);
                    if (session != null) {
                        UserManager userManager = AccessControlUtil.getUserManager(session);
                        if (userManager != null) {
                            Authorizable authorizable = userManager.getAuthorizable(nextPrincipal.getName());
                            if (authorizable != null) {
View Full Code Here

        }
        logger.info("Finished setting up SlingShot");
    }

    private void setupACL(final ResourceResolver resolver) throws RepositoryException {
        final Session session = resolver.adaptTo(Session.class);

        for(final String principalId : USERS) {
            final String resourcePath = SlingshotConstants.APP_ROOT_PATH + "/public/" + principalId;

            final Map<String, String> privileges = new HashMap<String, String>();
View Full Code Here

            public Session getSession(final HttpServletRequest req, final Repository repository, final String workspace)
                    throws LoginException, RepositoryException, ServletException {
                final ResourceResolver resolver = (ResourceResolver) req.getAttribute(AuthenticationSupport.REQUEST_ATTRIBUTE_RESOLVER);
                if (resolver != null) {
                    final Session session = resolver.adaptTo(Session.class);
                    if (session != null) {
                        final Session newSession = getLongLivedSession(session);
                        log.debug("getSession: Creating new Session ({}) for {}", newSession, newSession.getUserID());
                        return newSession;
                    }
                }

                throw new ServletException("ResourceResolver missing or not providing on JCR Session");
            }

            public void releaseSession(final Session session) {
                log.debug("releaseSession: Logging out long lived Session ({})", session);
                session.logout();
            }

            /**
             * Creates a new session for the user of the slingSession in the
             * same workspace as the slingSession.
             * <p>
             * Assumption: The admin session has permission to impersonate
             * as any user without restriction. If this is not the case
             * the Session.impersonate method throws a LoginException
             * which is folded into a RepositoryException.
             *
             * @param slingSession The session provided by the Sling
             *            authentication mechanis,
             * @return a new session which may (and will) outlast the request
             * @throws RepositoryException If an error occurrs creating the
             *             session.
             */
            private Session getLongLivedSession(final Session slingSession) throws RepositoryException {
                Session adminSession = null;
                final String user = slingSession.getUserID();
                try {
                    final SimpleCredentials credentials = new SimpleCredentials(user, EMPTY_PW);
                    final String wsp = slingSession.getWorkspace().getName();
                    adminSession = SlingDavExServlet.this.repository.loginAdministrative(wsp);
                    return adminSession.impersonate(credentials);
                } catch (RepositoryException re) {

                    // LoginException from impersonate (missing permission)
                    // and RepositoryException from loginAdministrative and
                    // impersonate folded into RepositoryException to
                    // cause a 403/FORBIDDEN response
                    throw new RepositoryException("Cannot get session for " + user, re);

                } finally {
                    if (adminSession != null) {
                        adminSession.logout();
                    }
                }
            }
        };
    }
View Full Code Here

    @Before
    public void setup() throws Exception {
        resourceResolver = resourceResolverFactory.getAdministrativeResourceResolver(null);

        Session session = resourceResolver.adaptTo(Session.class);
        testPath = "/test_" + new Date().getTime();
        Node testNode = session.getRootNode().addNode(testPath.substring(1), "nt:unstructured");
        testNode.setProperty("sling:resourceType", new String[] { "foo/bar", "bar/foo" });
        session.save();
    }
View Full Code Here

    protected void handleOperation(SlingHttpServletRequest request,
        AbstractPostResponse response, List<Modification> changes)
            throws RepositoryException {


        Session session = request.getResourceResolver().adaptTo(Session.class);
        String principalName = request.getParameter(SlingPostConstants.RP_NODE_NAME);
        User user = createUser(session,
                            principalName,
                            request.getParameter("pwd"),
                            request.getParameter("pwdConfirm"),
View Full Code Here

        Assert.assertEquals("foo/bar", res.getResourceType());
    }

    @After
    public void teardown() throws Exception {
        Session session = resourceResolver.adaptTo(Session.class);
        Node testNode = session.getNode(testPath);
        testNode.remove();
        session.save();
    }
View Full Code Here

            throw new RepositoryException(
                "Password value does not match the confirmation password");
        }

        User user = null;
        Session selfRegSession = jcrSession;
        boolean useAdminSession = !administrator && selfRegistrationEnabled;
        try {
            if (useAdminSession) {
                //the current user doesn't have permission to create the user,
                // but self-registration is enabled, so use an admin session
                // to do the work.
                selfRegSession = getSession();
            }

            UserManager userManager = AccessControlUtil.getUserManager(selfRegSession);
            Authorizable authorizable = userManager.getAuthorizable(name);

            if (authorizable != null) {
                // user already exists!
                throw new RepositoryException(
                    "A principal already exists with the requested name: "
                        + name);
            } else {
                user = userManager.createUser(name, password);
                String userPath = AuthorizableResourceProvider.SYSTEM_USER_MANAGER_USER_PREFIX
                    + user.getID();

                Map<String, RequestProperty> reqProperties = collectContent(
                    properties, userPath);

                changes.add(Modification.onCreated(userPath));

                // write content from form
                writeContent(selfRegSession, user, reqProperties, changes);

                if (selfRegSession.hasPendingChanges()) {
                    selfRegSession.save();
                }

                if (useAdminSession) {
                    //lookup the user from the user session so we can return a live object
                    UserManager userManager2 = AccessControlUtil.getUserManager(jcrSession);
View Full Code Here

        path = ResourceUtil.getParent(path);
        if (path != null) {
            response.setParentLocation(externalizePath(request, path));
        }

        Session session = request.getResourceResolver().adaptTo(Session.class);

        final List<Modification> changes = new ArrayList<Modification>();

        try {
            handleOperation(request, response, changes);

            // TODO: maybe handle SlingAuthorizablePostProcessor handlers here

            // set changes on html response
            for (Modification change : changes) {
                switch (change.getType()) {
                    case MODIFY:
                        response.onModified(change.getSource());
                        break;
                    case DELETE:
                        response.onDeleted(change.getSource());
                        break;
                    case MOVE:
                        response.onMoved(change.getSource(),
                            change.getDestination());
                        break;
                    case COPY:
                        response.onCopied(change.getSource(),
                            change.getDestination());
                        break;
                    case CREATE:
                        response.onCreated(change.getSource());
                        break;
                    case ORDER:
                        response.onChange("ordered", change.getSource(),
                            change.getDestination());
                        break;
                }
            }

            if (session.hasPendingChanges()) {
                session.save();
            }
        } catch (ResourceNotFoundException rnfe) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND,
                rnfe.getMessage());
        } catch (Throwable throwable) {
            log.debug("Exception while handling POST "
                + request.getResource().getPath() + " with "
                + getClass().getName(), throwable);
            response.setError(throwable);
        } finally {
            try {
                if (session.hasPendingChanges()) {
                    session.refresh(false);
                }
            } catch (RepositoryException e) {
                log.warn("RepositoryException in finally block: {}",
                    e.getMessage(), e);
            }
View Full Code Here

        session.getRootNode().addNode(path.substring(1), "nt:unstructured");
        session.save();
    }

    private void generateEvents() throws Exception {
        final Session session = getRepository().loginAdministrative(null);

        try {
            // create the nodes
            createNode(session, createdPath);
            createNode(session, pathToModify);
            createNode(session, pathToDelete);

            Thread.sleep(1000);

            // modify
            final Node modified = session.getNode(pathToModify);
            modified.setProperty("foo", "bar");

            session.save();

            // delete
            final Node deleted = session.getNode(pathToDelete);
            deleted.remove();
            session.save();

            Thread.sleep(3500);

        } finally {
            session.logout();
        }
    }
View Full Code Here

TOP

Related Classes of javax.jcr.Session

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.