/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.skywalking.apm.plugin.httpClient.v4;

import java.lang.reflect.Method;
import java.net.URL;
import org.apache.http.*;
import org.apache.skywalking.apm.agent.core.context.*;
import org.apache.skywalking.apm.agent.core.context.tag.Tags;
import org.apache.skywalking.apm.agent.core.context.trace.*;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.*;
import org.apache.skywalking.apm.network.trace.component.ComponentsDefine;

public class HttpClientExecuteInterceptor implements InstanceMethodsAroundInterceptor {

    @Override public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments,
        Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
        if (allArguments[0] == null || allArguments[1] == null) {
            // illegal args, can't trace. ignore.
            return;
        }
        final HttpHost httpHost = (HttpHost)allArguments[0];
        HttpRequest httpRequest = (HttpRequest)allArguments[1];
        final ContextCarrier contextCarrier = new ContextCarrier();

        String remotePeer = httpHost.getHostName() + ":" + (httpHost.getPort() > 0 ? httpHost.getPort() :
            "https".equals(httpHost.getSchemeName().toLowerCase()) ? 443 : 80);

        String uri = httpRequest.getRequestLine().getUri();
        String operationName = uri.startsWith("http") ? new URL(uri).getPath() : uri;
        AbstractSpan span = ContextManager.createExitSpan(operationName, contextCarrier, remotePeer);

        span.setComponent(ComponentsDefine.HTTPCLIENT);
        Tags.URL.set(span, uri);
        Tags.HTTP.METHOD.set(span, httpRequest.getRequestLine().getMethod());
        SpanLayer.asHttp(span);

        CarrierItem next = contextCarrier.items();
        while (next.hasNext()) {
            next = next.next();
            httpRequest.setHeader(next.getHeadKey(), next.getHeadValue());
        }
    }

    @Override public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments,
        Class<?>[] argumentsTypes, Object ret) throws Throwable {
        if (allArguments[0] == null || allArguments[1] == null) {
            return ret;
        }

        if (ret != null) {
            HttpResponse response = (HttpResponse)ret;
            StatusLine responseStatusLine = response.getStatusLine();
            if (responseStatusLine != null) {
                int statusCode = responseStatusLine.getStatusCode();
                AbstractSpan span = ContextManager.activeSpan();
                if (statusCode >= 400) {
                    span.errorOccurred();
                    Tags.STATUS_CODE.set(span, Integer.toString(statusCode));
                }
            }
        }
        
        ContextManager.stopSpan();
        return ret;
    }

    @Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
        Class<?>[] argumentsTypes, Throwable t) {
        AbstractSpan activeSpan = ContextManager.activeSpan();
        activeSpan.errorOccurred();
        activeSpan.log(t);
    }
}

@@ -909,289 +909,883 @@
 net.
-URL;%0Aimport org.apache.http.*;%0Aimport org.apache.skywalking.apm.agent.core.context.*;%0Aimport org.apache.skywalking.apm.agent.core.context.tag.Tags;%0Aimport org.apache.skywalking.apm.agent.core.context.trace.*;%0Aimport org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.*
+MalformedURLException;%0Aimport java.net.URL;%0Aimport org.apache.http.HttpHost;%0Aimport org.apache.http.HttpRequest;%0Aimport org.apache.http.HttpResponse;%0Aimport org.apache.http.StatusLine;%0Aimport org.apache.skywalking.apm.agent.core.context.CarrierItem;%0Aimport org.apache.skywalking.apm.agent.core.context.ContextCarrier;%0Aimport org.apache.skywalking.apm.agent.core.context.ContextManager;%0Aimport org.apache.skywalking.apm.agent.core.context.tag.Tags;%0Aimport org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan;%0Aimport org.apache.skywalking.apm.agent.core.context.trace.SpanLayer;%0Aimport org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;%0Aimport org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;%0Aimport org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult
 ;%0Aim
@@ -2717,24 +2717,72 @@
 ).getUri();%0A
+        String requestURI = getRequestURI(uri);%0A
         Stri
@@ -2829,30 +2829,18 @@
 ) ? 
-new URL(uri).getPath()
+requestURI
  : u
@@ -4160,24 +4160,16 @@
       %7D%0A
-        
 %0A       
@@ -4516,11 +4516,231 @@
 );%0A    %7D
+%0A%0A    private String getRequestURI(String uri) throws MalformedURLException %7B%0A        String requestPath = new URL(uri).getPath();%0A        return requestPath != null && requestPath.length() %3E 0 ? requestPath : %22/%22;%0A    %7D
 %0A%7D%0A

Fix operation name is empty issue (#1525)