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