Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>net.kencochrane</groupId>
<artifactId>raven-java</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>raven-java</name>
<description>Java Raven client and log4j appender.</description>

<dependencies>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>

</project>
62 changes: 25 additions & 37 deletions src/net/kencochrane/sentry/RavenClient.java
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand All @@ -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;
}
Expand Down Expand Up @@ -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();
}

}

/**
Expand Down
38 changes: 36 additions & 2 deletions src/net/kencochrane/sentry/RavenConfig.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.kencochrane.sentry;

import java.net.MalformedURLException;
import java.net.URL;
import java.net.*;

/**
* User: ken cochrane
Expand All @@ -12,13 +11,25 @@ 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
*
* @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);
Expand All @@ -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();
Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -74,6 +102,7 @@ public void setHost(String host) {

/**
* Sentry server protocol http https?
*
* @return http or https
*/
public String getProtocol() {
Expand All @@ -86,6 +115,7 @@ public void setProtocol(String protocol) {

/**
* The Sentry public key
*
* @return Sentry public key
*/
public String getPublicKey() {
Expand All @@ -98,6 +128,7 @@ public void setPublicKey(String publicKey) {

/**
* The Sentry secret key
*
* @return Sentry secret key
*/
public String getSecretKey() {
Expand All @@ -110,6 +141,7 @@ public void setSecretKey(String secretKey) {

/**
* sentry url path
*
* @return url path
*/
public String getPath() {
Expand All @@ -122,6 +154,7 @@ public void setPath(String path) {

/**
* Sentry project Id
*
* @return project Id
*/
public String getProjectId() {
Expand All @@ -134,6 +167,7 @@ public void setProjectId(String projectId) {

/**
* sentry server port
*
* @return server port
*/
public int getPort() {
Expand Down
11 changes: 10 additions & 1 deletion src/net/kencochrane/sentry/SentryAppender.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
public class SentryAppender extends AppenderSkeleton {

private String sentry_dsn;
private String proxy;

public String getSentry_dsn() {
return sentry_dsn;
Expand All @@ -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) {

Expand All @@ -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);
Expand Down