All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jexbox.connector.play.JexboxConnectorPlayImpl Maven / Gradle / Ivy

Go to download

Jexbox Play! Framework connector hooks as ExceptionHandler, so any uncaught exceptions in your Play! application will be sent to your Jexbox database. The connector extracts complete exception stack trace, including nested exceptions, error message and system properties and page/component traces

There is a newer version: 0.0.4
Show newest version
package com.jexbox.connector.play;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.codec.binary.Base64;

import play.api.PlayException;
import play.mvc.Http.RequestHeader;
import play.mvc.Http.Session;

import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.jexbox.connector.JexboxConnectorImpl;
import com.jexbox.connector.TransportException;

public class JexboxConnectorPlayImpl extends JexboxConnectorImpl implements JexboxConnectorPlay{
    private static Logger _logger = Logger.getLogger(JexboxConnectorPlayImpl.class.getName());
    
	public JexboxConnectorPlayImpl(Properties props) {
		super(props);
	}
	
	public void send(Throwable e, RequestHeader request, Session session){
		sendWithMeta(e, request, session, null);
	}
	
	public void sendWithMeta(Throwable ex, RequestHeader request, Session session, Map> metaD){
		try {
			if(metaD == null){
				metaD = new HashMap>();
			}
			
			addPageTrace(ex, metaD);
			
			Throwable e = removeInfrastructure(ex);
			
			JsonObject json = json(e, metaD);
			addRequestMetaData(request, json);
			
			addSessionMetaData(session, json);
			_notifier.send(json);
		} catch (UnsupportedEncodingException e1) {
			_logger.log(Level.SEVERE, "Could not able to send error to Jexbox", e1);
		} catch (TransportException e1) {
			_logger.log(Level.SEVERE, "Could not able to send error to Jexbox", e1);
		}
	}
	
	protected void addPageTrace(Throwable e, Map> metaD){
		if(e instanceof PlayException.ExceptionSource){
			PlayException.ExceptionSource error = (PlayException.ExceptionSource) e;
			
			StringBuffer buff = new StringBuffer();
			buff.append("
"); buff.append("

In "+error.sourceName()+":"+error.line()+"

"); buff.append("
"); String[] split = error.input().split("\n"); for (int i = 1; i <= split.length; i++) { String line = split[i-1]; if(i == error.line()){ buff.append("
");
				}else{
					buff.append("
");
				}
				buff.append(""+i+"");
				buff.append(""+line+"");
				buff.append("
"); } buff.append("
"); buff.append("
"); Map meta = new HashMap(); metaD.put("Page Trace", meta); String pageTrace64 = Base64.encodeBase64String( buff.toString().getBytes() ); meta.put("data", pageTrace64); } } protected void addSessionMetaData(Session session, JsonObject json){ if(session != null){ JsonObject meta = json.getAsJsonObject("meta"); if(meta == null){ meta = new JsonObject(); json.add("meta", meta); } JsonObject sessionD = new JsonObject(); meta.add("Session", sessionD); Set names = session.keySet(); for (String name : names) { Object attr = session.get(name); sessionD.add(name, new JsonPrimitive(String.valueOf(attr))); } } } protected void addRequestMetaData(RequestHeader reqHTTP, JsonObject json){ JsonObject meta = json.getAsJsonObject("meta"); if(meta == null){ meta = new JsonObject(); json.add("meta", meta); } json.add("uri", new JsonPrimitive(reqHTTP.uri())); JsonObject reqPara = new JsonObject(); meta.add("Query Strings", reqPara); Map queryString = reqHTTP.queryString(); for (String name : queryString.keySet()) { String attr = reqHTTP.getQueryString(name); if(attr != null){ reqPara.add(name, new JsonPrimitive(String.valueOf(attr))); }else{ reqPara.add(name, new JsonPrimitive("null")); } } JsonObject reqHead = new JsonObject(); meta.add("Request Headers", reqHead); Map headers = reqHTTP.headers(); for (String name : headers.keySet()) { String attr = reqHTTP.getHeader(name); if(attr != null){ reqHead.add(name, new JsonPrimitive(String.valueOf(attr))); }else{ reqHead.add(name, new JsonPrimitive("null")); } } JsonObject req = new JsonObject(); meta.add("Request", req); req.add("Host", new JsonPrimitive(String.valueOf(reqHTTP.host()))); req.add("Method", new JsonPrimitive(String.valueOf(reqHTTP.method()))); req.add("Path", new JsonPrimitive(String.valueOf(reqHTTP.path()))); req.add("Remote Addr", new JsonPrimitive(String.valueOf(reqHTTP.remoteAddress()))); req.add("Secure", new JsonPrimitive(String.valueOf(reqHTTP.secure()))); req.add("URI", new JsonPrimitive(String.valueOf(reqHTTP.uri()))); req.add("Version", new JsonPrimitive(String.valueOf(reqHTTP.version()))); } protected Throwable removeInfrastructure(Throwable e){ if(e.getCause() != null){ if(e.getClass().getName().startsWith("play.api.Application$$anon$")){ return removeInfrastructure(e.getCause()); }else{ return e; } }else{ return e; } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy