1 | package net.oni2.aeinstaller.gui.reporter;
|
---|
2 |
|
---|
3 | import java.awt.event.ActionEvent;
|
---|
4 | import java.awt.event.KeyEvent;
|
---|
5 | import java.io.File;
|
---|
6 | import java.text.SimpleDateFormat;
|
---|
7 | import java.util.Date;
|
---|
8 | import java.util.Properties;
|
---|
9 | import java.util.ResourceBundle;
|
---|
10 | import java.util.regex.Pattern;
|
---|
11 |
|
---|
12 | import javax.activation.DataHandler;
|
---|
13 | import javax.activation.DataSource;
|
---|
14 | import javax.activation.FileDataSource;
|
---|
15 | import javax.mail.AuthenticationFailedException;
|
---|
16 | import javax.mail.Authenticator;
|
---|
17 | import javax.mail.Message;
|
---|
18 | import javax.mail.Message.RecipientType;
|
---|
19 | import javax.mail.MessagingException;
|
---|
20 | import javax.mail.Multipart;
|
---|
21 | import javax.mail.PasswordAuthentication;
|
---|
22 | import javax.mail.Session;
|
---|
23 | import javax.mail.Transport;
|
---|
24 | import javax.mail.internet.InternetAddress;
|
---|
25 | import javax.mail.internet.MimeBodyPart;
|
---|
26 | import javax.mail.internet.MimeMessage;
|
---|
27 | import javax.mail.internet.MimeMultipart;
|
---|
28 | import javax.swing.AbstractAction;
|
---|
29 | import javax.swing.JCheckBox;
|
---|
30 | import javax.swing.JComponent;
|
---|
31 | import javax.swing.JDialog;
|
---|
32 | import javax.swing.JTextArea;
|
---|
33 | import javax.swing.JTextField;
|
---|
34 | import javax.swing.KeyStroke;
|
---|
35 |
|
---|
36 | import net.oni2.aeinstaller.backend.LogPrintStream;
|
---|
37 | import net.oni2.aeinstaller.backend.Paths;
|
---|
38 | import net.oni2.aeinstaller.backend.StringDataSource;
|
---|
39 | import net.oni2.resourcebundle.UTF8ResourceBundleLoader;
|
---|
40 | import net.oni2.swingcomponents.HTMLLinkLabel;
|
---|
41 |
|
---|
42 | import org.apache.commons.io.FileUtils;
|
---|
43 | import org.apache.commons.io.filefilter.IOFileFilter;
|
---|
44 | import org.apache.commons.io.filefilter.NameFileFilter;
|
---|
45 | import org.apache.commons.io.filefilter.NotFileFilter;
|
---|
46 | import org.apache.commons.io.filefilter.TrueFileFilter;
|
---|
47 | import org.javabuilders.BuildResult;
|
---|
48 | import org.javabuilders.annotations.DoInBackground;
|
---|
49 | import org.javabuilders.event.BackgroundEvent;
|
---|
50 | import org.javabuilders.swing.SwingJavaBuilder;
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * @author Christian Illy
|
---|
54 | */
|
---|
55 | public class ReporterDialog extends JDialog {
|
---|
56 | private static final long serialVersionUID = -5719515325671846620L;
|
---|
57 |
|
---|
58 | private ResourceBundle bundle = UTF8ResourceBundleLoader
|
---|
59 | .getBundle("net.oni2.aeinstaller.localization."
|
---|
60 | + getClass().getSimpleName());
|
---|
61 | @SuppressWarnings("unused")
|
---|
62 | private BuildResult result = SwingJavaBuilder.build(this, bundle);
|
---|
63 |
|
---|
64 | private HTMLLinkLabel lblInfo;
|
---|
65 | private HTMLLinkLabel lblFiles;
|
---|
66 | private JTextField txtMail;
|
---|
67 | private JTextArea txtMessage;
|
---|
68 | private JCheckBox chkGetCopy;
|
---|
69 |
|
---|
70 | private final String SMTP_HOST_NAME = "mail.illy.bz";
|
---|
71 | private final String SMTP_AUTH_USER = "aei_reports@oni2.net";
|
---|
72 | private final String SMTP_AUTH_PWD = "mb0NkjHuWrDFfMDe2BkUDU2Dknkkvq";
|
---|
73 | private final String EMAIL_SUBJECT = "AE support request";
|
---|
74 | private final String EMAIL_TO = "ae-support@oni2.net";
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Open the settings
|
---|
78 | */
|
---|
79 | public ReporterDialog() {
|
---|
80 | lblInfo.setText(bundle.getString("lblInfo"));
|
---|
81 | lblFiles.setText(bundle.getString("lblFiles"));
|
---|
82 |
|
---|
83 | AbstractAction closeAction = new AbstractAction() {
|
---|
84 |
|
---|
85 | private static final long serialVersionUID = 1L;
|
---|
86 |
|
---|
87 | public void actionPerformed(ActionEvent arg0) {
|
---|
88 | dispose();
|
---|
89 | }
|
---|
90 | };
|
---|
91 | KeyStroke ksCtrlW = KeyStroke
|
---|
92 | .getKeyStroke('W', KeyEvent.CTRL_DOWN_MASK);
|
---|
93 | getRootPane()
|
---|
94 | .getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
|
---|
95 | .put(ksCtrlW, "close");
|
---|
96 | getRootPane().getActionMap().put("close", closeAction);
|
---|
97 |
|
---|
98 | setLocationRelativeTo(null);
|
---|
99 |
|
---|
100 | initFields();
|
---|
101 | }
|
---|
102 |
|
---|
103 | private void initFields() {
|
---|
104 | }
|
---|
105 |
|
---|
106 | private class SMTPAuthenticator extends Authenticator {
|
---|
107 | public PasswordAuthentication getPasswordAuthentication() {
|
---|
108 | String username = SMTP_AUTH_USER;
|
---|
109 | String password = SMTP_AUTH_PWD;
|
---|
110 | return new PasswordAuthentication(username, password);
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | @DoInBackground(progressMessage = "send.title", cancelable = false, indeterminateProgress = true)
|
---|
115 | private boolean send(final BackgroundEvent evt) {
|
---|
116 | try {
|
---|
117 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
---|
118 |
|
---|
119 | StringBuffer msgText = new StringBuffer();
|
---|
120 | msgText.append("Support request\nTime: " + sdf.format(new Date())
|
---|
121 | + "\n");
|
---|
122 |
|
---|
123 | // Set the host smtp address
|
---|
124 | Properties props = new Properties();
|
---|
125 | props.put("mail.smtp.host", SMTP_HOST_NAME);
|
---|
126 | props.put("mail.smtp.auth", "true");
|
---|
127 | Authenticator auth = new SMTPAuthenticator();
|
---|
128 | Session session = Session.getDefaultInstance(props, auth);
|
---|
129 |
|
---|
130 | session.setDebug(false);
|
---|
131 |
|
---|
132 | // create a message
|
---|
133 | Message msg = new MimeMessage(session);
|
---|
134 | msg.setFrom(new InternetAddress(txtMail.getText()));
|
---|
135 | msg.setRecipient(RecipientType.TO, new InternetAddress(EMAIL_TO));
|
---|
136 | if (chkGetCopy.isSelected())
|
---|
137 | msg.addRecipient(RecipientType.CC,
|
---|
138 | new InternetAddress(txtMail.getText()));
|
---|
139 | msg.setSubject(EMAIL_SUBJECT);
|
---|
140 |
|
---|
141 | // Build multipart
|
---|
142 | Multipart multipart = new MimeMultipart();
|
---|
143 |
|
---|
144 | // aei_output.log (live from string)
|
---|
145 | MimeBodyPart mimeBody = new MimeBodyPart();
|
---|
146 | DataSource source = new StringDataSource(LogPrintStream
|
---|
147 | .getInstance().getLog());
|
---|
148 | mimeBody.setDataHandler(new DataHandler(source));
|
---|
149 | mimeBody.setFileName("aei_output.log");
|
---|
150 | multipart.addBodyPart(mimeBody);
|
---|
151 |
|
---|
152 | // updater_output.log
|
---|
153 | File f = new File(Paths.getInstallerPath(), "updater_output.log");
|
---|
154 | if (f.exists()) {
|
---|
155 | mimeBody = new MimeBodyPart();
|
---|
156 | source = new FileDataSource(f);
|
---|
157 | mimeBody.setDataHandler(new DataHandler(source));
|
---|
158 | mimeBody.setFileName("updater_output.log");
|
---|
159 | multipart.addBodyPart(mimeBody);
|
---|
160 | } else {
|
---|
161 | msgText.append("updater_output.log does not exist!\n");
|
---|
162 | }
|
---|
163 |
|
---|
164 | // Initialization.log
|
---|
165 | f = new File(Paths.getInstallerPath(), "Initialization.log");
|
---|
166 | if (f.exists()) {
|
---|
167 | mimeBody = new MimeBodyPart();
|
---|
168 | source = new FileDataSource(f);
|
---|
169 | mimeBody.setDataHandler(new DataHandler(source));
|
---|
170 | mimeBody.setFileName("Initialization.log");
|
---|
171 | multipart.addBodyPart(mimeBody);
|
---|
172 | } else {
|
---|
173 | msgText.append("Initialization.log does not exist!\n");
|
---|
174 | }
|
---|
175 |
|
---|
176 | // Installation.log
|
---|
177 | f = new File(Paths.getInstallerPath(), "Installation.log");
|
---|
178 | if (f.exists()) {
|
---|
179 | mimeBody = new MimeBodyPart();
|
---|
180 | source = new FileDataSource(f);
|
---|
181 | mimeBody.setDataHandler(new DataHandler(source));
|
---|
182 | mimeBody.setFileName("Installation.log");
|
---|
183 | multipart.addBodyPart(mimeBody);
|
---|
184 | } else {
|
---|
185 | msgText.append("Installation.log does not exist!\n");
|
---|
186 | }
|
---|
187 |
|
---|
188 | // startup.txt
|
---|
189 | f = new File(Paths.getEditionBasePath(), "startup.txt");
|
---|
190 | if (f.exists()) {
|
---|
191 | mimeBody = new MimeBodyPart();
|
---|
192 | source = new FileDataSource(f);
|
---|
193 | mimeBody.setDataHandler(new DataHandler(source));
|
---|
194 | mimeBody.setFileName("startup.txt");
|
---|
195 | multipart.addBodyPart(mimeBody);
|
---|
196 | } else {
|
---|
197 | msgText.append("startup.txt does not exist!\n");
|
---|
198 | }
|
---|
199 |
|
---|
200 | // debugger.txt
|
---|
201 | f = new File(Paths.getEditionBasePath(), "debugger.txt");
|
---|
202 | if (f.exists()) {
|
---|
203 | mimeBody = new MimeBodyPart();
|
---|
204 | source = new FileDataSource(f);
|
---|
205 | mimeBody.setDataHandler(new DataHandler(source));
|
---|
206 | mimeBody.setFileName("debugger.txt");
|
---|
207 | multipart.addBodyPart(mimeBody);
|
---|
208 | } else {
|
---|
209 | msgText.append("debugger.txt does not exist!\n");
|
---|
210 | }
|
---|
211 |
|
---|
212 | // File list (live from string)
|
---|
213 | StringBuffer fileList = new StringBuffer();
|
---|
214 | int baseLength = Paths.getEditionBasePath().getAbsolutePath()
|
---|
215 | .length();
|
---|
216 | IOFileFilter svnDirFilter = new NameFileFilter(
|
---|
217 | new String[] { ".svn" });
|
---|
218 | IOFileFilter notFilter = new NotFileFilter(svnDirFilter);
|
---|
219 | Pattern packagePattern = Pattern.compile(
|
---|
220 | ".*/packages/[0-9]{5}[^/]*/.+", Pattern.CASE_INSENSITIVE);
|
---|
221 | for (File flF : FileUtils.listFilesAndDirs(
|
---|
222 | Paths.getEditionBasePath(), TrueFileFilter.INSTANCE,
|
---|
223 | notFilter)) {
|
---|
224 | String name = flF.getAbsolutePath().substring(baseLength);
|
---|
225 | name = name.replace('\\', '/');
|
---|
226 | if (!packagePattern.matcher(name).matches())
|
---|
227 | fileList.append(name + "\n");
|
---|
228 | }
|
---|
229 |
|
---|
230 | mimeBody = new MimeBodyPart();
|
---|
231 | source = new StringDataSource(fileList.toString());
|
---|
232 | mimeBody.setDataHandler(new DataHandler(source));
|
---|
233 | mimeBody.setFileName("filelist.txt");
|
---|
234 | multipart.addBodyPart(mimeBody);
|
---|
235 |
|
---|
236 | // Build text part
|
---|
237 | msgText.append(txtMessage.getText());
|
---|
238 | mimeBody = new MimeBodyPart();
|
---|
239 | mimeBody.setText(msgText.toString());
|
---|
240 | multipart.addBodyPart(mimeBody, 0);
|
---|
241 |
|
---|
242 | msg.setContent(multipart);
|
---|
243 |
|
---|
244 | Transport.send(msg);
|
---|
245 |
|
---|
246 | return true;
|
---|
247 | } catch (AuthenticationFailedException e) {
|
---|
248 | e.printStackTrace();
|
---|
249 | } catch (MessagingException e) {
|
---|
250 | e.printStackTrace();
|
---|
251 | }
|
---|
252 | return false;
|
---|
253 | }
|
---|
254 | }
|
---|