View Javadoc
1   /*
2    * This software was designed and created by Jason Carroll.
3    * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4    * The author can be reached at jcarroll@cowsultants.com
5    * ITracker website: http://www.cowsultants.com
6    * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7    *
8    * This program is free software; you can redistribute it and/or modify
9    * it only under the terms of the GNU General Public License as published by
10   * the Free Software Foundation; either version 2 of the License, or
11   * (at your option) any later version.
12   *
13   * This program is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   * GNU General Public License for more details.
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