001
014
015 package com.liferay.portlet.admin.action;
016
017 import com.liferay.mail.service.MailServiceUtil;
018 import com.liferay.portal.captcha.CaptchaImpl;
019 import com.liferay.portal.captcha.recaptcha.ReCaptchaImpl;
020 import com.liferay.portal.captcha.simplecaptcha.SimpleCaptchaImpl;
021 import com.liferay.portal.convert.ConvertProcess;
022 import com.liferay.portal.kernel.cache.CacheRegistryUtil;
023 import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
024 import com.liferay.portal.kernel.captcha.Captcha;
025 import com.liferay.portal.kernel.captcha.CaptchaUtil;
026 import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
027 import com.liferay.portal.kernel.log.Log;
028 import com.liferay.portal.kernel.log.LogFactoryUtil;
029 import com.liferay.portal.kernel.mail.Account;
030 import com.liferay.portal.kernel.messaging.DestinationNames;
031 import com.liferay.portal.kernel.messaging.MessageBusUtil;
032 import com.liferay.portal.kernel.scripting.ScriptingException;
033 import com.liferay.portal.kernel.scripting.ScriptingUtil;
034 import com.liferay.portal.kernel.search.Indexer;
035 import com.liferay.portal.kernel.search.SearchEngineUtil;
036 import com.liferay.portal.kernel.servlet.SessionErrors;
037 import com.liferay.portal.kernel.servlet.SessionMessages;
038 import com.liferay.portal.kernel.util.Constants;
039 import com.liferay.portal.kernel.util.InstancePool;
040 import com.liferay.portal.kernel.util.ParamUtil;
041 import com.liferay.portal.kernel.util.PropsKeys;
042 import com.liferay.portal.kernel.util.StringBundler;
043 import com.liferay.portal.kernel.util.StringPool;
044 import com.liferay.portal.kernel.util.StringUtil;
045 import com.liferay.portal.kernel.util.Time;
046 import com.liferay.portal.kernel.util.Validator;
047 import com.liferay.portal.kernel.webcache.WebCachePoolUtil;
048 import com.liferay.portal.model.Portlet;
049 import com.liferay.portal.search.lucene.LuceneIndexer;
050 import com.liferay.portal.security.auth.PrincipalException;
051 import com.liferay.portal.security.permission.PermissionChecker;
052 import com.liferay.portal.service.PortletLocalServiceUtil;
053 import com.liferay.portal.service.ServiceComponentLocalServiceUtil;
054 import com.liferay.portal.struts.PortletAction;
055 import com.liferay.portal.theme.ThemeDisplay;
056 import com.liferay.portal.util.MaintenanceUtil;
057 import com.liferay.portal.util.PortalInstances;
058 import com.liferay.portal.util.PrefsPropsUtil;
059 import com.liferay.portal.util.ShutdownUtil;
060 import com.liferay.portal.util.WebKeys;
061 import com.liferay.portlet.ActionResponseImpl;
062 import com.liferay.util.log4j.Log4JUtil;
063
064 import java.util.Enumeration;
065 import java.util.Map;
066
067 import javax.portlet.ActionRequest;
068 import javax.portlet.ActionResponse;
069 import javax.portlet.PortletConfig;
070 import javax.portlet.PortletContext;
071 import javax.portlet.PortletPreferences;
072 import javax.portlet.PortletSession;
073 import javax.portlet.PortletURL;
074 import javax.portlet.WindowState;
075
076 import org.apache.log4j.Level;
077 import org.apache.log4j.Logger;
078 import org.apache.struts.action.ActionForm;
079 import org.apache.struts.action.ActionMapping;
080
081
084 public class EditServerAction extends PortletAction {
085
086 public void processAction(
087 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
088 ActionRequest actionRequest, ActionResponse actionResponse)
089 throws Exception {
090
091 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
092 WebKeys.THEME_DISPLAY);
093
094 PermissionChecker permissionChecker =
095 themeDisplay.getPermissionChecker();
096
097 if (!permissionChecker.isOmniadmin()) {
098 SessionErrors.add(
099 actionRequest, PrincipalException.class.getName());
100
101 setForward(actionRequest, "portlet.admin.error");
102
103 return;
104 }
105
106 PortletPreferences preferences = PrefsPropsUtil.getPreferences();
107
108 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
109
110 String redirect = null;
111
112 if (cmd.equals("addLogLevel")) {
113 addLogLevel(actionRequest);
114 }
115 else if (cmd.equals("cacheDb")) {
116 cacheDb();
117 }
118 else if (cmd.equals("cacheMulti")) {
119 cacheMulti();
120 }
121 else if (cmd.equals("cacheSingle")) {
122 cacheSingle();
123 }
124 else if (cmd.startsWith("convertProcess.")) {
125 redirect = convertProcess(actionRequest, actionResponse, cmd);
126 }
127 else if (cmd.equals("gc")) {
128 gc();
129 }
130 else if (cmd.equals("reindex")) {
131 reindex(actionRequest);
132 }
133 else if (cmd.equals("runScript")) {
134 runScript(portletConfig, actionRequest, actionResponse);
135 }
136 else if (cmd.equals("shutdown")) {
137 shutdown(actionRequest);
138 }
139 else if (cmd.equals("threadDump")) {
140 threadDump();
141 }
142 else if (cmd.equals("updateCaptcha")) {
143 updateCaptcha(actionRequest, preferences);
144 }
145 else if (cmd.equals("updateFileUploads")) {
146 updateFileUploads(actionRequest, preferences);
147 }
148 else if (cmd.equals("updateLogLevels")) {
149 updateLogLevels(actionRequest);
150 }
151 else if (cmd.equals("updateMail")) {
152 updateMail(actionRequest, preferences);
153 }
154 else if (cmd.equals("updateOpenOffice")) {
155 updateOpenOffice(actionRequest, preferences);
156 }
157 else if (cmd.equals("verifyPluginTables")) {
158 verifyPluginTables();
159 }
160
161 sendRedirect(actionRequest, actionResponse, redirect);
162 }
163
164 protected void addLogLevel(ActionRequest actionRequest) throws Exception {
165 String loggerName = ParamUtil.getString(actionRequest, "loggerName");
166 String priority = ParamUtil.getString(actionRequest, "priority");
167
168 Logger logger = Logger.getLogger(loggerName);
169
170 logger.setLevel(Level.toLevel(priority));
171 }
172
173 protected void cacheDb() throws Exception {
174 CacheRegistryUtil.clear();
175 }
176
177 protected void cacheMulti() throws Exception {
178 MultiVMPoolUtil.clear();
179 }
180
181 protected void cacheSingle() throws Exception {
182 WebCachePoolUtil.clear();
183 }
184
185 protected String convertProcess(
186 ActionRequest actionRequest, ActionResponse actionResponse,
187 String cmd)
188 throws Exception {
189
190 ActionResponseImpl actionResponseImpl =
191 (ActionResponseImpl)actionResponse;
192
193 PortletSession portletSession = actionRequest.getPortletSession();
194
195 String className = StringUtil.replaceFirst(
196 cmd, "convertProcess.", StringPool.BLANK);
197
198 ConvertProcess convertProcess = (ConvertProcess)InstancePool.get(
199 className);
200
201 String[] parameters = convertProcess.getParameterNames();
202
203 if (parameters != null) {
204 String[] values = new String[parameters.length];
205
206 for (int i = 0; i < parameters.length; i++) {
207 String parameter =
208 className + StringPool.PERIOD + parameters[i];
209
210 if (parameters[i].contains(StringPool.EQUAL)) {
211 String[] parameterPair = StringUtil.split(
212 parameters[i], StringPool.EQUAL);
213
214 parameter =
215 className + StringPool.PERIOD + parameterPair[0];
216 }
217
218 values[i] = ParamUtil.getString(actionRequest, parameter);
219 }
220
221 convertProcess.setParameterValues(values);
222 }
223
224 String path = convertProcess.getPath();
225
226 if (path != null) {
227 PortletURL portletURL = actionResponseImpl.createRenderURL();
228
229 portletURL.setWindowState(WindowState.MAXIMIZED);
230
231 portletURL.setParameter("struts_action", path);
232
233 return portletURL.toString();
234 }
235 else {
236 MaintenanceUtil.maintain(portletSession.getId(), className);
237
238 MessageBusUtil.sendMessage(
239 DestinationNames.CONVERT_PROCESS, className);
240
241 return null;
242 }
243 }
244
245 protected void gc() throws Exception {
246 Runtime.getRuntime().gc();
247 }
248
249 protected String getFileExtensions(
250 ActionRequest actionRequest, String name) {
251
252 String value = ParamUtil.getString(actionRequest, name);
253
254 return value.replace(", .", ",.");
255 }
256
257 protected void reindex(ActionRequest actionRequest) throws Exception {
258 String portletId = ParamUtil.getString(actionRequest, "portletId");
259
260 long[] companyIds = PortalInstances.getCompanyIds();
261
262 if (Validator.isNull(portletId)) {
263 for (long companyId : companyIds) {
264 try {
265 LuceneIndexer indexer = new LuceneIndexer(companyId);
266
267 indexer.reindex();
268 }
269 catch (Exception e) {
270 _log.error(e, e);
271 }
272 }
273 }
274 else {
275 Portlet portlet = PortletLocalServiceUtil.getPortletById(
276 companyIds[0], portletId);
277
278 if (portlet == null) {
279 return;
280 }
281
282 Indexer indexer = portlet.getIndexerInstance();
283
284 if (indexer == null) {
285 return;
286 }
287
288 for (long companyId : companyIds) {
289 try {
290 SearchEngineUtil.deletePortletDocuments(
291 companyId, portletId);
292
293 indexer.reindex(new String[] {String.valueOf(companyId)});
294 }
295 catch (Exception e) {
296 _log.error(e, e);
297 }
298 }
299 }
300 }
301
302 protected void runScript(
303 PortletConfig portletConfig, ActionRequest actionRequest,
304 ActionResponse actionResponse)
305 throws Exception {
306
307 String language = ParamUtil.getString(actionRequest, "language");
308 String script = ParamUtil.getString(actionRequest, "script");
309
310 PortletContext portletContext = portletConfig.getPortletContext();
311
312 Map<String, Object> portletObjects = ScriptingUtil.getPortletObjects(
313 portletConfig, portletContext, actionRequest, actionResponse);
314
315 UnsyncByteArrayOutputStream unsyncByteArrayOutputStream =
316 new UnsyncByteArrayOutputStream();
317
318 portletObjects.put("out", unsyncByteArrayOutputStream);
319
320 try {
321 ScriptingUtil.exec(null, portletObjects, language, script);
322
323 SessionMessages.add(
324 actionRequest, "script_output",
325 unsyncByteArrayOutputStream.toString());
326 }
327 catch (ScriptingException se) {
328 SessionErrors.add(
329 actionRequest, ScriptingException.class.getName(), se);
330
331 _log.error(se.getMessage());
332 }
333 }
334
335 protected void shutdown(ActionRequest actionRequest) throws Exception {
336 long minutes =
337 ParamUtil.getInteger(actionRequest, "minutes") * Time.MINUTE;
338 String message = ParamUtil.getString(actionRequest, "message");
339
340 if (minutes <= 0) {
341 ShutdownUtil.cancel();
342 }
343 else {
344 ShutdownUtil.shutdown(minutes, message);
345 }
346 }
347
348 protected void threadDump() throws Exception {
349 String jvm =
350 System.getProperty("java.vm.name") + " " +
351 System.getProperty("java.vm.version");
352
353 StringBundler sb = new StringBundler(
354 "Full thread dump " + jvm + "\n\n");
355
356 Map<Thread, StackTraceElement[]> stackTraces =
357 Thread.getAllStackTraces();
358
359 for (Map.Entry<Thread, StackTraceElement[]> entry :
360 stackTraces.entrySet()) {
361
362 Thread thread = entry.getKey();
363 StackTraceElement[] elements = entry.getValue();
364
365 sb.append(StringPool.QUOTE);
366 sb.append(thread.getName());
367 sb.append(StringPool.QUOTE);
368
369 if (thread.getThreadGroup() != null) {
370 sb.append(StringPool.SPACE);
371 sb.append(StringPool.OPEN_PARENTHESIS);
372 sb.append(thread.getThreadGroup().getName());
373 sb.append(StringPool.CLOSE_PARENTHESIS);
374 }
375
376 sb.append(", priority=");
377 sb.append(thread.getPriority());
378 sb.append(", id=");
379 sb.append(thread.getId());
380 sb.append(", state=");
381 sb.append(thread.getState());
382 sb.append("\n");
383
384 for (int i = 0; i < elements.length; i++) {
385 sb.append("\t");
386 sb.append(elements[i]);
387 sb.append("\n");
388 }
389
390 sb.append("\n");
391 }
392
393 if (_log.isInfoEnabled()) {
394 _log.info(sb.toString());
395 }
396 else {
397 _log.error(
398 "Thread dumps require the log level to be at least INFO for " +
399 getClass().getName());
400 }
401 }
402
403 protected void updateCaptcha(
404 ActionRequest actionRequest, PortletPreferences preferences)
405 throws Exception {
406
407 boolean reCaptchaEnabled = ParamUtil.getBoolean(
408 actionRequest, "reCaptchaEnabled");
409 String reCaptchaPrivateKey = ParamUtil.getString(
410 actionRequest, "reCaptchaPrivateKey");
411 String reCaptchaPublicKey = ParamUtil.getString(
412 actionRequest, "reCaptchaPublicKey");
413
414 Captcha captcha = null;
415
416 if (reCaptchaEnabled) {
417 captcha = new ReCaptchaImpl();
418 }
419 else {
420 captcha = new SimpleCaptchaImpl();
421 }
422
423 preferences.setValue(
424 PropsKeys.CAPTCHA_ENGINE_IMPL, captcha.getClass().getName());
425 preferences.setValue(
426 PropsKeys.CAPTCHA_ENGINE_RECAPTCHA_KEY_PRIVATE,
427 reCaptchaPrivateKey);
428 preferences.setValue(
429 PropsKeys.CAPTCHA_ENGINE_RECAPTCHA_KEY_PUBLIC, reCaptchaPublicKey);
430
431 preferences.store();
432
433 CaptchaImpl captchaImpl = (CaptchaImpl)CaptchaUtil.getCaptcha();
434
435 captchaImpl.setCaptcha(captcha);
436 }
437
438 protected void updateFileUploads(
439 ActionRequest actionRequest, PortletPreferences preferences)
440 throws Exception {
441
442 String dlFileExtensions = getFileExtensions(
443 actionRequest, "dlFileExtensions");
444 long dlFileMaxSize = ParamUtil.getLong(actionRequest, "dlFileMaxSize");
445 String igImageExtensions = getFileExtensions(
446 actionRequest, "igImageExtensions");
447 long igImageMaxSize = ParamUtil.getLong(
448 actionRequest, "igImageMaxSize");
449 long igThumbnailMaxDimension = ParamUtil.getLong(
450 actionRequest, "igImageThumbnailMaxDimensions");
451 String journalImageExtensions = getFileExtensions(
452 actionRequest, "journalImageExtensions");
453 long journalImageSmallMaxSize = ParamUtil.getLong(
454 actionRequest, "journalImageSmallMaxSize");
455 String shoppingImageExtensions = getFileExtensions(
456 actionRequest, "shoppingImageExtensions");
457 long scImageMaxSize = ParamUtil.getLong(
458 actionRequest, "scImageMaxSize");
459 long scImageThumbnailMaxHeight = ParamUtil.getLong(
460 actionRequest, "scImageThumbnailMaxHeight");
461 long scImageThumbnailMaxWidth = ParamUtil.getLong(
462 actionRequest, "scImageThumbnailMaxWidth");
463 long shoppingImageLargeMaxSize = ParamUtil.getLong(
464 actionRequest, "shoppingImageLargeMaxSize");
465 long shoppingImageMediumMaxSize = ParamUtil.getLong(
466 actionRequest, "shoppingImageMediumMaxSize");
467 long shoppingImageSmallMaxSize = ParamUtil.getLong(
468 actionRequest, "shoppingImageSmallMaxSize");
469 long uploadServletRequestImplMaxSize = ParamUtil.getLong(
470 actionRequest, "uploadServletRequestImplMaxSize");
471 String uploadServletRequestImplTempDir = ParamUtil.getString(
472 actionRequest, "uploadServletRequestImplTempDir");
473 long usersImageMaxSize = ParamUtil.getLong(
474 actionRequest, "usersImageMaxSize");
475
476 preferences.setValue(
477 PropsKeys.DL_FILE_EXTENSIONS, dlFileExtensions);
478 preferences.setValue(
479 PropsKeys.DL_FILE_MAX_SIZE, String.valueOf(dlFileMaxSize));
480 preferences.setValue(
481 PropsKeys.IG_IMAGE_EXTENSIONS, igImageExtensions);
482 preferences.setValue(
483 PropsKeys.IG_IMAGE_MAX_SIZE, String.valueOf(igImageMaxSize));
484 preferences.setValue(
485 PropsKeys.IG_IMAGE_THUMBNAIL_MAX_DIMENSION,
486 String.valueOf(igThumbnailMaxDimension));
487 preferences.setValue(
488 PropsKeys.JOURNAL_IMAGE_EXTENSIONS, journalImageExtensions);
489 preferences.setValue(
490 PropsKeys.JOURNAL_IMAGE_SMALL_MAX_SIZE,
491 String.valueOf(journalImageSmallMaxSize));
492 preferences.setValue(
493 PropsKeys.SHOPPING_IMAGE_EXTENSIONS, shoppingImageExtensions);
494 preferences.setValue(
495 PropsKeys.SHOPPING_IMAGE_LARGE_MAX_SIZE,
496 String.valueOf(shoppingImageLargeMaxSize));
497 preferences.setValue(
498 PropsKeys.SHOPPING_IMAGE_MEDIUM_MAX_SIZE,
499 String.valueOf(shoppingImageMediumMaxSize));
500 preferences.setValue(
501 PropsKeys.SHOPPING_IMAGE_SMALL_MAX_SIZE,
502 String.valueOf(shoppingImageSmallMaxSize));
503 preferences.setValue(
504 PropsKeys.SC_IMAGE_MAX_SIZE, String.valueOf(scImageMaxSize));
505 preferences.setValue(
506 PropsKeys.SC_IMAGE_THUMBNAIL_MAX_HEIGHT,
507 String.valueOf(scImageThumbnailMaxHeight));
508 preferences.setValue(
509 PropsKeys.SC_IMAGE_THUMBNAIL_MAX_WIDTH,
510 String.valueOf(scImageThumbnailMaxWidth));
511 preferences.setValue(
512 PropsKeys.UPLOAD_SERVLET_REQUEST_IMPL_MAX_SIZE,
513 String.valueOf(uploadServletRequestImplMaxSize));
514
515 if (Validator.isNotNull(uploadServletRequestImplTempDir)) {
516 preferences.setValue(
517 PropsKeys.UPLOAD_SERVLET_REQUEST_IMPL_TEMP_DIR,
518 uploadServletRequestImplTempDir);
519 }
520
521 preferences.setValue(
522 PropsKeys.USERS_IMAGE_MAX_SIZE, String.valueOf(usersImageMaxSize));
523
524 preferences.store();
525 }
526
527 protected void updateLogLevels(ActionRequest actionRequest)
528 throws Exception {
529
530 Enumeration<String> enu = actionRequest.getParameterNames();
531
532 while (enu.hasMoreElements()) {
533 String name = enu.nextElement();
534
535 if (name.startsWith("logLevel")) {
536 String loggerName = name.substring(8, name.length());
537
538 String priority = ParamUtil.getString(
539 actionRequest, name, Level.INFO.toString());
540
541 Log4JUtil.setLevel(loggerName, priority);
542 }
543 }
544 }
545
546 protected void updateMail(
547 ActionRequest actionRequest, PortletPreferences preferences)
548 throws Exception {
549
550 String advancedProperties = ParamUtil.getString(
551 actionRequest, "advancedProperties");
552 String pop3Host = ParamUtil.getString(actionRequest, "pop3Host");
553 String pop3Password = ParamUtil.getString(
554 actionRequest, "pop3Password");
555 int pop3Port = ParamUtil.getInteger(actionRequest, "pop3Port");
556 boolean pop3Secure = ParamUtil.getBoolean(actionRequest, "pop3Secure");
557 String pop3User = ParamUtil.getString(actionRequest, "pop3User");
558 String smtpHost = ParamUtil.getString(actionRequest, "smtpHost");
559 String smtpPassword = ParamUtil.getString(
560 actionRequest, "smtpPassword");
561 int smtpPort = ParamUtil.getInteger(actionRequest, "smtpPort");
562 boolean smtpSecure = ParamUtil.getBoolean(actionRequest, "smtpSecure");
563 String smtpUser = ParamUtil.getString(actionRequest, "smtpUser");
564
565 String storeProtocol = Account.PROTOCOL_POP;
566
567 if (pop3Secure) {
568 storeProtocol = Account.PROTOCOL_POPS;
569 }
570
571 String transportProtocol = Account.PROTOCOL_SMTP;
572
573 if (smtpSecure) {
574 transportProtocol = Account.PROTOCOL_SMTPS;
575 }
576
577 preferences.setValue(PropsKeys.MAIL_SESSION_MAIL, "true");
578 preferences.setValue(
579 PropsKeys.MAIL_SESSION_MAIL_ADVANCED_PROPERTIES,
580 advancedProperties);
581 preferences.setValue(PropsKeys.MAIL_SESSION_MAIL_POP3_HOST, pop3Host);
582 preferences.setValue(
583 PropsKeys.MAIL_SESSION_MAIL_POP3_PASSWORD, pop3Password);
584 preferences.setValue(
585 PropsKeys.MAIL_SESSION_MAIL_POP3_PORT, String.valueOf(pop3Port));
586 preferences.setValue(PropsKeys.MAIL_SESSION_MAIL_POP3_USER, pop3User);
587 preferences.setValue(PropsKeys.MAIL_SESSION_MAIL_SMTP_HOST, smtpHost);
588 preferences.setValue(
589 PropsKeys.MAIL_SESSION_MAIL_SMTP_PASSWORD, smtpPassword);
590 preferences.setValue(
591 PropsKeys.MAIL_SESSION_MAIL_SMTP_PORT, String.valueOf(smtpPort));
592 preferences.setValue(PropsKeys.MAIL_SESSION_MAIL_SMTP_USER, smtpUser);
593 preferences.setValue(
594 PropsKeys.MAIL_SESSION_MAIL_STORE_PROTOCOL, storeProtocol);
595 preferences.setValue(
596 PropsKeys.MAIL_SESSION_MAIL_TRANSPORT_PROTOCOL, transportProtocol);
597
598 preferences.store();
599
600 MailServiceUtil.clearSession();
601 }
602
603 protected void updateOpenOffice(
604 ActionRequest actionRequest, PortletPreferences preferences)
605 throws Exception {
606
607 boolean enabled = ParamUtil.getBoolean(actionRequest, "enabled");
608 int port = ParamUtil.getInteger(actionRequest, "port");
609
610 preferences.setValue(
611 PropsKeys.OPENOFFICE_SERVER_ENABLED, String.valueOf(enabled));
612 preferences.setValue(
613 PropsKeys.OPENOFFICE_SERVER_PORT, String.valueOf(port));
614
615 preferences.store();
616 }
617
618 protected void verifyPluginTables() throws Exception {
619 ServiceComponentLocalServiceUtil.verifyDB();
620 }
621
622 private static Log _log = LogFactoryUtil.getLog(EditServerAction.class);
623
624 }