@Override
 protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
     throws ServletException, IOException {
   Resource resource = request.getResource();
   Node node = resource.adaptTo(Node.class);
   Content content = resource.adaptTo(Content.class);
   if (node != null) {
     try {
       Session jcrSession = request.getResourceResolver().adaptTo(Session.class);
       JSONWriter write = new JSONWriter(response.getWriter());
       FileUtils.writeLinkNode(node, jcrSession, write);
     } catch (JSONException e) {
       response.sendError(500, "Unable to parse JSON.");
     } catch (RepositoryException e) {
       LOGGER.warn("Unable to get file info for link.");
       response.sendError(500, "Unable get file info.");
     }
   } else {
     try {
       org.sakaiproject.nakamura.api.lite.Session session =
           resource.adaptTo(org.sakaiproject.nakamura.api.lite.Session.class);
       JSONWriter write = new JSONWriter(response.getWriter());
       FileUtils.writeLinkNode(content, session, write);
     } catch (StorageClientException e) {
       LOGGER.warn("Unable to get file info for link.");
       response.sendError(500, "Unable get file info.");
     } catch (JSONException e) {
       response.sendError(500, "Unable to parse JSON.");
     }
   }
 }
  @Test
  public void testIsTag() throws RepositoryException {
    Node node = new MockNode("/path/to/tag");
    node.setProperty(SLING_RESOURCE_TYPE_PROPERTY, FilesConstants.RT_SAKAI_TAG);
    boolean result = FileUtils.isTag(node);
    assertEquals(true, result);

    node.setProperty(SLING_RESOURCE_TYPE_PROPERTY, "foobar");
    result = FileUtils.isTag(node);
    assertEquals(false, result);
  }
  /**
   * Give a JSON representation of the content.
   *
   * @param content
   * @param session
   * @param write The {@link JSONWriter} to output to.
   * @param depth
   * @throws JSONException
   * @throws StorageClientException
   */
  protected void handleContent(
      final Content content, final Session session, final JSONWriter write, final int depth)
      throws JSONException, StorageClientException {

    write.object();
    final String type = (String) content.getProperty(SLING_RESOURCE_TYPE_PROPERTY);
    if (FilesConstants.RT_SAKAI_LINK.equals(type)) {
      FileUtils.writeLinkNode(content, session, write, true);
    } else {
      FileUtils.writeFileNode(content, session, write, depth, true);
    }
    FileUtils.writeComments(content, session, write);
    FileUtils.writeCommentCountProperty(content, session, write, repository);
    write.endObject();
  }
  @Test
  public void testCreateLinkNode() throws AccessDeniedException, RepositoryException {

    Node fileNode = createFileNode();
    Session session = mock(Session.class);
    Session adminSession = mock(Session.class);
    SlingRepository slingRepository = mock(SlingRepository.class);
    String linkPath = "/path/to/link";
    String sitePath = "/path/to/site";

    when(session.getUserID()).thenReturn("alice");
    when(fileNode.getSession()).thenReturn(session);
    NodeType[] nodeTypes = new NodeType[0];
    when(fileNode.getMixinNodeTypes()).thenReturn(nodeTypes);

    when(session.getItem(fileNode.getPath())).thenReturn(fileNode);
    when(adminSession.getItem(fileNode.getPath())).thenReturn(fileNode);
    when(slingRepository.loginAdministrative(null)).thenReturn(adminSession);
    when(adminSession.hasPendingChanges()).thenReturn(true);
    when(session.hasPendingChanges()).thenReturn(true);

    // link
    Node linkNode = mock(Node.class);
    when(session.itemExists(linkPath)).thenReturn(true);
    when(session.getItem(linkPath)).thenReturn(linkNode);

    FileUtils.createLink(fileNode, linkPath, null, slingRepository);

    verify(fileNode).addMixin(FilesConstants.REQUIRED_MIXIN);
    verify(session).save();
    verify(adminSession).save();
    verify(adminSession).logout();
  }
 private Node copyFile(
     String zipEntryName,
     String fileName,
     String sitePath,
     String contentType,
     Session session,
     ZipFile zip) {
   final String id = uniqueId();
   final String path = FileUtils.getHashedPath(FilesConstants.USER_FILESTORE, id);
   Node node = null;
   try {
     final InputStream in = zip.getInputStream(zip.getEntry(zipEntryName));
     node = FileUtils.saveFile(session, path, id, in, fileName, contentType, slingRepository);
     final String linkPath = sitePath + "/_files/" + fileName;
     FileUtils.createLink(session, node, linkPath, sitePath, slingRepository);
   } catch (RepositoryException e) {
     throw new Error(e);
   } catch (IOException e) {
     throw new Error(e);
   }
   return node;
 }
  @Test
  public void testWriteSiteInfo() throws JSONException, RepositoryException, IOException {
    Node siteNode = new MockNode("/sites/foo");
    SiteService siteService = mock(SiteService.class);
    when(siteService.getMemberCount(siteNode)).thenReturn(11);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Writer w = new PrintWriter(baos);
    JSONWriter write = new JSONWriter(w);

    FileUtils.writeSiteInfo(siteNode, write, siteService);
    w.flush();

    String s = baos.toString("UTF-8");
    JSONObject o = new JSONObject(s);
    assertEquals("11", o.get("member-count"));
    assertEquals(siteNode.getPath(), o.get("jcr:path"));
  }
  @Test
  public void testWriteFileNode()
      throws JSONException, RepositoryException, UnsupportedEncodingException, IOException {
    Session session = mock(Session.class);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Writer w = new PrintWriter(baos);
    JSONWriter write = new JSONWriter(w);
    SiteService siteService = mock(SiteService.class);

    Node node = createFileNode();

    FileUtils.writeFileNode(node, session, write, siteService);

    w.flush();
    String s = baos.toString("UTF-8");
    JSONObject j = new JSONObject(s);

    assertFileNodeInfo(j);
  }
  @Test
  public void testWriteLinkNode() throws JSONException, RepositoryException, IOException {
    Session session = mock(Session.class);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Writer w = new PrintWriter(baos);
    JSONWriter write = new JSONWriter(w);
    SiteService siteService = mock(SiteService.class);

    Node node = new MockNode("/path/to/link");
    node.setProperty(FilesConstants.SAKAI_LINK, "uuid");
    node.setProperty("foo", "bar");
    Node fileNode = createFileNode();
    when(session.getNodeByIdentifier("uuid")).thenReturn(fileNode);

    FileUtils.writeLinkNode(node, session, write, siteService);
    w.flush();
    String s = baos.toString("UTF-8");
    JSONObject j = new JSONObject(s);

    assertEquals("/path/to/link", j.getString("jcr:path"));
    assertEquals("bar", j.getString("foo"));
    assertFileNodeInfo(j.getJSONObject("file"));
  }