/edu/uncc/parsets/gui/CrashReporter.java

https://code.google.com/p/parsets/ · Java · 136 lines · 85 code · 23 blank · 28 comment · 8 complexity · 8dab0452ce7b1be6138cc962427e9d7b MD5 · raw file

  1. package edu.uncc.parsets.gui;
  2. import java.io.BufferedReader;
  3. import java.io.InputStreamReader;
  4. import java.io.PrintWriter;
  5. import java.net.HttpURLConnection;
  6. import java.net.URL;
  7. import java.net.URLEncoder;
  8. import java.util.HashMap;
  9. import java.util.Map;
  10. import java.util.Vector;
  11. import java.util.Map.Entry;
  12. import javax.swing.JFrame;
  13. import javax.swing.JOptionPane;
  14. import org.apache.log4j.AppenderSkeleton;
  15. import org.apache.log4j.Level;
  16. import org.apache.log4j.spi.LoggingEvent;
  17. import edu.uncc.parsets.util.PSLogging;
  18. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
  19. * Copyright (c) 2009, Robert Kosara, Caroline Ziemkiewicz,
  20. * and others (see Authors.txt for full list)
  21. * All rights reserved.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions are met:
  25. *
  26. * * Redistributions of source code must retain the above copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * * Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * * Neither the name of UNC Charlotte nor the names of its contributors
  32. * may be used to endorse or promote products derived from this software
  33. * without specific prior written permission.
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY ITS AUTHORS ''AS IS'' AND ANY
  36. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  37. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
  39. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  40. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  41. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  42. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  44. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  46. public class CrashReporter extends AppenderSkeleton {
  47. private static final String MESSAGE =
  48. "Parallel Sets has encountered an error that it cannot recover from.\n" +
  49. "Crash reports help us improve the program. They contain no personal data.\n" +
  50. "Do you want to submit a crash report? \n";
  51. private static final String TITLE = "Fatal Error Occurred";
  52. private static final String REPORTURL = "http://data.eagereyes.org/parsets/postCrashReport.php";
  53. private static final String RESULTTITLE = "Report Submitted!";
  54. private static final String RESULTMSG =
  55. "Your report has been submitted.\n" +
  56. "Your reference code is ";
  57. private JFrame frame;
  58. public CrashReporter(JFrame mainFrame) {
  59. frame = mainFrame;
  60. }
  61. @Override
  62. protected void append(LoggingEvent e) {
  63. if (e.getLevel() == Level.FATAL) {
  64. int choice = JOptionPane.showConfirmDialog(frame, MESSAGE,
  65. TITLE, JOptionPane.ERROR_MESSAGE, JOptionPane.YES_NO_OPTION);
  66. if (choice == 0) {
  67. String logFile = PSLogging.getLogFileAsString();
  68. HashMap<String, String> data = new HashMap<String, String>();
  69. data.put("data", logFile);
  70. String result[] = postRequest(REPORTURL, data);
  71. JOptionPane.showMessageDialog(frame, RESULTMSG+result[0].substring(0, 6), RESULTTITLE, JOptionPane.INFORMATION_MESSAGE);
  72. }
  73. frame.dispose();
  74. }
  75. }
  76. public static String[] postRequest(String urlString, Map<String, String> params) {
  77. try {
  78. URL url = new URL(urlString);
  79. HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  80. connection.setRequestMethod("POST");
  81. connection.setDoOutput(true);
  82. PrintWriter out = new PrintWriter(connection.getOutputStream());
  83. StringBuffer data = new StringBuffer();
  84. boolean first = true;
  85. for (Entry<String, String> p : params.entrySet()) {
  86. if (first)
  87. first = false;
  88. else
  89. data.append("&");
  90. data.append(p.getKey()+"="+URLEncoder.encode(p.getValue(), "UTF-8"));
  91. }
  92. out.println(data);
  93. out.close();
  94. BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  95. String line;
  96. Vector<String> lines = new Vector<String>();
  97. while ((line = in.readLine()) != null) {
  98. lines.add(line);
  99. }
  100. in.close();
  101. String result[] = new String[lines.size()];
  102. return lines.toArray(result);
  103. } catch (Exception e) {
  104. PSLogging.logger.error("Error submitting POST request.", e);
  105. return null;
  106. }
  107. }
  108. public void close() {
  109. }
  110. public boolean requiresLayout() {
  111. return false;
  112. }
  113. }