View Javadoc
1   /*
2    * Copyright 2012-2021 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.app.logic;
17  
18  import javax.annotation.Resource;
19  
20  import org.codelibs.fess.mylasta.action.FessUserBean;
21  import org.dbflute.hook.AccessContext;
22  import org.dbflute.optional.OptionalThing;
23  import org.lastaflute.core.time.TimeManager;
24  import org.lastaflute.db.dbflute.accesscontext.AccessContextResource;
25  
26  public class AccessContextLogic {
27  
28      // ===================================================================================
29      //                                                                           Attribute
30      //                                                                           =========
31      @Resource
32      private TimeManager timeManager;
33  
34      // ===================================================================================
35      //                                                                  Resource Interface
36      //                                                                  ==================
37      @FunctionalInterface
38      public interface UserTypeSupplier {
39          OptionalThing<String> supply();
40      }
41  
42      @FunctionalInterface
43      public interface UserBeanSupplier {
44          OptionalThing<FessUserBean> supply();
45      }
46  
47      @FunctionalInterface
48      public interface AppTypeSupplier {
49          String supply();
50      }
51  
52      // ===================================================================================
53      //                                                                      Create Context
54      //                                                                      ==============
55      public AccessContext create(final AccessContextResource resource, final UserTypeSupplier userTypeSupplier,
56              final UserBeanSupplier userBeanSupplier, final AppTypeSupplier appTypeSupplier) {
57          final AccessContext context = new AccessContext();
58          context.setAccessLocalDateTimeProvider(() -> timeManager.currentDateTime());
59          context.setAccessUserProvider(() -> buildAccessUserTrace(resource, userTypeSupplier, appTypeSupplier));
60          return context;
61      }
62  
63      private String buildAccessUserTrace(final AccessContextResource resource, final UserTypeSupplier userTypeSupplier,
64              final AppTypeSupplier appTypeSupplier) {
65          final StringBuilder sb = new StringBuilder();
66          sb.append(userTypeSupplier.supply().orElse("_"));
67          sb.append(",").append(appTypeSupplier.supply()).append(",").append(resource.getModuleName());
68          final String trace = sb.toString();
69          final int columnSize = 200;
70          return trace.length() > columnSize ? trace.substring(0, columnSize) : trace;
71      }
72  }