/**
 * Copyright (C) 2015 Red Hat, Inc.
 *
 * 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 io.fabric8.openshift.client;

import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.ConfigBuilder;
import io.fabric8.kubernetes.client.internal.Utils;
import io.sundr.builder.annotations.Buildable;
import io.sundr.builder.annotations.BuildableReference;

public class OpenshiftConfig extends Config {

  public static final String KUBERNETES_OAPI_VERSION_SYSTEM_PROPERTY = "kubernetes.oapi.version";

  private String oapiVersion = "v1";
  private String openShiftUrl;
  private Config kubernetesConfig;

  public OpenshiftConfig() {
    this(new ConfigBuilder().build());
  }

  public OpenshiftConfig(Config kubernetesConfig) {
    this(kubernetesConfig, "v1", null);
  }

  @Buildable(builderPackage = "io.fabric8.kubernetes.api.builder",
  refs = {@BuildableReference(Config.class)}
  )
  public OpenshiftConfig(Config kubernetesConfig, String oapiVersion, String openShiftUrl) {
    super(kubernetesConfig.isTrustCerts(),
      kubernetesConfig.getMasterUrl(), kubernetesConfig.getApiVersion(), kubernetesConfig.getEnabledProtocols(),
      kubernetesConfig.getCaCertFile(), kubernetesConfig.getCaCertData(),
      kubernetesConfig.getClientCertFile(), kubernetesConfig.getClientCertData(),
      kubernetesConfig.getClientKeyFile(), kubernetesConfig.getClientKeyData(),
      kubernetesConfig.getClientKeyAlgo(), kubernetesConfig.getClientKeyPassphrase(),
      kubernetesConfig.getUsername(), kubernetesConfig.getPassword(), kubernetesConfig.getOauthToken());
    this.kubernetesConfig=kubernetesConfig;
    this.oapiVersion = Utils.getSystemPropertyOrEnvVar(KUBERNETES_OAPI_VERSION_SYSTEM_PROPERTY, oapiVersion);
    this.openShiftUrl = openShiftUrl;

    if (this.openShiftUrl == null) {
      this.openShiftUrl = getMasterUrl() + "oapi/" + this.oapiVersion + "/";
    }
  }

  public Config getKubernetesConfig() {
    return this.kubernetesConfig;
  }

  public String getOapiVersion() {
    return oapiVersion;
  }

  public String getOpenShiftUrl() {
    return openShiftUrl;
  }
}

@@ -733,16 +733,79 @@
 uilder;%0A
+import io.fabric8.kubernetes.client.KubernetesClientException;%0A
 import i
@@ -952,16 +952,77 @@
 rence;%0A%0A
+import java.net.MalformedURLException;%0Aimport java.net.URL;%0A%0A
 public c
@@ -2451,24 +2451,38 @@
  == null) %7B%0A
+      try %7B%0A  
       this.o
@@ -2495,16 +2495,32 @@
 ftUrl = 
+new URL(new URL(
 getMaste
@@ -2525,20 +2525,21 @@
 terUrl()
- +
+),
  %22
+/
 oapi/%22 +
@@ -2561,17 +2561,140 @@
 on + %22/%22
-;
+).toString();%0A      %7D catch (MalformedURLException e) %7B%0A        throw KubernetesClientException.launderThrowable(e);%0A      %7D
 %0A    %7D%0A 

Fix openshift url, in cases where the openshift client is created via adapt().