2015-10-14 82 views

回答

0

爲什麼要隱藏它們,當你可以刪除它們?這個簡單的例子是一個Servlet。你可以每晚運行OSGi scheduler

package com.foo.bar; 

import org.apache.felix.scr.annotations.sling.SlingServlet; 
import org.apache.sling.api.SlingHttpServletRequest; 
import org.apache.sling.api.SlingHttpServletResponse; 
import org.apache.sling.api.resource.Resource; 
import org.apache.sling.api.resource.ResourceResolver; 
import org.apache.sling.api.servlets.SlingSafeMethodsServlet; 

import javax.jcr.query.Query; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServletResponse; 

import java.io.IOException; 
import java.io.PrintWriter; 
import java.util.Iterator; 

import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 

@SlingServlet(paths={"/bin/deletedsstore"}) 
public class DeleteDSStoreServlet extends SlingSafeMethodsServlet { 
    private static final long serialVersionUID = 1L; 
    private static final Logger log = LoggerFactory.getLogger(DeleteDSStoreServlet.class); 

    private static final String SQL2_QUERY = "SELECT * FROM [nt:base] AS s WHERE ISDESCENDANTNODE([/content]) and NAME() = '.DS_Store'"; 
    private static final int SAVE_THRESHOLD = 100; 

    @Override 
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { 
     ResourceResolver resolver = request.getResourceResolver(); 
     Iterator<Resource> resources = resolver.findResources(SQL2_QUERY, Query.JCR_SQL2); 

     int deleted = 0; 

     while (resources.hasNext()) { 
      Resource resource = resources.next(); 
      String path = resource.getPath(); 

      resolver.delete(resource); 
      log.info("Deleted node: " + path); 

      deleted++; 

      if (deleted % SAVE_THRESHOLD == 0) { 
       resolver.commit(); 
      } 
     } 

     if (resolver.hasChanges()) { 
      resolver.commit(); 
     } 

     response.setStatus(HttpServletResponse.SC_OK); 
     PrintWriter out = response.getWriter(); 
     out.write("Deleted " + deleted + " .DS_Store nodes"); 
    } 
}