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

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

see #12652 - rework bug report classes to improve unit tests coverage

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