/*
 * Copyright 2019 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.plugin.reactor.netty.interceptor;

import com.navercorp.pinpoint.bootstrap.plugin.request.RequestAdaptor;
import com.navercorp.pinpoint.bootstrap.util.NetworkUtils;
import com.navercorp.pinpoint.common.plugin.util.HostAndPort;

import io.netty.handler.codec.http.HttpHeaders;
import reactor.netty.http.server.HttpServerRequest;

import java.net.InetAddress;
import java.net.InetSocketAddress;

/**
 * @author jaehong.kim
 */
public class HttpRequestAdaptor implements RequestAdaptor<HttpServerRequest> {

    @Override
    public String getHeader(HttpServerRequest request, String name) {
        try {
            final HttpHeaders headers = request.requestHeaders();
            if (headers != null) {
                return headers.get(name);
            }
        } catch (Exception ignored) {
        }
        return null;
    }

    @Override
    public String getRpcName(HttpServerRequest request) {
        try {
            return request.uri();
        } catch (Exception ignored) {
        }

        return null;
    }

    @Override
    public String getEndPoint(HttpServerRequest request) {
        try {
            return request.uri();
        } catch (Exception ignored) {
        }

        return "Unknown";
    }

    @Override
    public String getRemoteAddress(HttpServerRequest request) {
        final InetSocketAddress inetSocketAddress = request.remoteAddress();
        final InetAddress remoteAddress = inetSocketAddress.getAddress();
        if (remoteAddress != null) {
            return HostAndPort.toHostAndPortString(remoteAddress.getHostAddress(), inetSocketAddress.getPort());
        }

        return null;
    }

    @Override
    public String getAcceptorHost(HttpServerRequest request) {
        try {
            return NetworkUtils.getHostFromURL(request.uri());
        } catch (Exception ignored) {
        }

        return null;
    }
}
@@ -731,67 +731,8 @@
 or;%0A
-import com.navercorp.pinpoint.bootstrap.util.NetworkUtils;%0A
 impo
@@ -1558,33 +1558,32 @@
 ed) %7B%0A        %7D%0A
-%0A
         return n
@@ -1679,85 +1679,112 @@
     
-try %7B%0A            return request.uri();%0A        %7D catch (Exception ignored) %7B
+final String host = getHost(request.hostAddress());%0A        if (host != null) %7B%0A            return host;
 %0A   
@@ -1782,33 +1782,32 @@
 host;%0A        %7D%0A
-%0A
         return %22
@@ -1913,77 +1913,320 @@
     
-final InetSocketAddress inetSocketAddress = request.remoteAddress();%0A
+return getHost(request.remoteAddress());%0A    %7D%0A%0A    @Override%0A    public String getAcceptorHost(HttpServerRequest request) %7B%0A        return getHost(request.hostAddress());%0A    %7D%0A%0A    private String getHost(InetSocketAddress inetSocketAddress) %7B%0A        if (inetSocketAddress != null) %7B%0A            try %7B%0A        
     
@@ -2247,23 +2247,17 @@
 Address 
-remoteA
+a
 ddress =
@@ -2301,19 +2301,21 @@
     
-if (remoteA
+        if (a
 ddre
@@ -2320,32 +2320,40 @@
 ress != null) %7B%0A
+        
             retu
@@ -2387,23 +2387,17 @@
 tString(
-remoteA
+a
 ddress.g
@@ -2447,35 +2447,32 @@
 ));%0A        
-%7D%0A%0A
         
 return null;
@@ -2463,182 +2463,14 @@
     
-return null;%0A    %7D%0A%0A    @Override%0A    public String getAcceptorHost(HttpServerRequest request) %7B%0A        try %7B%0A            return NetworkUtils.getHostFromURL(request.uri());%0A
+%7D%0A    
     
@@ -2507,26 +2507,39 @@
 ) %7B%0A        
+    %7D%0A        
 %7D
-%0A
 %0A        ret

Fix endpoint of reactor-netty plugin