Skip to content

Commit

Permalink
[#4271] code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad authored and jaehong-kim committed Jul 11, 2018
1 parent b478c6b commit 851e25b
Show file tree
Hide file tree
Showing 14 changed files with 418 additions and 218 deletions.
@@ -1,11 +1,11 @@
/*
* Copyright 2017 NAVER Corp.
* Copyright 2018 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -21,6 +21,7 @@
import com.navercorp.pinpoint.bootstrap.logging.PLogger;
import com.navercorp.pinpoint.bootstrap.logging.PLoggerFactory;
import com.navercorp.pinpoint.bootstrap.plugin.RequestWrapper;
import com.navercorp.pinpoint.common.util.StringUtils;

/**
* @author jaehong.kim
Expand Down Expand Up @@ -63,7 +64,7 @@ public void record(final SpanRecorder recorder, final RequestWrapper requestWrap

private void parseAndRecord(final SpanRecorder recorder, final RequestWrapper requestWrapper, final String name, final int type) {
final String value = requestWrapper.getHeader(name);
if (value == null || value.isEmpty()) {
if (StringUtils.isEmpty(value)) {
return;
}

Expand Down
@@ -0,0 +1,33 @@
/*
* Copyright 2018 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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.navercorp.pinpoint.bootstrap.plugin.request;

import com.navercorp.pinpoint.bootstrap.plugin.RequestWrapper;

/**
* @author Woonduk Kang(emeroad)
*/
public class BypassRemoteAddressResolver implements RemoteAddressResolver {

BypassRemoteAddressResolver() {
}

@Override
public String getRemoteAddress(RequestWrapper requestWrapper, String remoteAddr) {
return remoteAddr;
}
}
@@ -0,0 +1,53 @@
/*
* Copyright 2018 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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.navercorp.pinpoint.bootstrap.plugin.request;

import com.navercorp.pinpoint.bootstrap.plugin.RequestWrapper;
import com.navercorp.pinpoint.common.util.Assert;
import com.navercorp.pinpoint.common.util.StringUtils;

/**
* @author Woonduk Kang(emeroad)
*/
public class RealIpHeaderResolver implements RemoteAddressResolver {

private final String realIpHeaderName;
private final String realIpHeaderEmptyValue;

RealIpHeaderResolver(final String realIpHeaderName, final String realIpHeaderEmptyValue) {
this.realIpHeaderName = Assert.requireNonNull(realIpHeaderName, "realIpHeaderName must not be null");
this.realIpHeaderEmptyValue = realIpHeaderEmptyValue;
}

public String getRemoteAddress(final RequestWrapper requestWrapper, final String remoteAddr) {
final String realIp = requestWrapper.getHeader(realIpHeaderName);
if (StringUtils.isEmpty(realIp)) {
return remoteAddr;
}

if (realIpHeaderEmptyValue != null && realIpHeaderEmptyValue.equalsIgnoreCase(realIp)) {
return remoteAddr;
}

final int firstIndex = realIp.indexOf(',');
if (firstIndex == -1) {
return realIp;
} else {
return realIp.substring(0, firstIndex);
}
}
}
@@ -0,0 +1,26 @@
/*
* Copyright 2018 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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.navercorp.pinpoint.bootstrap.plugin.request;

import com.navercorp.pinpoint.bootstrap.plugin.RequestWrapper;
/**
* @author Woonduk Kang(emeroad)
*/
public interface RemoteAddressResolver {

String getRemoteAddress(final RequestWrapper requestWrapper, final String remoteAddr);
}
@@ -0,0 +1,35 @@
/*
* Copyright 2018 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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.navercorp.pinpoint.bootstrap.plugin.request;


import com.navercorp.pinpoint.common.util.StringUtils;

/**
* @author Woonduk Kang(emeroad)
*/
public class RemoteAddressResolverFactory {


public static final RemoteAddressResolver newRemoteAddressResolver(final String realIpHeaderName, final String realIpHeaderEmptyValue) {
if(!StringUtils.hasLength(realIpHeaderEmptyValue)) {
return new BypassRemoteAddressResolver();
}

return new RealIpHeaderResolver(realIpHeaderName, realIpHeaderEmptyValue);
}
}
Expand Up @@ -5,7 +5,7 @@
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
Expand Down Expand Up @@ -108,7 +108,7 @@ public void initialized(final ServletServerRequestWrapper servletServerRequestWr
}
}

Trace createTrace(final ServletServerRequestWrapper servletServerRequestWrapper, final ServiceType serviceType) {
private Trace createTrace(final ServletServerRequestWrapper servletServerRequestWrapper, final ServiceType serviceType) {
final String requestURI = servletServerRequestWrapper.getRpcName();
if (this.excludeUrlFilter.filter(requestURI)) {
if (isTrace) {
Expand Down
Expand Up @@ -5,7 +5,7 @@
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -22,13 +22,13 @@
* @author jaehong.kim
*/
public class ServletServerRequestWrapper implements ServerRequestWrapper {
private String rpcName;
private String endPoint;
private String remoteAddress;
private String acceptorHost;
private String method;
private String parameters;
private RequestWrapper requestWrapper;
private final String rpcName;
private final String endPoint;
private final String remoteAddress;
private final String acceptorHost;
private final String method;
private final String parameters;
private final RequestWrapper requestWrapper;

public ServletServerRequestWrapper(final RequestWrapper requestWrapper, final String rpcName, final String endPoint, final String remoteAddr, final String acceptorHost, final String method, final String parameters) {
this.requestWrapper = requestWrapper;
Expand Down
Expand Up @@ -5,7 +5,7 @@
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -20,6 +20,7 @@
import com.navercorp.pinpoint.bootstrap.util.NetworkUtils;
import com.navercorp.pinpoint.common.plugin.util.HostAndPort;
import com.navercorp.pinpoint.common.util.ArrayUtils;
import com.navercorp.pinpoint.common.util.Assert;
import com.navercorp.pinpoint.common.util.StringUtils;

import java.util.Map;
Expand All @@ -31,43 +32,22 @@ public class ServletServerRequestWrapperFactory {
public static final int PARAMETER_EACH_LIMIT = 64;
public static final int PARAMETER_TOTAL_LIMIT = 512;

private final String realIpHeaderName;
private final String realIpHeaderEmptyValue;
private final RemoteAddressResolver remoteAddressResolver;

public ServletServerRequestWrapperFactory(String realIpHeaderName, String realIpHeaderEmptyValue) {
this.realIpHeaderName = realIpHeaderName;
this.realIpHeaderEmptyValue = realIpHeaderEmptyValue;
public ServletServerRequestWrapperFactory(RemoteAddressResolver remoteAddressResolver) {
this.remoteAddressResolver = Assert.requireNonNull(remoteAddressResolver, "remoteAddressResolver must not be null");
}

public ServletServerRequestWrapper get(final RequestWrapper requestWrapper, final String uri, final String serverName, final int serverPort, final String remoteAddr, final StringBuffer url, final String method, final Map<String, String[]> parameterMap) {
final String endPoint = HostAndPort.toHostAndPortString(serverName, serverPort);
final String remoteAddress = getRemoteAddress(this.realIpHeaderName, this.realIpHeaderEmptyValue, requestWrapper, remoteAddr);

final String remoteAddress = remoteAddressResolver.getRemoteAddress(requestWrapper, remoteAddr);
final String acceptorHost = url != null ? NetworkUtils.getHostFromURL(url.toString()) : null;
final String parameters = getRequestParameter(parameterMap, PARAMETER_EACH_LIMIT, PARAMETER_TOTAL_LIMIT);

return new ServletServerRequestWrapper(requestWrapper, uri, endPoint, remoteAddress, acceptorHost, method, parameters);
}

private static String getRemoteAddress(final String realIpHeaderName, final String realIpHeaderEmptyValue, final RequestWrapper requestWrapper, final String remoteAddr) {
if (!StringUtils.hasLength(realIpHeaderName)) {
return remoteAddr;
}
final String realIp = requestWrapper.getHeader(realIpHeaderName);
if (StringUtils.isEmpty(realIp)) {
return remoteAddr;
}

if (realIpHeaderEmptyValue != null && realIpHeaderEmptyValue.equalsIgnoreCase(realIp)) {
return remoteAddr;
}

final int firstIndex = realIp.indexOf(',');
if (firstIndex == -1) {
return realIp;
} else {
return realIp.substring(0, firstIndex);
}
}

private static String getRequestParameter(final Map<String, String[]> parameterMap, final int eachLimit, final int totalLimit) {
final StringBuilder params = new StringBuilder(64);
Expand Down
Expand Up @@ -5,7 +5,7 @@
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
Expand Down Expand Up @@ -35,7 +35,8 @@ public class ServletServerRequestWrapperFactoryTest {

@Test
public void getRemoteAddress0() throws Exception {
final ServletServerRequestWrapperFactory factory = new ServletServerRequestWrapperFactory("x-forwarded-for", "unknown");
final RemoteAddressResolver remoteAddressResolver = RemoteAddressResolverFactory.newRemoteAddressResolver("x-forwarded-for", "unknown");
final ServletServerRequestWrapperFactory factory = new ServletServerRequestWrapperFactory(remoteAddressResolver);
final RequestWrapper requestWrapper = mock(RequestWrapper.class);
when(requestWrapper.getHeader(X_FORWARDED_FOR)).thenReturn("127.0.0.1");

Expand All @@ -47,7 +48,8 @@ public void getRemoteAddress0() throws Exception {

@Test
public void getRemoteAddress1() throws Exception {
final ServletServerRequestWrapperFactory factory = new ServletServerRequestWrapperFactory("x-forwarded-for", "unknown");
final RemoteAddressResolver remoteAddressResolver = RemoteAddressResolverFactory.newRemoteAddressResolver("x-forwarded-for", "unknown");
final ServletServerRequestWrapperFactory factory = new ServletServerRequestWrapperFactory(remoteAddressResolver);
final RequestWrapper requestWrapper = mock(RequestWrapper.class);
when(requestWrapper.getHeader(X_FORWARDED_FOR)).thenReturn("127.0.0.1, proxy1, proxy2");

Expand All @@ -59,7 +61,8 @@ public void getRemoteAddress1() throws Exception {

@Test
public void getRemoteAddress2() throws Exception {
final ServletServerRequestWrapperFactory factory = new ServletServerRequestWrapperFactory("x-forwarded-for", "unknown");
final RemoteAddressResolver remoteAddressResolver = RemoteAddressResolverFactory.newRemoteAddressResolver("x-forwarded-for", "unknown");
final ServletServerRequestWrapperFactory factory = new ServletServerRequestWrapperFactory(remoteAddressResolver);
final RequestWrapper requestWrapper = mock(RequestWrapper.class);

final String remoteAddr = "127.0.0.2";
Expand Down

0 comments on commit 851e25b

Please sign in to comment.