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

Last change on this file since 12538 was 10618, checked in by Don-vip, 8 years ago

see #11390 - Java 8: use java.util.Base64

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