diff --git a/pom.xml b/pom.xml new file mode 100644 index 00000000000..d2236b46b3e --- /dev/null +++ b/pom.xml @@ -0,0 +1,64 @@ + + + 4.0.0 + + net.kencochrane + raven-java + 1.0-SNAPSHOT + jar + raven-java + Java Raven client and log4j appender. + + + + commons-lang + commons-lang + 2.5 + + + commons-codec + commons-codec + 1.6 + + + org.json + json + 20090211 + + + log4j + log4j + 1.2.16 + + + junit + junit + 4.8.1 + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.6 + 1.6 + UTF-8 + + + + org.apache.maven.plugins + maven-resources-plugin + + UTF-8 + + + + + + \ No newline at end of file diff --git a/src/net/kencochrane/sentry/RavenClient.java b/src/net/kencochrane/sentry/RavenClient.java index 0d1137e5775..0819ced448f 100644 --- a/src/net/kencochrane/sentry/RavenClient.java +++ b/src/net/kencochrane/sentry/RavenClient.java @@ -1,13 +1,13 @@ package net.kencochrane.sentry; -import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.entity.StringEntity; -import org.apache.http.impl.client.DefaultHttpClient; -import org.apache.http.util.EntityUtils; import org.json.simple.JSONObject; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.URL; + import static org.apache.commons.codec.binary.Base64.encodeBase64String; /** @@ -27,6 +27,11 @@ public RavenClient(String sentryDSN) { this.config = new RavenConfig(sentryDSN); } + public RavenClient(String sentryDSN, String proxy) { + this.sentryDSN = sentryDSN; + this.config = new RavenConfig(sentryDSN, proxy); + } + public RavenConfig getConfig() { return config; } @@ -143,48 +148,31 @@ private String buildAuthHeader(String hmacSignature, long timestamp, String publ * @param timestamp the timestamp of the message */ private void sendMessage(String messageBody, long timestamp) { - - DefaultHttpClient httpClient = new DefaultHttpClient(); + HttpURLConnection connection = null; try { - // build up the Post method - HttpPost httppost = new HttpPost(getConfig().getSentryURL()); - // get the hmac Signature for the header String hmacSignature = RavenUtils.getSignature(messageBody, timestamp, config.getSecretKey()); // get the auth header String authHeader = buildAuthHeader(hmacSignature, timestamp, getConfig().getPublicKey()); - // add auth header to http post - httppost.addHeader("X-Sentry-Auth", authHeader); - - // set the body into the post - StringEntity reqEntity = new StringEntity(messageBody); - httppost.setEntity(reqEntity); - - // call the server and get response - HttpResponse response = httpClient.execute(httppost); - HttpEntity resEntity = response.getEntity(); - - // not needed right now, keeping around for debugging purposes - //if (resEntity != null) { - //String content = EntityUtils.toString(resEntity); - //System.out.println(content); - //} - - // need to consume the response - EntityUtils.consume(resEntity); - } catch (Exception e) { + URL endpoint = new URL(getConfig().getSentryURL()); + connection = (HttpURLConnection) endpoint.openConnection(getConfig().getProxy()); + connection.setRequestMethod("POST"); + connection.setDoOutput(true); + connection.setReadTimeout(10000); + connection.setRequestProperty("X-Sentry-Auth", authHeader); + OutputStream output = connection.getOutputStream(); + output.write(messageBody.getBytes()); + output.close(); + connection.connect(); + InputStream input = connection.getInputStream(); + input.close(); + } catch (IOException e) { // Eat the errors, we don't want to cause problems if there are major issues. e.printStackTrace(); - } finally { - // When HttpClient instance is no longer needed, - // shut down the connection manager to ensure - // immediate de-allocation of all system resources - httpClient.getConnectionManager().shutdown(); } - } /** diff --git a/src/net/kencochrane/sentry/RavenConfig.java b/src/net/kencochrane/sentry/RavenConfig.java index 006d77a020d..4118a9b8d4b 100644 --- a/src/net/kencochrane/sentry/RavenConfig.java +++ b/src/net/kencochrane/sentry/RavenConfig.java @@ -1,7 +1,6 @@ package net.kencochrane.sentry; -import java.net.MalformedURLException; -import java.net.URL; +import java.net.*; /** * User: ken cochrane @@ -12,6 +11,8 @@ public class RavenConfig { private String host, protocol, publicKey, secretKey, path, projectId; private int port; + private String proxyType, proxyHost; + private int proxyPort; /** * Takes in a sentryDSN and builds up the configuration @@ -19,6 +20,16 @@ public class RavenConfig { * @param sentryDSN '{PROTOCOL}://{PUBLIC_KEY}:{SECRET_KEY}@{HOST}/{PATH}{PROJECT_ID}' */ public RavenConfig(String sentryDSN) { + this(sentryDSN, null); + } + + /** + * Takes in a sentryDSN and builds up the configuration + * + * @param sentryDSN '{PROTOCOL}://{PUBLIC_KEY}:{SECRET_KEY}@{HOST}/{PATH}{PROJECT_ID}' + * @param proxy proxy to use for the HTTP connections; blank or null when no proxy is to be used + */ + public RavenConfig(String sentryDSN, String proxy) { try { URL url = new URL(sentryDSN); @@ -37,6 +48,13 @@ public RavenConfig(String sentryDSN) { this.port = url.getPort(); + if (proxy != null && !proxy.isEmpty()) { + String[] proxyParts = proxy.split(":"); + this.proxyType = proxyParts[0]; + this.proxyHost = proxyParts[1]; + this.proxyPort = Integer.parseInt(proxyParts[2]); + } + } catch (MalformedURLException e) { e.printStackTrace(); @@ -46,6 +64,7 @@ public RavenConfig(String sentryDSN) { /** * The Sentry server URL that we post the message to. + * * @return sentry server url */ public String getSentryURL() { @@ -60,8 +79,17 @@ public String getSentryURL() { return serverUrl.toString(); } + public Proxy getProxy() { + if (proxyType == null || Proxy.Type.DIRECT.name().equals(proxyType)) { + return Proxy.NO_PROXY; + } + SocketAddress proxyAddress = new InetSocketAddress(proxyHost, proxyPort); + return new Proxy(Proxy.Type.valueOf(proxyType), proxyAddress); + } + /** * The sentry server host + * * @return server host */ public String getHost() { @@ -74,6 +102,7 @@ public void setHost(String host) { /** * Sentry server protocol http https? + * * @return http or https */ public String getProtocol() { @@ -86,6 +115,7 @@ public void setProtocol(String protocol) { /** * The Sentry public key + * * @return Sentry public key */ public String getPublicKey() { @@ -98,6 +128,7 @@ public void setPublicKey(String publicKey) { /** * The Sentry secret key + * * @return Sentry secret key */ public String getSecretKey() { @@ -110,6 +141,7 @@ public void setSecretKey(String secretKey) { /** * sentry url path + * * @return url path */ public String getPath() { @@ -122,6 +154,7 @@ public void setPath(String path) { /** * Sentry project Id + * * @return project Id */ public String getProjectId() { @@ -134,6 +167,7 @@ public void setProjectId(String projectId) { /** * sentry server port + * * @return server port */ public int getPort() { diff --git a/src/net/kencochrane/sentry/SentryAppender.java b/src/net/kencochrane/sentry/SentryAppender.java index aed14e21bc1..10dd3efc4aa 100644 --- a/src/net/kencochrane/sentry/SentryAppender.java +++ b/src/net/kencochrane/sentry/SentryAppender.java @@ -11,6 +11,7 @@ public class SentryAppender extends AppenderSkeleton { private String sentry_dsn; + private String proxy; public String getSentry_dsn() { return sentry_dsn; @@ -20,6 +21,14 @@ public void setSentry_dsn(String sentry_dsn) { this.sentry_dsn = sentry_dsn; } + public String getProxy() { + return proxy; + } + + public void setProxy(String proxy) { + this.proxy = proxy; + } + @Override protected void append(LoggingEvent loggingEvent) { @@ -40,7 +49,7 @@ protected void append(LoggingEvent loggingEvent) { String culprit = loggingEvent.getLoggerName(); // create the client passing in the sentry DSN from the log4j properties file. - RavenClient client = new RavenClient(getSentry_dsn()); + RavenClient client = new RavenClient(getSentry_dsn(), getProxy()); // send the message to the sentry server client.logMessage(logMessage, timestamp, loggingClass, logLevel, culprit);