Skip to content

Commit fe173cf

Browse files
chris-smith-zocdocbretthoerner
authored andcommitted
Allow the creation of the HttpInterface object from just primitive objects. (getsentry#690)
Add HttpInterface to the context and contextbuilderhelper, allowing integration with other web frameworks that are not Servlets
1 parent 5a2b534 commit fe173cf

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

sentry/src/main/java/io/sentry/context/Context.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import io.sentry.event.Breadcrumb;
44
import io.sentry.event.User;
5+
import io.sentry.event.interfaces.HttpInterface;
56
import io.sentry.util.CircularFifoQueue;
67

78
import java.io.Serializable;
@@ -42,6 +43,12 @@ public class Context implements Serializable {
4243
*/
4344
private volatile Map<String, Object> extra;
4445

46+
/**
47+
* HTTP data to add to any future {@link io.sentry.event.Event}s.
48+
*/
49+
private volatile HttpInterface http;
50+
51+
4552
/**
4653
* Create a new (empty) Context object with the default Breadcrumb limit.
4754
*/
@@ -67,6 +74,7 @@ public synchronized void clear() {
6774
clearUser();
6875
clearTags();
6976
clearExtra();
77+
clearHttp();
7078
}
7179

7280
/**
@@ -178,6 +186,32 @@ public synchronized void clearExtra() {
178186
extra = null;
179187
}
180188

189+
190+
/**
191+
* Store the http information in the context.
192+
*
193+
* @param http Http data to store in context.
194+
*/
195+
public synchronized void setHttp(HttpInterface http) {
196+
this.http = http;
197+
}
198+
199+
/**
200+
* Gets the http information from the context.
201+
*
202+
* @return HttpInterface currently stored in context.
203+
*/
204+
public synchronized HttpInterface getHttp() {
205+
return http;
206+
}
207+
208+
/**
209+
* Clear the http data from this context.
210+
*/
211+
public synchronized void clearHttp() {
212+
http = null;
213+
}
214+
181215
/**
182216
* Record a single {@link Breadcrumb} into this context.
183217
*

sentry/src/main/java/io/sentry/event/helper/ContextBuilderHelper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public void helpBuildingEvent(EventBuilder eventBuilder) {
3939
eventBuilder.withBreadcrumbs(breadcrumbs);
4040
}
4141

42+
if (context.getHttp() != null) {
43+
eventBuilder.withSentryInterface(context.getHttp());
44+
}
45+
4246
if (context.getUser() != null) {
4347
eventBuilder.withSentryInterface(fromUser(context.getUser()));
4448
}

sentry/src/main/java/io/sentry/event/interfaces/HttpInterface.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,69 @@ public HttpInterface(HttpServletRequest request, RemoteAddressResolver remoteAdd
9494
this.body = body;
9595
}
9696

97+
// CHECKSTYLE.OFF: ParameterNumber
98+
99+
/**
100+
* Creates an HTTP element for an {@link io.sentry.event.Event}.
101+
*
102+
* @param requestUrl The full url including the scheme, host, path and query parameters
103+
* @param method The HTTP method
104+
* @param parameters Extra request parameters from either the query string or posted form data.
105+
* @param queryString The query string
106+
* @param cookies Collection of values for each cookie
107+
* @param remoteAddr The remote ip address
108+
* @param serverName The server's hostname
109+
* @param serverPort The port from the server's hostname
110+
* @param localAddr The IP that received the request
111+
* @param localName The hostname of the IP that received the request
112+
* @param localPort The port that received the request
113+
* @param protocol The protocol name and version
114+
* @param secure True if the request was made using a secure channel
115+
* @param asyncStarted Servlet specific
116+
* @param authType Servlet specific
117+
* @param remoteUser The login of the user making the request
118+
* @param headers The headers sent with the request
119+
* @param body The request body
120+
*/
121+
public HttpInterface(String requestUrl,
122+
String method,
123+
Map<String, Collection<String>> parameters,
124+
String queryString, Map<String, String> cookies,
125+
String remoteAddr,
126+
String serverName,
127+
int serverPort,
128+
String localAddr,
129+
String localName,
130+
int localPort,
131+
String protocol,
132+
boolean secure,
133+
boolean asyncStarted,
134+
String authType,
135+
String remoteUser,
136+
Map<String, Collection<String>> headers,
137+
String body) {
138+
139+
this.requestUrl = requestUrl;
140+
this.method = method;
141+
this.parameters = parameters;
142+
this.queryString = queryString;
143+
this.cookies = cookies;
144+
this.remoteAddr = remoteAddr;
145+
this.serverName = serverName;
146+
this.serverPort = serverPort;
147+
this.localAddr = localAddr;
148+
this.localName = localName;
149+
this.localPort = localPort;
150+
this.protocol = protocol;
151+
this.secure = secure;
152+
this.asyncStarted = asyncStarted;
153+
this.authType = authType;
154+
this.remoteUser = remoteUser;
155+
this.headers = headers;
156+
this.body = body;
157+
}
158+
// CHECKSTYLE.ON: ParameterNumber
159+
97160
@Override
98161
public String getInterfaceName() {
99162
return HTTP_INTERFACE;

0 commit comments

Comments
 (0)