View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation 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.seasar.tuigwaa.controller;
17  
18  import java.io.IOException;
19  import java.util.ArrayList;
20  import java.util.Enumeration;
21  import java.util.Iterator;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.jar.JarInputStream;
25  import java.util.jar.JarOutputStream;
26  import java.util.zip.ZipInputStream;
27  import java.util.zip.ZipOutputStream;
28  
29  import javax.servlet.ServletContext;
30  import javax.servlet.http.HttpServletRequest;
31  import javax.servlet.http.HttpServletResponse;
32  
33  import org.apache.commons.beanutils.DynaBean;
34  import org.apache.commons.beanutils.PropertyUtils;
35  import org.apache.commons.httpclient.HttpClient;
36  import org.apache.commons.httpclient.HttpException;
37  import org.apache.commons.httpclient.HttpStatus;
38  import org.apache.commons.httpclient.methods.GetMethod;
39  import org.apache.commons.io.IOUtils;
40  import org.apache.commons.validator.ValidatorResources;
41  import org.apache.struts.Globals;
42  import org.apache.struts.action.ActionForm;
43  import org.apache.struts.action.ActionForward;
44  import org.apache.struts.action.ActionMapping;
45  import org.apache.struts.action.ActionMessage;
46  import org.apache.struts.action.ActionMessages;
47  import org.apache.struts.actions.MappingDispatchAction;
48  import org.apache.struts.config.ActionConfig;
49  import org.apache.struts.config.FormBeanConfig;
50  import org.apache.struts.config.ModuleConfig;
51  import org.apache.struts.upload.FormFile;
52  import org.apache.struts.validator.ValidatorPlugIn;
53  import org.seasar.framework.log.Logger;
54  import org.seasar.tuigwaa.cms.ContentsStoreService;
55  import org.seasar.tuigwaa.database.BasicDatabaseService;
56  import org.seasar.tuigwaa.database.DatabaseInfo;
57  import org.seasar.tuigwaa.security.DirectoryService;
58  import org.seasar.tuigwaa.security.SecurityService;
59  import org.seasar.tuigwaa.security.auth.TgwRole;
60  import org.seasar.tuigwaa.security.auth.TgwUser;
61  import org.seasar.tuigwaa.system.BackupDto;
62  import org.seasar.tuigwaa.system.Constants;
63  import org.seasar.tuigwaa.system.DesignConfig;
64  import org.seasar.tuigwaa.system.DesignService;
65  import org.seasar.tuigwaa.system.LogSearchCondition;
66  import org.seasar.tuigwaa.system.LogService;
67  import org.seasar.tuigwaa.system.SiteConfig;
68  import org.seasar.tuigwaa.system.SiteService;
69  import org.seasar.tuigwaa.system.SiteStatus;
70  import org.seasar.tuigwaa.system.TgwException;
71  import org.seasar.tuigwaa.system.TgwServiceException;
72  import org.seasar.tuigwaa.util.MimeMappings;
73  
74  import com.isenshi.util.CharUtil;
75  import com.isenshi.util.HttpUtils;
76  import com.isenshi.util.ResourceUtils;
77  import com.isenshi.util.extlib.StrutsUtil;
78  
79  /***
80   * @author nishioka
81   */
82  public class SystemAction extends MappingDispatchAction {
83  
84  	private static final String TGWAR_CONTENT_TYPE = MimeMappings
85  			.getContentType(MimeMappings.EXTENSION_TGWAR);
86  
87  	private static final String ZIP_CONTENT_TYPE = MimeMappings
88  			.getContentType(MimeMappings.EXTENSION_ZIP);
89  
90  	private static final Logger log = Logger.getLogger(SystemAction.class);
91  
92  	private SiteService siteService;
93  
94  	private BasicDatabaseService basicDatabaseService;
95  
96  	private DirectoryService directoryService;
97  
98  	private ContentsStoreService contentsStoreService;
99  
100 	private DesignService designService;
101 
102 	private LogService logService;
103 
104 	public SystemAction(SiteService siteService,
105 			BasicDatabaseService basicDatabaseService,
106 			DirectoryService directoryService,
107 			ContentsStoreService contentsStoreService,
108 			DesignService designService, LogService logService) {
109 		this.siteService = siteService;
110 		this.basicDatabaseService = basicDatabaseService;
111 		this.directoryService = directoryService;
112 		this.contentsStoreService = contentsStoreService;
113 		this.designService = designService;
114 		this.logService = logService;
115 
116 	}
117 
118 	public ActionForward saveDefaultSite(ActionMapping mapping,
119 			ActionForm form, HttpServletRequest request,
120 			HttpServletResponse response) throws Exception {
121 		String siteName = request.getParameter(Constants.PARAM_SITENAME);
122 		getServlet().getServletContext().setAttribute(
123 				Constants.CATTR_DEFAULTSITENAME, siteName);
124 		ResourceUtils.writeContent(Constants.FILE_DEFAULTSITE, siteName);
125 		return mapping.findForward(Constants.LOCAL_FORWARD_SUCCESS);
126 	}
127 
128 	public ActionForward top(ActionMapping mapping, ActionForm form,
129 			HttpServletRequest request, HttpServletResponse response)
130 			throws Exception {
131 		bindSiteList(request);
132 		bindStatusList(request);
133 		bindUserList(request);
134 		bindRoleList(request);
135 		return mapping.findForward(Constants.LOCAL_FORWARD_SUCCESS);
136 	}
137 
138 	public ActionForward pageRestore(ActionMapping mapping, ActionForm form,
139 			HttpServletRequest request, HttpServletResponse response)
140 			throws Exception {
141 		bindSiteList(request);
142 		String siteName = request.getParameter(Constants.PARAM_SITENAME);
143 		String backupDate = request.getParameter(Constants.PARAM_BACKUPDATE);
144 		BackupDto dto = new BackupDto();
145 		dto.setBackupDate(backupDate);
146 		try {
147 			siteService.restore(siteName, dto);
148 		} catch (TgwServiceException tse) {
149 			// FIXME: needs message handling
150 			tse.printStackTrace();
151 		}
152 		return mapping.findForward(Constants.LOCAL_FORWARD_SUCCESS);
153 	}
154 
155 	public ActionForward deleteBackup(ActionMapping mapping, ActionForm form,
156 			HttpServletRequest request, HttpServletResponse response)
157 			throws Exception {
158 		bindSiteList(request);
159 		String siteName = request.getParameter(Constants.PARAM_SITENAME);
160 		String backupDate = request.getParameter(Constants.PARAM_BACKUPDATE);
161 		BackupDto backupDto = new BackupDto();
162 		backupDto.setBackupDate(backupDate);
163 
164 		try {
165 			siteService.deleteBackup(siteName, backupDto);
166 		} catch (TgwServiceException tse) {
167 			// FIXME: needs message hendling
168 			tse.printStackTrace();
169 		}
170 		return mapping.findForward(Constants.LOCAL_FORWARD_SUCCESS);
171 	}
172 
173 	public ActionForward listBackup(ActionMapping mapping, ActionForm form,
174 			HttpServletRequest request, HttpServletResponse response)
175 			throws Exception {
176 		bindSiteList(request);
177 		String siteName = request.getParameter(Constants.PARAM_SITENAME);
178 
179 		if (siteName == null) {
180 			// this might be illegal call, need to error message handling ??
181 			return null;
182 		}
183 		bindBackupList(request, siteName);
184 		return mapping.findForward(Constants.LOCAL_FORWARD_SUCCESS);
185 	}
186 
187 	public ActionForward createBackup(ActionMapping mapping, ActionForm form,
188 			HttpServletRequest request, HttpServletResponse response)
189 			throws Exception {
190 
191 		bindSiteList(request);
192 		DynaBean bean = (DynaBean) form;
193 		String siteName = (String) bean.get(Constants.PARAM_SITENAME);
194 		String type = (String) bean.get("type");
195 		String[] tablenames = (String[]) bean.get("tablenames");
196 
197 		BackupDto dto = new BackupDto();
198 		dto.setType(type);
199 		dto.setTablenames(tablenames);
200 
201 		try {
202 			siteService.backup(siteName, dto);
203 		} catch (TgwServiceException tse) {
204 			// FIXME: needs message handling
205 			tse.printStackTrace();
206 		}
207 
208 		bindBackupList(request, siteName);
209 		return mapping.findForward(Constants.LOCAL_FORWARD_SUCCESS);
210 	}
211 
212 	public ActionForward restoreBackup(ActionMapping mapping, ActionForm form,
213 			HttpServletRequest request, HttpServletResponse response)
214 			throws Exception {
215 
216 		bindSiteList(request);
217 
218 		String siteName = request.getParameter(Constants.PARAM_SITENAME);
219 		String backupDate = request.getParameter(Constants.PARAM_BACKUPDATE);
220 
221 		if (siteName == null || "".equals(siteName)) {
222 			return null;
223 		}
224 
225 		BackupDto dto = new BackupDto();
226 		dto.setBackupDate(backupDate);
227 		try {
228 			siteService.restore(siteName, dto);
229 		} catch (TgwServiceException tse) {
230 			// FIXME: needs message handling
231 			tse.printStackTrace();
232 		}
233 		return mapping.findForward(Constants.LOCAL_FORWARD_SUCCESS);
234 	}
235 
236 	public ActionForward createSite(ActionMapping mapping, ActionForm form,
237 			HttpServletRequest request, HttpServletResponse response)
238 			throws Exception {
239 		bindSiteList(request);
240 		request.setAttribute(Constants.RATTR_ROLELIST, directoryService
241 				.getRoles());
242 
243 		List list = basicDatabaseService.getExternalDatabaseInfoList();
244 		request.setAttribute(Constants.RATTR_EXTERNALDBINFOLIST, list);
245 
246 		String[] skins = designService.getAllSkinNames();
247 		SiteForm siteForm = new SiteForm();
248 		siteForm.setSkins(skins);
249 
250 		request.setAttribute("siteForm", siteForm);
251 
252 		return mapping.findForward(Constants.LOCAL_FORWARD_SUCCESS);
253 	}
254 
255 	public ActionForward editSite(ActionMapping mapping, ActionForm form,
256 			HttpServletRequest request, HttpServletResponse response)
257 			throws Exception {
258 		bindSiteList(request);
259 
260 		request.setAttribute(Constants.RATTR_ROLELIST, directoryService
261 				.getRoles());
262 
263 		String siteName = request.getParameter(Constants.PARAM_SITENAME);
264 		SiteConfig config = siteService.getSiteConfig(siteName);
265 
266 		SiteForm siteForm = new SiteForm(config);
267 		siteForm.setSkins(designService.getAllSkinNames());
268 		request.setAttribute("siteForm", siteForm);
269 
270 		return mapping.findForward(Constants.LOCAL_FORWARD_SUCCESS);
271 	}
272 
273 	public ActionForward listSite(ActionMapping mapping, ActionForm form,
274 			HttpServletRequest request, HttpServletResponse response)
275 			throws Exception {
276 		bindSiteList(request);
277 		return mapping.findForward(Constants.LOCAL_FORWARD_SUCCESS);
278 	}
279 
280 	public ActionForward saveSite(ActionMapping mapping, ActionForm form,
281 			HttpServletRequest request, HttpServletResponse response)
282 			throws Exception {
283 		SiteForm siteForm = (SiteForm) form;
284 
285 		String title = siteForm.getTitle();
286 		String name = siteForm.getName();
287 		String description = siteForm.getDescription();
288 		String[] roles = siteForm.getRoles();
289 		boolean easyMode = siteForm.isEasyMode();
290 		int pageEditMode = siteForm.getPageEditMode();
291 
292 		boolean unauthenticatedFlag = siteForm.isUnauthenticated();
293 		boolean resctriction = siteForm.isRestriction();
294 		if (resctriction && unauthenticatedFlag) {
295 			String[] addroles = new String[roles.length + 1];
296 			System.arraycopy(roles, 0, addroles, 0, roles.length);
297 			addroles[addroles.length - 1] = SecurityService.ROLE_UNAUTHENTICATED;
298 			roles = addroles;
299 		} else if (!resctriction) {
300 			roles = new String[0];
301 		}
302 
303 		String rolestr = CharUtil.join(roles, ",");
304 
305 		SiteConfig siteConfig = new SiteConfig();
306 		siteConfig.setTitle(title);
307 		siteConfig.setName(name);
308 		siteConfig.setDescription(description);
309 		siteConfig.setRoles(rolestr);
310 		siteConfig.setEasyMode(easyMode);
311 		siteConfig.setPageEditMode(pageEditMode);
312 		siteConfig.setSkin(siteForm.getSkin());
313 
314 		if (!siteForm.isBaseDatabase()) {
315 			siteConfig.setUseBaseDatabase(false);
316 			siteConfig.setDbName(siteForm.getDbName());
317 			siteConfig.setDbSchema(siteForm.getDbSchema());
318 		}
319 
320 		if (siteService.getSiteConfig(name) == null) {
321 			siteService.createSite(siteConfig);
322 		} else {
323 			siteService.editSite(siteConfig);
324 		}
325 		return top(mapping, form, request, response);
326 	}
327 
328 	public ActionForward deleteSite(ActionMapping mapping, ActionForm form,
329 			HttpServletRequest request, HttpServletResponse response)
330 			throws Exception {
331 		String siteName = request.getParameter(Constants.PARAM_SITENAME);
332 		siteService.deleteSite(siteName);
333 		return top(mapping, form, request, response);
334 	}
335 
336 	public ActionForward copySite(ActionMapping mapping, ActionForm form,
337 			HttpServletRequest request, HttpServletResponse response)
338 			throws Exception {
339 
340 		DynaBean bean = (DynaBean) form;
341 		String srcSiteName = (String) bean.get("srcSiteName");
342 		String destSiteName = (String) bean.get("destSiteName");
343 
344 		if (siteService.getSiteConfig(destSiteName) != null) {
345 			throw new TgwException("ETGW0501", new Object[] { destSiteName });
346 		}
347 
348 		siteService.copySite(srcSiteName, destSiteName);
349 		return top(mapping, form, request, response);
350 	}
351 
352 	public ActionForward uploadSiteFromWeb(ActionMapping mapping,
353 			ActionForm form, HttpServletRequest request,
354 			HttpServletResponse response) throws Exception {
355 
356 		String url = (String) ((DynaBean) form).get("url");
357 		String[] path = url.split("/");
358 		String fileName = path[path.length - 1];
359 		String siteName = fileName.split("//.")[0];
360 
361 		if (siteService.getSiteConfig(siteName) == null) {
362 			HttpClient client = new HttpClient();
363 			GetMethod method = new GetMethod(url);
364 			try {
365 				// Execute the method.
366 				int statusCode = client.executeMethod(method);
367 				if (statusCode != HttpStatus.SC_OK) {
368 					saveMessage(request, "urlupload.method.faild", null);
369 					return mapping.findForward(Constants.LOCAL_FORWARD_ERROR);
370 				}
371 
372 				BackupDto dto = new BackupDto();
373 				siteService.uploadSite(siteName, new JarInputStream(method
374 						.getResponseBodyAsStream()), dto);
375 			} catch (HttpException e) {
376 				saveMessage(request, "urlupload.fatal.protocol.violation", e);
377 				return mapping.findForward(Constants.LOCAL_FORWARD_SUCCESS);
378 			} catch (IOException e) {
379 				saveMessage(request, "urlupload.fatal.protocol.violation", e);
380 				return mapping.findForward(Constants.LOCAL_FORWARD_SUCCESS);
381 			} finally {
382 				log.log("ETGW4002", new Object[] { url });
383 				method.releaseConnection();
384 			}
385 
386 		} else {
387 			saveMessage(request, "msg.site.exist", null);
388 		}
389 		return mapping.findForward(Constants.LOCAL_FORWARD_SUCCESS);
390 	}
391 
392 	private void saveMessage(HttpServletRequest request, String key, Throwable e) {
393 		ActionMessages msgs = new ActionMessages();
394 		msgs.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(key));
395 		saveMessages(request, msgs);
396 		if (e != null) {
397 			log.error(key + ": " + e.getMessage());
398 		} else {
399 			log.error(key);
400 		}
401 	}
402 
403 	public ActionForward uploadSite(ActionMapping mapping, ActionForm form,
404 			HttpServletRequest request, HttpServletResponse response)
405 			throws Exception {
406 
407 		DynaBean bean = (DynaBean) form;
408 		FormFile file = (FormFile) bean.get("file");
409 		String destSiteName = (String) bean.get("name");
410 
411 		if (file == null) {
412 			saveMessage(request,"file.sizeexceed",null);
413 			return mapping.findForward(Constants.LOCAL_FORWARD_ERROR);
414 		}
415 
416 		String fileName = file.getFileName();
417 		String siteName = fileName.split("//.")[0];
418 
419 		if (siteService.getSiteConfig(siteName) == null) {
420 			BackupDto dto = new BackupDto();
421 			siteService.uploadSite(siteName, new JarInputStream(file.getInputStream()), dto);
422 			
423 			if (destSiteName != null && !"".equals(destSiteName)
424 					&& siteService.getSiteConfig(destSiteName) == null) {
425 				siteService.copySite(siteName, destSiteName);
426 				siteService.deleteSite(siteName);
427 			}			
428 		} else {
429 			saveMessage(request,"msg.site.exist",null);			
430 		}
431 		
432 		return mapping.findForward(Constants.LOCAL_FORWARD_SUCCESS);
433 	}
434 
435 	public ActionForward downloadSite(ActionMapping mapping, ActionForm form,
436 			HttpServletRequest request, HttpServletResponse response)
437 			throws Exception {
438 
439 		String siteName = request.getParameter(Constants.PARAM_SITENAME);
440 		String backupDate = request.getParameter(Constants.PARAM_BACKUPDATE);
441 
442 		HttpUtils.setResponseHeader(request, response, TGWAR_CONTENT_TYPE,
443 				siteName + "." + MimeMappings.EXTENSION_TGWAR);
444 		JarOutputStream jarout = new JarOutputStream(response.getOutputStream());
445 
446 		BackupDto dto = new BackupDto();
447 		if (backupDate != null) {
448 			dto.setBackupDate(backupDate);
449 		}
450 		siteService.downloadSite(siteName, jarout, dto);
451 		jarout.close();
452 		return null;
453 	}
454 
455 	public ActionForward viewStrutsConfig(ActionMapping mapping,
456 			ActionForm form, HttpServletRequest request,
457 			HttpServletResponse response) throws Exception {
458 
459 		bindSiteList(request);
460 
461 		ModuleConfig config = mapping.getModuleConfig();
462 		StringBuffer buf = new StringBuffer();
463 		buf.append("<h2>Struts Config</h2>");
464 		buf.append("<h3>" + config.getPrefix() + "</h3>");
465 
466 		writeConfig(config, buf);
467 
468 		Iterator moduleItr = getModuleList().iterator();
469 		while (moduleItr.hasNext()) {
470 			writeConfig((ModuleConfig) moduleItr.next(), buf);
471 		}
472 
473 		buf.append("<h3>Validator</h3>");
474 		ValidatorResources resources = (ValidatorResources) getServlet()
475 				.getServletContext().getAttribute(
476 						ValidatorPlugIn.VALIDATOR_KEY
477 								+ mapping.getModuleConfig().getPrefix());
478 		Map map = resources.getValidatorActions();
479 		Iterator actionItr = map.keySet().iterator();
480 		buf.append("<ul>");
481 		while (actionItr.hasNext()) {
482 			Object obj = actionItr.next();
483 			buf.append("<li>" + obj + "=" + map.get(obj) + "</li>");
484 		}
485 		buf.append("</ul>");
486 		buf.append(resources);
487 
488 		request.setAttribute(Constants.RATTR_HTML_CONFIG, buf.toString());
489 
490 		return mapping.findForward(Constants.LOCAL_FORWARD_SUCCESS);
491 	}
492 
493 	public ActionForward deleteDatabase(ActionMapping mapping, ActionForm form,
494 			HttpServletRequest request, HttpServletResponse response)
495 			throws Exception {
496 
497 		String databseName = StrutsUtil.getURLDecodedParameter(request,
498 				Constants.PARAM_DATABASE_NAME);
499 
500 		if (!basicDatabaseService.hasDomain(databseName)) {
501 			basicDatabaseService.deleteExternalDatabase(databseName);
502 		}
503 
504 		bindSiteList(request);
505 		bindDatabaseList(request);
506 		return mapping.findForward(Constants.LOCAL_FORWARD_SUCCESS);
507 	}
508 
509 	public ActionForward listDatabase(ActionMapping mapping, ActionForm form,
510 			HttpServletRequest request, HttpServletResponse response)
511 			throws Exception {
512 		bindSiteList(request);
513 
514 		DatabaseInfo baseinfo = basicDatabaseService.getBaseDatabaseInfo();
515 		List list = basicDatabaseService.getExternalDatabaseInfoList();
516 
517 		request.getSession().setAttribute(Constants.SATTR_BASEDBINFO, baseinfo);
518 		request.setAttribute(Constants.RATTR_EXTERNALDBINFOLIST, list);
519 
520 		return mapping.findForward(Constants.LOCAL_FORWARD_SUCCESS);
521 	}
522 
523 	public ActionForward createDatabase(ActionMapping mapping, ActionForm form,
524 			HttpServletRequest request, HttpServletResponse response)
525 			throws Exception {
526 		bindSiteList(request);
527 		return mapping.findForward(Constants.LOCAL_FORWARD_SUCCESS);
528 	}
529 
530 	public ActionForward saveDatabase(ActionMapping mapping, ActionForm form,
531 			HttpServletRequest request, HttpServletResponse response)
532 			throws Exception {
533 
534 		DynaBean bean = (DynaBean) form;
535 
536 		String name = (String) bean.get("name");
537 		String username = (String) bean.get("username");
538 		String type = (String) bean.get("type");
539 		String password = (String) bean.get("password");
540 		String url = (String) bean.get("url");
541 
542 		basicDatabaseService.addExternalDatabase(name, type, username,
543 				password, url);
544 
545 		return listDatabase(mapping, form, request, response);
546 	}
547 
548 	public ActionForward listUser(ActionMapping mapping, ActionForm form,
549 			HttpServletRequest request, HttpServletResponse response)
550 			throws Exception {
551 		bindSiteList(request);
552 		bindUserList(request);
553 		return mapping.findForward(Constants.LOCAL_FORWARD_SUCCESS);
554 	}
555 
556 	public ActionForward createUser(ActionMapping mapping, ActionForm form,
557 			HttpServletRequest request, HttpServletResponse response)
558 			throws Exception {
559 		bindSiteList(request);
560 		List list = directoryService.getRoles();
561 		request.setAttribute(Constants.RATTR_ROLELIST, list);
562 
563 		String dn = request.getParameter(Constants.PARAM_USERDN_NAME);
564 		if (dn != null && !dn.equals("")) {
565 			TgwUser user = directoryService.getUser(dn);
566 			UserForm userForm = new UserForm();
567 			PropertyUtils.copyProperties(userForm, user);
568 			userForm.setPasswordconfirm(user.getPassword());
569 			request.setAttribute(Constants.RATTR_USERFORM, userForm);
570 			request.setAttribute(Constants.RATTR_USER_MODIFYFLAG, "true");
571 		}
572 		return mapping.findForward(Constants.LOCAL_FORWARD_SUCCESS);
573 	}
574 
575 	public ActionForward saveUser(ActionMapping mapping, ActionForm form,
576 			HttpServletRequest request, HttpServletResponse response)
577 			throws Exception {
578 
579 		UserForm userForm = (UserForm) form;
580 		TgwUser user = new TgwUser();
581 		PropertyUtils.copyProperties(user, userForm);
582 
583 		if (userForm.isModify()) {
584 			directoryService.modifyUser(user);
585 		} else {
586 			directoryService.addUser(user);
587 		}
588 		return mapping.findForward(Constants.LOCAL_FORWARD_SUCCESS);
589 	}
590 
591 	public ActionForward deleteUser(ActionMapping mapping, ActionForm form,
592 			HttpServletRequest request, HttpServletResponse response)
593 			throws Exception {
594 		String dn = request.getParameter(Constants.PARAM_USERDN_NAME);
595 		directoryService.deleteUser(dn);
596 		return mapping.findForward(Constants.LOCAL_FORWARD_SUCCESS);
597 	}
598 
599 	public ActionForward listRole(ActionMapping mapping, ActionForm form,
600 			HttpServletRequest request, HttpServletResponse response)
601 			throws Exception {
602 		bindSiteList(request);
603 		bindRoleList(request);
604 		return mapping.findForward(Constants.LOCAL_FORWARD_SUCCESS);
605 	}
606 
607 	public ActionForward createRole(ActionMapping mapping, ActionForm form,
608 			HttpServletRequest request, HttpServletResponse response)
609 			throws Exception {
610 		bindSiteList(request);
611 
612 		String dn = request.getParameter(Constants.PARAM_ROLEDN_NAME);
613 		if (dn != null && !dn.equals("")) {
614 			TgwRole role = directoryService.getRole(dn);
615 			RoleForm roleForm = new RoleForm();
616 			PropertyUtils.copyProperties(roleForm, role);
617 			request.setAttribute(Constants.RATTR_ROLEFORM, roleForm);
618 			request.setAttribute(Constants.RATTR_ROLE_MODIFYFLAG, "true");
619 		}
620 
621 		return mapping.findForward(Constants.LOCAL_FORWARD_SUCCESS);
622 	}
623 
624 	public ActionForward saveRole(ActionMapping mapping, ActionForm form,
625 			HttpServletRequest request, HttpServletResponse response)
626 			throws Exception {
627 		RoleForm roleForm = (RoleForm) form;
628 
629 		TgwRole role = new TgwRole();
630 		PropertyUtils.copyProperties(role, roleForm);
631 
632 		if (roleForm.isModify()) {
633 			directoryService.modifyRole(role);
634 		} else {
635 			directoryService.addRole(role);
636 		}
637 		return mapping.findForward(Constants.LOCAL_FORWARD_SUCCESS);
638 	}
639 
640 	public ActionForward deleteRole(ActionMapping mapping, ActionForm form,
641 			HttpServletRequest request, HttpServletResponse response)
642 			throws Exception {
643 		String dn = request.getParameter(Constants.PARAM_ROLEDN_NAME);
644 		directoryService.deleteRole(dn);
645 		return mapping.findForward(Constants.LOCAL_FORWARD_SUCCESS);
646 	}
647 
648 	public ActionForward listSkin(ActionMapping mapping, ActionForm form,
649 			HttpServletRequest request, HttpServletResponse response)
650 			throws Exception {
651 
652 		bindSiteList(request);
653 		bindSkinList(request);
654 		return mapping.findForward(Constants.LOCAL_FORWARD_SUCCESS);
655 	}
656 
657 	public ActionForward deleteSkin(ActionMapping mapping, ActionForm form,
658 			HttpServletRequest request, HttpServletResponse response)
659 			throws Exception {
660 
661 		bindSiteList(request);
662 		String skinName = request.getParameter(Constants.PARAM_SKIN_NAME);
663 		try {
664 			designService.deleteSkin(skinName, siteService.getSiteConfigList());
665 		} catch (TgwServiceException e) {
666 			log.error(e.getMessage());
667 			ActionMessages msgs = new ActionMessages();
668 			msgs.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
669 					"error.message.skinused"));
670 			saveMessages(request, msgs);
671 		}
672 		bindSkinList(request);
673 		return mapping.findForward(Constants.LOCAL_FORWARD_SUCCESS);
674 	}
675 
676 	public ActionForward uploadSkin(ActionMapping mapping, ActionForm form,
677 			HttpServletRequest request, HttpServletResponse response)
678 			throws Exception {
679 
680 		bindSiteList(request);
681 		DynaBean bean = (DynaBean) form;
682 		FormFile file = (FormFile) bean.get("file");
683 		String skinName = (String) bean.get("skinname");
684 
685 		if (file == null) {			
686 			saveMessage(request,"file.sizeexceed",null);			
687 			return mapping.findForward(Constants.LOCAL_FORWARD_ERROR);
688 		}
689 
690 		try {
691 			designService.uploadSkin(skinName, new ZipInputStream(file
692 					.getInputStream()));
693 		} catch (TgwServiceException tse) {
694 			saveMessage(request,"msg.skin.uploadSkin.uploadFail",tse);			
695 		}
696 		bindSkinList(request);
697 
698 		return mapping.findForward(Constants.LOCAL_FORWARD_SUCCESS);
699 	}
700 
701 	public ActionForward downloadSkin(ActionMapping mapping, ActionForm form,
702 			HttpServletRequest request, HttpServletResponse response)
703 			throws Exception {
704 
705 		String skinName = request.getParameter(Constants.PARAM_SKIN_NAME);
706 		HttpUtils.setResponseHeader(request, response, ZIP_CONTENT_TYPE,
707 				skinName + "." + MimeMappings.EXTENSION_ZIP);
708 
709 		ZipOutputStream zipOutput = null;
710 		try {
711 			zipOutput = new ZipOutputStream(response.getOutputStream());
712 			designService.downloadSkin(skinName, zipOutput);
713 		} catch (TgwServiceException tse) {
714 			log.error(tse.getMessage());
715 		} finally {
716 			IOUtils.closeQuietly(zipOutput);
717 		}
718 		return null;
719 	}
720 
721 	public ActionForward editSkinConfig(ActionMapping mapping, ActionForm form,
722 			HttpServletRequest request, HttpServletResponse response)
723 			throws Exception {
724 
725 		bindSiteList(request);
726 		String skinName = request.getParameter(Constants.PARAM_SKIN_NAME);
727 
728 		DesignConfig designConfig = designService.getDesignConfig(skinName);
729 
730 		SkinForm skinForm = new SkinForm();
731 		skinForm.setName(skinName);
732 		skinForm.setDisplayName(designConfig.getDisplayName());
733 		skinForm.setAuthor(designConfig.getAuthor());
734 		skinForm.setDescription(designConfig.getDescription());
735 
736 		request.setAttribute("skinConfigForm", skinForm);
737 		return mapping.findForward(Constants.LOCAL_FORWARD_SUCCESS);
738 	}
739 
740 	public ActionForward saveSkinConfig(ActionMapping mapping, ActionForm form,
741 			HttpServletRequest request, HttpServletResponse response)
742 			throws Exception {
743 
744 		SkinForm skinForm = (SkinForm) form;
745 		DesignConfig designConfig = designService.getDesignConfig(skinForm
746 				.getName());
747 		designConfig.setDisplayName(skinForm.getDisplayName());
748 		designConfig.setAuthor(skinForm.getAuthor());
749 		designConfig.setDescription(skinForm.getDescription());
750 
751 		try {
752 			designService.saveConfig(designConfig);
753 		} catch (TgwServiceException tse) {
754 			// needs message handling
755 			tse.printStackTrace();
756 		}
757 		return mapping.findForward(Constants.LOCAL_FORWARD_SUCCESS);
758 	}
759 
760 	public ActionForward listLog(ActionMapping mapping, ActionForm form,
761 			HttpServletRequest request, HttpServletResponse response)
762 			throws Exception {
763 		bindSiteList(request);
764 
765 		ServletContext context = getServlet().getServletContext();
766 		if (context.getAttribute(Constants.CATTR_LOGFILELIST) == null) {
767 			List logList = logService.getLogfiles();
768 			context.setAttribute(Constants.CATTR_LOGFILELIST, logList);
769 		}
770 		return mapping.findForward(Constants.LOCAL_FORWARD_SUCCESS);
771 	}
772 
773 	public ActionForward searchLog(ActionMapping mapping, ActionForm form,
774 			HttpServletRequest request, HttpServletResponse response)
775 			throws Exception {
776 		bindSiteList(request);
777 
778 		DynaBean bean = (DynaBean) form;
779 		LogSearchCondition condition = new LogSearchCondition();
780 		PropertyUtils.copyProperties(condition, form);
781 
782 		List list = logService.search((String) bean.get("fileName"), condition);
783 		request.setAttribute(Constants.RATTR_LOG_RESULT, list);
784 
785 		return mapping.findForward(Constants.LOCAL_FORWARD_SUCCESS);
786 	}
787 
788 	// ----- [Start] private methods -----
789 
790 	private void writeConfig(ModuleConfig config, StringBuffer buf) {
791 
792 		FormBeanConfig[] fconfigs = config.findFormBeanConfigs();
793 		ActionConfig[] aconfigs = config.findActionConfigs();
794 
795 		buf.append("<h3>FORM</h3>");
796 		buf.append("<ul>");
797 		for (int i = 0; i < fconfigs.length; i++) {
798 			buf.append("<li>" + fconfigs[i]);
799 			if (fconfigs[i].getDynamic()) {
800 				buf.append("<ul>");
801 				buf.append(fconfigs[i].getDynaActionFormClass());
802 				buf.append("</ul>");
803 			}
804 		}
805 		buf.append("</ul>");
806 
807 		buf.append("<h3>Action</h3>");
808 		buf.append("<ul>");
809 		for (int i = 0; i < aconfigs.length; i++) {
810 			buf.append("<li>" + aconfigs[i]);
811 		}
812 		buf.append("</ul>");
813 
814 	}
815 
816 	private List getModuleList() {
817 		Enumeration enumeration = getServlet().getServletContext()
818 				.getAttributeNames();
819 		List list = new ArrayList();
820 		while (enumeration.hasMoreElements()) {
821 			String prefix = (String) enumeration.nextElement();
822 			if (!prefix.startsWith(Globals.MODULE_KEY + "/")) {
823 				continue;
824 			}
825 
826 			log.info(prefix + ":"
827 					+ getServlet().getServletContext().getAttribute(prefix));
828 			// String moduleName =
829 			// prefix.substring(Globals.MODULE_KEY.length());
830 			list.add(getServlet().getServletContext().getAttribute(prefix));
831 		}
832 		return list;
833 	}
834 
835 	private void bindDatabaseList(HttpServletRequest request) throws Exception {
836 		DatabaseInfo baseinfo = basicDatabaseService.getBaseDatabaseInfo();
837 		List list = basicDatabaseService.getExternalDatabaseInfoList();
838 		request.getSession().setAttribute(Constants.SATTR_BASEDBINFO, baseinfo);
839 		request.setAttribute(Constants.RATTR_EXTERNALDBINFOLIST, list);
840 	}
841 
842 	private void bindSiteList(HttpServletRequest request) throws Exception {
843 		List siteConfigList = siteService.getSiteConfigList();
844 		request.setAttribute(Constants.RATTR_SITECONFIGS, siteConfigList);
845 
846 		String[] names = basicDatabaseService.getExternalDatabaseNames();
847 		request.setAttribute(Constants.RATTR_EXTERNALDBNAMES, names);
848 
849 	}
850 
851 	private void bindStatusList(HttpServletRequest request) throws Exception {
852 		List siteConfigList = siteService.getSiteConfigList();
853 		List siteStatusList = new ArrayList();
854 
855 		for (Iterator i = siteConfigList.iterator(); i.hasNext();) {
856 			SiteConfig siteConfig = (SiteConfig) i.next();
857 			SiteStatus status = new SiteStatus();
858 
859 			List resourceList = contentsStoreService
860 					.getRecursivePageOrFolderList(siteConfig.getName(), "/");
861 			List tableList = basicDatabaseService.getTableNames(siteConfig
862 					.getName());
863 
864 			status.setSiteConfig(siteConfig);
865 			status.setNumOfContents(resourceList.size());
866 			status.setNumOfTables(tableList.size());
867 			siteStatusList.add(status);
868 		}
869 		request.setAttribute(Constants.RATTR_STATUSLIST, siteStatusList);
870 	}
871 
872 	private void bindUserList(HttpServletRequest request) {
873 		List list = directoryService.getUsers();
874 		request.setAttribute(Constants.RATTR_USERLIST, list);
875 	}
876 
877 	private void bindRoleList(HttpServletRequest request) {
878 		List list = directoryService.getRoles();
879 		request.setAttribute(Constants.RATTR_ROLELIST, list);
880 	}
881 
882 	private void bindSkinList(HttpServletRequest request) {
883 		String[] skins = designService.getAllSkinNames();
884 		request.setAttribute(Constants.RATTR_SKINS, skins);
885 	}
886 
887 	private void bindBackupList(HttpServletRequest request, String siteName) {
888 
889 		SiteConfig siteConfig = siteService.getSiteConfig(siteName);
890 		List backupDates = siteService.getBackupDates(siteName);
891 		List tableList = basicDatabaseService.getTableNames(siteName);
892 
893 		request.setAttribute(Constants.RATTR_SITECONFIG, siteConfig);
894 		request.setAttribute(Constants.RATTR_SITEBACKUPDATES, backupDates);
895 		request.setAttribute(Constants.RATTR_TABLELIST, tableList);
896 	}
897 
898 }