public void configure(DBBroker broker, Collection parent, Map parameters)
      throws CollectionConfigurationException {
    super.configure(broker, parent, parameters);
    String stylesheet = (String) parameters.get("src");
    if (stylesheet == null)
      throw new CollectionConfigurationException(
          "STXTransformerTrigger requires an " + "attribute 'src'");
    String origProperty = System.getProperty("javax.xml.transform.TransformerFactory");
    System.setProperty(
        "javax.xml.transform.TransformerFactory", "net.sf.joost.trax.TransformerFactoryImpl");
    factory = (SAXTransformerFactory) TransformerFactory.newInstance();
    // reset property to previous setting
    if (origProperty != null)
      System.setProperty("javax.xml.transform.TransformerFactory", origProperty);

    getLogger().debug("compiling stylesheet " + stylesheet);
    XmldbURI stylesheetUri = null;
    try {
      stylesheetUri = XmldbURI.xmldbUriFor(stylesheet);
    } catch (URISyntaxException e) {
    }
    // TODO: allow full XmldbURIs to be used as well.
    if (stylesheetUri == null || stylesheet.indexOf(':') == Constants.STRING_NOT_FOUND) {
      stylesheetUri = parent.getURI().resolveCollectionPath(stylesheetUri);
      DocumentImpl doc;
      try {
        doc = (DocumentImpl) broker.getXMLResource(stylesheetUri);
        if (doc == null)
          throw new CollectionConfigurationException(
              "stylesheet " + stylesheetUri + " not found in database");
        Serializer serializer = broker.getSerializer();
        TemplatesHandler thandler = factory.newTemplatesHandler();
        serializer.setSAXHandlers(thandler, null);
        serializer.toSAX(doc);
        template = thandler.getTemplates();
        handler = factory.newTransformerHandler(template);
      } catch (TransformerConfigurationException e) {
        throw new CollectionConfigurationException(e.getMessage(), e);
      } catch (PermissionDeniedException e) {
        throw new CollectionConfigurationException(e.getMessage(), e);
      } catch (SAXException e) {
        throw new CollectionConfigurationException(e.getMessage(), e);
      }
    } else
      try {
        template = factory.newTemplates(new StreamSource(stylesheet));
        handler = factory.newTransformerHandler(template);
      } catch (TransformerConfigurationException e) {
        throw new CollectionConfigurationException(e.getMessage(), e);
      }
  }
Beispiel #2
0
  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);
      }
    }
  }
  @Override
  public void execute(DBBroker broker) throws EXistException {
    final Agent agentInstance = AgentFactory.getInstance();
    final BrokerPool brokerPool = broker.getBrokerPool();
    final TaskStatus endStatus = new TaskStatus(TaskStatus.Status.STOPPED_OK);

    agentInstance.changeStatus(brokerPool, new TaskStatus(TaskStatus.Status.INIT));

    if (paused) {
      LOG.info("Consistency check is paused.");
      agentInstance.changeStatus(brokerPool, new TaskStatus(TaskStatus.Status.PAUSED));
      return;
    }

    brokerPool.getProcessMonitor().startJob(ProcessMonitor.ACTION_BACKUP, null, monitor);

    PrintWriter report = null;
    try {
      boolean doBackup = createBackup;
      // TODO: don't use the direct access feature for now. needs more testing
      List<ErrorReport> errors = null;
      if (!incremental || incrementalCheck) {

        LOG.info("Starting consistency check...");

        report = openLog();
        final CheckCallback cb = new CheckCallback(report);

        final ConsistencyCheck check = new ConsistencyCheck(broker, false, checkDocs);
        agentInstance.changeStatus(brokerPool, new TaskStatus(TaskStatus.Status.RUNNING_CHECK));
        errors = check.checkAll(cb);

        if (!errors.isEmpty()) {
          endStatus.setStatus(TaskStatus.Status.STOPPED_ERROR);
          endStatus.setReason(errors);

          LOG.error("Errors found: " + errors.size());

          doBackup = true;

          if (fatalErrorsFound(errors)) {
            LOG.error("Fatal errors were found: pausing the consistency check task.");
            paused = true;
          }
        }

        LOG.info("Finished consistency check");
      }

      if (doBackup) {
        LOG.info("Starting backup...");

        final SystemExport sysexport = new SystemExport(broker, logCallback, monitor, false);
        lastExportedBackup = sysexport.export(exportDir, incremental, maxInc, createZip, errors);
        agentInstance.changeStatus(brokerPool, new TaskStatus(TaskStatus.Status.RUNNING_BACKUP));

        if (lastExportedBackup != null) {
          LOG.info("Created backup to file: " + lastExportedBackup.getAbsolutePath());
        }

        LOG.info("Finished backup");
      }

    } catch (final TerminatedException e) {
      throw new EXistException(e.getMessage(), e);

    } catch (final PermissionDeniedException e) {
      // TODO should maybe throw PermissionDeniedException instead!
      throw new EXistException(e.getMessage(), e);

    } finally {
      if (report != null) {
        report.close();
      }

      agentInstance.changeStatus(brokerPool, endStatus);
      brokerPool.getProcessMonitor().endJob();
    }
  }