1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.itracker.web.actions.admin.language;
20
21 import org.apache.commons.beanutils.PropertyUtils;
22 import org.apache.log4j.Logger;
23 import org.apache.struts.action.*;
24 import org.itracker.core.resources.ITrackerResources;
25 import org.itracker.model.NameValuePair;
26 import org.itracker.model.util.UserUtilities;
27 import org.itracker.services.ConfigurationService;
28 import org.itracker.web.actions.base.ItrackerBaseAction;
29 import org.itracker.web.util.HTMLUtilities;
30 import org.itracker.web.util.LoginUtilities;
31 import org.itracker.web.util.ServletContextUtils;
32
33 import javax.servlet.ServletException;
34 import javax.servlet.ServletOutputStream;
35 import javax.servlet.http.HttpServletRequest;
36 import javax.servlet.http.HttpServletResponse;
37 import java.io.IOException;
38 import java.lang.reflect.InvocationTargetException;
39 import java.util.List;
40
41
42 public class ExportLanguageAction extends ItrackerBaseAction {
43 private static final Logger log = Logger.getLogger(ExportLanguageAction.class);
44
45 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
46 ActionMessages errors = new ActionMessages();
47
48
49 if (!LoginUtilities.hasPermission(UserUtilities.PERMISSION_USER_ADMIN, request, response)) {
50 return mapping.findForward("unauthorized");
51 }
52
53 try {
54 ConfigurationService configurationService = ServletContextUtils.getItrackerServices().getConfigurationService();
55
56 String locale = (String) PropertyUtils.getSimpleProperty(form, "locale");
57 if (locale != null && !locale.equals("")) {
58 StringBuffer output = new StringBuffer("# ITracker language properties file for locale " + locale + "\n\n");
59
60 List<NameValuePair> items = configurationService.getDefinedKeysAsArray(locale);
61 for (int i = 0; i < items.size(); i++) {
62 if (items.get(i).getName() != null && items.get(i).getValue() != null) {
63 output.append(ITrackerResources.escapeUnicodeString(items.get(i).getName(), false) + "=" + ITrackerResources.escapeUnicodeString(HTMLUtilities.escapeNewlines(items.get(i).getValue()), false) + "\n");
64 }
65 }
66 response.setHeader("Content-Disposition", "attachment; filename=\"ITracker" + (locale.equals(ITrackerResources.BASE_LOCALE) ? "" : "_" + locale) + ".properties\"");
67 response.setHeader("Content-Type", "application/x-itracker-language-export; charset=UTF-8");
68 ServletOutputStream out = response.getOutputStream();
69 out.print(output.toString());
70 out.flush();
71 return null;
72 }
73 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidlocale"));
74 } catch (RuntimeException e) {
75 log.error("Exception while exporting language.", e);
76 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
77 } catch (IllegalAccessException e) {
78 log.error("Exception while exporting language.", e);
79 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
80 } catch (InvocationTargetException e) {
81 log.error("Exception while exporting language.", e);
82 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
83 } catch (NoSuchMethodException e) {
84 log.error("Exception while exporting language.", e);
85 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
86 }
87
88 if (!errors.isEmpty()) {
89 saveErrors(request, errors);
90 }
91
92 return mapping.findForward("error");
93 }
94
95 }
96