From d3418462d974cf3e1a94bf8522acc3ce2cfce135 Mon Sep 17 00:00:00 2001 From: ayyoob Date: Sun, 17 Apr 2016 12:18:49 +0530 Subject: [PATCH] few changes in api manager application registration --- ...ApiApplicationRegistrationServiceImpl.java | 7 ++-- .../APIManagementProviderService.java | 2 +- .../APIManagementProviderServiceImpl.java | 38 ++++++++++++++++- .../publisher/APIPublisherServiceImpl.java | 42 ++++++++++++------- .../APIPublisherLifecycleListener.java | 33 ++++++++------- .../framework/WebappAuthenticationValve.java | 2 - 6 files changed, 85 insertions(+), 39 deletions(-) diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension.api/src/main/java/org/wso2/carbon/apimgt/application/extension/api/ApiApplicationRegistrationServiceImpl.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension.api/src/main/java/org/wso2/carbon/apimgt/application/extension/api/ApiApplicationRegistrationServiceImpl.java index 8a7ffd0d9e..7c24b40ffd 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension.api/src/main/java/org/wso2/carbon/apimgt/application/extension/api/ApiApplicationRegistrationServiceImpl.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension.api/src/main/java/org/wso2/carbon/apimgt/application/extension/api/ApiApplicationRegistrationServiceImpl.java @@ -95,8 +95,9 @@ public class ApiApplicationRegistrationServiceImpl implements ApiApplicationRegi ApiApplicationConstants.DEFAULT_VALIDITY_PERIOD); apiManagementProviderService.registerExistingOAuthApplicationToAPIApplication( jsonStringObject.toJSONString(), registrationProfile.getApplicationName(), - registrationProfile.getConsumerKey(), username, registrationProfile.isAllowedToAllDomains()); - return Response.status(Response.Status.ACCEPTED).entity("OAuth App is mapped as APIM App").build(); + registrationProfile.getConsumerKey(), username, registrationProfile.isAllowedToAllDomains(), + ApiApplicationConstants.DEFAULT_TOKEN_TYPE); + return Response.status(Response.Status.ACCEPTED).entity("true").build(); } else { ApiApplicationKey apiApplicationKey = apiManagementProviderService.generateAndRetrieveApplicationKeys( registrationProfile.getApplicationName(), registrationProfile.getTags(), @@ -107,7 +108,7 @@ public class ApiApplicationRegistrationServiceImpl implements ApiApplicationRegi String msg = "Error occurred while registering an application '" + registrationProfile.getApplicationName() + "'"; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("false").build(); } } diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension/src/main/java/org/wso2/carbon/apimgt/application/extension/APIManagementProviderService.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension/src/main/java/org/wso2/carbon/apimgt/application/extension/APIManagementProviderService.java index 643d66c23d..33003b9768 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension/src/main/java/org/wso2/carbon/apimgt/application/extension/APIManagementProviderService.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension/src/main/java/org/wso2/carbon/apimgt/application/extension/APIManagementProviderService.java @@ -59,7 +59,7 @@ public interface APIManagementProviderService { * Register existing Oauth application as apim application. */ void registerExistingOAuthApplicationToAPIApplication(String jsonString, String applicationName, String clientId, - String username, boolean isAllowedAllDomains) + String username, boolean isAllowedAllDomains, String keyType) throws APIManagerException; /** diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension/src/main/java/org/wso2/carbon/apimgt/application/extension/APIManagementProviderServiceImpl.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension/src/main/java/org/wso2/carbon/apimgt/application/extension/APIManagementProviderServiceImpl.java index 986e173f32..8c0e51a7f9 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension/src/main/java/org/wso2/carbon/apimgt/application/extension/APIManagementProviderServiceImpl.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension/src/main/java/org/wso2/carbon/apimgt/application/extension/APIManagementProviderServiceImpl.java @@ -118,13 +118,47 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe @Override public void registerExistingOAuthApplicationToAPIApplication(String jsonString, String applicationName, String clientId, String username, - boolean isAllowedAllDomains) + boolean isAllowedAllDomains, String keyType) throws APIManagerException { try { APIConsumer apiConsumer = APIManagerFactory.getInstance().getAPIConsumer(username); if (apiConsumer != null) { String groupId = getLoggedInUserGroupId(username, APIManagerUtil.getTenantDomain()); - createApplication(apiConsumer, applicationName, username, groupId); + int applicationId = createApplication(apiConsumer, applicationName, username, groupId); + Subscriber subscriber = apiConsumer.getSubscriber(username); + if (subscriber == null) { + String tenantDomain = MultitenantUtils.getTenantDomain(username); + addSubscriber(username, "", groupId, APIManagerUtil.getTenantId(tenantDomain)); + subscriber = apiConsumer.getSubscriber(username); + } + Application[] applications = apiConsumer.getApplications(subscriber, groupId); + Application application = null; + for (Application app : applications) { + if (app.getId() == applicationId) { + application = app; + } + } + if (application == null) { + throw new APIManagerException( + "Api application creation failed for " + applicationName + " to the user " + username); + } + + APIKey retrievedApiApplicationKey = null; + for (APIKey apiKey : application.getKeys()) { + String applicationKeyType = apiKey.getType(); + if (applicationKeyType != null && applicationKeyType.equals(keyType)) { + retrievedApiApplicationKey = apiKey; + break; + } + } + if (retrievedApiApplicationKey != null) { + if (retrievedApiApplicationKey.getConsumerKey().equals(clientId)) { + return; + } else { + throw new APIManagerException("Api application already mapped to another OAuth App"); + } + } + String[] allowedDomains = new String[1]; if (isAllowedAllDomains) { allowedDomains[0] = ApiApplicationConstants.ALLOWED_DOMAINS; diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/APIPublisherServiceImpl.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/APIPublisherServiceImpl.java index dae050d1d7..8213d3ca76 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/APIPublisherServiceImpl.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/APIPublisherServiceImpl.java @@ -37,7 +37,11 @@ import org.wso2.carbon.utils.multitenancy.MultitenantUtils; import javax.xml.stream.XMLStreamException; import java.io.FileNotFoundException; +import java.util.HashMap; +import java.util.Iterator; import java.util.List; +import java.util.Map; +import java.util.Set; /** * This class represents the concrete implementation of the APIPublisherService that corresponds to providing all @@ -63,6 +67,10 @@ public class APIPublisherServiceImpl implements APIPublisherService { APIProvider provider = APIManagerFactory.getInstance().getAPIProvider(api.getApiOwner()); MultitenantUtils.getTenantDomain(api.getApiOwner()); if (provider != null) { + if (provider.isDuplicateContextTemplate(api.getContext())) { + throw new APIManagementException("Error occurred while adding the API. A duplicate API" + + " context already exists for " + api.getContext()); + } if (!provider.isAPIAvailable(api.getId())) { provider.addAPI(api); log.info("Successfully published API '" + api.getId().getApiName() + "' with context '" + @@ -90,38 +98,40 @@ public class APIPublisherServiceImpl implements APIPublisherService { } private String createSwaggerDefinition(API api) { - //{"paths":{"/controller/*":{"get":{"responses":{"200":{}}}},"/manager/*":{"get":{"responses":{"200":{}}}}}, - // "swagger":"2.0","info":{"title":"RaspberryPi","version":"1.0.0"}} - JsonObject swaggerDefinition = new JsonObject(); + Map httpVerbsMap = new HashMap<>(); - JsonObject paths = new JsonObject(); for (URITemplate uriTemplate : api.getUriTemplates()) { JsonObject response = new JsonObject(); response.addProperty("200", ""); JsonObject responses = new JsonObject(); responses.add("responses", response); + JsonObject httpVerbs = httpVerbsMap.get(uriTemplate.getUriTemplate()); + if (httpVerbs == null) { + httpVerbs = new JsonObject(); + } + httpVerbs.add(uriTemplate.getHTTPVerb().toLowerCase(), responses); + httpVerbsMap.put(uriTemplate.getUriTemplate(), httpVerbs); + } - JsonObject httpVerb = new JsonObject(); - httpVerb.add(uriTemplate.getHTTPVerb().toLowerCase(), responses); - - JsonObject path = new JsonObject(); - path.add(uriTemplate.getUriTemplate(), httpVerb); - - paths.add(uriTemplate.getUriTemplate(), httpVerb); + Iterator it = httpVerbsMap.entrySet().iterator(); + JsonObject paths = new JsonObject(); + while (it.hasNext()) { + Map.Entry pair = (Map.Entry)it.next(); + paths.add(pair.getKey(), pair.getValue()); + it.remove(); } - swaggerDefinition.add("paths", paths); - swaggerDefinition.addProperty("swagger", "2.0"); JsonObject info = new JsonObject(); info.addProperty("title", api.getId().getApiName()); info.addProperty("version", api.getId().getVersion()); + + JsonObject swaggerDefinition = new JsonObject(); + swaggerDefinition.add("paths", paths); + swaggerDefinition.addProperty("swagger", "2.0"); swaggerDefinition.add("info", info); return swaggerDefinition.toString(); - //return "{\"paths\":{\"/controller/*\":{\"get\":{\"responses\":{\"200\":{}}}}, - // \"/manager/*\":{\"get\":{\"responses\":{\"200\":{}}}}},\"swagger\":\"2.0\", - // \"info\":{\"title\":\"RaspberryPi\",\"version\":\"1.0.0\"}}"; } @Override diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/lifecycle/listener/APIPublisherLifecycleListener.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/lifecycle/listener/APIPublisherLifecycleListener.java index a611a321ea..9e47ce2939 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/lifecycle/listener/APIPublisherLifecycleListener.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/lifecycle/listener/APIPublisherLifecycleListener.java @@ -47,13 +47,14 @@ public class APIPublisherLifecycleListener implements LifecycleListener { private static final String PARAM_MANAGED_API_VERSION = "managed-api-version"; private static final String PARAM_MANAGED_API_CONTEXT = "managed-api-context"; private static final String PARAM_MANAGED_API_ENDPOINT = "managed-api-endpoint"; + private static final String PARAM_MANAGED_API_ENDPOINT_CONTEXT = "managed-api-endpoint-context"; private static final String PARAM_MANAGED_API_OWNER = "managed-api-owner"; private static final String PARAM_MANAGED_API_TRANSPORTS = "managed-api-transports"; private static final String PARAM_MANAGED_API_IS_SECURED = "managed-api-isSecured"; private static final String PARAM_MANAGED_API_APPLICATION = "managed-api-application"; - private static final String PARAM_MANAGED_API_CONTEXT_TEMPLATE = "managed-api-context-template"; private static final String PARAM_SHARED_WITH_ALL_TENANTS = "isSharedWithAllTenants"; private static final String PARAM_PROVIDER_TENANT_DOMAIN = "providerTenantDomain"; + private static final String VERSION_PLACEHOLDER = "{version}"; private static final Log log = LogFactory.getLog(APIPublisherLifecycleListener.class); @Override @@ -156,14 +157,13 @@ public class APIPublisherLifecycleListener implements LifecycleListener { apiConfig.setTags(tags); } - String contextTemplate = servletContext.getInitParameter(PARAM_MANAGED_API_CONTEXT_TEMPLATE); - if (contextTemplate == null || contextTemplate.isEmpty()) { - if (log.isDebugEnabled()) { - log.debug("'managed-api-context-template' attribute is not configured. Therefore, using the default," + - " " + - "which is the original context template assigned to the web application"); - } - contextTemplate = servletContext.getContextPath(); + String tenantDomain = servletContext.getInitParameter(PARAM_PROVIDER_TENANT_DOMAIN); + tenantDomain = (tenantDomain != null && !tenantDomain.isEmpty()) ? tenantDomain : + MultitenantConstants.SUPER_TENANT_DOMAIN_NAME; + apiConfig.setTenantDomain(tenantDomain); + String contextTemplate = context + "/" + VERSION_PLACEHOLDER; + if (!tenantDomain.equals(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)) { + contextTemplate = context + "/t/" + tenantDomain + "/" + VERSION_PLACEHOLDER; } apiConfig.setContextTemplate(contextTemplate); @@ -172,7 +172,13 @@ public class APIPublisherLifecycleListener implements LifecycleListener { if (log.isDebugEnabled()) { log.debug("'managed-api-endpoint' attribute is not configured"); } - endpoint = APIPublisherUtil.getApiEndpointUrl(context); + String endpointContext = servletContext.getInitParameter(PARAM_MANAGED_API_ENDPOINT_CONTEXT); + if (endpointContext != null && !endpointContext.isEmpty()) { + endpoint = APIPublisherUtil.getApiEndpointUrl(endpointContext); + } else { + endpoint = APIPublisherUtil.getApiEndpointUrl(context); + } + } apiConfig.setEndpoint(endpoint); @@ -208,13 +214,10 @@ public class APIPublisherLifecycleListener implements LifecycleListener { apiConfig.setTransports(transports); String sharingValueParam = servletContext.getInitParameter(PARAM_SHARED_WITH_ALL_TENANTS); - boolean isSharedWithAllTenants = (sharingValueParam == null || (!sharingValueParam.isEmpty()) && Boolean.parseBoolean(sharingValueParam) ); + boolean isSharedWithAllTenants = (sharingValueParam == null || (!sharingValueParam.isEmpty()) && Boolean.parseBoolean( + sharingValueParam)); apiConfig.setSharedWithAllTenants(isSharedWithAllTenants); - String tenantDomain = servletContext.getInitParameter(PARAM_PROVIDER_TENANT_DOMAIN); - tenantDomain = (tenantDomain!= null && !tenantDomain.isEmpty()) ? tenantDomain : MultitenantConstants.SUPER_TENANT_DOMAIN_NAME; - apiConfig.setTenantDomain(tenantDomain); - Set uriTemplates = new LinkedHashSet(); for (APIResource apiResource : apidef.getResources()) { URITemplate template = new URITemplate(); diff --git a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/WebappAuthenticationValve.java b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/WebappAuthenticationValve.java index f7211fef13..93ab9c32a3 100644 --- a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/WebappAuthenticationValve.java +++ b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/WebappAuthenticationValve.java @@ -45,8 +45,6 @@ public class WebappAuthenticationValve extends CarbonTomcatValve { return; } - - WebappAuthenticator authenticator = WebappAuthenticatorFactory.getAuthenticator(request); if (authenticator == null) { String msg = "Failed to load an appropriate authenticator to authenticate the request";