few changes in api manager application registration

merge-requests/7/head
ayyoob 9 years ago
parent 9d4defaa01
commit d3418462d9

@ -95,8 +95,9 @@ public class ApiApplicationRegistrationServiceImpl implements ApiApplicationRegi
ApiApplicationConstants.DEFAULT_VALIDITY_PERIOD); ApiApplicationConstants.DEFAULT_VALIDITY_PERIOD);
apiManagementProviderService.registerExistingOAuthApplicationToAPIApplication( apiManagementProviderService.registerExistingOAuthApplicationToAPIApplication(
jsonStringObject.toJSONString(), registrationProfile.getApplicationName(), jsonStringObject.toJSONString(), registrationProfile.getApplicationName(),
registrationProfile.getConsumerKey(), username, registrationProfile.isAllowedToAllDomains()); registrationProfile.getConsumerKey(), username, registrationProfile.isAllowedToAllDomains(),
return Response.status(Response.Status.ACCEPTED).entity("OAuth App is mapped as APIM App").build(); ApiApplicationConstants.DEFAULT_TOKEN_TYPE);
return Response.status(Response.Status.ACCEPTED).entity("true").build();
} else { } else {
ApiApplicationKey apiApplicationKey = apiManagementProviderService.generateAndRetrieveApplicationKeys( ApiApplicationKey apiApplicationKey = apiManagementProviderService.generateAndRetrieveApplicationKeys(
registrationProfile.getApplicationName(), registrationProfile.getTags(), registrationProfile.getApplicationName(), registrationProfile.getTags(),
@ -107,7 +108,7 @@ public class ApiApplicationRegistrationServiceImpl implements ApiApplicationRegi
String msg = "Error occurred while registering an application '" String msg = "Error occurred while registering an application '"
+ registrationProfile.getApplicationName() + "'"; + registrationProfile.getApplicationName() + "'";
log.error(msg, e); 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();
} }
} }

@ -59,7 +59,7 @@ public interface APIManagementProviderService {
* Register existing Oauth application as apim application. * Register existing Oauth application as apim application.
*/ */
void registerExistingOAuthApplicationToAPIApplication(String jsonString, String applicationName, String clientId, void registerExistingOAuthApplicationToAPIApplication(String jsonString, String applicationName, String clientId,
String username, boolean isAllowedAllDomains) String username, boolean isAllowedAllDomains, String keyType)
throws APIManagerException; throws APIManagerException;
/** /**

@ -118,13 +118,47 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
@Override @Override
public void registerExistingOAuthApplicationToAPIApplication(String jsonString, String applicationName, public void registerExistingOAuthApplicationToAPIApplication(String jsonString, String applicationName,
String clientId, String username, String clientId, String username,
boolean isAllowedAllDomains) boolean isAllowedAllDomains, String keyType)
throws APIManagerException { throws APIManagerException {
try { try {
APIConsumer apiConsumer = APIManagerFactory.getInstance().getAPIConsumer(username); APIConsumer apiConsumer = APIManagerFactory.getInstance().getAPIConsumer(username);
if (apiConsumer != null) { if (apiConsumer != null) {
String groupId = getLoggedInUserGroupId(username, APIManagerUtil.getTenantDomain()); 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]; String[] allowedDomains = new String[1];
if (isAllowedAllDomains) { if (isAllowedAllDomains) {
allowedDomains[0] = ApiApplicationConstants.ALLOWED_DOMAINS; allowedDomains[0] = ApiApplicationConstants.ALLOWED_DOMAINS;

@ -37,7 +37,11 @@ import org.wso2.carbon.utils.multitenancy.MultitenantUtils;
import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamException;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List; 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 * 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()); APIProvider provider = APIManagerFactory.getInstance().getAPIProvider(api.getApiOwner());
MultitenantUtils.getTenantDomain(api.getApiOwner()); MultitenantUtils.getTenantDomain(api.getApiOwner());
if (provider != null) { 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())) { if (!provider.isAPIAvailable(api.getId())) {
provider.addAPI(api); provider.addAPI(api);
log.info("Successfully published API '" + api.getId().getApiName() + "' with context '" + log.info("Successfully published API '" + api.getId().getApiName() + "' with context '" +
@ -90,38 +98,40 @@ public class APIPublisherServiceImpl implements APIPublisherService {
} }
private String createSwaggerDefinition(API api) { private String createSwaggerDefinition(API api) {
//{"paths":{"/controller/*":{"get":{"responses":{"200":{}}}},"/manager/*":{"get":{"responses":{"200":{}}}}}, Map<String, JsonObject> httpVerbsMap = new HashMap<>();
// "swagger":"2.0","info":{"title":"RaspberryPi","version":"1.0.0"}}
JsonObject swaggerDefinition = new JsonObject();
JsonObject paths = new JsonObject();
for (URITemplate uriTemplate : api.getUriTemplates()) { for (URITemplate uriTemplate : api.getUriTemplates()) {
JsonObject response = new JsonObject(); JsonObject response = new JsonObject();
response.addProperty("200", ""); response.addProperty("200", "");
JsonObject responses = new JsonObject(); JsonObject responses = new JsonObject();
responses.add("responses", response); 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(); Iterator it = httpVerbsMap.entrySet().iterator();
httpVerb.add(uriTemplate.getHTTPVerb().toLowerCase(), responses); JsonObject paths = new JsonObject();
while (it.hasNext()) {
JsonObject path = new JsonObject(); Map.Entry<String, JsonObject> pair = (Map.Entry)it.next();
path.add(uriTemplate.getUriTemplate(), httpVerb); paths.add(pair.getKey(), pair.getValue());
it.remove();
paths.add(uriTemplate.getUriTemplate(), httpVerb);
} }
swaggerDefinition.add("paths", paths);
swaggerDefinition.addProperty("swagger", "2.0");
JsonObject info = new JsonObject(); JsonObject info = new JsonObject();
info.addProperty("title", api.getId().getApiName()); info.addProperty("title", api.getId().getApiName());
info.addProperty("version", api.getId().getVersion()); info.addProperty("version", api.getId().getVersion());
JsonObject swaggerDefinition = new JsonObject();
swaggerDefinition.add("paths", paths);
swaggerDefinition.addProperty("swagger", "2.0");
swaggerDefinition.add("info", info); swaggerDefinition.add("info", info);
return swaggerDefinition.toString(); return swaggerDefinition.toString();
//return "{\"paths\":{\"/controller/*\":{\"get\":{\"responses\":{\"200\":{}}}},
// \"/manager/*\":{\"get\":{\"responses\":{\"200\":{}}}}},\"swagger\":\"2.0\",
// \"info\":{\"title\":\"RaspberryPi\",\"version\":\"1.0.0\"}}";
} }
@Override @Override

@ -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_VERSION = "managed-api-version";
private static final String PARAM_MANAGED_API_CONTEXT = "managed-api-context"; 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 = "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_OWNER = "managed-api-owner";
private static final String PARAM_MANAGED_API_TRANSPORTS = "managed-api-transports"; 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_IS_SECURED = "managed-api-isSecured";
private static final String PARAM_MANAGED_API_APPLICATION = "managed-api-application"; 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_SHARED_WITH_ALL_TENANTS = "isSharedWithAllTenants";
private static final String PARAM_PROVIDER_TENANT_DOMAIN = "providerTenantDomain"; 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); private static final Log log = LogFactory.getLog(APIPublisherLifecycleListener.class);
@Override @Override
@ -156,14 +157,13 @@ public class APIPublisherLifecycleListener implements LifecycleListener {
apiConfig.setTags(tags); apiConfig.setTags(tags);
} }
String contextTemplate = servletContext.getInitParameter(PARAM_MANAGED_API_CONTEXT_TEMPLATE); String tenantDomain = servletContext.getInitParameter(PARAM_PROVIDER_TENANT_DOMAIN);
if (contextTemplate == null || contextTemplate.isEmpty()) { tenantDomain = (tenantDomain != null && !tenantDomain.isEmpty()) ? tenantDomain :
if (log.isDebugEnabled()) { MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
log.debug("'managed-api-context-template' attribute is not configured. Therefore, using the default," + apiConfig.setTenantDomain(tenantDomain);
" " + String contextTemplate = context + "/" + VERSION_PLACEHOLDER;
"which is the original context template assigned to the web application"); if (!tenantDomain.equals(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)) {
} contextTemplate = context + "/t/" + tenantDomain + "/" + VERSION_PLACEHOLDER;
contextTemplate = servletContext.getContextPath();
} }
apiConfig.setContextTemplate(contextTemplate); apiConfig.setContextTemplate(contextTemplate);
@ -172,8 +172,14 @@ public class APIPublisherLifecycleListener implements LifecycleListener {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("'managed-api-endpoint' attribute is not configured"); log.debug("'managed-api-endpoint' attribute is not configured");
} }
String endpointContext = servletContext.getInitParameter(PARAM_MANAGED_API_ENDPOINT_CONTEXT);
if (endpointContext != null && !endpointContext.isEmpty()) {
endpoint = APIPublisherUtil.getApiEndpointUrl(endpointContext);
} else {
endpoint = APIPublisherUtil.getApiEndpointUrl(context); endpoint = APIPublisherUtil.getApiEndpointUrl(context);
} }
}
apiConfig.setEndpoint(endpoint); apiConfig.setEndpoint(endpoint);
String owner = servletContext.getInitParameter(PARAM_MANAGED_API_OWNER); String owner = servletContext.getInitParameter(PARAM_MANAGED_API_OWNER);
@ -208,13 +214,10 @@ public class APIPublisherLifecycleListener implements LifecycleListener {
apiConfig.setTransports(transports); apiConfig.setTransports(transports);
String sharingValueParam = servletContext.getInitParameter(PARAM_SHARED_WITH_ALL_TENANTS); 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); 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<URITemplate> uriTemplates = new LinkedHashSet<URITemplate>(); Set<URITemplate> uriTemplates = new LinkedHashSet<URITemplate>();
for (APIResource apiResource : apidef.getResources()) { for (APIResource apiResource : apidef.getResources()) {
URITemplate template = new URITemplate(); URITemplate template = new URITemplate();

@ -45,8 +45,6 @@ public class WebappAuthenticationValve extends CarbonTomcatValve {
return; return;
} }
WebappAuthenticator authenticator = WebappAuthenticatorFactory.getAuthenticator(request); WebappAuthenticator authenticator = WebappAuthenticatorFactory.getAuthenticator(request);
if (authenticator == null) { if (authenticator == null) {
String msg = "Failed to load an appropriate authenticator to authenticate the request"; String msg = "Failed to load an appropriate authenticator to authenticate the request";

Loading…
Cancel
Save