在Alfresco 4.2+中记录CMIS查询

时间:2016-05-05 15:19:29

标签: logging alfresco cmis

我想记录alfresco从客户那里获得的CMIS查询。

我已尝试将所有cmis类别设置为调试...大量信息但没有查询字符串:

log4j.logger.org.alfresco.opencmis=debug
log4j.logger.org.alfresco.opencmis.AlfrescoCmisServiceInterceptor=debug
log4j.logger.org.alfresco.cmis=debug
log4j.logger.org.alfresco.cmis.dictionary=debug
log4j.logger.org.apache.chemistry.opencmis=debug

有人知道这是否可行?我一直在寻找源头,但我很难找到任何东西。

2 个答案:

答案 0 :(得分:2)

我找不到任何有用的日志记录,所以我将一个servlet过滤器放在一起来记录请求。请记住,查询字符串实际上位于POST请求的有效负载中,因此必须创建请求包装器,以便过滤器可以读取有效负载,而不会干扰下游需要它的服务。当我在它的时候,我也记录了内容类型和参数,因为它们可以非破坏性地访问请求信息,因此不需要包装器。要使用此方法,请创建两个类,一个用于过滤器,另一个用于包装器。把它们放在/ tomcat / webapps / alfresco / WEB-INF / classes中,然后像这样在web.xml中添加一个过滤器......

    <filter>
       <filter-name>post-body-filter</filter-name> 
       <filter-class>filter.PostBodyFilter</filter-class> 
    </filter> 
    <filter-mapping> 
       <filter-name>post-body-filter</filter-name> 
       <url-pattern>/*</url-pattern> 
    </filter-mapping> 

可以通过在server.xml中包含一个Valve条目将结果放到日志文件中。在日志中,您将在REQBODY中看到查询。您应该查找的日志前缀是localhost_access_log。

        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
           prefix="localhost_access_log." suffix=".txt"
           pattern=" %t %r contentType:%{REQCONTENTTYPE}r parameters:%{REQPARAMETERS}r body:%{REQBODY}r %s" />    

课程本身就是这样的。我承认可以从其他过滤器示例中自由借用。如果我只记得在哪里,我会给予归因(或责备)。我确信它可以改进。

package filter;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;

import filter.FilterRequestWrapper

public class PostBodyFilter implements Filter {

    private FilterConfig filterConfig = null;

    public void destroy() { 
        this.filterConfig = null;
    }

    public void doFilter(ServletRequest request, ServletResponse response,
                FilterChain chain) throws IOException, ServletException {
        if (filterConfig == null)
            return;
        FilterRequestWrapper requestWrapper = new FilterRequestWrapper((HttpServletRequest) request);
        String contentType = request.getContentType();
        StringBuffer parameters = getParameters(request);
        StringBuffer body = getBody(requestWrapper);
        request.setAttribute("REQPARAMETERS", parameters);
        request.setAttribute("REQBODY", body);
        request.setAttribute("REQCONTENTTYPE", contentType);
        chain.doFilter(requestWrapper, response);
    }

    private static StringBuffer getParameters(ServletRequest request) {
        Enumeration<String> names = request.getParameterNames();
        StringBuffer parameters = new StringBuffer();
        while (names.hasMoreElements()) {
            String name = (String) names.nextElement();
            parameters.append(name + "=");
            String values[] = request.getParameterValues(name);
            for (int i = 0; i < values.length; i++) {
                if (i > 0)
                    parameters.append("' ");
                parameters.append(values[i]);
            }
            if (names.hasMoreElements())
                parameters.append(" ");
        }
        return parameters;
    }

    private static StringBuffer getBody(ServletRequest request)
            throws IOException {
        StringBuffer body = new StringBuffer();
        BufferedReader bufferedReader = null;
        try {
            InputStream inputStream = request.getInputStream();
            if (inputStream != null) {
                bufferedReader = new BufferedReader(new InputStreamReader(
                        inputStream));
                char[] charBuffer = new char[128];
                int bytesRead = -1;
                while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
                    body.append(charBuffer, 0, bytesRead);
                }
            } else {
                body.append("");
            }
        } catch (IOException ex) {
            throw ex;
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException ex) {
                    throw ex;
                }
            }
        }
        return body;
    }   

    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
    }
}

package filter;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.servlet.ServletInputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;    

public class FilterRequestWrapper extends HttpServletRequestWrapper {

    private final String body;

    public FilterRequestWrapper(HttpServletRequest request) {
        super(request);

        StringBuilder stringBuilder = new StringBuilder();
        BufferedReader bufferedReader = null;

        try {
            InputStream inputStream = request.getInputStream();

            if (inputStream != null) {
                bufferedReader = new BufferedReader(new InputStreamReader(
                        inputStream));

                char[] charBuffer = new char[128];
                int bytesRead = -1;

                while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
                    stringBuilder.append(charBuffer, 0, bytesRead);
                }
            } else {
                stringBuilder.append("");
            }
        } catch (IOException ex) {

        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException ex) {
                }
            }
        }

        body = stringBuilder.toString();
    }

    @Override
    public ServletInputStream getInputStream() throws IOException {
        final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
            body.getBytes());   

        ServletInputStream inputStream = new ServletInputStream() {
            public int read() throws IOException {
                return byteArrayInputStream.read();
            }
        };  

        return inputStream;
    }
}

答案 1 :(得分:0)

我有Spring Boot应用程序

  @Bean
  public Session sessionBean() {
    // No connection to Alfresco available, create a new one
    SessionFactory sessionFactory = SessionFactoryImpl.newInstance();
    Map<String, String> parameters = new HashMap<>();
    parameters.put(SessionParameter.USER, username);
    parameters.put(SessionParameter.PASSWORD, password);
    parameters.put(SessionParameter.ATOMPUB_URL, host);
    parameters.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());
    parameters.put(SessionParameter.COMPRESSION, "true");
    parameters.put(SessionParameter.CACHE_TTL_OBJECTS, "0"); // Caching is turned off

    parameters.put(SessionParameter.HTTP_INVOKER_CLASS, "my.package.AlfrescoHttpInvoiker");



    // If there is only one repository exposed (e.g. Alfresco), these
    // lines will help detect it and its ID
    List<Repository> repositories = sessionFactory.getRepositories(parameters);
    Repository repository;
    if (repositories != null && repositories.size() > 0) {
      log.debug("Found (" + repositories.size() + ") Alfresco repositories");
      repository = repositories.get(0);
    } else {
      throw new CmisConnectionException(
              "Could not connect to the Alfresco Server, no repository found!");
    }
    return repository.createSession();
  }

关键行是

    parameters.put(SessionParameter.HTTP_INVOKER_CLASS, "my.package.AlfrescoHttpInvoiker");

然后转到类org.apache.chemistry.opencmis.client.bindings.spi.http.DefaultHttpInvoker,复制并粘贴全部内容,然后修改“后负责”的代码行,

OutputStream out = new BufferedOutputStream(connOut, BUFFER_SIZE);
writer.write(out);
out.close();

这样做:

try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
    writer.write(bos);
    out.write(bos.toByteArray());
    String str = new String(bos.toByteArray());
    LOG.debug("CMIS request body: " + str);
}