Skip to content

Commit

Permalink
add https url to url list
Browse files Browse the repository at this point in the history
fixes:#3649
  • Loading branch information
AmilaSamith committed Oct 29, 2024
1 parent 018598e commit 6468edb
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 @@ -43,10 +43,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 @@ -58,7 +63,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 @@ -115,8 +120,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 @@ -196,7 +203,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 @@ -244,59 +253,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;
}

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

if (!serverContext.isEmpty()) {
return serverContext;
}
String portValue;
String protocol;
private List<String> getServerContext(AxisConfiguration configuration) {

TransportInDescription transportInDescription = configuration.getTransportIn("http");
if (Objects.isNull(transportInDescription)) {
transportInDescription = configuration.getTransportIn("https");
}
// 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()
));

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

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 6468edb

Please sign in to comment.