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>
* <servlet-mapping>
* <servlet-name>Repository</servlet-name>
* <url-pattern>/repository/*</url-pattern>
* </servlet-mapping>
* </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");
}
}
}
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());
}
}
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);
}
}
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);
}
}
}
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();
}
}
Aggregations