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