PageRenderTime 30ms CodeModel.GetById 22ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

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