Search in sources :

Example 11 with Repository

use of javax.jcr.Repository in project jackrabbit by apache.

the class JackrabbitRepositoryStub method getOrCreateRepository.

protected Repository getOrCreateRepository(String dir, String xml) throws Exception {
    synchronized (REPOSITORY_INSTANCES) {
        Repository repo = REPOSITORY_INSTANCES.get(dir);
        if (repo == null) {
            repo = createRepository(dir, xml);
            Session session = repo.login(superuser);
            try {
                TestContentLoader loader = new TestContentLoader();
                loader.loadTestContent(session);
            } finally {
                session.logout();
            }
            REPOSITORY_INSTANCES.put(dir, repo);
        }
        return repo;
    }
}
Also used : Repository(javax.jcr.Repository) Session(javax.jcr.Session)

Example 12 with Repository

use of javax.jcr.Repository in project jackrabbit by apache.

the class RepositoryStubImpl method getRepository.

@Override
public Repository getRepository() throws RepositoryStubException {
    if (repository == null) {
        repository = super.getRepository();
    }
    if (connector == null) {
        connector = new SocketConnector();
        connector.setHost("localhost");
        String pvalue = System.getProperty("org.apache.jackrabbit.jcr2dav.RepositoryStubImpl.port", "0");
        int port = pvalue.equals("") ? 0 : Integer.parseInt(pvalue);
        connector.setPort(port);
    }
    if (server == null) {
        server = new Server();
        server.addConnector(connector);
        ServletHolder holder = new ServletHolder(new JcrRemotingServlet() {

            protected Repository getRepository() {
                return repository;
            }
        });
        holder.setInitParameter(JCRWebdavServerServlet.INIT_PARAM_RESOURCE_PATH_PREFIX, "");
        holder.setInitParameter(JCRWebdavServerServlet.INIT_PARAM_MISSING_AUTH_MAPPING, "");
        holder.setInitParameter(JcrRemotingServlet.INIT_PARAM_PROTECTED_HANDLERS_CONFIG, protectedRemoveImplClass);
        Context context = new Context(server, "/");
        context.addServlet(holder, "/*");
        server.addHandler(context);
        try {
            server.start();
        } catch (Exception e) {
            throw new RepositoryStubException(e);
        }
    }
    if (client == null) {
        try {
            Map<String, String> parameters = new HashMap<String, String>();
            String uri = "http://localhost:" + connector.getLocalPort() + "/";
            String parmName = System.getProperty(this.getClass().getName() + ".REPURIPARM", JcrUtils.REPOSITORY_URI);
            parameters.put(parmName, uri);
            parameters.put(PROP_ACCESSCONTROL_PROVIDER_CLASS, acProviderImplClass);
            client = JcrUtils.getRepository(parameters);
        } catch (Exception e) {
            throw new RepositoryStubException(e);
        }
    }
    return client;
}
Also used : Context(org.mortbay.jetty.servlet.Context) Repository(javax.jcr.Repository) Server(org.mortbay.jetty.Server) HashMap(java.util.HashMap) ServletHolder(org.mortbay.jetty.servlet.ServletHolder) RepositoryStubException(org.apache.jackrabbit.test.RepositoryStubException) SocketConnector(org.mortbay.jetty.bio.SocketConnector) JcrRemotingServlet(org.apache.jackrabbit.server.remoting.davex.JcrRemotingServlet) RepositoryStubException(org.apache.jackrabbit.test.RepositoryStubException) RepositoryException(javax.jcr.RepositoryException) NotExecutableException(org.apache.jackrabbit.test.NotExecutableException)

Example 13 with Repository

use of javax.jcr.Repository in project jackrabbit by apache.

the class JcrUtils method getRepository.

/**
     * Looks up the available {@link RepositoryFactory repository factories}
     * and returns the {@link Repository repository} that one of the factories
     * returns for the given settings.
     * <p>
     * Note that unlike {@link RepositoryFactory#getRepository(Map)} this
     * method will throw an exception instead of returning <code>null</code>
     * if the given parameters can not be interpreted.
     *
     * @param parameters repository settings
     * @return repository reference
     * @throws RepositoryException if the repository can not be accessed,
     *                             or if an appropriate repository factory
     *                             is not available
     */
public static Repository getRepository(Map<String, String> parameters) throws RepositoryException {
    String newline = System.getProperty("line.separator");
    // Prepare the potential error message (JCR-2459)
    StringBuilder log = new StringBuilder("Unable to access a repository");
    if (parameters != null) {
        log.append(" with the following settings:");
        for (Map.Entry<String, String> entry : parameters.entrySet()) {
            log.append(newline);
            log.append("    ");
            log.append(entry.getKey());
            log.append(": ");
            log.append(entry.getValue());
        }
    } else {
        log.append(" with the default settings.");
    }
    // Use the query part of a repository URI as additional parameters
    if (parameters != null && parameters.containsKey(JcrUtils.REPOSITORY_URI)) {
        String uri = parameters.get(JcrUtils.REPOSITORY_URI);
        try {
            URI u = new URI(uri);
            String query = u.getRawQuery();
            if (query != null) {
                Map<String, String> copy = new HashMap<String, String>(parameters);
                for (String entry : query.split("&")) {
                    int i = entry.indexOf('=');
                    if (i != -1) {
                        copy.put(decode(entry.substring(0, i), "UTF-8"), decode(entry.substring(i + 1), "UTF-8"));
                    } else {
                        copy.put(decode(entry, "UTF-8"), Boolean.TRUE.toString());
                    }
                }
                copy.put(JcrUtils.REPOSITORY_URI, new URI(u.getScheme(), u.getRawAuthority(), u.getRawPath(), null, u.getRawFragment()).toASCIIString());
                parameters = copy;
            }
        } catch (URISyntaxException e) {
            log.append(newline);
            log.append("Note that the given repository URI was invalid:");
            log.append(newline);
            log.append("        ").append(uri);
            log.append(newline);
            log.append("        ").append(e.getMessage());
        } catch (UnsupportedEncodingException e) {
            throw new RepositoryException("UTF-8 is not supported!", e);
        }
    }
    // Iterate through the available RepositoryFactories, with logging
    log.append(newline);
    log.append("The following RepositoryFactory classes were consulted:");
    Iterator<RepositoryFactory> iterator = ServiceRegistry.lookupProviders(RepositoryFactory.class);
    while (iterator.hasNext()) {
        RepositoryFactory factory = iterator.next();
        log.append(newline);
        log.append("    ");
        log.append(factory.getClass().getName());
        try {
            Repository repository = factory.getRepository(parameters);
            if (repository != null) {
                // and just ignore the error message being built.
                return repository;
            } else {
                log.append(": declined");
            }
        } catch (Exception e) {
            log.append(": failed");
            for (Throwable c = e; c != null; c = c.getCause()) {
                log.append(newline);
                log.append("        because of ");
                log.append(c.getClass().getSimpleName());
                log.append(": ");
                log.append(c.getMessage());
            }
        }
    }
    log.append(newline);
    log.append("Perhaps the repository you are trying" + " to access is not available at the moment.");
    // detailed information we gathered during the above process.
    throw new RepositoryException(log.toString());
}
Also used : HashMap(java.util.HashMap) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RepositoryException(javax.jcr.RepositoryException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) PathNotFoundException(javax.jcr.PathNotFoundException) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Repository(javax.jcr.Repository) RepositoryFactory(javax.jcr.RepositoryFactory) HashMap(java.util.HashMap) Map(java.util.Map)

Example 14 with Repository

use of javax.jcr.Repository in project jackrabbit by apache.

the class FirstHop method main.

/**
     * The main entry point of the example application.
     * 
     * @param args
     *            command line arguments (ignored)
     * @throws Exception
     *             if an error occurs
     */
public static void main(String[] args) throws Exception {
    Repository repository = JcrUtils.getRepository();
    Session session = repository.login(new GuestCredentials());
    try {
        String user = session.getUserID();
        String name = repository.getDescriptor(Repository.REP_NAME_DESC);
        System.out.println("Logged in as " + user + " to a " + name + " repository.");
    } finally {
        session.logout();
    }
}
Also used : Repository(javax.jcr.Repository) GuestCredentials(javax.jcr.GuestCredentials) Session(javax.jcr.Session)

Example 15 with Repository

use of javax.jcr.Repository in project jackrabbit by apache.

the class TestRepository method getIntegratedInstance.

/**
     * Attempts to retrieve the test repository instance used by the
     * Jackrabbit main test suite without having a direct dependency to any
     * of the classes in src/test/java. This method assumes that we are
     * running within the Jackrabbit main test suite if the AbstractJCRTest
     * class is available. The initialized RepositoryHelper instance is
     * retrieved from the static "helper" field of the AbstractJCRTest class,
     * and the underlying repository and configured superuser credentials are
     * extracted from the helper instance. This information is in turn used
     * to create a custom Repository adapter that delegates calls to the
     * underlying repository and uses the superuser credentials for the login
     * methods where no credentials are passed by the client.
     *
     * @return test repository instance
     * @throws Exception if the test repository could not be retrieved
     */
private static Repository getIntegratedInstance() throws Exception {
    Class test = Class.forName("org.apache.jackrabbit.test.AbstractJCRTest");
    Map helper = new BeanMap(test.getField("helper").get(null));
    final Repository repository = (Repository) helper.get("repository");
    final Credentials superuser = (Credentials) helper.get("superuserCredentials");
    return new ProxyRepository(new RepositoryFactory() {

        public Repository getRepository() throws RepositoryException {
            return repository;
        }
    }) {

        public Session login(String workspace) throws RepositoryException {
            return repository.login(superuser, workspace);
        }

        public Session login() throws RepositoryException {
            return repository.login(superuser);
        }
    };
}
Also used : BeanMap(org.apache.commons.collections.BeanMap) Repository(javax.jcr.Repository) ProxyRepository(org.apache.jackrabbit.commons.repository.ProxyRepository) ProxyRepository(org.apache.jackrabbit.commons.repository.ProxyRepository) RepositoryException(javax.jcr.RepositoryException) RepositoryFactory(org.apache.jackrabbit.commons.repository.RepositoryFactory) Map(java.util.Map) BeanMap(org.apache.commons.collections.BeanMap) Credentials(javax.jcr.Credentials)

Aggregations

Repository (javax.jcr.Repository)107 Session (javax.jcr.Session)31 RepositoryException (javax.jcr.RepositoryException)26 SimpleCredentials (javax.jcr.SimpleCredentials)15 Test (org.junit.Test)13 HashMap (java.util.HashMap)12 Node (javax.jcr.Node)12 DocumentMK (org.apache.jackrabbit.oak.plugins.document.DocumentMK)10 SlingRepository (org.apache.sling.jcr.api.SlingRepository)10 JackrabbitRepository (org.apache.jackrabbit.api.JackrabbitRepository)9 Oak (org.apache.jackrabbit.oak.Oak)7 Jcr (org.apache.jackrabbit.oak.jcr.Jcr)7 IOException (java.io.IOException)6 Map (java.util.Map)6 InitialContext (javax.naming.InitialContext)6 NamingException (javax.naming.NamingException)6 ServletContext (javax.servlet.ServletContext)5 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)5 File (java.io.File)4 OutputStreamWriter (java.io.OutputStreamWriter)4