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());
}
}
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());
}
}
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;
}
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
}
}
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();
}
}
}
Aggregations