Skip to content

Commit

Permalink
[pinpoint-apm#9623] Remove private pinpoint error data on PinpointErr…
Browse files Browse the repository at this point in the history
…orAttributes
  • Loading branch information
ga-ram committed Jan 10, 2023
1 parent 7374de4 commit b5ce119
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,20 @@
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Component
public class PinpointErrorAttributes extends DefaultErrorAttributes {

private PinpointErrorData pinpointErrorData;

@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
this.pinpointErrorData = new PinpointErrorData(request);
return super.resolveException(request, response, handler, ex);
}

@Override
public Map<String, Object> getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) {
Map<String, Object> errorAttributes = super.getErrorAttributes(webRequest, options);
this.addCustomData(errorAttributes);
this.addCustomData(webRequest, errorAttributes);
return errorAttributes;
}

private void addCustomData(Map<String, Object> errorAttributes) {
private void addCustomData(WebRequest webRequest, Map<String, Object> errorAttributes) {
PinpointErrorData pinpointErrorData = new PinpointErrorData(webRequest);
errorAttributes.put("data", pinpointErrorData);
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package com.navercorp.pinpoint.web.view.error;

import com.fasterxml.jackson.annotation.JsonInclude;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.context.request.WebRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.Enumeration;
import java.util.Collections;
import java.util.*;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class PinpointErrorData {
private final RequestInfo requestInfo;

public PinpointErrorData(HttpServletRequest request) {
public PinpointErrorData(WebRequest request) {
this.requestInfo = new RequestInfo(request);
}

Expand All @@ -26,14 +23,22 @@ public static class RequestInfo {
private static final String UNKNOWN = "UNKNOWN";
private final String method;
private final String url;
private final Map<String, List<String>> headers;
private final Map<String, String[]> parameters;

public RequestInfo(HttpServletRequest request) {
this.method = request.getMethod();
this.url = getRequestUrl(request);
this.headers = getRequestHeaders(request);
this.parameters = request.getParameterMap();;
private Map<String, List<String>> headers;
private Map<String, String[]> parameters;

public RequestInfo(WebRequest request) {
if (request instanceof ServletWebRequest) {
ServletWebRequest webRequest = (ServletWebRequest) request;
this.method = webRequest.getRequest().getMethod();
this.url = String.valueOf(webRequest.getRequest().getRequestURL());
this.headers = getRequestHeader(webRequest);
this.parameters = request.getParameterMap();
} else {
this.method = "UNKNOWN";
this.url = "UNKNOWN";
this.headers = null;
this.parameters = null;
}
}

public String getMethod() {
Expand All @@ -52,49 +57,23 @@ public Map<String, String[]> getParameters() {
return parameters;
}

private String getRequestUrl(HttpServletRequest request) {
if (request.getRequestURL() == null) {
return UNKNOWN;
}
return request.getRequestURL().toString();
}

private Map<String, List<String>> getRequestHeaders(HttpServletRequest request) {
Enumeration<String> keys = request.getHeaderNames();
private Map<String, List<String>> getRequestHeader(ServletWebRequest webRequest) {
Iterator<String> keys = webRequest.getHeaderNames();
if (keys == null) {
return Collections.emptyMap();
}

Map<String, List<String>> result = new HashMap<>();
while (keys.hasMoreElements()) {
String key = keys.nextElement();
while(keys.hasNext()) {
String key = keys.next();
if (key == null) {
continue;
}

result.put(key, getRequestHeaderValueList(request, key));
result.put(key, List.of(webRequest.getHeaderValues(key)));
}

return result;
}

private List<String> getRequestHeaderValueList(HttpServletRequest request, String key) {
Enumeration<String> headerValues = request.getHeaders(key);
if (headerValues == null) {
return Collections.emptyList();
}

List<String> headerValueList = new ArrayList<>();
while (headerValues.hasMoreElements()) {
String headerValue = headerValues.nextElement();
if (headerValue == null) {
headerValueList.add("null");
} else {
headerValueList.add(headerValue);
}
}

return headerValueList;
}

@Override
Expand Down

0 comments on commit b5ce119

Please sign in to comment.