@BeforeClass public static void setUp() throws Exception { TransactionManager transact = null; Txn transaction = null; try { pool = startDB(); broker = pool.get(pool.getSecurityManager().getSystemSubject()); transact = pool.getTransactionManager(); transaction = transact.beginTransaction(); root = broker.getOrCreateCollection( transaction, XmldbURI.create(XmldbURI.ROOT_COLLECTION + "/test")); broker.saveCollection(transaction, root); String existHome = System.getProperty("exist.home"); File existDir = existHome == null ? new File(".") : new File(existHome); String directory = "samples/shakespeare"; File dir = new File(existDir, directory); // store some documents. for (File f : dir.listFiles(new XMLFilenameFilter())) { IndexInfo info = root.validateXMLResource( transaction, broker, XmldbURI.create(f.getName()), new InputSource(f.toURI().toASCIIString())); root.store(transaction, broker, info, new InputSource(f.toURI().toASCIIString()), false); } IndexInfo info = root.validateXMLResource(transaction, broker, XmldbURI.create("nested.xml"), NESTED_XML); root.store(transaction, broker, info, NESTED_XML, false); transact.commit(transaction); // for the tests docs = root.allDocs(broker, new DefaultDocumentSet(), true); seqSpeech = executeQuery(broker, "//SPEECH", 2628, null); } catch (Exception e) { if (pool != null) { pool.release(broker); BrokerPool.stopAll(false); pool = null; root = null; } throw e; } }
private <R> R executeWithBroker(BrokerOperation<R> brokerOperation) throws XMLDBException, LockException, PermissionDeniedException, IOException, EXistException, TriggerException, SyntaxException { final Subject preserveSubject = pool.getSubject(); DBBroker broker = null; try { broker = pool.get(user); return brokerOperation.withBroker(broker); } finally { if (broker != null) { pool.release(broker); } if (preserveSubject != null) { pool.setSubject(preserveSubject); } } }
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException { try { // Get the path String path = request.getPathInfo(); if (path == null) { response.sendError( HttpServletResponse.SC_BAD_REQUEST, "URL has no extra path information specified."); return; } int firstSlash = path.indexOf('/', 1); if (firstSlash < 0 && path.length() == 1) { response.sendError(400, "Module not specified."); return; } String moduleName = firstSlash < 0 ? path.substring(1) : path.substring(1, firstSlash); path = firstSlash < 0 ? "" : path.substring(firstSlash); AtomModule module = (AtomModule) modules.get(moduleName); if (module == null) { response.sendError(400, "Module " + moduleName + " not found."); return; } User user = null; if (noAuth.get(moduleName) == null) { // Authenticate user = authenticate(request, response); if (user == null) { // You now get a challenge if there is no user return; } } final Principal principal = new UserXmldbPrincipal(WebDAV.BASIC_AUTH, user); HttpServletRequest wrappedRequest = new HttpServletRequestWrapper(request) { public Principal getUserPrincipal() { return principal; } }; // Handle the resource DBBroker broker = null; try { broker = pool.get(user); module.process( broker, new HttpRequestMessage(request, path, '/' + moduleName), new HttpResponseMessage(response)); } catch (NotFoundException ex) { LOG.info("Resource " + path + " not found by " + moduleName, ex); response.sendError(404, ex.getMessage()); } catch (PermissionDeniedException ex) { LOG.info( "Permission denied to " + path + " by " + moduleName + " for " + user.getName(), ex); response.sendError(401, ex.getMessage()); } catch (BadRequestException ex) { LOG.info("Bad request throw from module " + moduleName, ex); response.sendError(400, ex.getMessage()); } catch (EXistException ex) { LOG.fatal("Exception getting broker from pool for user " + user.getName(), ex); response.sendError(500, "Service is not available."); } finally { pool.release(broker); } } catch (IOException ex) { LOG.fatal("I/O exception on request.", ex); try { response.sendError(500, "Service is not available."); } catch (IOException finalEx) { LOG.fatal("Cannot return 500 on exception.", ex); } } }