use of org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException in project carbon-apimgt by wso2.
the class DBRetriever method retrieveAllArtifacts.
@Override
public List<String> retrieveAllArtifacts(String label, String tenantDomain) throws ArtifactSynchronizerException {
List<String> gatewayRuntimeArtifactsArray = new ArrayList<>();
try {
String endcodedgatewayLabel = URLEncoder.encode(label, APIConstants.DigestAuthConstants.CHARSET);
String path = APIConstants.GatewayArtifactSynchronizer.GATEAY_SYNAPSE_ARTIFACTS + "?gatewayLabel=" + endcodedgatewayLabel + "&type=Synapse";
String endpoint = baseURL + path;
try (CloseableHttpResponse httpResponse = invokeService(endpoint, tenantDomain)) {
JSONArray jsonArray = retrieveArtifact(httpResponse);
if (jsonArray != null) {
for (int i = 0; i < jsonArray.length(); i++) {
gatewayRuntimeArtifactsArray.add(jsonArray.getString(i));
}
}
}
return gatewayRuntimeArtifactsArray;
} catch (IOException e) {
String msg = "Error while executing the http client";
log.error(msg, e);
throw new ArtifactSynchronizerException(msg, e);
}
}
use of org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException in project carbon-apimgt by wso2.
the class DBRetriever method retrieveArtifact.
@Override
public String retrieveArtifact(String apiId, String gatewayLabel) throws ArtifactSynchronizerException {
String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
try {
Thread.sleep(gatewayArtifactSynchronizerProperties.getEventWaitingTime());
} catch (InterruptedException e) {
log.error("Error occurred while waiting to retrieve artifacts from event hub");
}
try {
String encodedGatewayLabel = URLEncoder.encode(gatewayLabel, APIConstants.DigestAuthConstants.CHARSET);
encodedGatewayLabel = encodedGatewayLabel.replace("\\+", "%20");
String path = APIConstants.GatewayArtifactSynchronizer.GATEAY_SYNAPSE_ARTIFACTS + "?apiId=" + apiId + "&gatewayLabel=" + encodedGatewayLabel + "&type=Synapse";
String endpoint = baseURL + path;
try (CloseableHttpResponse httpResponse = invokeService(endpoint, tenantDomain)) {
JSONArray jsonArray = retrieveArtifact(httpResponse);
if (jsonArray != null && jsonArray.length() > 0) {
return jsonArray.getString(0);
}
}
} catch (IOException e) {
String msg = "Error while executing the http client";
log.error(msg, e);
throw new ArtifactSynchronizerException(msg, e);
}
return null;
}
use of org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException in project carbon-apimgt by wso2.
the class DBRetriever method invokeService.
private CloseableHttpResponse invokeService(String endpoint, String tenantDomain) throws IOException, ArtifactSynchronizerException {
HttpGet method = new HttpGet(endpoint);
URL url = new URL(endpoint);
String username = eventHubConfigurationDto.getUsername();
String password = eventHubConfigurationDto.getPassword();
byte[] credentials = Base64.encodeBase64((username + APIConstants.DELEM_COLON + password).getBytes(APIConstants.DigestAuthConstants.CHARSET));
int port = url.getPort();
String protocol = url.getProtocol();
method.setHeader(APIConstants.AUTHORIZATION_HEADER_DEFAULT, APIConstants.AUTHORIZATION_BASIC + new String(credentials, APIConstants.DigestAuthConstants.CHARSET));
if (tenantDomain != null) {
method.setHeader(APIConstants.HEADER_TENANT, tenantDomain);
}
HttpClient httpClient = APIUtil.getHttpClient(port, protocol);
try {
return APIUtil.executeHTTPRequest(method, httpClient);
} catch (APIManagementException e) {
throw new ArtifactSynchronizerException(e);
}
}
use of org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException in project carbon-apimgt by wso2.
the class DBRetriever method retrieveAttributes.
@Override
public Map<String, String> retrieveAttributes(String apiName, String version, String tenantDomain) throws ArtifactSynchronizerException {
CloseableHttpResponse httpResponse = null;
try {
String endcodedVersion = URLEncoder.encode(version, APIConstants.DigestAuthConstants.CHARSET);
String path = APIConstants.GatewayArtifactSynchronizer.SYNAPSE_ATTRIBUTES + "?apiName=" + apiName + "&tenantDomain=" + tenantDomain + "&version=" + endcodedVersion;
String endpoint = baseURL + path;
httpResponse = invokeService(endpoint, tenantDomain);
String responseString;
if (httpResponse.getEntity() != null) {
responseString = EntityUtils.toString(httpResponse.getEntity(), APIConstants.DigestAuthConstants.CHARSET);
httpResponse.close();
} else {
throw new ArtifactSynchronizerException("HTTP response is empty");
}
Map<String, String> apiAttribute = new HashMap<>();
JSONObject artifactObject = new JSONObject(responseString);
String label = null;
String apiId = null;
try {
apiId = (String) artifactObject.get(APIConstants.GatewayArtifactSynchronizer.API_ID);
String labelsStr = artifactObject.get(APIConstants.GatewayArtifactSynchronizer.LABELS).toString();
Set<String> labelsSet = new Gson().fromJson(labelsStr, new TypeToken<HashSet<String>>() {
}.getType());
Set<String> gatewaySubscribedLabel = gatewayArtifactSynchronizerProperties.getGatewayLabels();
if (!labelsSet.isEmpty() || !gatewaySubscribedLabel.isEmpty()) {
labelsSet.retainAll(gatewaySubscribedLabel);
if (!labelsSet.isEmpty()) {
label = labelsSet.iterator().next();
}
}
} catch (ClassCastException e) {
log.error("Unexpected response received from the storage." + e.getMessage());
}
apiAttribute.put(APIConstants.GatewayArtifactSynchronizer.API_ID, apiId);
apiAttribute.put(APIConstants.GatewayArtifactSynchronizer.LABEL, label);
return apiAttribute;
} catch (IOException e) {
String msg = "Error while executing the http client";
log.error(msg, e);
throw new ArtifactSynchronizerException(msg, e);
}
}
use of org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException in project carbon-apimgt by wso2.
the class EndpointCertificateDeployer method invokeService.
private CloseableHttpResponse invokeService(String endpoint, String tenantDomain) throws IOException, ArtifactSynchronizerException {
HttpGet method = new HttpGet(endpoint);
URL url = new URL(endpoint);
String username = eventHubConfigurationDto.getUsername();
String password = eventHubConfigurationDto.getPassword();
byte[] credentials = Base64.encodeBase64((username + APIConstants.DELEM_COLON + password).getBytes(APIConstants.DigestAuthConstants.CHARSET));
int port = url.getPort();
String protocol = url.getProtocol();
method.setHeader(APIConstants.AUTHORIZATION_HEADER_DEFAULT, APIConstants.AUTHORIZATION_BASIC + new String(credentials, APIConstants.DigestAuthConstants.CHARSET));
if (tenantDomain != null) {
method.setHeader(APIConstants.HEADER_TENANT, tenantDomain);
}
HttpClient httpClient = APIUtil.getHttpClient(port, protocol);
try {
return APIUtil.executeHTTPRequest(method, httpClient);
} catch (APIManagementException e) {
throw new ArtifactSynchronizerException(e);
}
}
Aggregations