Skip to content

Commit

Permalink
Merge pull request #3750 from AmilaSamith/httpsURL-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
AmilaSamith authored Dec 18, 2024
2 parents 19347b0 + 6468edb commit d66c807
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,15 @@
import java.net.MalformedURLException;
import java.net.SocketException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

Expand All @@ -61,7 +66,7 @@ public class ApiResource extends APIResource {

private static final String API_NAME = "apiName";
private static final String URL_VERSION_TYPE = "url";
private String serverContext = ""; // base server url
private List<String> serverContext = new ArrayList<>(); // base server urls

public ApiResource(String urlTemplate) {
super(urlTemplate);
Expand Down Expand Up @@ -128,8 +133,10 @@ private void setResponseBody(Collection<API> resultList, MessageContext messageC
for (API api: resultList) {
JSONObject apiObject = new JSONObject();
String apiUrl = getApiUrl(api, messageContext);
JSONArray urlArray = new JSONArray(apiUrl.split(","));
apiObject.put(Constants.NAME, api.getName());
apiObject.put(Constants.URL, apiUrl);
apiObject.put(Constants.URL, urlArray.get(0));
apiObject.put(Constants.URL_LIST, urlArray);
apiObject.put(Constants.TRACING,
api.getAspectConfiguration().isTracingEnabled() ? Constants.ENABLED : Constants.DISABLED);
jsonBody.getJSONArray(Constants.LIST).put(apiObject);
Expand Down Expand Up @@ -209,7 +216,9 @@ private JSONObject convertApiToJsonObject(API api, MessageContext messageContext

apiObject.put(Constants.NAME, api.getName());
String apiUrl = getApiUrl(api, messageContext);
apiObject.put(Constants.URL, apiUrl);
JSONArray urlArray = new JSONArray(apiUrl.split(","));
apiObject.put(Constants.URL, urlArray.get(0));
apiObject.put(Constants.URL_LIST, urlArray);

String version = api.getVersion().equals("") ? "N/A" : api.getVersion();

Expand Down Expand Up @@ -257,59 +266,61 @@ private JSONObject convertApiToJsonObject(API api, MessageContext messageContext
private String getApiUrl(API api, MessageContext msgCtx) {

org.apache.axis2.context.MessageContext axisMsgCtx = ((Axis2MessageContext) msgCtx).getAxis2MessageContext();
String serverUrl = getServerContext(axisMsgCtx.getConfigurationContext().getAxisConfiguration());
String versionUrl = "";
List<String> serverUrls = serverContext.isEmpty() ? getServerContext(axisMsgCtx.getConfigurationContext().getAxisConfiguration()) : serverContext;

// Build version specific context
String versionUrl;
if (URL_VERSION_TYPE.equals(api.getVersionStrategy().getVersionType())) {
versionUrl = "/" + api.getVersion();
} else {
versionUrl = "";
}
return serverUrl.equals("err") ? api.getContext() : serverUrl + api.getContext() + versionUrl;

return serverUrls.isEmpty() ? api.getContext() : serverUrls.stream()
.map(url -> url + api.getContext() + versionUrl)
.collect(Collectors.joining(","));
}

private String getServerContext(AxisConfiguration configuration) {
private List<String> getServerContext(AxisConfiguration configuration) {

if (!serverContext.isEmpty()) {
return serverContext;
}
String portValue;
String protocol;
// Capture protocol list
List<String> protocols = Arrays.asList("http", "https");
Map<String, String> protocolPortMap = protocols.stream()
.map(configuration::getTransportIn)
.filter(Objects::nonNull)
.collect(Collectors.toMap(
TransportInDescription::getName,
transportInDescription -> (String) transportInDescription.getParameter("port").getValue()
));

TransportInDescription transportInDescription = configuration.getTransportIn("http");
if (Objects.isNull(transportInDescription)) {
transportInDescription = configuration.getTransportIn("https");
if (protocolPortMap.isEmpty()) {
return Collections.emptyList();
}

if (Objects.nonNull(transportInDescription)) {
protocol = transportInDescription.getName();
portValue = (String) transportInDescription.getParameter("port").getValue();
} else {
return "err";
}

String host;

Parameter hostParam = configuration.getParameter("hostname");

if (Objects.nonNull(hostParam)) {
host = (String)hostParam.getValue();
} else {
try {
host = NetworkUtils.getLocalHostname();
} catch (SocketException e) {
host = "localhost";
}
}
String url;
try {
int port = Integer.parseInt(portValue);
if (("http".equals(protocol) && port == 80) || ("https".equals(protocol) && port == 443)) {
port = -1;
}
URL serverURL = new URL(protocol, host, port, "");
url = serverURL.toExternalForm();
} catch (MalformedURLException e) {
url = "err";
}
this.serverContext = url;
return url;
String host = Optional.ofNullable(configuration.getParameter("hostname"))
.map(Parameter::getValue)
.map(String.class::cast)
.orElseGet(() -> {
try {
return NetworkUtils.getLocalHostname();
} catch (SocketException e) {
return "localhost";
}
});

// Build URLs
serverContext = protocolPortMap.entrySet().stream()
.map(entry -> {
String protocol = entry.getKey();
int port = Integer.parseInt(entry.getValue());
try {
return new URL(protocol, host, port, "").toExternalForm();
} catch (MalformedURLException e) {
return null;
}
})
.filter(Objects::nonNull) // Filter out any null URLs
.collect(Collectors.toList());
return serverContext;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public class Constants {
public static final String NAME = "name";
public static final String STATUS = "status";
public static final String URL = "url";
public static final String URL_LIST= "urlList";
public static final String VERSION = "version";
public static final String CONTAINER = "container";
public static final String TYPE = "type";
Expand Down

0 comments on commit d66c807

Please sign in to comment.