Skip to content

Commit 8abbff7

Browse files
committed
Another try to resolve CodeQL warning
1 parent 93fd75f commit 8abbff7

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

instrumentation/servlet/servlet-3.0/testing/src/main/java/io/opentelemetry/instrumentation/servlet/v3_0/RequestDispatcherServlet.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@ public static class Forward extends HttpServlet {
2222
@Override
2323
protected void service(HttpServletRequest req, HttpServletResponse resp)
2424
throws ServletException, IOException {
25-
String target = req.getServletPath().replace("/dispatch", "");
26-
ServerEndpoint endpoint = ServerEndpoint.forPath(target);
27-
if (endpoint == null) {
28-
throw new IllegalStateException("Unexpected endpoint: " + target);
29-
}
25+
String target = getTargetSafely(req);
3026
ServletContext context = getServletContext();
3127
RequestDispatcher dispatcher = context.getRequestDispatcher(target);
3228
dispatcher.forward(req, resp);
@@ -38,24 +34,28 @@ public static class Include extends HttpServlet {
3834
@Override
3935
protected void service(HttpServletRequest req, HttpServletResponse resp)
4036
throws ServletException, IOException {
41-
String target = req.getServletPath().replace("/dispatch", "");
37+
String target = getTargetSafely(req);
4238
ServletContext context = getServletContext();
4339
RequestDispatcher dispatcher = context.getRequestDispatcher(target);
44-
ServerEndpoint endpoint = ServerEndpoint.forPath(target);
45-
if (endpoint == null) {
46-
throw new IllegalStateException("Unexpected endpoint: " + target);
47-
}
4840
// for HTML test case, set the content type before calling include because
4941
// setContentType will be rejected if called inside include
5042
// check
5143
// https://docs.oracle.com/javaee/7/api/javax/servlet/RequestDispatcher.html#include-javax.servlet.ServletRequest-javax.servlet.ServletResponse-
52-
if (endpoint == AbstractServlet3Test.HTML_PRINT_WRITER
53-
|| endpoint == AbstractServlet3Test.HTML_SERVLET_OUTPUT_STREAM) {
44+
if ("/htmlPrintWriter".equals(target) || "/htmlServletOutputStream".equals(target)) {
5445
resp.setContentType("text/html");
5546
}
5647
dispatcher.include(req, resp);
5748
}
5849
}
5950

51+
private static String getTargetSafely(HttpServletRequest req) {
52+
String target = req.getServletPath().replace("/dispatch", "");
53+
ServerEndpoint endpoint = ServerEndpoint.forPath(target);
54+
if (endpoint != null) {
55+
return endpoint.getPath();
56+
}
57+
throw new IllegalStateException("Unexpected endpoint: " + target);
58+
}
59+
6060
private RequestDispatcherServlet() {}
6161
}

instrumentation/servlet/servlet-5.0/testing/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v5_0/RequestDispatcherServlet.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,7 @@ public static class Forward extends HttpServlet {
2424
@Override
2525
protected void service(HttpServletRequest req, HttpServletResponse resp)
2626
throws ServletException, IOException {
27-
String target = req.getServletPath().replace("/dispatch", "");
28-
ServerEndpoint endpoint = ServerEndpoint.forPath(target);
29-
if (endpoint == null) {
30-
throw new IllegalStateException("Unexpected endpoint: " + target);
31-
}
27+
String target = getTargetSafely(req);
3228
ServletContext context = getServletContext();
3329
RequestDispatcher dispatcher = context.getRequestDispatcher(target);
3430
dispatcher.forward(req, resp);
@@ -42,22 +38,26 @@ public static class Include extends HttpServlet {
4238
@Override
4339
protected void service(HttpServletRequest req, HttpServletResponse resp)
4440
throws ServletException, IOException {
45-
String target = req.getServletPath().replace("/dispatch", "");
41+
String target = getTargetSafely(req);
4642
ServletContext context = getServletContext();
4743
RequestDispatcher dispatcher = context.getRequestDispatcher(target);
48-
ServerEndpoint endpoint = ServerEndpoint.forPath(target);
49-
if (endpoint == null) {
50-
throw new IllegalStateException("Unexpected endpoint: " + target);
51-
}
5244
// For the HTML test cases, set the content type before include() because the response is
5345
// already committed for header updates inside the included resource.
54-
if (endpoint == AbstractServlet5Test.HTML_PRINT_WRITER
55-
|| endpoint == AbstractServlet5Test.HTML_SERVLET_OUTPUT_STREAM) {
46+
if ("/htmlPrintWriter".equals(target) || "/htmlServletOutputStream".equals(target)) {
5647
resp.setContentType("text/html");
5748
}
5849
dispatcher.include(req, resp);
5950
}
6051
}
6152

53+
private static String getTargetSafely(HttpServletRequest req) {
54+
String target = req.getServletPath().replace("/dispatch", "");
55+
ServerEndpoint endpoint = ServerEndpoint.forPath(target);
56+
if (endpoint != null) {
57+
return endpoint.getPath();
58+
}
59+
throw new IllegalStateException("Unexpected endpoint: " + target);
60+
}
61+
6262
private RequestDispatcherServlet() {}
6363
}

0 commit comments

Comments
 (0)