/**
   * Manejador principal del restlet.
   *
   * @param req Request
   * @param res Response
   */
  @Override
  public void handle(Request req, Response res) {
    /* Conflicto con Checkstyle. No se pueden declarar como final los métodos de
     * beans EJB que hagan uso de dependencias inyectadas, ya que dichas
     * dependencias toman el valor null.
     * No se declara como final debido a que en ese caso
     * la inyección de dependencias dejaría de funcionar.
     */
    String repo = (String) req.getAttributes().get("repo");
    String cdcDocId = (String) req.getAttributes().get("docid");
    if (cdcDocId == null) {
      handleError(res, "you must specify a CdC source document Id.");
    } else {
      String depth = getQueryParamValue(req, "depth", "1");

      if (repo == null || repo.equals("*")) {
        handleError(res, "you must specify a repository");
      } else {

        int profundidad = Integer.parseInt(depth);
        // String cdcPath =
        // Framework.getProperty(ArchivoConstantes.PROPIEDAD_CDC_PATH);
        DOMDocumentFactory domFactory = new DOMDocumentFactory();
        DOMDocument result = (DOMDocument) domFactory.createDocument();
        try {

          navigationContext.setCurrentServerLocation(new RepositoryLocation(repo));
          documentManager = navigationContext.getOrCreateDocumentManager();
          DocumentModel cdcRoot = documentManager.getDocument(new IdRef(cdcDocId));
          if (cdcRoot != null) {
            Element current = result.createElement("document");

            current.setAttribute("title", cdcRoot.getTitle());
            current.setAttribute("type", cdcRoot.getType());
            current.setAttribute("id", cdcRoot.getId());
            current.setAttribute("path", cdcRoot.getPathAsString());

            result.setRootElement((org.dom4j.Element) current);

            addChildren(result, current, cdcRoot, profundidad);
          } else {
            Element noEncontrado = result.createElement("cdCNoRegistrado");
            noEncontrado.setAttribute("variable", ArchivoConstantes.PROPIEDAD_CDC_PATH);
            noEncontrado.setAttribute("valor", cdcDocId);
            result.setRootElement((org.dom4j.Element) noEncontrado);
            LOG.error(
                "No se ha configurado la ruta del CdC; por favor configure la ruta en la propiedad "
                    + ArchivoConstantes.PROPIEDAD_CDC_PATH);
          }

          res.setEntity(result.asXML(), MediaType.TEXT_XML);
          res.getEntity().setCharacterSet(CharacterSet.UTF_8);

        }
        /**
         * Conflicto con checkstyle. Es necesario capturar la excepción Exception, dado que
         * el código nativo de Nuxeo lanza dicha excepción. En caso contrario, este
         * código no compilaría
         */
        catch (Exception e) {
          LOG.error(e);
          handleError(res, e);
        }
      }
    }
  }
  @Override
  public void handle(Request req, Response res) {
    UserManager userMgr = null;
    String user = null;
    try {

      userMgr = Framework.getService(UserManager.class);
      if (userMgr == null) {
        handleError(res, "User Manager can't be loaded");
        return;
      }

      user = (String) req.getAttributes().get("username");
      if (user == null) {
        handleError(res, "you must specify an user to search");
        return;
      }
    } catch (ClientException e) {
      handleError(res, e);
      return;
    } catch (Exception e) {
      handleError(res, "User Manager can't be loaded");
      return;
    }

    try {
      DocumentModel userModel = null;
      if (user != null && !user.isEmpty()) {
        userModel = userMgr.getUserModel(user);
      }

      // build the XML response document holding the ref
      DOMDocumentFactory domfactory = new DOMDocumentFactory();
      DOMDocument resultDocument = (DOMDocument) domfactory.createDocument();

      if (userModel == null) {
        handleError(res, "User is not found");
        return;
      }

      NuxeoPrincipal principal =
          userMgr.getPrincipal((String) userModel.getProperty("user", "username"));
      Element userElement = resultDocument.addElement("user");
      userElement.addAttribute("username", principal.getName());
      userElement.addAttribute("firstName", principal.getFirstName());
      userElement.addAttribute("lastName", principal.getLastName());
      userElement.addAttribute("email", (String) userModel.getProperty("user", "email"));
      userElement.addAttribute("company", principal.getCompany());

      List<String> groups = principal.getAllGroups();
      if (groups != null && groups.size() > 0) {
        for (String groupName : groups) {
          Element groupElement = userElement.addElement("group");
          groupElement.addAttribute("name", groupName);
        }
      }

      Representation rep =
          new StringRepresentation(resultDocument.asXML(), MediaType.APPLICATION_XML);
      rep.setCharacterSet(CharacterSet.UTF_8);
      res.setEntity(rep);
    } catch (ClientException e) {
      handleError(res, e);
    }
  }
Пример #3
0
  @Override
  protected void doHandleStatelessRequest(Request req, Response res) {
    String repo = (String) req.getAttributes().get("repo");
    String docid = (String) req.getAttributes().get("docid");

    DOMDocumentFactory domFactory = new DOMDocumentFactory();

    DOMDocument result = (DOMDocument) domFactory.createDocument();

    if (repo == null || repo.equals("*")) {
      try {
        Element serversNode = result.createElement("avalaibleServers");
        result.setRootElement((org.dom4j.Element) serversNode);

        RepositoryManager repositoryManager = Framework.getLocalService(RepositoryManager.class);
        for (String repositoryName : repositoryManager.getRepositoryNames()) {
          Element server = result.createElement("server");
          server.setAttribute("title", repositoryName);
          server.setAttribute("url", getRelURL(repositoryName, "*"));
          serversNode.appendChild(server);
        }
        res.setEntity(result.asXML(), MediaType.TEXT_XML);
        res.getEntity().setCharacterSet(CharacterSet.UTF_8);
        return;
      } catch (DOMException e) {
        handleError(result, res, e);
        return;
      }
    } else {
      DocumentModel dm;

      boolean init = initRepository(res, repo);
      boolean isRoot = false;
      try {
        if (init) {
          if (docid == null || docid.equals("*")) {
            dm = session.getRootDocument();
            isRoot = true;
          } else {
            dm = session.getDocument(new IdRef(docid));
          }
        } else {
          handleError(res, "Unable to init repository");
          return;
        }
      } catch (NuxeoException e) {
        handleError(res, e);
        return;
      }

      Element current = result.createElement("document");
      try {
        current.setAttribute("title", dm.getTitle());
      } catch (DOMException | NuxeoException e) {
        handleError(res, e);
      }
      current.setAttribute("type", dm.getType());
      current.setAttribute("id", dm.getId());
      current.setAttribute("name", dm.getName());
      if (isRoot) {
        current.setAttribute("url", getRelURL(repo, ""));
      } else {
        current.setAttribute("url", getRelURL(repo, dm.getRef().toString()));
      }
      result.setRootElement((org.dom4j.Element) current);

      if (dm.isFolder()) {
        // Element childrenElem = result.createElement("children");
        // root.appendChild(childrenElem);

        DocumentModelList children;
        try {
          children = session.getChildren(dm.getRef());
        } catch (NuxeoException e) {
          handleError(result, res, e);
          return;
        }

        for (DocumentModel child : children) {
          Element el = result.createElement("document");
          try {
            el.setAttribute("title", child.getTitle());
          } catch (DOMException e) {
            handleError(res, e);
          } catch (NuxeoException e) {
            handleError(res, e);
          }
          el.setAttribute("type", child.getType());
          el.setAttribute("id", child.getId());
          el.setAttribute("name", child.getName());
          el.setAttribute("url", getRelURL(repo, child.getRef().toString()));
          current.appendChild(el);
        }
      }

      res.setEntity(result.asXML(), MediaType.TEXT_XML);
      res.getEntity().setCharacterSet(CharacterSet.UTF_8);
    }
  }