Ejemplo n.º 1
0
  /**
   * @param path
   * @param strings
   * @param strings2
   * @param strings3
   * @throws JCRNodeFactoryServiceException
   * @throws RepositoryException
   * @throws IOException
   * @throws UnsupportedEncodingException
   */
  private Map<String, Object> saveProperties(String path, MapParams params)
      throws RepositoryException, JCRNodeFactoryServiceException, UnsupportedEncodingException,
          IOException {
    InputStream in = null;

    try {
      Node n = jcrNodeFactoryService.getNode(path);
      Map<String, Object> map = null;
      if (n != null) {
        in = jcrNodeFactoryService.getInputStream(path);
        String content = IOUtils.readFully(in, "UTF-8");
        try {
          in.close();
        } catch (IOException ex) {
        }
        map = beanConverter.convertToObject(content, Map.class);
      } else {
        map = new HashMap<String, Object>();
      }
      for (int i = 0; i < params.names.length; i++) {
        if (REMOVE_ACTION.equals(params.actions[i])) {
          map.remove(params.names[i]);
        } else {
          map.put(params.names[i], params.values[i]);
        }
      }
      String result = beanConverter.convertToString(map);
      in = new ByteArrayInputStream(result.getBytes("UTF-8"));
      n = jcrNodeFactoryService.setInputStream(path, in, RestProvider.CONTENT_TYPE);

      // deal with indexed properties.
      for (int i = 0; i < params.names.length; i++) {
        boolean index = false;
        if (params.indexes != null && "1".equals(params.indexes[i])) {
          index = true;
        }
        if (n.hasProperty("sakai:" + params.names[i])) {
          // if remove, remove it, else update
          if (REMOVE_ACTION.equals(params.actions[i])) {
            n.getProperty("sakai:" + params.names[i]).remove();
          } else {
            n.setProperty("sakai:" + params.names[i], params.values[i]);
          }
        } else if (index) {
          // add it
          n.setProperty("sakai:" + params.names[i], params.values[i]);
        }
      }
      n.getSession().save(); // verify changes
      Map<String, Object> outputMap = new HashMap<String, Object>();
      outputMap.put("response", "OK");
      return outputMap;
    } finally {
      try {
        in.close();
      } catch (Exception ex) {
      }
    }
  }
Ejemplo n.º 2
0
  @Override
  protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
      throws ServletException, IOException {

    Resource resource = request.getResource();
    Node node = (Node) resource.adaptTo(Node.class);

    String filename = null;
    try {
      if (node.hasNode(JcrConstants.JCR_CONTENT)) {
        Node content = node.getNode(JcrConstants.JCR_CONTENT);
        response.setHeader(
            "Content-Type", content.getProperty(JcrConstants.JCR_MIMETYPE).getString());
        response.setHeader(
            "Content-Length", "" + content.getProperty(JcrConstants.JCR_DATA).getLength());
      }
      if (node.hasProperty(FilesConstants.SAKAI_FILENAME)) {
        filename = node.getProperty(FilesConstants.SAKAI_FILENAME).getString();
      }

      // If we provided a filename and we haven't changed the name in a previous request.
      if (filename != null && !response.containsHeader("Content-Disposition")) {
        response.setHeader("Content-Disposition", "filename=\"" + filename + "\"");
      }

      response.setStatus(HttpServletResponse.SC_OK);
      InputStream in = (InputStream) request.getResource().adaptTo(InputStream.class);
      OutputStream out = response.getOutputStream();

      IOUtils.stream(in, out);
    } catch (RepositoryException e) {
      logger.warn("Unable to download file due to repositoryexception!");
      e.printStackTrace();
      response.sendError(500);
    }
  }