Search in sources :

Example 61 with Repository

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

the class WorkspaceResourceImpl method spool.

/**
     * @param outputContext
     * @throws IOException
     */
@Override
public void spool(OutputContext outputContext) throws IOException {
    outputContext.setProperty("Link", "<?" + EventJournalResourceImpl.RELURIFROMWORKSPACE + ">; title=\"Event Journal\"; rel=alternate; type=\"" + AtomFeedConstants.MEDIATYPE + "\"");
    if (outputContext.hasStream()) {
        Session session = getRepositorySession();
        Repository rep = session.getRepository();
        String repName = rep.getDescriptor(Repository.REP_NAME_DESC);
        String repURL = rep.getDescriptor(Repository.REP_VENDOR_URL_DESC);
        String repVersion = rep.getDescriptor(Repository.REP_VERSION_DESC);
        String repostr = repName + " " + repVersion;
        StringBuilder sb = new StringBuilder();
        sb.append("<html><head><title>");
        sb.append(repostr);
        sb.append("</title>");
        sb.append("<link rel=alternate type=\"" + AtomFeedConstants.MEDIATYPE + "\" title=\"Event Journal\" href=\"?" + EventJournalResourceImpl.RELURIFROMWORKSPACE + "\">");
        sb.append("</head>");
        sb.append("<body><h2>").append(repostr).append("</h2><ul>");
        sb.append("<li><a href=\"..\">..</a></li>");
        DavResourceIterator it = getMembers();
        while (it.hasNext()) {
            DavResource res = it.nextResource();
            sb.append("<li><a href=\"");
            sb.append(res.getHref());
            sb.append("\">");
            sb.append(res.getDisplayName());
            sb.append("</a></li>");
        }
        sb.append("</ul><hr size=\"1\"><em>Powered by <a href=\"");
        sb.append(repURL).append("\">").append(repName);
        sb.append("</a> ").append(repVersion);
        sb.append("</em></body></html>");
        outputContext.setContentLength(sb.length());
        outputContext.setModificationTime(getModificationTime());
        PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputContext.getOutputStream(), "utf8"));
        writer.print(sb.toString());
        writer.close();
    } else {
        outputContext.setContentLength(0);
        outputContext.setModificationTime(getModificationTime());
    }
}
Also used : DavResourceIterator(org.apache.jackrabbit.webdav.DavResourceIterator) Repository(javax.jcr.Repository) DavResource(org.apache.jackrabbit.webdav.DavResource) OutputStreamWriter(java.io.OutputStreamWriter) Session(javax.jcr.Session) PrintWriter(java.io.PrintWriter)

Example 62 with Repository

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

the class RootCollection method spool.

/**
     * Sets content lengths to '0' and retrieves the modification time.
     *
     * @param outputContext
     * @throws IOException
     * @see DavResource#spool(org.apache.jackrabbit.webdav.io.OutputContext)
     */
@Override
public void spool(OutputContext outputContext) throws IOException {
    if (outputContext.hasStream()) {
        Session session = getRepositorySession();
        Repository rep = session.getRepository();
        String repName = rep.getDescriptor(Repository.REP_NAME_DESC);
        String repURL = rep.getDescriptor(Repository.REP_VENDOR_URL_DESC);
        String repVersion = rep.getDescriptor(Repository.REP_VERSION_DESC);
        String repostr = repName + " " + repVersion;
        StringBuilder sb = new StringBuilder();
        sb.append("<html><head><title>");
        sb.append(repostr);
        sb.append("</title></head>");
        sb.append("<body><h2>").append(repostr).append("</h2>");
        sb.append("<h3>Available Workspace Resources:</h3><ul>");
        DavResourceIterator it = getMembers();
        while (it.hasNext()) {
            DavResource res = it.nextResource();
            sb.append("<li><a href=\"");
            sb.append(res.getHref());
            sb.append("\">");
            sb.append(res.getDisplayName());
            sb.append("</a></li>");
        }
        sb.append("</ul><hr size=\"1\"><em>Powered by <a href=\"");
        sb.append(repURL).append("\">").append(repName);
        sb.append("</a> ").append(repVersion);
        sb.append("</em></body></html>");
        outputContext.setContentLength(sb.length());
        outputContext.setModificationTime(getModificationTime());
        PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputContext.getOutputStream(), "utf8"));
        writer.print(sb.toString());
        writer.close();
    } else {
        outputContext.setContentLength(0);
        outputContext.setModificationTime(getModificationTime());
    }
}
Also used : DavResourceIterator(org.apache.jackrabbit.webdav.DavResourceIterator) Repository(javax.jcr.Repository) DavResource(org.apache.jackrabbit.webdav.DavResource) OutputStreamWriter(java.io.OutputStreamWriter) Session(javax.jcr.Session) PrintWriter(java.io.PrintWriter)

Example 63 with Repository

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

the class RepositoryDescriptorsReport method toXml.

/**
     * Returns a Xml representation of the repository descriptors according
     * to the info object.
     *
     * @return Xml representation of the repository descriptors
     * @see org.apache.jackrabbit.webdav.xml.XmlSerializable#toXml(Document)
     * @param document
     */
public Element toXml(Document document) {
    Repository repository = getRepositorySession().getRepository();
    Element report = DomUtil.createElement(document, "repositorydescriptors-report", NAMESPACE);
    for (String key : repository.getDescriptorKeys()) {
        Element elem = DomUtil.addChildElement(report, XML_DESCRIPTOR, NAMESPACE);
        DomUtil.addChildElement(elem, XML_DESCRIPTORKEY, NAMESPACE, key);
        for (Value v : repository.getDescriptorValues(key)) {
            String value;
            try {
                value = v.getString();
            } catch (RepositoryException e) {
                log.error("Internal error while reading descriptor value: ", e);
                value = repository.getDescriptor(key);
            }
            Element child = DomUtil.addChildElement(elem, XML_DESCRIPTORVALUE, NAMESPACE, value);
            if (PropertyType.STRING != v.getType()) {
                child.setAttribute(ATTR_VALUE_TYPE, PropertyType.nameFromValue(v.getType()));
            }
        }
    }
    return report;
}
Also used : Repository(javax.jcr.Repository) Element(org.w3c.dom.Element) Value(javax.jcr.Value) RepositoryException(javax.jcr.RepositoryException)

Example 64 with Repository

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

the class TokenBasedLoginTest method testLogin.

public void testLogin() throws RepositoryException {
    Repository repo = getHelper().getRepository();
    // make sure regular simple login works.
    Session s = repo.login(creds);
    s.logout();
    // test if token creation works.
    creds.setAttribute(TOKEN_ATTRIBUTE, "");
    // an additional attribute that must match
    creds.setAttribute(TOKEN_ATTRIBUTE + ".any", "any");
    // an attribute just for info purposes
    creds.setAttribute("attr", "attr");
    String token = null;
    s = repo.login(creds);
    try {
        // token credentials must be created
        Set<TokenCredentials> tokenCreds = ((SessionImpl) s).getSubject().getPublicCredentials(TokenCredentials.class);
        assertFalse(tokenCreds.isEmpty());
        assertEquals(1, tokenCreds.size());
        TokenCredentials tc = tokenCreds.iterator().next();
        token = tc.getToken();
        // original simple credentials: token attribute should be updated
        assertNotNull(creds.getAttribute(TOKEN_ATTRIBUTE));
        assertFalse("".equals(creds.getAttribute(TOKEN_ATTRIBUTE)));
        // simple credentials must also be present on the subject
        Set<SimpleCredentials> scs = ((SessionImpl) s).getSubject().getPublicCredentials(SimpleCredentials.class);
        assertFalse(scs.isEmpty());
        assertEquals(1, scs.size());
        SimpleCredentials sc = scs.iterator().next();
        assertNotNull(sc.getAttribute(TOKEN_ATTRIBUTE));
        assertFalse("".equals(sc.getAttribute(TOKEN_ATTRIBUTE)));
        // test if session attributes only exposed non-mandatory attributes
        assertNull(s.getAttribute(TOKEN_ATTRIBUTE));
        for (String attrName : tc.getAttributeNames()) {
            if (TokenBasedAuthentication.isMandatoryAttribute(attrName)) {
                assertNull(s.getAttribute(attrName));
            } else {
                assertEquals(tc.getAttribute(attrName), s.getAttribute(attrName));
            }
        }
        // workspace as 'superuser' has been created for.
        if (superuser.nodeExists(testuserPath)) {
            Node userNode = superuser.getNode(testuserPath);
            assertTrue(userNode.hasNode(TOKENS_NAME));
            Node tNode = userNode.getNode(TOKENS_NAME);
            assertTrue(tNode.hasNodes());
            Node ttNode = tNode.getNodes().nextNode();
            assertTrue(ttNode.hasProperty("attr"));
            assertEquals("attr", ttNode.getProperty("attr").getString());
            assertTrue(ttNode.hasProperty(TOKEN_ATTRIBUTE + ".any"));
            assertEquals("any", ttNode.getProperty(TOKEN_ATTRIBUTE + ".any").getString());
            String id = ttNode.getIdentifier();
            assertTrue(token.startsWith(id));
        }
    } finally {
        s.logout();
    }
    // login with token only must succeed as well.
    TokenCredentials tokenOnly = new TokenCredentials(token);
    tokenOnly.setAttribute(TOKEN_ATTRIBUTE + ".any", "any");
    s = repo.login(tokenOnly);
    try {
        assertEquals(creds.getUserID(), s.getUserID());
        Set<TokenCredentials> tokenCreds = ((SessionImpl) s).getSubject().getPublicCredentials(TokenCredentials.class);
        assertFalse(tokenCreds.isEmpty());
        assertEquals(1, tokenCreds.size());
        TokenCredentials tc = tokenCreds.iterator().next();
        String tk = tc.getToken();
        assertEquals(token, tk);
        assertNull(s.getAttribute(TOKEN_ATTRIBUTE));
        for (String attrName : tc.getAttributeNames()) {
            if (TokenBasedAuthentication.isMandatoryAttribute(attrName)) {
                assertNull(s.getAttribute(attrName));
            } else {
                assertEquals(tc.getAttribute(attrName), s.getAttribute(attrName));
            }
        }
    } finally {
        s.logout();
    }
    // the non-mandatory attribute may have any value if present with the creds.
    tokenOnly.setAttribute("attr", "another");
    s = repo.login(tokenOnly);
    try {
        assertEquals(creds.getUserID(), s.getUserID());
    } finally {
        s.logout();
        tokenOnly.removeAttribute("attr");
    }
    // login with token but wrong mandatory attribute
    tokenOnly.setAttribute(TOKEN_ATTRIBUTE + ".any", "another");
    try {
        s = repo.login(tokenOnly);
        s.logout();
        fail("The additional mandatory attr doesn't match. login must fail.");
    } catch (LoginException e) {
    // success
    }
    // login with token but missing the mandatory attribute
    tokenOnly.removeAttribute(TOKEN_ATTRIBUTE + ".any");
    try {
        s = repo.login(tokenOnly);
        s.logout();
        fail("The additional mandatory attr is missing. login must fail.");
    } catch (LoginException e) {
    // success
    }
}
Also used : SimpleCredentials(javax.jcr.SimpleCredentials) Repository(javax.jcr.Repository) Node(javax.jcr.Node) LoginException(javax.jcr.LoginException) Session(javax.jcr.Session) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession) TokenCredentials(org.apache.jackrabbit.api.security.authentication.token.TokenCredentials)

Example 65 with Repository

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

the class LitmusTest method testLitmus.

public void testLitmus() throws Exception {
    File dir = new File("target", "litmus");
    String litmus = System.getProperty("litmus", "litmus");
    if (Boolean.getBoolean("jackrabbit.test.integration") && isLitmusAvailable(litmus)) {
        final Repository repository = JcrUtils.getRepository("jcr-jackrabbit://" + Text.escapePath(dir.getCanonicalPath()));
        // for the TransientRepository
        Session session = repository.login();
        try {
            SocketConnector connector = new SocketConnector();
            connector.setHost("localhost");
            connector.setPort(Integer.getInteger("litmus.port", 0));
            Server server = new Server();
            server.addConnector(connector);
            ServletHolder holder = new ServletHolder(new SimpleWebdavServlet() {

                @Override
                public Repository getRepository() {
                    return repository;
                }
            });
            holder.setInitParameter("resource-config", "/config.xml");
            Context context = new Context(server, "/");
            context.setResourceBase("src/test/resources");
            context.addServlet(holder, "/*");
            server.addHandler(context);
            server.start();
            try {
                int port = connector.getLocalPort();
                String url = "http://localhost:" + port + "/default";
                ProcessBuilder builder = new ProcessBuilder(litmus, url, "admin", "admin");
                builder.directory(dir);
                builder.redirectErrorStream();
                assertLitmus(builder, "basic", 0);
                assertLitmus(builder, "http", 0);
                assertLitmus(builder, "props", 0);
                // FIXME: JCR-2637: WebDAV shallow copy test failure
                assertLitmus(builder, "copymove", 1);
                // FIXME: JCR-2638: Litmus locks test failures
                assertLitmus(builder, "locks", 1);
            } finally {
                server.stop();
            }
        } finally {
            session.logout();
        }
    }
}
Also used : Context(org.mortbay.jetty.servlet.Context) Repository(javax.jcr.Repository) Server(org.mortbay.jetty.Server) ServletHolder(org.mortbay.jetty.servlet.ServletHolder) File(java.io.File) SocketConnector(org.mortbay.jetty.bio.SocketConnector) 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