Search in sources :

Example 1 with MultipartMessage

use of jmri.util.MultipartMessage in project JMRI by JMRI.

the class ReportPanel method sendButtonActionPerformed.

@SuppressWarnings("unchecked")
public void sendButtonActionPerformed(java.awt.event.ActionEvent e) {
    try {
        sendButton.setEnabled(false);
        log.debug("initial checks");
        InternetAddress email = new InternetAddress(emailField.getText());
        email.validate();
        log.debug("start send");
        //NO18N
        String charSet = "UTF-8";
        //NOI18N
        String requestURL = "http://jmri.org/problem-report.php";
        MultipartMessage msg = new MultipartMessage(requestURL, charSet);
        // add reporter email address
        log.debug("start creating message");
        msg.addFormField("reporter", emailField.getText());
        // add if to Cc sender
        msg.addFormField("sendcopy", checkCopy.isSelected() ? "yes" : "no");
        // add problem summary
        msg.addFormField("summary", summaryField.getText());
        // build detailed error report (include context if selected)
        String report = descField.getText() + "\r\n";
        if (checkContext.isSelected()) {
            //NOI18N
            report += "=========================================================\r\n";
            report += (new ReportContext()).getReport(checkNetwork.isSelected() && checkNetwork.isEnabled());
        }
        msg.addFormField("problem", report);
        log.debug("start adding attachments");
        // add panel file if OK
        if (checkPanel.isSelected()) {
            log.debug("prepare panel attachment");
            // Check that some startup panel files have been loaded
            for (PerformFileModel m : InstanceManager.getDefault(StartupActionsManager.class).getActions(PerformFileModel.class)) {
                String fn = m.getFileName();
                File f = new File(fn);
                log.debug("add startup panel file: {}", f);
                msg.addFilePart("logfileupload[]", f);
            }
            // Check that a manual panel file has been loaded
            File file = jmri.configurexml.LoadXmlUserAction.getCurrentFile();
            if (file != null) {
                log.debug("add manual panel file: {}", file.getPath());
                msg.addFilePart("logfileupload[]", jmri.configurexml.LoadXmlUserAction.getCurrentFile());
            } else {
                // No panel file loaded
                log.warn("No manual panel file loaded - not sending");
            }
        }
        // add profile files if OK
        if (checkProfile.isSelected()) {
            log.debug("prepare profile attachment");
            // Check that a profile has been loaded
            Profile profile = ProfileManager.getDefault().getActiveProfile();
            File file = profile.getPath();
            if (file != null) {
                log.debug("add profile: {}", file.getPath());
                // Now zip-up contents of profile
                // Create temp file that will be deleted when Java quits
                File temp = File.createTempFile("profile", ".zip");
                temp.deleteOnExit();
                FileOutputStream out = new FileOutputStream(temp);
                ZipOutputStream zip = new ZipOutputStream(out);
                addDirectory(zip, file);
                zip.close();
                out.close();
                msg.addFilePart("logfileupload[]", temp);
            } else {
                // No profile loaded
                log.warn("No profile loaded - not sending");
            }
        }
        // add the log if OK
        if (checkLog.isSelected()) {
            log.debug("prepare log attachments");
            // search for an appender that stores a file
            for (java.util.Enumeration<org.apache.log4j.Appender> en = org.apache.log4j.Logger.getRootLogger().getAllAppenders(); en.hasMoreElements(); ) {
                // does this have a file?
                org.apache.log4j.Appender a = en.nextElement();
                // see if it's one of the ones we know
                if (log.isDebugEnabled()) {
                    log.debug("check appender {}", a);
                }
                try {
                    org.apache.log4j.FileAppender f = (org.apache.log4j.FileAppender) a;
                    log.debug("find file: {}", f.getFile());
                    msg.addFilePart("logfileupload[]", new File(f.getFile()), "application/octet-stream");
                } catch (ClassCastException ex) {
                }
            }
        }
        log.debug("done adding attachments");
        // finalise and get server response (if any)
        log.debug("posting report...");
        List<String> response = msg.finish();
        log.debug("send complete");
        log.debug("server response:");
        boolean checkResponse = false;
        for (String line : response) {
            log.debug("               :{}", line);
            if (line.contains("<p>Message successfully sent!</p>")) {
                checkResponse = true;
            }
        }
        if (checkResponse) {
            JOptionPane.showMessageDialog(null, rb.getString("InfoMessage"), rb.getString("InfoTitle"), JOptionPane.INFORMATION_MESSAGE);
            // close containing Frame
            getTopLevelAncestor().setVisible(false);
        } else {
            // TODO add Bundle to folder and use ErrorTitle key in NamedBeanBundle props
            JOptionPane.showMessageDialog(null, rb.getString("ErrMessage"), rb.getString("ErrTitle"), JOptionPane.ERROR_MESSAGE);
            sendButton.setEnabled(true);
        }
    } catch (IOException ex) {
        log.error("Error when attempting to send report: " + ex);
        sendButton.setEnabled(true);
    } catch (AddressException ex) {
        log.error("Invalid email address: " + ex);
        // TODO add Bundle to folder and use ErrorTitle key in NamedBeanBundle props
        JOptionPane.showMessageDialog(null, rb.getString("ErrAddress"), rb.getString("ErrTitle"), JOptionPane.ERROR_MESSAGE);
        sendButton.setEnabled(true);
    }
}
Also used : MultipartMessage(jmri.util.MultipartMessage) InternetAddress(javax.mail.internet.InternetAddress) StartupActionsManager(apps.StartupActionsManager) IOException(java.io.IOException) Profile(jmri.profile.Profile) PerformFileModel(apps.PerformFileModel) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) AddressException(javax.mail.internet.AddressException) File(java.io.File)

Aggregations

PerformFileModel (apps.PerformFileModel)1 StartupActionsManager (apps.StartupActionsManager)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 AddressException (javax.mail.internet.AddressException)1 InternetAddress (javax.mail.internet.InternetAddress)1 Profile (jmri.profile.Profile)1 MultipartMessage (jmri.util.MultipartMessage)1