Skip to content

Commit

Permalink
Adding Jakarta support (#372)
Browse files Browse the repository at this point in the history
* Adding Jakarta support
  • Loading branch information
atshaw43 authored Feb 21, 2023
1 parent cbf9f62 commit 8215450
Show file tree
Hide file tree
Showing 7 changed files with 1,185 additions and 0 deletions.
2 changes: 2 additions & 0 deletions aws-xray-recorder-sdk-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ dependencies {

compileOnly("com.google.code.findbugs:jsr305:3.0.2")
compileOnly("javax.servlet:javax.servlet-api:3.1.0")
compileOnly("jakarta.servlet:jakarta.servlet-api:5.0.0")

testImplementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310")
testImplementation("com.github.stefanbirkner:system-rules:1.16.0")
Expand All @@ -16,6 +17,7 @@ dependencies {
testImplementation("org.powermock:powermock-module-junit4:2.0.7")
testImplementation("org.powermock:powermock-api-mockito2:2.0.7")
testImplementation("org.skyscreamer:jsonassert:1.3.0")
testImplementation("jakarta.servlet:jakarta.servlet-api:5.0.0")
}

tasks.jar {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package com.amazonaws.xray.jakarta.servlet;

import com.amazonaws.xray.AWSXRayRecorder;
import com.amazonaws.xray.entities.Entity;
import jakarta.servlet.AsyncEvent;
import jakarta.servlet.AsyncListener;
import java.io.IOException;
import org.checkerframework.checker.initialization.qual.UnderInitialization;
import org.checkerframework.checker.nullness.qual.Nullable;

class AWSXRayServletAsyncListener implements AsyncListener {

public static final String ENTITY_ATTRIBUTE_KEY = "com.amazonaws.xray.entities.Entity";

@Nullable
private AWSXRayRecorder recorder;
private final AWSXRayServletFilter filter;

// TODO(anuraaga): Better define lifecycle relationship between this listener and the filter.
@SuppressWarnings("nullness")
AWSXRayServletAsyncListener(@UnderInitialization AWSXRayServletFilter filter, @Nullable AWSXRayRecorder recorder) {
this.filter = filter;
this.recorder = recorder;
}

private void processEvent(AsyncEvent event) throws IOException {
Entity entity = (Entity) event.getSuppliedRequest().getAttribute(ENTITY_ATTRIBUTE_KEY);
entity.run(() -> {
if (event.getThrowable() != null) {
entity.addException(event.getThrowable());
}
filter.postFilter(event.getSuppliedRequest(), event.getSuppliedResponse());
});
}

@Override
public void onComplete(AsyncEvent event) throws IOException {
processEvent(event);
}

@Override
public void onTimeout(AsyncEvent event) throws IOException {
processEvent(event);
}

@Override
public void onError(AsyncEvent event) throws IOException {
processEvent(event);
}

@Override
public void onStartAsync(AsyncEvent event) throws IOException {
}
}
Loading

0 comments on commit 8215450

Please sign in to comment.