View Javadoc
1   /*
2    * Copyright 2012-2019 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.web.admin.systeminfo;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  import javax.annotation.Resource;
24  
25  import org.codelibs.core.lang.StringUtil;
26  import org.codelibs.core.misc.DynamicProperties;
27  import org.codelibs.fess.Constants;
28  import org.codelibs.fess.app.web.base.FessAdminAction;
29  import org.codelibs.fess.mylasta.direction.FessConfig;
30  import org.codelibs.fess.util.ComponentUtil;
31  import org.codelibs.fess.util.RenderDataUtil;
32  import org.lastaflute.core.direction.ObjectiveConfig;
33  import org.lastaflute.web.Execute;
34  import org.lastaflute.web.response.HtmlResponse;
35  import org.lastaflute.web.response.render.RenderData;
36  import org.lastaflute.web.ruts.process.ActionRuntime;
37  
38  /**
39   * @author Keiichi Watanabe
40   */
41  public class AdminSysteminfoAction extends FessAdminAction {
42  
43      private static final String MASKED_VALUE = "XXXXXXXX";
44  
45      // ===================================================================================
46      //                                                                           Attribute
47      //                                                                           =========
48      @Resource
49      protected DynamicProperties systemProperties;
50  
51      private static final String[] bugReportLabels = { "file.separator", "file.encoding", "java.runtime.version", "java.vm.info",
52              "java.vm.name", "java.vm.vendor", "java.vm.version", "os.arch", "os.name", "os.version", "user.country", "user.language",
53              "user.timezone" };
54  
55      // ===================================================================================
56      //                                                                               Hook
57      //                                                                              ======
58      @Override
59      protected void setupHtmlData(final ActionRuntime runtime) {
60          super.setupHtmlData(runtime);
61          runtime.registerData("helpLink", systemHelper.getHelpLink(fessConfig.getOnlineHelpNameSysteminfo()));
62      }
63  
64      // ===================================================================================
65      //                                                                              Index
66      //                                                                      ==============
67      @Execute
68      public HtmlResponse index() {
69          return asHtml(path_AdminSysteminfo_AdminSysteminfoJsp).renderWith(data -> {
70              registerEnvItems(data);
71              registerPropItems(data);
72              registerFessPropItems(data);
73              registerBugReportItems(data);
74          });
75      }
76  
77      // ===================================================================================
78      //                                                                        Assist Logic
79      //                                                                        ============
80  
81      protected void registerEnvItems(final RenderData data) {
82          RenderDataUtil.register(data, "envItems", getEnvItems());
83      }
84  
85      protected void registerPropItems(final RenderData data) {
86          RenderDataUtil.register(data, "propItems", getPropItems());
87      }
88  
89      protected void registerFessPropItems(final RenderData data) {
90          RenderDataUtil.register(data, "fessPropItems", getFessPropItems(fessConfig));
91      }
92  
93      protected void registerBugReportItems(final RenderData data) {
94          RenderDataUtil.register(data, "bugReportItems", getBugReportItems());
95      }
96  
97      public static List<Map<String, String>> getEnvItems() {
98          final List<Map<String, String>> itemList = new ArrayList<>();
99          for (final Map.Entry<String, String> entry : System.getenv().entrySet()) {
100             itemList.add(createItem(entry.getKey(), entry.getValue()));
101         }
102         return itemList;
103     }
104 
105     public static List<Map<String, String>> getPropItems() {
106         final List<Map<String, String>> itemList = new ArrayList<>();
107         for (final Map.Entry<Object, Object> entry : System.getProperties().entrySet()) {
108             itemList.add(createItem(entry.getKey(), entry.getValue()));
109         }
110         return itemList;
111     }
112 
113     public static List<Map<String, String>> getFessPropItems(final FessConfig fessConfig) {
114         final List<Map<String, String>> itemList = new ArrayList<>();
115         ComponentUtil.getSystemProperties().entrySet().stream().forEach(e -> {
116             final String k = e.getKey().toString();
117             final String value;
118             if (isMaskedValue(k)) {
119                 value = MASKED_VALUE;
120             } else {
121                 value = e.getValue().toString();
122             }
123             itemList.add(createItem(k, value));
124         });
125         if (fessConfig instanceof ObjectiveConfig) {
126             final ObjectiveConfig config = (ObjectiveConfig) fessConfig;
127             config.keySet().stream().forEach(k -> {
128                 final String value;
129                 if (isMaskedValue(k)) {
130                     value = MASKED_VALUE;
131                 } else {
132                     value = config.get(k);
133                 }
134                 itemList.add(createItem(k, value));
135             });
136         }
137         return itemList;
138     }
139 
140     protected static boolean isMaskedValue(final String key) {
141         return "http.proxy.password".equals(key) //
142                 || "ldap.admin.security.credentials".equals(key) //
143                 || "spnego.preauth.password".equals(key) //
144                 || "app.cipher.key".equals(key) //
145                 || "oic.client.id".equals(key) //
146                 || "oic.client.secret".equals(key);
147     }
148 
149     public static List<Map<String, String>> getBugReportItems() {
150         final List<Map<String, String>> itemList = new ArrayList<>();
151         for (final String label : bugReportLabels) {
152             itemList.add(createPropItem(label));
153         }
154 
155         final DynamicProperties systemProperties = ComponentUtil.getSystemProperties();
156         for (final Map.Entry<Object, Object> entry : systemProperties.entrySet()) {
157             if (isBugReportTarget(entry.getKey())) {
158                 itemList.add(createItem(entry.getKey(), entry.getValue()));
159             }
160         }
161 
162         return itemList;
163     }
164 
165     private static boolean isBugReportTarget(final Object key) {
166         if ("snapshot.path".equals(key) || "label.value".equals(key)) {
167             return false;
168         }
169         return true;
170     }
171 
172     protected static Map<String, String> createPropItem(final String key) {
173         return createItem(key, System.getProperty(key));
174     }
175 
176     protected static Map<String, String> createItem(final Object label, final Object value) {
177         final Map<String, String> map = new HashMap<>(2);
178         map.put(Constants.ITEM_LABEL, label != null ? label.toString() : StringUtil.EMPTY);
179         map.put(Constants.ITEM_VALUE, value != null ? value.toString() : StringUtil.EMPTY);
180         return map;
181     }
182 
183 }