use of org.wso2.carbon.apimgt.api.model.URITemplate in project carbon-apimgt by wso2.
the class APIKeyValidationService method getAPIProductURITemplates.
public ArrayList<URITemplate> getAPIProductURITemplates(String context, String version) throws APIManagementException {
Timer timer6 = MetricManager.timer(org.wso2.carbon.metrics.manager.Level.INFO, MetricManager.name(APIConstants.METRICS_PREFIX, this.getClass().getSimpleName(), "GET_URI_TEMPLATE"));
Timer.Context timerContext6 = timer6.start();
if (log.isDebugEnabled()) {
log.debug("getAllURITemplates request from gateway to keymanager: requestTime=" + new SimpleDateFormat("[yyyy.MM.dd HH:mm:ss,SSS zzz]").format(new Date()) + " ,for:" + context);
}
ArrayList<URITemplate> templates = getTemplates(context, version);
if (log.isDebugEnabled()) {
log.debug("getAllURITemplates response from keyManager to gateway for:" + context + " at " + new SimpleDateFormat("[yyyy.MM.dd HH:mm:ss,SSS zzz]").format(new Date()));
}
timerContext6.stop();
return templates;
}
use of org.wso2.carbon.apimgt.api.model.URITemplate in project carbon-apimgt by wso2.
the class APIKeyValidationService method getAllURITemplates.
/**
* Return the URI Templates for an API
*
* @param context Requested context
* @param version API Version
* @return APIKeyValidationInfoDTO with authorization info and tier info if authorized. If it is not
* authorized, tier information will be <pre>null</pre>
*/
public ArrayList<URITemplate> getAllURITemplates(String context, String version) throws APIManagementException {
Timer timer6 = MetricManager.timer(org.wso2.carbon.metrics.manager.Level.INFO, MetricManager.name(APIConstants.METRICS_PREFIX, this.getClass().getSimpleName(), "GET_URI_TEMPLATE"));
Timer.Context timerContext6 = timer6.start();
if (log.isDebugEnabled()) {
log.debug("getAllURITemplates request from gateway to keymanager: requestTime=" + new SimpleDateFormat("[yyyy.MM.dd HH:mm:ss,SSS zzz]").format(new Date()) + " ,for:" + context);
}
ArrayList<URITemplate> templates = getTemplates(context, version);
if (log.isDebugEnabled()) {
log.debug("getAllURITemplates response from keyManager to gateway for:" + context + " at " + new SimpleDateFormat("[yyyy.MM.dd HH:mm:ss,SSS zzz]").format(new Date()));
}
timerContext6.stop();
return templates;
}
use of org.wso2.carbon.apimgt.api.model.URITemplate in project carbon-apimgt by wso2.
the class APIUtil method getAPI.
public static API getAPI(GovernanceArtifact artifact) throws APIManagementException {
API api;
try {
String providerName = artifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER);
String apiName = artifact.getAttribute(APIConstants.API_OVERVIEW_NAME);
String apiVersion = artifact.getAttribute(APIConstants.API_OVERVIEW_VERSION);
APIIdentifier apiIdentifier = new APIIdentifier(providerName, apiName, apiVersion);
api = new API(apiIdentifier);
int apiId = ApiMgtDAO.getInstance().getAPIID(artifact.getId());
if (apiId == -1) {
return null;
}
// set uuid
api.setUUID(artifact.getId());
api.setRating(getAverageRating(apiId));
api.setThumbnailUrl(artifact.getAttribute(APIConstants.API_OVERVIEW_THUMBNAIL_URL));
api.setStatus(getLcStateFromArtifact(artifact));
api.setContext(artifact.getAttribute(APIConstants.API_OVERVIEW_CONTEXT));
api.setVisibility(artifact.getAttribute(APIConstants.API_OVERVIEW_VISIBILITY));
api.setVisibleRoles(artifact.getAttribute(APIConstants.API_OVERVIEW_VISIBLE_ROLES));
api.setVisibleTenants(artifact.getAttribute(APIConstants.API_OVERVIEW_VISIBLE_TENANTS));
api.setTransports(artifact.getAttribute(APIConstants.API_OVERVIEW_TRANSPORTS));
api.setInSequence(artifact.getAttribute(APIConstants.API_OVERVIEW_INSEQUENCE));
api.setOutSequence(artifact.getAttribute(APIConstants.API_OVERVIEW_OUTSEQUENCE));
api.setFaultSequence(artifact.getAttribute(APIConstants.API_OVERVIEW_FAULTSEQUENCE));
api.setDescription(artifact.getAttribute(APIConstants.API_OVERVIEW_DESCRIPTION));
api.setResponseCache(artifact.getAttribute(APIConstants.API_OVERVIEW_RESPONSE_CACHING));
api.setType(artifact.getAttribute(APIConstants.API_OVERVIEW_TYPE));
api.setEnableStore(Boolean.parseBoolean(artifact.getAttribute(APIConstants.API_OVERVIEW_ENABLE_STORE)));
api.setTestKey(artifact.getAttribute(APIConstants.API_OVERVIEW_TESTKEY));
int cacheTimeout = APIConstants.API_RESPONSE_CACHE_TIMEOUT;
try {
cacheTimeout = Integer.parseInt(artifact.getAttribute(APIConstants.API_OVERVIEW_CACHE_TIMEOUT));
} catch (NumberFormatException e) {
// ignore
}
api.setCacheTimeout(cacheTimeout);
String apiLevelTier = ApiMgtDAO.getInstance().getAPILevelTier(apiId);
api.setApiLevelPolicy(apiLevelTier);
Set<Tier> availablePolicy = new HashSet<Tier>();
String[] subscriptionPolicy = ApiMgtDAO.getInstance().getPolicyNames(PolicyConstants.POLICY_LEVEL_SUB, replaceEmailDomainBack(providerName));
List<String> definedPolicyNames = Arrays.asList(subscriptionPolicy);
String policies = artifact.getAttribute(APIConstants.API_OVERVIEW_TIER);
if (policies != null && !"".equals(policies)) {
String[] policyNames = policies.split("\\|\\|");
for (String policyName : policyNames) {
if (definedPolicyNames.contains(policyName) || APIConstants.UNLIMITED_TIER.equals(policyName)) {
Tier p = new Tier(policyName);
availablePolicy.add(p);
} else {
log.warn("Unknown policy: " + policyName + " found on API: " + apiName);
}
}
}
api.addAvailableTiers(availablePolicy);
String tenantDomainName = MultitenantUtils.getTenantDomain(replaceEmailDomainBack(providerName));
api.setMonetizationCategory(getAPIMonetizationCategory(availablePolicy, tenantDomainName));
api.setRedirectURL(artifact.getAttribute(APIConstants.API_OVERVIEW_REDIRECT_URL));
api.setApiOwner(artifact.getAttribute(APIConstants.API_OVERVIEW_OWNER));
api.setAdvertiseOnly(Boolean.parseBoolean(artifact.getAttribute(APIConstants.API_OVERVIEW_ADVERTISE_ONLY)));
api.setEndpointConfig(artifact.getAttribute(APIConstants.API_OVERVIEW_ENDPOINT_CONFIG));
api.setSubscriptionAvailability(artifact.getAttribute(APIConstants.API_OVERVIEW_SUBSCRIPTION_AVAILABILITY));
api.setSubscriptionAvailableTenants(artifact.getAttribute(APIConstants.API_OVERVIEW_SUBSCRIPTION_AVAILABLE_TENANTS));
api.setAsDefaultVersion(Boolean.parseBoolean(artifact.getAttribute(APIConstants.API_OVERVIEW_IS_DEFAULT_VERSION)));
api.setImplementation(artifact.getAttribute(APIConstants.PROTOTYPE_OVERVIEW_IMPLEMENTATION));
api.setTechnicalOwner(artifact.getAttribute(APIConstants.API_OVERVIEW_TEC_OWNER));
api.setTechnicalOwnerEmail(artifact.getAttribute(APIConstants.API_OVERVIEW_TEC_OWNER_EMAIL));
api.setBusinessOwner(artifact.getAttribute(APIConstants.API_OVERVIEW_BUSS_OWNER));
api.setBusinessOwnerEmail(artifact.getAttribute(APIConstants.API_OVERVIEW_BUSS_OWNER_EMAIL));
ArrayList<URITemplate> urlPatternsList;
urlPatternsList = ApiMgtDAO.getInstance().getAllURITemplates(api.getContext(), api.getId().getVersion());
Set<URITemplate> uriTemplates = new HashSet<URITemplate>(urlPatternsList);
for (URITemplate uriTemplate : uriTemplates) {
uriTemplate.setResourceURI(api.getUrl());
uriTemplate.setResourceSandboxURI(api.getSandboxUrl());
}
api.setUriTemplates(uriTemplates);
String environments = artifact.getAttribute(APIConstants.API_OVERVIEW_ENVIRONMENTS);
api.setEnvironments(extractEnvironmentsForAPI(environments));
api.setCorsConfiguration(getCorsConfigurationFromArtifact(artifact));
api.setAuthorizationHeader(artifact.getAttribute(APIConstants.API_OVERVIEW_AUTHORIZATION_HEADER));
api.setApiSecurity(artifact.getAttribute(APIConstants.API_OVERVIEW_API_SECURITY));
// non empty URLs to API object
try {
api.setEnvironmentList(extractEnvironmentListForAPI(artifact.getAttribute(APIConstants.API_OVERVIEW_ENDPOINT_CONFIG)));
} catch (ParseException e) {
String msg = "Failed to parse endpoint config JSON of API: " + apiName + " " + apiVersion;
log.error(msg, e);
throw new APIManagementException(msg, e);
} catch (ClassCastException e) {
String msg = "Invalid endpoint config JSON found in API: " + apiName + " " + apiVersion;
log.error(msg, e);
throw new APIManagementException(msg, e);
}
} catch (GovernanceException e) {
String msg = "Failed to get API from artifact ";
throw new APIManagementException(msg, e);
}
return api;
}
use of org.wso2.carbon.apimgt.api.model.URITemplate in project carbon-apimgt by wso2.
the class APIUtil method updateAPIProductDependencies.
/**
* This method used to retrieve the api resource dependencies
*
* @param api api object
* @param registry registry
* @throws APIManagementException
*/
public static void updateAPIProductDependencies(API api, Registry registry) throws APIManagementException {
for (URITemplate uriTemplate : api.getUriTemplates()) {
Set<APIProductIdentifier> usedByProducts = uriTemplate.retrieveUsedByProducts();
for (APIProductIdentifier usedByProduct : usedByProducts) {
// TODO : removed registry call until find a proper fix
String apiProductPath = APIUtil.getAPIProductPath(usedByProduct);
usedByProduct.setUUID(apiProductPath);
}
}
}
use of org.wso2.carbon.apimgt.api.model.URITemplate in project carbon-apimgt by wso2.
the class APIUtil method createAPIArtifactContent.
/**
* Create Governance artifact from given attributes
*
* @param artifact initial governance artifact
* @param api API object with the attributes value
* @return GenericArtifact
* @throws org.wso2.carbon.apimgt.api.APIManagementException if failed to create API
*/
public static GenericArtifact createAPIArtifactContent(GenericArtifact artifact, API api) throws APIManagementException {
try {
String apiStatus = api.getStatus();
artifact.setAttribute(APIConstants.API_OVERVIEW_NAME, api.getId().getApiName());
artifact.setAttribute(APIConstants.API_OVERVIEW_VERSION, api.getId().getVersion());
artifact.setAttribute(APIConstants.API_OVERVIEW_VERSION_TIMESTAMP, api.getVersionTimestamp());
artifact.setAttribute(APIConstants.API_OVERVIEW_CONTEXT, api.getContext());
artifact.setAttribute(APIConstants.API_OVERVIEW_PROVIDER, api.getId().getProviderName());
artifact.setAttribute(APIConstants.API_OVERVIEW_DESCRIPTION, api.getDescription());
artifact.setAttribute(APIConstants.API_OVERVIEW_WSDL, api.getWsdlUrl());
artifact.setAttribute(APIConstants.API_OVERVIEW_WADL, api.getWadlUrl());
artifact.setAttribute(APIConstants.API_OVERVIEW_THUMBNAIL_URL, api.getThumbnailUrl());
artifact.setAttribute(APIConstants.API_OVERVIEW_STATUS, apiStatus);
artifact.setAttribute(APIConstants.API_OVERVIEW_TEC_OWNER, api.getTechnicalOwner());
artifact.setAttribute(APIConstants.API_OVERVIEW_TEC_OWNER_EMAIL, api.getTechnicalOwnerEmail());
artifact.setAttribute(APIConstants.API_OVERVIEW_BUSS_OWNER, api.getBusinessOwner());
artifact.setAttribute(APIConstants.API_OVERVIEW_BUSS_OWNER_EMAIL, api.getBusinessOwnerEmail());
artifact.setAttribute(APIConstants.API_OVERVIEW_VISIBILITY, api.getVisibility());
artifact.setAttribute(APIConstants.API_OVERVIEW_VISIBLE_ROLES, api.getVisibleRoles());
artifact.setAttribute(APIConstants.API_OVERVIEW_VISIBLE_TENANTS, api.getVisibleTenants());
artifact.setAttribute(APIConstants.API_OVERVIEW_ENDPOINT_SECURED, Boolean.toString(api.isEndpointSecured()));
artifact.setAttribute(APIConstants.API_OVERVIEW_ENDPOINT_AUTH_DIGEST, Boolean.toString(api.isEndpointAuthDigest()));
artifact.setAttribute(APIConstants.API_OVERVIEW_ENDPOINT_USERNAME, api.getEndpointUTUsername());
artifact.setAttribute(APIConstants.API_OVERVIEW_ENDPOINT_PASSWORD, api.getEndpointUTPassword());
artifact.setAttribute(APIConstants.API_OVERVIEW_TRANSPORTS, api.getTransports());
artifact.setAttribute(APIConstants.API_OVERVIEW_INSEQUENCE, api.getInSequence());
artifact.setAttribute(APIConstants.API_OVERVIEW_OUTSEQUENCE, api.getOutSequence());
artifact.setAttribute(APIConstants.API_OVERVIEW_FAULTSEQUENCE, api.getFaultSequence());
artifact.setAttribute(APIConstants.API_OVERVIEW_RESPONSE_CACHING, api.getResponseCache());
artifact.setAttribute(APIConstants.API_OVERVIEW_CACHE_TIMEOUT, Integer.toString(api.getCacheTimeout()));
artifact.setAttribute(APIConstants.API_OVERVIEW_REDIRECT_URL, api.getRedirectURL());
artifact.setAttribute(APIConstants.API_OVERVIEW_EXTERNAL_PRODUCTION_ENDPOINT, api.getApiExternalProductionEndpoint());
artifact.setAttribute(APIConstants.API_OVERVIEW_EXTERNAL_SANDBOX_ENDPOINT, api.getApiExternalSandboxEndpoint());
artifact.setAttribute(APIConstants.API_OVERVIEW_OWNER, api.getApiOwner());
artifact.setAttribute(APIConstants.API_OVERVIEW_ADVERTISE_ONLY, Boolean.toString(api.isAdvertiseOnly()));
artifact.setAttribute(APIConstants.API_OVERVIEW_ENDPOINT_CONFIG, api.getEndpointConfig());
artifact.setAttribute(APIConstants.API_OVERVIEW_SUBSCRIPTION_AVAILABILITY, api.getSubscriptionAvailability());
artifact.setAttribute(APIConstants.API_OVERVIEW_SUBSCRIPTION_AVAILABLE_TENANTS, api.getSubscriptionAvailableTenants());
artifact.setAttribute(APIConstants.PROTOTYPE_OVERVIEW_IMPLEMENTATION, api.getImplementation());
artifact.setAttribute(APIConstants.API_PRODUCTION_THROTTLE_MAXTPS, api.getProductionMaxTps());
artifact.setAttribute(APIConstants.API_SANDBOX_THROTTLE_MAXTPS, api.getSandboxMaxTps());
artifact.setAttribute(APIConstants.API_OVERVIEW_AUTHORIZATION_HEADER, api.getAuthorizationHeader());
artifact.setAttribute(APIConstants.API_OVERVIEW_API_SECURITY, api.getApiSecurity());
artifact.setAttribute(APIConstants.API_OVERVIEW_ENABLE_JSON_SCHEMA, Boolean.toString(api.isEnabledSchemaValidation()));
artifact.setAttribute(APIConstants.API_OVERVIEW_ENABLE_STORE, Boolean.toString(api.isEnableStore()));
artifact.setAttribute(APIConstants.API_OVERVIEW_TESTKEY, api.getTestKey());
// Validate if the API has an unsupported context before setting it in the artifact
String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
if (APIConstants.SUPER_TENANT_DOMAIN.equals(tenantDomain)) {
String invalidContext = File.separator + APIConstants.VERSION_PLACEHOLDER;
if (invalidContext.equals(api.getContextTemplate())) {
throw new APIManagementException("API : " + api.getId() + " has an unsupported context : " + api.getContextTemplate());
}
} else {
String invalidContext = APIConstants.TENANT_PREFIX + tenantDomain + File.separator + APIConstants.VERSION_PLACEHOLDER;
if (invalidContext.equals(api.getContextTemplate())) {
throw new APIManagementException("API : " + api.getId() + " has an unsupported context : " + api.getContextTemplate());
}
}
// This is to support the pluggable version strategy.
artifact.setAttribute(APIConstants.API_OVERVIEW_CONTEXT_TEMPLATE, api.getContextTemplate());
artifact.setAttribute(APIConstants.API_OVERVIEW_VERSION_TYPE, "context");
artifact.setAttribute(APIConstants.API_OVERVIEW_TYPE, api.getType());
StringBuilder policyBuilder = new StringBuilder();
for (Tier tier : api.getAvailableTiers()) {
policyBuilder.append(tier.getName());
policyBuilder.append("||");
}
String policies = policyBuilder.toString();
if (!"".equals(policies)) {
policies = policies.substring(0, policies.length() - 2);
artifact.setAttribute(APIConstants.API_OVERVIEW_TIER, policies);
}
StringBuilder tiersBuilder = new StringBuilder();
for (Tier tier : api.getAvailableTiers()) {
tiersBuilder.append(tier.getName());
tiersBuilder.append("||");
}
String tiers = tiersBuilder.toString();
if (!"".equals(tiers)) {
tiers = tiers.substring(0, tiers.length() - 2);
artifact.setAttribute(APIConstants.API_OVERVIEW_TIER, tiers);
} else {
artifact.setAttribute(APIConstants.API_OVERVIEW_TIER, tiers);
}
if (APIConstants.PUBLISHED.equals(apiStatus)) {
artifact.setAttribute(APIConstants.API_OVERVIEW_IS_LATEST, "true");
}
String[] keys = artifact.getAttributeKeys();
for (String key : keys) {
if (key.contains("URITemplate")) {
artifact.removeAttribute(key);
}
}
Set<URITemplate> uriTemplateSet = api.getUriTemplates();
int i = 0;
for (URITemplate uriTemplate : uriTemplateSet) {
artifact.addAttribute(APIConstants.API_URI_PATTERN + i, uriTemplate.getUriTemplate());
artifact.addAttribute(APIConstants.API_URI_HTTP_METHOD + i, uriTemplate.getHTTPVerb());
artifact.addAttribute(APIConstants.API_URI_AUTH_TYPE + i, uriTemplate.getAuthType());
i++;
}
artifact.setAttribute(APIConstants.API_OVERVIEW_ENVIRONMENTS, writeEnvironmentsToArtifact(api));
artifact.setAttribute(APIConstants.API_OVERVIEW_CORS_CONFIGURATION, APIUtil.getCorsConfigurationJsonFromDto(api.getCorsConfiguration()));
artifact.setAttribute(APIConstants.API_OVERVIEW_WEBSUB_SUBSCRIPTION_CONFIGURATION, APIUtil.getWebsubSubscriptionConfigurationJsonFromDto(api.getWebsubSubscriptionConfiguration()));
artifact.setAttribute(APIConstants.API_OVERVIEW_WS_URI_MAPPING, APIUtil.getWsUriMappingJsonFromDto(api.getWsUriMapping()));
// attaching api categories to the API
List<APICategory> attachedApiCategories = api.getApiCategories();
artifact.removeAttribute(APIConstants.API_CATEGORIES_CATEGORY_NAME);
if (attachedApiCategories != null) {
for (APICategory category : attachedApiCategories) {
artifact.addAttribute(APIConstants.API_CATEGORIES_CATEGORY_NAME, category.getName());
}
}
// set monetization status (i.e - enabled or disabled)
artifact.setAttribute(APIConstants.Monetization.API_MONETIZATION_STATUS, Boolean.toString(api.getMonetizationStatus()));
// set additional monetization data
if (api.getMonetizationProperties() != null) {
artifact.setAttribute(APIConstants.Monetization.API_MONETIZATION_PROPERTIES, api.getMonetizationProperties().toJSONString());
}
if (api.getKeyManagers() != null) {
artifact.setAttribute(APIConstants.API_OVERVIEW_KEY_MANAGERS, new Gson().toJson(api.getKeyManagers()));
}
// check in github code to see this method was removed
String apiSecurity = artifact.getAttribute(APIConstants.API_OVERVIEW_API_SECURITY);
if (apiSecurity != null && !apiSecurity.contains(APIConstants.DEFAULT_API_SECURITY_OAUTH2) && !apiSecurity.contains(APIConstants.API_SECURITY_API_KEY)) {
artifact.setAttribute(APIConstants.API_OVERVIEW_TIER, "");
}
} catch (GovernanceException e) {
String msg = "Failed to create API for : " + api.getId().getApiName();
log.error(msg, e);
throw new APIManagementException(msg, e);
}
return artifact;
}
Aggregations