Skip to content

Commit 13eb61c

Browse files
committed
Merge pull request #1 from roam/master
Mavenized, HttpClient removed and proxy support added
2 parents af55885 + 78fb2d8 commit 13eb61c

File tree

4 files changed

+135
-40
lines changed

4 files changed

+135
-40
lines changed

pom.xml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>net.kencochrane</groupId>
8+
<artifactId>raven-java</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<packaging>jar</packaging>
11+
<name>raven-java</name>
12+
<description>Java Raven client and log4j appender.</description>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>commons-lang</groupId>
17+
<artifactId>commons-lang</artifactId>
18+
<version>2.5</version>
19+
</dependency>
20+
<dependency>
21+
<groupId>commons-codec</groupId>
22+
<artifactId>commons-codec</artifactId>
23+
<version>1.6</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.json</groupId>
27+
<artifactId>json</artifactId>
28+
<version>20090211</version>
29+
</dependency>
30+
<dependency>
31+
<groupId>log4j</groupId>
32+
<artifactId>log4j</artifactId>
33+
<version>1.2.16</version>
34+
</dependency>
35+
<dependency>
36+
<groupId>junit</groupId>
37+
<artifactId>junit</artifactId>
38+
<version>4.8.1</version>
39+
<scope>test</scope>
40+
</dependency>
41+
</dependencies>
42+
43+
<build>
44+
<plugins>
45+
<plugin>
46+
<groupId>org.apache.maven.plugins</groupId>
47+
<artifactId>maven-compiler-plugin</artifactId>
48+
<configuration>
49+
<source>1.6</source>
50+
<target>1.6</target>
51+
<encoding>UTF-8</encoding>
52+
</configuration>
53+
</plugin>
54+
<plugin>
55+
<groupId>org.apache.maven.plugins</groupId>
56+
<artifactId>maven-resources-plugin</artifactId>
57+
<configuration>
58+
<encoding>UTF-8</encoding>
59+
</configuration>
60+
</plugin>
61+
</plugins>
62+
</build>
63+
64+
</project>

src/net/kencochrane/sentry/RavenClient.java

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package net.kencochrane.sentry;
22

3-
import org.apache.http.HttpEntity;
4-
import org.apache.http.HttpResponse;
5-
import org.apache.http.client.methods.HttpPost;
6-
import org.apache.http.entity.StringEntity;
7-
import org.apache.http.impl.client.DefaultHttpClient;
8-
import org.apache.http.util.EntityUtils;
93
import org.json.simple.JSONObject;
104

5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.io.OutputStream;
8+
import java.net.HttpURLConnection;
9+
import java.net.URL;
10+
1111
import static org.apache.commons.codec.binary.Base64.encodeBase64String;
1212

1313
/**
@@ -27,6 +27,11 @@ public RavenClient(String sentryDSN) {
2727
this.config = new RavenConfig(sentryDSN);
2828
}
2929

30+
public RavenClient(String sentryDSN, String proxy) {
31+
this.sentryDSN = sentryDSN;
32+
this.config = new RavenConfig(sentryDSN, proxy);
33+
}
34+
3035
public RavenConfig getConfig() {
3136
return config;
3237
}
@@ -143,48 +148,31 @@ private String buildAuthHeader(String hmacSignature, long timestamp, String publ
143148
* @param timestamp the timestamp of the message
144149
*/
145150
private void sendMessage(String messageBody, long timestamp) {
146-
147-
DefaultHttpClient httpClient = new DefaultHttpClient();
151+
HttpURLConnection connection = null;
148152
try {
149153

150-
// build up the Post method
151-
HttpPost httppost = new HttpPost(getConfig().getSentryURL());
152-
153154
// get the hmac Signature for the header
154155
String hmacSignature = RavenUtils.getSignature(messageBody, timestamp, config.getSecretKey());
155156

156157
// get the auth header
157158
String authHeader = buildAuthHeader(hmacSignature, timestamp, getConfig().getPublicKey());
158159

159-
// add auth header to http post
160-
httppost.addHeader("X-Sentry-Auth", authHeader);
161-
162-
// set the body into the post
163-
StringEntity reqEntity = new StringEntity(messageBody);
164-
httppost.setEntity(reqEntity);
165-
166-
// call the server and get response
167-
HttpResponse response = httpClient.execute(httppost);
168-
HttpEntity resEntity = response.getEntity();
169-
170-
// not needed right now, keeping around for debugging purposes
171-
//if (resEntity != null) {
172-
//String content = EntityUtils.toString(resEntity);
173-
//System.out.println(content);
174-
//}
175-
176-
// need to consume the response
177-
EntityUtils.consume(resEntity);
178-
} catch (Exception e) {
160+
URL endpoint = new URL(getConfig().getSentryURL());
161+
connection = (HttpURLConnection) endpoint.openConnection(getConfig().getProxy());
162+
connection.setRequestMethod("POST");
163+
connection.setDoOutput(true);
164+
connection.setReadTimeout(10000);
165+
connection.setRequestProperty("X-Sentry-Auth", authHeader);
166+
OutputStream output = connection.getOutputStream();
167+
output.write(messageBody.getBytes());
168+
output.close();
169+
connection.connect();
170+
InputStream input = connection.getInputStream();
171+
input.close();
172+
} catch (IOException e) {
179173
// Eat the errors, we don't want to cause problems if there are major issues.
180174
e.printStackTrace();
181-
} finally {
182-
// When HttpClient instance is no longer needed,
183-
// shut down the connection manager to ensure
184-
// immediate de-allocation of all system resources
185-
httpClient.getConnectionManager().shutdown();
186175
}
187-
188176
}
189177

190178
/**

src/net/kencochrane/sentry/RavenConfig.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package net.kencochrane.sentry;
22

3-
import java.net.MalformedURLException;
4-
import java.net.URL;
3+
import java.net.*;
54

65
/**
76
* User: ken cochrane
@@ -12,13 +11,25 @@ public class RavenConfig {
1211

1312
private String host, protocol, publicKey, secretKey, path, projectId;
1413
private int port;
14+
private String proxyType, proxyHost;
15+
private int proxyPort;
1516

1617
/**
1718
* Takes in a sentryDSN and builds up the configuration
1819
*
1920
* @param sentryDSN '{PROTOCOL}://{PUBLIC_KEY}:{SECRET_KEY}@{HOST}/{PATH}{PROJECT_ID}'
2021
*/
2122
public RavenConfig(String sentryDSN) {
23+
this(sentryDSN, null);
24+
}
25+
26+
/**
27+
* Takes in a sentryDSN and builds up the configuration
28+
*
29+
* @param sentryDSN '{PROTOCOL}://{PUBLIC_KEY}:{SECRET_KEY}@{HOST}/{PATH}{PROJECT_ID}'
30+
* @param proxy proxy to use for the HTTP connections; blank or null when no proxy is to be used
31+
*/
32+
public RavenConfig(String sentryDSN, String proxy) {
2233

2334
try {
2435
URL url = new URL(sentryDSN);
@@ -37,6 +48,13 @@ public RavenConfig(String sentryDSN) {
3748

3849
this.port = url.getPort();
3950

51+
if (proxy != null && !proxy.isEmpty()) {
52+
String[] proxyParts = proxy.split(":");
53+
this.proxyType = proxyParts[0];
54+
this.proxyHost = proxyParts[1];
55+
this.proxyPort = Integer.parseInt(proxyParts[2]);
56+
}
57+
4058

4159
} catch (MalformedURLException e) {
4260
e.printStackTrace();
@@ -46,6 +64,7 @@ public RavenConfig(String sentryDSN) {
4664

4765
/**
4866
* The Sentry server URL that we post the message to.
67+
*
4968
* @return sentry server url
5069
*/
5170
public String getSentryURL() {
@@ -60,8 +79,17 @@ public String getSentryURL() {
6079
return serverUrl.toString();
6180
}
6281

82+
public Proxy getProxy() {
83+
if (proxyType == null || Proxy.Type.DIRECT.name().equals(proxyType)) {
84+
return Proxy.NO_PROXY;
85+
}
86+
SocketAddress proxyAddress = new InetSocketAddress(proxyHost, proxyPort);
87+
return new Proxy(Proxy.Type.valueOf(proxyType), proxyAddress);
88+
}
89+
6390
/**
6491
* The sentry server host
92+
*
6593
* @return server host
6694
*/
6795
public String getHost() {
@@ -74,6 +102,7 @@ public void setHost(String host) {
74102

75103
/**
76104
* Sentry server protocol http https?
105+
*
77106
* @return http or https
78107
*/
79108
public String getProtocol() {
@@ -86,6 +115,7 @@ public void setProtocol(String protocol) {
86115

87116
/**
88117
* The Sentry public key
118+
*
89119
* @return Sentry public key
90120
*/
91121
public String getPublicKey() {
@@ -98,6 +128,7 @@ public void setPublicKey(String publicKey) {
98128

99129
/**
100130
* The Sentry secret key
131+
*
101132
* @return Sentry secret key
102133
*/
103134
public String getSecretKey() {
@@ -110,6 +141,7 @@ public void setSecretKey(String secretKey) {
110141

111142
/**
112143
* sentry url path
144+
*
113145
* @return url path
114146
*/
115147
public String getPath() {
@@ -122,6 +154,7 @@ public void setPath(String path) {
122154

123155
/**
124156
* Sentry project Id
157+
*
125158
* @return project Id
126159
*/
127160
public String getProjectId() {
@@ -134,6 +167,7 @@ public void setProjectId(String projectId) {
134167

135168
/**
136169
* sentry server port
170+
*
137171
* @return server port
138172
*/
139173
public int getPort() {

src/net/kencochrane/sentry/SentryAppender.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
public class SentryAppender extends AppenderSkeleton {
1212

1313
private String sentry_dsn;
14+
private String proxy;
1415

1516
public String getSentry_dsn() {
1617
return sentry_dsn;
@@ -20,6 +21,14 @@ public void setSentry_dsn(String sentry_dsn) {
2021
this.sentry_dsn = sentry_dsn;
2122
}
2223

24+
public String getProxy() {
25+
return proxy;
26+
}
27+
28+
public void setProxy(String proxy) {
29+
this.proxy = proxy;
30+
}
31+
2332
@Override
2433
protected void append(LoggingEvent loggingEvent) {
2534

@@ -40,7 +49,7 @@ protected void append(LoggingEvent loggingEvent) {
4049
String culprit = loggingEvent.getLoggerName();
4150

4251
// create the client passing in the sentry DSN from the log4j properties file.
43-
RavenClient client = new RavenClient(getSentry_dsn());
52+
RavenClient client = new RavenClient(getSentry_dsn(), getProxy());
4453

4554
// send the message to the sentry server
4655
client.logMessage(logMessage, timestamp, loggingClass, logLevel, culprit);

0 commit comments

Comments
 (0)