Skip to content

Commit

Permalink
[#noissue] Generalized rule of rewriting into front
Browse files Browse the repository at this point in the history
  • Loading branch information
smilu97 committed Sep 6, 2023
1 parent e8d52e5 commit a561fba
Showing 1 changed file with 27 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,19 @@
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.regex.Pattern;

/**
* @author Taejin Koo
*/
public class RewriteForV2Filter implements Filter {

public static final String DEFAULT_INDEX = "/index.html";
private static final char PATH_DELIMITER = '/';
private static final String[] URI_PREFIXES_FOR_FRONT = {
"/auth",
"/browserNotSupported",
"/config",
"/error",
"/filteredMap",
"/inspector",
"/main",
"/realtime",
"/scatterFullScreenMode",
"/threadDump",
"/transactionDetail",
"/transactionList",
"/transactionView",
"/urlStatistic",
"/metric",
};
private static final Pattern API_PATTERN = Pattern.compile("(/v2|/v3)?/(.*\\.(js|css|png|ico)|assets/.+)|/api/.+");
private static final String LEGACY_FRONT_ENTRY_HTML = "/index.html";
private static final String V2_FRONT_ENTRY_HTML = "/v2/index.html";
private static final String V3_FRONT_ENTRY_HTML = "/v3/index.html";

private final Logger logger = LogManager.getLogger(this.getClass());
private final boolean isDebugEnabled = logger.isDebugEnabled();

private final boolean enabled;

Expand All @@ -79,43 +64,41 @@ public void doFilter(
ServletResponse response,
FilterChain chain
) throws IOException, ServletException {
if (this.shouldServeFrontApplication(request)) {
this.serveFrontApplication(request, response);
if (this.enabled && isRequestForFrontAppEntry(request)) {
this.serveFrontAppEntry((HttpServletRequest) request, response);

Check warning on line 68 in web/src/main/java/com/navercorp/pinpoint/web/servlet/RewriteForV2Filter.java

View check run for this annotation

Codecov / codecov/patch

web/src/main/java/com/navercorp/pinpoint/web/servlet/RewriteForV2Filter.java#L68

Added line #L68 was not covered by tests
} else {
chain.doFilter(request, response);
}
}

private void serveFrontApplication(ServletRequest req, ServletResponse res) throws ServletException, IOException {
if (isDebugEnabled) {
logger.debug("requestUri: {} --(forward)--> {}", ((HttpServletRequest) req).getRequestURI(), DEFAULT_INDEX);
}
RequestDispatcher dispatcher = req.getRequestDispatcher(DEFAULT_INDEX);
dispatcher.forward(req, res);
}
private void serveFrontAppEntry(
HttpServletRequest request,
ServletResponse response
) throws ServletException, IOException {
String uri = request.getRequestURI();
String entry = getFrontAppEntryHtml(uri);
logger.debug("requestUri: {} --(forward)--> {}", uri, entry);

private boolean shouldServeFrontApplication(ServletRequest req) {
return this.enabled && isURIForFront(getRequestURI(req));
RequestDispatcher dispatcher = request.getRequestDispatcher(entry);
dispatcher.forward(request, response);

Check warning on line 83 in web/src/main/java/com/navercorp/pinpoint/web/servlet/RewriteForV2Filter.java

View check run for this annotation

Codecov / codecov/patch

web/src/main/java/com/navercorp/pinpoint/web/servlet/RewriteForV2Filter.java#L83

Added line #L83 was not covered by tests
}

private static boolean isURIForFront(String uri) {
if (uri == null) {
private static boolean isRequestForFrontAppEntry(ServletRequest request) {
if (!(request instanceof HttpServletRequest)) {
return false;
}

for (String prefix : URI_PREFIXES_FOR_FRONT) {
if (uri.equals(prefix) || uri.startsWith(prefix + PATH_DELIMITER)) {
return true;
}
}
return false;
String uri = ((HttpServletRequest) request).getRequestURI();
return !API_PATTERN.matcher(uri).matches();
}

private static String getRequestURI(ServletRequest req) {
if (req instanceof HttpServletRequest) {
return ((HttpServletRequest) req).getRequestURI();
private static String getFrontAppEntryHtml(String uri) {
if (uri.startsWith("/v2")) {
return V2_FRONT_ENTRY_HTML;

Check warning on line 96 in web/src/main/java/com/navercorp/pinpoint/web/servlet/RewriteForV2Filter.java

View check run for this annotation

Codecov / codecov/patch

web/src/main/java/com/navercorp/pinpoint/web/servlet/RewriteForV2Filter.java#L96

Added line #L96 was not covered by tests
}
if (uri.startsWith("/v3")) {
return V3_FRONT_ENTRY_HTML;

Check warning on line 99 in web/src/main/java/com/navercorp/pinpoint/web/servlet/RewriteForV2Filter.java

View check run for this annotation

Codecov / codecov/patch

web/src/main/java/com/navercorp/pinpoint/web/servlet/RewriteForV2Filter.java#L99

Added line #L99 was not covered by tests
}
return null;
return LEGACY_FRONT_ENTRY_HTML;
}

}

0 comments on commit a561fba

Please sign in to comment.