source: josm/trunk/src/org/openstreetmap/josm/tools/bugreport/BugReportSender.java@ 12649

Last change on this file since 12649 was 12649, checked in by Don-vip, 7 years ago

see #15182 - code refactoring to avoid dependence on GUI packages from Preferences

  • Property svn:eol-style set to native
File size: 6.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools.bugreport;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.GridBagConstraints;
7import java.awt.GridBagLayout;
8import java.io.IOException;
9import java.io.InputStream;
10import java.net.URL;
11import java.net.URLEncoder;
12import java.nio.charset.StandardCharsets;
13import java.util.Base64;
14
15import javax.swing.JOptionPane;
16import javax.swing.JPanel;
17import javax.swing.SwingUtilities;
18import javax.xml.parsers.ParserConfigurationException;
19import javax.xml.xpath.XPath;
20import javax.xml.xpath.XPathConstants;
21import javax.xml.xpath.XPathExpressionException;
22import javax.xml.xpath.XPathFactory;
23
24import org.openstreetmap.josm.Main;
25import org.openstreetmap.josm.gui.bugreport.DebugTextDisplay;
26import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
27import org.openstreetmap.josm.gui.widgets.UrlLabel;
28import org.openstreetmap.josm.tools.GBC;
29import org.openstreetmap.josm.tools.HttpClient;
30import org.openstreetmap.josm.tools.HttpClient.Response;
31import org.openstreetmap.josm.tools.Logging;
32import org.openstreetmap.josm.tools.OpenBrowser;
33import org.openstreetmap.josm.tools.Utils;
34import org.w3c.dom.Document;
35import org.xml.sax.SAXException;
36
37/**
38 * This class handles sending the bug report to JOSM website.
39 * <p>
40 * Currently, we try to open a browser window for the user that displays the bug report.
41 *
42 * @author Michael Zangl
43 * @since 10055
44 */
45public class BugReportSender extends Thread {
46
47 private final String statusText;
48 private String errorMessage;
49
50 /**
51 * Creates a new sender.
52 * @param statusText The status text to send.
53 */
54 protected BugReportSender(String statusText) {
55 super("Bug report sender");
56 this.statusText = statusText;
57 }
58
59 @Override
60 public void run() {
61 try {
62 // first, send the debug text using post.
63 String debugTextPasteId = pasteDebugText();
64
65 // then open a browser to display the pasted text.
66 String openBrowserError = OpenBrowser.displayUrl(getJOSMTicketURL() + "?pdata_stored=" + debugTextPasteId);
67 if (openBrowserError != null) {
68 Logging.warn(openBrowserError);
69 failed(openBrowserError);
70 }
71 } catch (BugReportSenderException e) {
72 Logging.warn(e);
73 failed(e.getMessage());
74 }
75 }
76
77 /**
78 * Sends the debug text to the server.
79 * @return The token which was returned by the server. We need to pass this on to the ticket system.
80 * @throws BugReportSenderException if sending the report failed.
81 */
82 private String pasteDebugText() throws BugReportSenderException {
83 try {
84 String text = Utils.strip(statusText);
85 String pdata = Base64.getEncoder().encodeToString(text.getBytes(StandardCharsets.UTF_8));
86 String postQuery = "pdata=" + URLEncoder.encode(pdata, "UTF-8");
87 HttpClient client = HttpClient.create(new URL(getJOSMTicketURL()), "POST")
88 .setHeader("Content-Type", "application/x-www-form-urlencoded")
89 .setRequestBody(postQuery.getBytes(StandardCharsets.UTF_8));
90
91 Response connection = client.connect();
92
93 if (connection.getResponseCode() >= 500) {
94 throw new BugReportSenderException("Internal server error.");
95 }
96
97 try (InputStream in = connection.getContent()) {
98 return retrieveDebugToken(Utils.parseSafeDOM(in));
99 }
100 } catch (IOException | SAXException | ParserConfigurationException | XPathExpressionException t) {
101 throw new BugReportSenderException(t);
102 }
103 }
104
105 private static String getJOSMTicketURL() {
106 return Main.getJOSMWebsite() + "/josmticket";
107 }
108
109 private static String retrieveDebugToken(Document document) throws XPathExpressionException, BugReportSenderException {
110 XPathFactory factory = XPathFactory.newInstance();
111 XPath xpath = factory.newXPath();
112 String status = (String) xpath.compile("/josmticket/@status").evaluate(document, XPathConstants.STRING);
113 if (!"ok".equals(status)) {
114 String message = (String) xpath.compile("/josmticket/error/text()").evaluate(document,
115 XPathConstants.STRING);
116 if (message.isEmpty()) {
117 message = "Error in server response but server did not tell us what happened.";
118 }
119 throw new BugReportSenderException(message);
120 }
121
122 String token = (String) xpath.compile("/josmticket/preparedid/text()")
123 .evaluate(document, XPathConstants.STRING);
124 if (token.isEmpty()) {
125 throw new BugReportSenderException("Server did not respond with a prepared id.");
126 }
127 return token;
128 }
129
130 private void failed(String string) {
131 errorMessage = string;
132 SwingUtilities.invokeLater(() -> {
133 JPanel errorPanel = new JPanel(new GridBagLayout());
134 errorPanel.add(new JMultilineLabel(
135 tr("Opening the bug report failed. Please report manually using this website:")),
136 GBC.eol().fill(GridBagConstraints.HORIZONTAL));
137 errorPanel.add(new UrlLabel(Main.getJOSMWebsite() + "/newticket", 2), GBC.eop().insets(8, 0, 0, 0));
138 errorPanel.add(new DebugTextDisplay(statusText));
139
140 JOptionPane.showMessageDialog(Main.parent, errorPanel, tr("You have encountered a bug in JOSM"),
141 JOptionPane.ERROR_MESSAGE);
142 });
143 }
144
145 /**
146 * Returns the error message that could have occured during bug sending.
147 * @return the error message, or {@code null} if successful
148 */
149 public final String getErrorMessage() {
150 return errorMessage;
151 }
152
153 private static class BugReportSenderException extends Exception {
154 BugReportSenderException(String message) {
155 super(message);
156 }
157
158 BugReportSenderException(Throwable cause) {
159 super(cause);
160 }
161 }
162
163 /**
164 * Opens the bug report window on the JOSM server.
165 * @param statusText The status text to send along to the server.
166 * @return bug report sender started thread
167 */
168 public static BugReportSender reportBug(String statusText) {
169 BugReportSender sender = new BugReportSender(statusText);
170 sender.start();
171 return sender;
172 }
173}
Note: See TracBrowser for help on using the repository browser.