View Javadoc
1   /*
2    * Copyright 2012-2017 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.helper;
17  
18  import java.time.ZonedDateTime;
19  import java.time.format.DateTimeFormatter;
20  
21  import javax.annotation.PostConstruct;
22  
23  import org.codelibs.fess.mylasta.action.FessUserBean;
24  import org.codelibs.fess.util.ComponentUtil;
25  import org.dbflute.optional.OptionalThing;
26  import org.lastaflute.web.util.LaRequestUtil;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  /**
31   * @author shinsuke
32   *
33   */
34  public class ActivityHelper {
35      private Logger logger = null;
36  
37      protected String loggerName = "fess.log.audit";
38  
39      @PostConstruct
40      public void init() {
41          logger = LoggerFactory.getLogger(loggerName);
42      }
43  
44      public void login(final OptionalThing<FessUserBean> user) {
45          final StringBuilder buf = new StringBuilder(100);
46          buf.append("action:");
47          buf.append(Action.LOGIN);
48          buf.append('\t');
49          buf.append("user:");
50          buf.append(user.map(u -> u.getUserId()).orElse("-"));
51          log(buf);
52      }
53  
54      public void logout(final OptionalThing<FessUserBean> user) {
55          final StringBuilder buf = new StringBuilder(100);
56          buf.append("action:");
57          buf.append(Action.LOGOUT);
58          buf.append('\t');
59          buf.append("user:");
60          buf.append(user.map(u -> u.getUserId()).orElse("-"));
61          log(buf);
62      }
63  
64      public void access(final OptionalThing<FessUserBean> user, final String path, final String execute) {
65          final StringBuilder buf = new StringBuilder(100);
66          buf.append("action:");
67          buf.append(Action.ACCESS);
68          buf.append('\t');
69          buf.append("user:");
70          buf.append(user.map(u -> u.getUserId()).orElse("-"));
71          buf.append('\t');
72          buf.append("path:");
73          buf.append(path);
74          buf.append('\t');
75          buf.append("execute:");
76          buf.append(execute);
77          log(buf);
78      }
79  
80      private void log(final StringBuilder buf) {
81          buf.append('\t');
82          buf.append("ip:");
83          buf.append(getClientIp());
84          buf.append('\t');
85          buf.append("time:");
86          buf.append(DateTimeFormatter.ISO_INSTANT.format(ZonedDateTime.now()));
87          logger.info(buf.toString());
88      }
89  
90      protected static String getClientIp() {
91          return LaRequestUtil.getOptionalRequest().map(req -> ComponentUtil.getViewHelper().getClientIp(req)).orElse("-");
92      }
93  
94      protected enum Action {
95          LOGIN, LOGOUT, ACCESS;
96      }
97  
98      public void setLoggerName(final String loggerName) {
99          this.loggerName = loggerName;
100     }
101 }