Skip to content

Commit

Permalink
Merge pull request #48 from gullerya/yuri-fixing-proxy-resolve
Browse files Browse the repository at this point in the history
defect #663077: fixing the logic of proxy resolution to work on host only and not on the full URLs
  • Loading branch information
radislavB authored Jun 28, 2018
2 parents a458c72 + f7559b2 commit d69b215
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 100 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2017 EntIT Software LLC, a Micro Focus company, L.P.
* 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.hp.octane.integrations.exceptions;


public class OctaneSDKGeneralException extends RuntimeException {

public OctaneSDKGeneralException(Throwable throwable) {
super(throwable);
}

public OctaneSDKGeneralException(String message) {
super(message);
}

public OctaneSDKGeneralException(String message, Throwable throwable) {
super(message, throwable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.hp.octane.integrations.dto.connectivity.HttpMethod;
import com.hp.octane.integrations.dto.connectivity.OctaneRequest;
import com.hp.octane.integrations.dto.connectivity.OctaneResponse;
import com.hp.octane.integrations.util.CIPluginSDKUtils;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
Expand Down Expand Up @@ -118,7 +119,8 @@ public OctaneResponse validateConfiguration(OctaneConfiguration configuration) t
throw new IllegalArgumentException("configuration " + configuration + " is not valid");
}

CIProxyConfiguration proxyConfiguration = pluginServices.getProxyConfiguration(configuration.getUrl());
String octaneHost = CIPluginSDKUtils.extractHostFromURL(configuration.getUrl());
CIProxyConfiguration proxyConfiguration = pluginServices.getProxyConfiguration(octaneHost);
RestClient restClientImpl = restService.createClient(proxyConfiguration);
OctaneRequest request = dtoFactory.newDTO(OctaneRequest.class)
.setMethod(HttpMethod.GET)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,8 @@ private HttpClientContext createHttpContext(String requestUrl, boolean isLoginRe
}

// configure proxy if needed
CIProxyConfiguration proxyConfiguration = pluginServices.getProxyConfiguration(requestUrl);
String requestHost = CIPluginSDKUtils.extractHostFromURL(requestUrl);
CIProxyConfiguration proxyConfiguration = pluginServices.getProxyConfiguration(requestHost);
if (proxyConfiguration != null) {
logger.debug("proxy will be used with the following setup: " + proxyConfiguration);
HttpHost proxyHost = new HttpHost(proxyConfiguration.getHost(), proxyConfiguration.getPort());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
package com.hp.octane.integrations.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hp.octane.integrations.exceptions.OctaneSDKGeneralException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.LinkedList;
Expand All @@ -47,15 +50,35 @@ public static void doWait(long period) {
}
}

public static boolean isNonProxyHost(String targetHost, String nonProxyHostsStr) {
boolean noProxyHost = false;
for (Pattern pattern : getNoProxyHostPatterns(nonProxyHostsStr)) {
if (pattern.matcher(targetHost).find()) {
noProxyHost = true;
break;
}
// [YG] should be removed from here and implemented with the relevant plugin, as this implementation is specific to some plugin (Bamboo or TeamCity, i presume)
// @Deprecated
// public static boolean isNonProxyHost(String targetHost, String nonProxyHostsStr) {
// boolean noProxyHost = false;
// List<Pattern> noProxyHosts = new LinkedList<>();
// if (nonProxyHostsStr != null && !nonProxyHostsStr.isEmpty()) {
// String[] hosts = nonProxyHostsStr.split("[ \t\n,|]+");
// for (String host : hosts) {
// if (!host.isEmpty()) {
// noProxyHosts.add(Pattern.compile(host.replace(".", "\\.").replace("*", ".*")));
// }
// }
// }
// for (Pattern pattern : noProxyHosts) {
// if (pattern.matcher(targetHost).find()) {
// noProxyHost = true;
// break;
// }
// }
// return noProxyHost;
// }

public static String extractHostFromURL(String input) {
try {
URL url = new URL(input);
return url.getHost();
} catch (MalformedURLException murle) {
throw new OctaneSDKGeneralException("failed to extract host from URL '" + input + "'", murle);
}
return noProxyHost;
}

public static String urlEncodePathParam(String input) {
Expand All @@ -77,18 +100,5 @@ public static String urlEncodeQueryParam(String input) {
}
return result;
}

private static List<Pattern> getNoProxyHostPatterns(String noProxyHost) {
List<Pattern> result = new LinkedList<>();
if (noProxyHost != null && !noProxyHost.isEmpty()) {
String[] hosts = noProxyHost.split("[ \t\n,|]+");
for (String host : hosts) {
if (!host.isEmpty()) {
result.add(Pattern.compile(host.replace(".", "\\.").replace("*", ".*")));
}
}
}
return result;
}
}

This file was deleted.

0 comments on commit d69b215

Please sign in to comment.