1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.admin.action;
16  
17  import com.liferay.mail.service.MailServiceUtil;
18  import com.liferay.portal.convert.ConvertProcess;
19  import com.liferay.portal.kernel.cache.CacheRegistry;
20  import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
21  import com.liferay.portal.kernel.log.Log;
22  import com.liferay.portal.kernel.log.LogFactoryUtil;
23  import com.liferay.portal.kernel.mail.Account;
24  import com.liferay.portal.kernel.messaging.DestinationNames;
25  import com.liferay.portal.kernel.messaging.MessageBusUtil;
26  import com.liferay.portal.kernel.search.Indexer;
27  import com.liferay.portal.kernel.search.SearchEngineUtil;
28  import com.liferay.portal.kernel.servlet.SessionErrors;
29  import com.liferay.portal.kernel.util.Constants;
30  import com.liferay.portal.kernel.util.InstancePool;
31  import com.liferay.portal.kernel.util.ParamUtil;
32  import com.liferay.portal.kernel.util.PropsKeys;
33  import com.liferay.portal.kernel.util.StringPool;
34  import com.liferay.portal.kernel.util.StringUtil;
35  import com.liferay.portal.kernel.util.ThreadUtil;
36  import com.liferay.portal.kernel.util.Time;
37  import com.liferay.portal.kernel.util.Validator;
38  import com.liferay.portal.kernel.webcache.WebCachePoolUtil;
39  import com.liferay.portal.model.Portlet;
40  import com.liferay.portal.search.lucene.LuceneIndexer;
41  import com.liferay.portal.security.auth.PrincipalException;
42  import com.liferay.portal.security.permission.PermissionChecker;
43  import com.liferay.portal.service.PortletLocalServiceUtil;
44  import com.liferay.portal.service.ServiceComponentLocalServiceUtil;
45  import com.liferay.portal.struts.PortletAction;
46  import com.liferay.portal.theme.ThemeDisplay;
47  import com.liferay.portal.util.MaintenanceUtil;
48  import com.liferay.portal.util.PortalInstances;
49  import com.liferay.portal.util.PrefsPropsUtil;
50  import com.liferay.portal.util.ShutdownUtil;
51  import com.liferay.portal.util.WebKeys;
52  import com.liferay.portlet.ActionResponseImpl;
53  import com.liferay.util.log4j.Log4JUtil;
54  
55  import java.util.Enumeration;
56  
57  import javax.portlet.ActionRequest;
58  import javax.portlet.ActionResponse;
59  import javax.portlet.PortletConfig;
60  import javax.portlet.PortletPreferences;
61  import javax.portlet.PortletSession;
62  import javax.portlet.PortletURL;
63  import javax.portlet.WindowState;
64  
65  import org.apache.log4j.Level;
66  import org.apache.log4j.Logger;
67  import org.apache.struts.action.ActionForm;
68  import org.apache.struts.action.ActionMapping;
69  
70  /**
71   * <a href="EditServerAction.java.html"><b><i>View Source</i></b></a>
72   *
73   * @author Brian Wing Shun Chan
74   */
75  public class EditServerAction extends PortletAction {
76  
77      public void processAction(
78              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
79              ActionRequest actionRequest, ActionResponse actionResponse)
80          throws Exception {
81  
82          ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
83              WebKeys.THEME_DISPLAY);
84  
85          PermissionChecker permissionChecker =
86              themeDisplay.getPermissionChecker();
87  
88          if (!permissionChecker.isOmniadmin()) {
89              SessionErrors.add(
90                  actionRequest, PrincipalException.class.getName());
91  
92              setForward(actionRequest, "portlet.admin.error");
93  
94              return;
95          }
96  
97          PortletPreferences preferences = PrefsPropsUtil.getPreferences();
98  
99          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
100 
101         String redirect = null;
102 
103         if (cmd.equals("addLogLevel")) {
104             addLogLevel(actionRequest);
105         }
106         else if (cmd.equals("cacheDb")) {
107             cacheDb();
108         }
109         else if (cmd.equals("cacheMulti")) {
110             cacheMulti();
111         }
112         else if (cmd.equals("cacheSingle")) {
113             cacheSingle();
114         }
115         else if (cmd.startsWith("convertProcess.")) {
116             redirect = convertProcess(actionRequest, actionResponse, cmd);
117         }
118         else if (cmd.equals("gc")) {
119             gc();
120         }
121         else if (cmd.equals("reIndex")) {
122             reIndex(actionRequest);
123         }
124         else if (cmd.equals("shutdown")) {
125             shutdown(actionRequest);
126         }
127         else if (cmd.equals("threadDump")) {
128             threadDump();
129         }
130         else if (cmd.equals("updateFileUploads")) {
131             updateFileUploads(actionRequest, preferences);
132         }
133         else if (cmd.equals("updateLogLevels")) {
134             updateLogLevels(actionRequest);
135         }
136         else if (cmd.equals("updateMail")) {
137             updateMail(actionRequest, preferences);
138         }
139         else if (cmd.equals("updateOpenOffice")) {
140             updateOpenOffice(actionRequest, preferences);
141         }
142         else if (cmd.equals("verifyPluginTables")) {
143             verifyPluginTables();
144         }
145 
146         sendRedirect(actionRequest, actionResponse, redirect);
147     }
148 
149     protected void addLogLevel(ActionRequest actionRequest) throws Exception {
150         String loggerName = ParamUtil.getString(actionRequest, "loggerName");
151         String priority = ParamUtil.getString(actionRequest, "priority");
152 
153         Logger logger = Logger.getLogger(loggerName);
154 
155         logger.setLevel(Level.toLevel(priority));
156     }
157 
158     protected void cacheDb() throws Exception {
159         CacheRegistry.clear();
160     }
161 
162     protected void cacheMulti() throws Exception {
163         MultiVMPoolUtil.clear();
164     }
165 
166     protected void cacheSingle() throws Exception {
167         WebCachePoolUtil.clear();
168     }
169 
170     protected String convertProcess(
171             ActionRequest actionRequest, ActionResponse actionResponse,
172             String cmd)
173         throws Exception {
174 
175         ActionResponseImpl actionResponseImpl =
176             (ActionResponseImpl)actionResponse;
177 
178         PortletSession portletSession = actionRequest.getPortletSession();
179 
180         String className = StringUtil.replaceFirst(
181             cmd, "convertProcess.", StringPool.BLANK);
182 
183         ConvertProcess convertProcess = (ConvertProcess)InstancePool.get(
184             className);
185 
186         String[] parameters = convertProcess.getParameterNames();
187 
188         if (parameters != null) {
189             String[] values = new String[parameters.length];
190 
191             for (int i = 0; i < parameters.length; i++) {
192                 String parameter =
193                     className + StringPool.PERIOD + parameters[i];
194 
195                 if (parameters[i].contains(StringPool.EQUAL)) {
196                     String[] parameterPair = StringUtil.split(
197                         parameters[i], StringPool.EQUAL);
198 
199                     parameter =
200                         className + StringPool.PERIOD + parameterPair[0];
201                 }
202 
203                 values[i] = ParamUtil.getString(actionRequest, parameter);
204             }
205 
206             convertProcess.setParameterValues(values);
207         }
208 
209         String path = convertProcess.getPath();
210 
211         if (path != null) {
212             PortletURL portletURL = actionResponseImpl.createRenderURL();
213 
214             portletURL.setWindowState(WindowState.MAXIMIZED);
215 
216             portletURL.setParameter("struts_action", path);
217 
218             return portletURL.toString();
219         }
220         else {
221             MaintenanceUtil.maintain(portletSession.getId(), className);
222 
223             MessageBusUtil.sendMessage(
224                 DestinationNames.CONVERT_PROCESS, className);
225 
226             return null;
227         }
228     }
229 
230     protected void gc() throws Exception {
231         Runtime.getRuntime().gc();
232     }
233 
234     protected String getFileExtensions(
235         ActionRequest actionRequest, String name) {
236 
237         String value = ParamUtil.getString(actionRequest, name);
238 
239         return value.replace(", .", ",.");
240     }
241 
242     protected void reIndex(ActionRequest actionRequest) throws Exception {
243         String portletId = ParamUtil.getString(actionRequest, "portletId");
244 
245         long[] companyIds = PortalInstances.getCompanyIds();
246 
247         if (Validator.isNull(portletId)) {
248             for (long companyId : companyIds) {
249                 try {
250                     LuceneIndexer indexer = new LuceneIndexer(companyId);
251 
252                     indexer.reIndex();
253                 }
254                 catch (Exception e) {
255                     _log.error(e, e);
256                 }
257             }
258         }
259         else {
260             Portlet portlet = PortletLocalServiceUtil.getPortletById(
261                 companyIds[0], portletId);
262 
263             if (portlet == null) {
264                 return;
265             }
266 
267             Indexer indexer = portlet.getIndexerInstance();
268 
269             if (indexer == null) {
270                 return;
271             }
272 
273             for (long companyId : companyIds) {
274                 try {
275                     SearchEngineUtil.deletePortletDocuments(
276                         companyId, portletId);
277 
278                     indexer.reIndex(new String[] {String.valueOf(companyId)});
279                 }
280                 catch (Exception e) {
281                     _log.error(e, e);
282                 }
283             }
284         }
285     }
286 
287     protected void shutdown(ActionRequest actionRequest) throws Exception {
288         long minutes =
289             ParamUtil.getInteger(actionRequest, "minutes") * Time.MINUTE;
290         String message = ParamUtil.getString(actionRequest, "message");
291 
292         if (minutes <= 0) {
293             ShutdownUtil.cancel();
294         }
295         else {
296             ShutdownUtil.shutdown(minutes, message);
297         }
298     }
299 
300     protected void threadDump() throws Exception {
301         if (_log.isInfoEnabled()) {
302             _log.info(ThreadUtil.threadDump());
303         }
304         else {
305             _log.error(
306                 "Thread dumps require the log level to be at least INFO for " +
307                     getClass().getName());
308         }
309     }
310 
311     protected void updateFileUploads(
312             ActionRequest actionRequest, PortletPreferences preferences)
313         throws Exception {
314 
315         String dlFileExtensions = getFileExtensions(
316             actionRequest, "dlFileExtensions");
317         long dlFileMaxSize = ParamUtil.getLong(actionRequest, "dlFileMaxSize");
318         String igImageExtensions = getFileExtensions(
319             actionRequest, "igImageExtensions");
320         long igImageMaxSize = ParamUtil.getLong(
321             actionRequest, "igImageMaxSize");
322         long igThumbnailMaxDimension = ParamUtil.getLong(
323             actionRequest, "igImageThumbnailMaxDimensions");
324         String journalImageExtensions = getFileExtensions(
325             actionRequest, "journalImageExtensions");
326         long journalImageSmallMaxSize = ParamUtil.getLong(
327             actionRequest, "journalImageSmallMaxSize");
328         String shoppingImageExtensions = getFileExtensions(
329             actionRequest, "shoppingImageExtensions");
330         long scImageMaxSize = ParamUtil.getLong(
331             actionRequest, "scImageMaxSize");
332         long scImageThumbnailMaxHeight = ParamUtil.getLong(
333             actionRequest, "scImageThumbnailMaxHeight");
334         long scImageThumbnailMaxWidth = ParamUtil.getLong(
335             actionRequest, "scImageThumbnailMaxWidth");
336         long shoppingImageLargeMaxSize = ParamUtil.getLong(
337             actionRequest, "shoppingImageLargeMaxSize");
338         long shoppingImageMediumMaxSize = ParamUtil.getLong(
339             actionRequest, "shoppingImageMediumMaxSize");
340         long shoppingImageSmallMaxSize = ParamUtil.getLong(
341             actionRequest, "shoppingImageSmallMaxSize");
342         long uploadServletRequestImplMaxSize = ParamUtil.getLong(
343             actionRequest, "uploadServletRequestImplMaxSize");
344         String uploadServletRequestImplTempDir = ParamUtil.getString(
345             actionRequest, "uploadServletRequestImplTempDir");
346         long usersImageMaxSize = ParamUtil.getLong(
347             actionRequest, "usersImageMaxSize");
348 
349         preferences.setValue(
350             PropsKeys.DL_FILE_EXTENSIONS, dlFileExtensions);
351         preferences.setValue(
352             PropsKeys.DL_FILE_MAX_SIZE, String.valueOf(dlFileMaxSize));
353         preferences.setValue(
354             PropsKeys.IG_IMAGE_EXTENSIONS, igImageExtensions);
355         preferences.setValue(
356             PropsKeys.IG_IMAGE_MAX_SIZE, String.valueOf(igImageMaxSize));
357         preferences.setValue(
358             PropsKeys.IG_IMAGE_THUMBNAIL_MAX_DIMENSION,
359             String.valueOf(igThumbnailMaxDimension));
360         preferences.setValue(
361             PropsKeys.JOURNAL_IMAGE_EXTENSIONS, journalImageExtensions);
362         preferences.setValue(
363             PropsKeys.JOURNAL_IMAGE_SMALL_MAX_SIZE,
364             String.valueOf(journalImageSmallMaxSize));
365         preferences.setValue(
366             PropsKeys.SHOPPING_IMAGE_EXTENSIONS, shoppingImageExtensions);
367         preferences.setValue(
368             PropsKeys.SHOPPING_IMAGE_LARGE_MAX_SIZE,
369             String.valueOf(shoppingImageLargeMaxSize));
370         preferences.setValue(
371             PropsKeys.SHOPPING_IMAGE_MEDIUM_MAX_SIZE,
372             String.valueOf(shoppingImageMediumMaxSize));
373         preferences.setValue(
374             PropsKeys.SHOPPING_IMAGE_SMALL_MAX_SIZE,
375             String.valueOf(shoppingImageSmallMaxSize));
376         preferences.setValue(
377             PropsKeys.SC_IMAGE_MAX_SIZE, String.valueOf(scImageMaxSize));
378         preferences.setValue(
379             PropsKeys.SC_IMAGE_THUMBNAIL_MAX_HEIGHT,
380             String.valueOf(scImageThumbnailMaxHeight));
381         preferences.setValue(
382             PropsKeys.SC_IMAGE_THUMBNAIL_MAX_WIDTH,
383             String.valueOf(scImageThumbnailMaxWidth));
384         preferences.setValue(
385             PropsKeys.UPLOAD_SERVLET_REQUEST_IMPL_MAX_SIZE,
386             String.valueOf(uploadServletRequestImplMaxSize));
387 
388         if (Validator.isNotNull(uploadServletRequestImplTempDir)) {
389             preferences.setValue(
390                 PropsKeys.UPLOAD_SERVLET_REQUEST_IMPL_TEMP_DIR,
391                 uploadServletRequestImplTempDir);
392         }
393 
394         preferences.setValue(
395             PropsKeys.USERS_IMAGE_MAX_SIZE, String.valueOf(usersImageMaxSize));
396 
397         preferences.store();
398     }
399 
400     protected void updateLogLevels(ActionRequest actionRequest)
401         throws Exception {
402 
403         Enumeration<String> enu = actionRequest.getParameterNames();
404 
405         while (enu.hasMoreElements()) {
406             String name = enu.nextElement();
407 
408             if (name.startsWith("logLevel")) {
409                 String loggerName = name.substring(8, name.length());
410 
411                 String priority = ParamUtil.getString(
412                     actionRequest, name, Level.INFO.toString());
413 
414                 Log4JUtil.setLevel(loggerName, priority);
415             }
416         }
417     }
418 
419     protected void updateMail(
420             ActionRequest actionRequest, PortletPreferences preferences)
421         throws Exception {
422 
423         String advancedProperties = ParamUtil.getString(
424             actionRequest, "advancedProperties");
425         String pop3Host = ParamUtil.getString(actionRequest, "pop3Host");
426         String pop3Password = ParamUtil.getString(
427             actionRequest, "pop3Password");
428         int pop3Port = ParamUtil.getInteger(actionRequest, "pop3Port");
429         boolean pop3Secure = ParamUtil.getBoolean(actionRequest, "pop3Secure");
430         String pop3User = ParamUtil.getString(actionRequest, "pop3User");
431         String smtpHost = ParamUtil.getString(actionRequest, "smtpHost");
432         String smtpPassword = ParamUtil.getString(
433             actionRequest, "smtpPassword");
434         int smtpPort = ParamUtil.getInteger(actionRequest, "smtpPort");
435         boolean smtpSecure = ParamUtil.getBoolean(actionRequest, "smtpSecure");
436         String smtpUser = ParamUtil.getString(actionRequest, "smtpUser");
437 
438         String storeProtocol = Account.PROTOCOL_POP;
439 
440         if (pop3Secure) {
441             storeProtocol = Account.PROTOCOL_POPS;
442         }
443 
444         String transportProtocol = Account.PROTOCOL_SMTP;
445 
446         if (smtpSecure) {
447             transportProtocol = Account.PROTOCOL_SMTPS;
448         }
449 
450         preferences.setValue(PropsKeys.MAIL_SESSION_MAIL, "true");
451         preferences.setValue(
452             PropsKeys.MAIL_SESSION_MAIL_ADVANCED_PROPERTIES,
453             advancedProperties);
454         preferences.setValue(PropsKeys.MAIL_SESSION_MAIL_POP3_HOST, pop3Host);
455         preferences.setValue(
456             PropsKeys.MAIL_SESSION_MAIL_POP3_PASSWORD, pop3Password);
457         preferences.setValue(
458             PropsKeys.MAIL_SESSION_MAIL_POP3_PORT, String.valueOf(pop3Port));
459         preferences.setValue(PropsKeys.MAIL_SESSION_MAIL_POP3_USER, pop3User);
460         preferences.setValue(PropsKeys.MAIL_SESSION_MAIL_SMTP_HOST, smtpHost);
461         preferences.setValue(
462             PropsKeys.MAIL_SESSION_MAIL_SMTP_PASSWORD, smtpPassword);
463         preferences.setValue(
464             PropsKeys.MAIL_SESSION_MAIL_SMTP_PORT, String.valueOf(smtpPort));
465         preferences.setValue(PropsKeys.MAIL_SESSION_MAIL_SMTP_USER, smtpUser);
466         preferences.setValue(
467             PropsKeys.MAIL_SESSION_MAIL_STORE_PROTOCOL, storeProtocol);
468         preferences.setValue(
469             PropsKeys.MAIL_SESSION_MAIL_TRANSPORT_PROTOCOL, transportProtocol);
470 
471         preferences.store();
472 
473         MailServiceUtil.clearSession();
474     }
475 
476     protected void updateOpenOffice(
477             ActionRequest actionRequest, PortletPreferences preferences)
478         throws Exception {
479 
480         boolean enabled = ParamUtil.getBoolean(actionRequest, "enabled");
481         int port = ParamUtil.getInteger(actionRequest, "port");
482 
483         preferences.setValue(
484             PropsKeys.OPENOFFICE_SERVER_ENABLED, String.valueOf(enabled));
485         preferences.setValue(
486             PropsKeys.OPENOFFICE_SERVER_PORT, String.valueOf(port));
487 
488         preferences.store();
489     }
490 
491     protected void verifyPluginTables() throws Exception {
492         ServiceComponentLocalServiceUtil.verifyDB();
493     }
494 
495     private static Log _log = LogFactoryUtil.getLog(EditServerAction.class);
496 
497 }