Search in sources :

Example 66 with Repository

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

the class AbstractRepositoryServlet method doGet.

/**
     * Outputs the repository descriptors either as a collection of properties
     * (see {@link Properties#store(java.io.OutputStream, String)} or
     * individually addressable text/plain resources based on the request URI.
     * <p>
     * A typical mapping for a repository servlet would be:
     * <pre>
     * &lt;servlet-mapping&gt;
     *   &lt;servlet-name&gt;Repository&lt;/servlet-name&gt;
     *   &lt;url-pattern&gt;/repository/*&lt;/url-pattern&gt;
     * &lt;/servlet-mapping&gt;
     * </pre>
     * <p>
     * This mapping would allow clients to retrieve all repository descriptors
     * from <code>http://server/context/repository/</code> and to address
     * individual descriptors by key with URIs like
     * <code>http://server/context/repository/<i>key</i></code>.
     * For example, the name of the repository vendor could be retrieved from
     * <code>http://server/context/repository/jcr.repository.vendor</code>.
     * Likewise, a 404 (not found) response from
     * <code>http://server/context/repository/level.2.supported</code> would
     * indicate that the repository does not support Level 2 features.
     * <p>
     * Note that mapping a repository servlet to the URL space is optional,
     * as the main purpose of the servlet is to make a repository available
     * in the servlet context, not to expose repository information to web
     * clients.
     *
     * @param request HTTP request
     * @param response HTTP response
     * @throws IOException on IO errors
     * @throws ServletException on servlet errors
     */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    Repository repository = new ServletRepository(this);
    String info = request.getPathInfo();
    if (info == null || info.equals("/")) {
        Properties descriptors = new Properties();
        String[] keys = repository.getDescriptorKeys();
        for (int i = 0; i < keys.length; i++) {
            descriptors.setProperty(keys[i], repository.getDescriptor(keys[i]));
        }
        // TODO: Using UTF-8 instead of ISO-8859-1 would be better, but
        // would require re-implementing the Properties.store() method
        response.setContentType("text/plain; charset=ISO-8859-1");
        descriptors.store(response.getOutputStream(), getAttributeName());
    } else {
        // skip the leading "/"
        String key = info.substring(1);
        String descriptor = repository.getDescriptor(key);
        if (descriptor != null) {
            response.setContentType("text/plain; charset=UTF-8");
            response.getWriter().write(descriptor);
        } else {
            response.sendError(HttpServletResponse.SC_NOT_FOUND, "Repository descriptor " + key + " not found");
        }
    }
}
Also used : Repository(javax.jcr.Repository) ProxyRepository(org.apache.jackrabbit.commons.repository.ProxyRepository) Properties(java.util.Properties)

Example 67 with Repository

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

the class ContextRepositoryServlet method getRepository.

/**
     * Creates and returns the repository in the configured servlet
     * context attribute.
     *
     * @return repository
     */
protected Repository getRepository() throws RepositoryException {
    String path = getInitParameter("path");
    String name = getInitParameter("name", Repository.class.getName());
    ServletContext context = getServletContext();
    if (path != null && context.equals(context.getContext(path))) {
        path = null;
    }
    if (path == null && name.equals(getAttributeName())) {
        throw new RepositoryException("Invalid configuration: Can not duplicate attribute " + name + " of servlet " + getServletName());
    }
    ServletContext otherContext = context.getContext(path);
    if (otherContext == null) {
        throw new RepositoryException("Repository not found: Servlet context " + path + " does not exist or is not accessible from context " + context.getServletContextName());
    }
    Object repository = otherContext.getAttribute(name);
    if (repository instanceof Repository) {
        return (Repository) repository;
    } else if (repository != null) {
        throw new RepositoryException("Invalid repository: Attribute " + name + " in servlet context " + otherContext.getServletContextName() + " is an instance of " + repository.getClass().getName());
    } else {
        throw new RepositoryException("Repository not found: Attribute " + name + " does not exist in servlet context " + otherContext.getServletContextName());
    }
}
Also used : Repository(javax.jcr.Repository) ServletContext(javax.servlet.ServletContext) RepositoryException(javax.jcr.RepositoryException)

Example 68 with Repository

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

the class JNDIRepositoryServlet method getRepository.

/**
     * Returns a JNDI repository based on the configured init parameters.
     *
     * @return JNDI repository
     */
protected Repository getRepository() throws RepositoryException {
    try {
        String location = Repository.class.getName().replace('.', '/');
        Hashtable environment = new Hashtable();
        Enumeration names = getInitParameterNames();
        while (names.hasMoreElements()) {
            String name = (String) names.nextElement();
            if (name.equals("location")) {
                location = getInitParameter(name);
            } else if (!name.equals(Repository.class.getName())) {
                environment.put(name, getInitParameter(name));
            }
        }
        return new JNDIRepositoryFactory(new InitialContext(environment), location).getRepository();
    } catch (NamingException e) {
        throw new RepositoryException("Repository not found: Invalid JNDI context", e);
    }
}
Also used : Repository(javax.jcr.Repository) Enumeration(java.util.Enumeration) Hashtable(java.util.Hashtable) JNDIRepositoryFactory(org.apache.jackrabbit.commons.repository.JNDIRepositoryFactory) NamingException(javax.naming.NamingException) RepositoryException(javax.jcr.RepositoryException) InitialContext(javax.naming.InitialContext)

Example 69 with Repository

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

the class RepositoryDescriptorTest method testRequiredDescriptors.

/**
     * Tests that the required repository descriptors are available.
     */
public void testRequiredDescriptors() {
    Repository rep = session.getRepository();
    for (Iterator<String> it = requiredDescriptorKeys.iterator(); it.hasNext(); ) {
        String descName = it.next();
        assertTrue(descName + " is a standard descriptor", rep.isStandardDescriptor(descName));
        if (rep.isSingleValueDescriptor(descName)) {
            Value val = rep.getDescriptorValue(descName);
            assertNotNull("Required descriptor is missing: " + descName, val);
        } else {
            Value[] vals = rep.getDescriptorValues(descName);
            assertNotNull("Required descriptor is missing: " + descName, vals);
        }
    }
}
Also used : Repository(javax.jcr.Repository) Value(javax.jcr.Value)

Example 70 with Repository

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

the class SecondHop 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 SimpleCredentials("admin", "admin".toCharArray()));
    try {
        Node root = session.getRootNode();
        // Store content
        Node hello = root.addNode("hello");
        Node world = hello.addNode("world");
        world.setProperty("message", "Hello, World!");
        session.save();
        // Retrieve content
        Node node = root.getNode("hello/world");
        System.out.println(node.getPath());
        System.out.println(node.getProperty("message").getString());
        // Remove content
        root.getNode("hello").remove();
        session.save();
    } finally {
        session.logout();
    }
}
Also used : SimpleCredentials(javax.jcr.SimpleCredentials) Repository(javax.jcr.Repository) Node(javax.jcr.Node) Session(javax.jcr.Session)

Aggregations

Repository (javax.jcr.Repository)111 Session (javax.jcr.Session)33 RepositoryException (javax.jcr.RepositoryException)28 SimpleCredentials (javax.jcr.SimpleCredentials)15 Node (javax.jcr.Node)14 Test (org.junit.Test)13 HashMap (java.util.HashMap)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