Search in sources :

Example 36 with ApplicationDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationDTO in project carbon-apimgt by wso2.

the class ApplicationsApiServiceImpl method applicationsApplicationIdGet.

/**
 * Get an application by Id
 *
 * @param applicationId   application identifier
 * @param ifNoneMatch     If-None-Match header value
 * @return response containing the required application object
 */
@Override
public Response applicationsApplicationIdGet(String applicationId, String ifNoneMatch, String xWSO2Tenant, MessageContext messageContext) {
    String username = RestApiCommonUtil.getLoggedInUsername();
    try {
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        APIConsumer apiConsumer = APIManagerFactory.getInstance().getAPIConsumer(username);
        Application application = apiConsumer.getApplicationByUUID(applicationId, organization);
        if (application != null) {
            // Remove hidden attributes and set the rest of the attributes from config
            JSONArray applicationAttributesFromConfig = apiConsumer.getAppAttributesFromConfig(username);
            Map<String, String> existingApplicationAttributes = application.getApplicationAttributes();
            Map<String, String> applicationAttributes = new HashMap<>();
            if (existingApplicationAttributes != null && applicationAttributesFromConfig != null) {
                for (Object object : applicationAttributesFromConfig) {
                    JSONObject attribute = (JSONObject) object;
                    Boolean hidden = (Boolean) attribute.get(APIConstants.ApplicationAttributes.HIDDEN);
                    String attributeName = (String) attribute.get(APIConstants.ApplicationAttributes.ATTRIBUTE);
                    if (!BooleanUtils.isTrue(hidden)) {
                        String attributeVal = existingApplicationAttributes.get(attributeName);
                        if (attributeVal != null) {
                            applicationAttributes.put(attributeName, attributeVal);
                        } else {
                            applicationAttributes.put(attributeName, "");
                        }
                    }
                }
            }
            application.setApplicationAttributes(applicationAttributes);
            if (RestAPIStoreUtils.isUserAccessAllowedForApplication(application)) {
                ApplicationDTO applicationDTO = ApplicationMappingUtil.fromApplicationtoDTO(application);
                applicationDTO.setHashEnabled(OAuthServerConfiguration.getInstance().isClientSecretHashEnabled());
                Set<Scope> scopes = apiConsumer.getScopesForApplicationSubscription(username, application.getId(), organization);
                List<ScopeInfoDTO> scopeInfoList = ApplicationMappingUtil.getScopeInfoDTO(scopes);
                applicationDTO.setSubscriptionScopes(scopeInfoList);
                return Response.ok().entity(applicationDTO).build();
            } else {
                RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_APPLICATION, applicationId, log);
            }
        } else {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_APPLICATION, applicationId, log);
        }
    } catch (APIManagementException e) {
        RestApiUtil.handleInternalServerError("Error while retrieving application " + applicationId, e, log);
    }
    return null;
}
Also used : ApplicationDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationDTO) HashMap(java.util.HashMap) JSONArray(org.json.simple.JSONArray) ScopeInfoDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.ScopeInfoDTO) JSONObject(org.json.simple.JSONObject) Scope(org.wso2.carbon.apimgt.api.model.Scope) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) JsonObject(com.google.gson.JsonObject) JSONObject(org.json.simple.JSONObject) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer) ExportedApplication(org.wso2.carbon.apimgt.rest.api.store.v1.models.ExportedApplication) Application(org.wso2.carbon.apimgt.api.model.Application)

Example 37 with ApplicationDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationDTO in project carbon-apimgt by wso2.

the class ApplicationMappingUtil method fromApplicationtoDTO.

public static ApplicationDTO fromApplicationtoDTO(Application application) {
    ApplicationDTO applicationDTO = new ApplicationDTO();
    applicationDTO.setApplicationId(application.getUUID());
    applicationDTO.setThrottlingPolicy(application.getTier());
    applicationDTO.setDescription(application.getDescription());
    Map<String, String> applicationAttributes = application.getApplicationAttributes();
    applicationDTO.setAttributes(applicationAttributes);
    applicationDTO.setName(application.getName());
    applicationDTO.setStatus(application.getStatus());
    applicationDTO.setOwner(application.getOwner());
    if (StringUtils.isNotEmpty(application.getGroupId())) {
        applicationDTO.setGroups(Arrays.asList(application.getGroupId().split(",")));
    }
    applicationDTO.setTokenType(ApplicationDTO.TokenTypeEnum.OAUTH);
    applicationDTO.setSubscriptionCount(application.getSubscriptionCount());
    if (StringUtils.isNotEmpty(application.getTokenType()) && !APIConstants.DEFAULT_TOKEN_TYPE.equals(application.getTokenType())) {
        applicationDTO.setTokenType(ApplicationDTO.TokenTypeEnum.valueOf(application.getTokenType()));
    }
    return applicationDTO;
}
Also used : ApplicationDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.ApplicationDTO)

Example 38 with ApplicationDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationDTO in project carbon-apimgt by wso2.

the class ApplicationsApiServiceImpl method applicationsApplicationIdGet.

@Override
public Response applicationsApplicationIdGet(String applicationId, MessageContext messageContext) throws APIManagementException {
    String username = RestApiCommonUtil.getLoggedInUsername();
    String organization = RestApiUtil.getOrganization(messageContext);
    try {
        APIConsumer apiConsumer = APIManagerFactory.getInstance().getAPIConsumer(username);
        String tenantDomain = CarbonContext.getThreadLocalCarbonContext().getTenantDomain();
        Application application = apiConsumer.getApplicationByUUID(applicationId, organization);
        if (application != null) {
            String applicationTenantDomain = MultitenantUtils.getTenantDomain(application.getOwner());
            // and verify that the invoking user's tenant domain has an API subscribed by this application
            if (tenantDomain.equals(applicationTenantDomain)) {
                // Remove hidden attributes and set the rest of the attributes from config
                JSONArray applicationAttributesFromConfig = apiConsumer.getAppAttributesFromConfig(username);
                Map<String, String> existingApplicationAttributes = application.getApplicationAttributes();
                Map<String, String> applicationAttributes = new HashMap<>();
                if (existingApplicationAttributes != null && applicationAttributesFromConfig != null) {
                    for (Object object : applicationAttributesFromConfig) {
                        JSONObject attribute = (JSONObject) object;
                        Boolean hidden = (Boolean) attribute.get(APIConstants.ApplicationAttributes.HIDDEN);
                        String attributeName = (String) attribute.get(APIConstants.ApplicationAttributes.ATTRIBUTE);
                        if (!BooleanUtils.isTrue(hidden)) {
                            String attributeVal = existingApplicationAttributes.get(attributeName);
                            if (attributeVal != null) {
                                applicationAttributes.put(attributeName, attributeVal);
                            } else {
                                applicationAttributes.put(attributeName, "");
                            }
                        }
                    }
                }
                application.setApplicationAttributes(applicationAttributes);
                ApplicationDTO applicationDTO = ApplicationMappingUtil.fromApplicationtoDTO(application);
                Set<Scope> scopes = apiConsumer.getScopesForApplicationSubscription(username, application.getId(), organization);
                List<ScopeInfoDTO> scopeInfoList = ApplicationMappingUtil.getScopeInfoDTO(scopes);
                applicationDTO.setSubscriptionScopes(scopeInfoList);
                return Response.ok().entity(applicationDTO).build();
            }
        }
    } catch (APIManagementException e) {
        RestApiUtil.handleInternalServerError("Error while retrieving application " + applicationId, e, log);
    }
    RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_APPLICATION, applicationId, log);
    return null;
}
Also used : ApplicationDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.ApplicationDTO) HashMap(java.util.HashMap) JSONArray(org.json.simple.JSONArray) ScopeInfoDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.ScopeInfoDTO) JSONObject(org.json.simple.JSONObject) Scope(org.wso2.carbon.apimgt.api.model.Scope) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) JSONObject(org.json.simple.JSONObject) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer) Application(org.wso2.carbon.apimgt.api.model.Application)

Example 39 with ApplicationDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationDTO in project carbon-apimgt by wso2.

the class ApplicationsApiServiceImpl method applicationsApplicationIdGet.

/**
 * Retrives an existing application
 *
 * @param applicationId   application Id
 * @param ifNoneMatch     If-None-Match header value
 * @param ifModifiedSince If-Modified-Since header value
 * @param request         msf4j request object
 * @return Requested application detials as the response
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response applicationsApplicationIdGet(String applicationId, String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
    ApplicationDTO applicationDTO = null;
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIStore apiConsumer = RestApiUtil.getConsumer(username);
        String existingFingerprint = applicationsApplicationIdGetFingerprint(applicationId, ifNoneMatch, ifModifiedSince, request);
        if (!StringUtils.isEmpty(ifNoneMatch) && !StringUtils.isEmpty(existingFingerprint) && ifNoneMatch.contains(existingFingerprint)) {
            return Response.notModified().build();
        }
        Application application = apiConsumer.getApplication(applicationId, username);
        if (application != null) {
            applicationDTO = ApplicationMappingUtil.fromApplicationToDTO(application);
            return Response.ok().entity(applicationDTO).header(HttpHeaders.ETAG, "\"" + existingFingerprint + "\"").build();
        } else {
            String errorMessage = "Application not found: " + applicationId;
            APIMgtResourceNotFoundException e = new APIMgtResourceNotFoundException(errorMessage, ExceptionCodes.APPLICATION_NOT_FOUND);
            HashMap<String, String> paramList = new HashMap<String, String>();
            paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_ID, applicationId);
            ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
            log.error(errorMessage, e);
            return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
        }
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving application: " + applicationId;
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_ID, applicationId);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : ApplicationDTO(org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationDTO) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException) Application(org.wso2.carbon.apimgt.core.models.Application) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 40 with ApplicationDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationDTO in project carbon-apimgt by wso2.

the class ApplicationMappingUtil method fromApplicationToDTO.

public static ApplicationDTO fromApplicationToDTO(Application application) {
    ApplicationDTO applicationDTO = new ApplicationDTO();
    applicationDTO.setApplicationId(application.getId());
    applicationDTO.setThrottlingTier(application.getPolicy().getPolicyName());
    applicationDTO.setDescription(application.getDescription());
    applicationDTO.setName(application.getName());
    applicationDTO.setLifeCycleStatus(application.getStatus());
    applicationDTO.setPermission(application.getPermissionString());
    applicationDTO.setSubscriber(application.getCreatedUser());
    List<ApplicationKeysDTO> applicationKeyDTOs = new ArrayList<>();
    for (OAuthApplicationInfo applicationKeys : application.getApplicationKeys()) {
        ApplicationKeysDTO applicationKeysDTO = ApplicationKeyMappingUtil.fromApplicationKeysToDTO(applicationKeys);
        applicationKeyDTOs.add(applicationKeysDTO);
    }
    applicationDTO.setToken(ApplicationKeyMappingUtil.fromApplicationTokenToDTO(application.getApplicationToken()));
    applicationDTO.setKeys(applicationKeyDTOs);
    return applicationDTO;
}
Also used : ApplicationDTO(org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationDTO) ApplicationKeysDTO(org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationKeysDTO) OAuthApplicationInfo(org.wso2.carbon.apimgt.core.models.OAuthApplicationInfo) ArrayList(java.util.ArrayList)

Aggregations

BeforeClass (org.testng.annotations.BeforeClass)17 ApplicationDTO (org.wso2.micro.gateway.tests.common.model.ApplicationDTO)17 Application (org.wso2.carbon.apimgt.core.models.Application)11 ApplicationDTO (org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationDTO)11 ArrayList (java.util.ArrayList)10 JSONObject (org.json.JSONObject)10 HashMap (java.util.HashMap)9 Application (org.wso2.carbon.apimgt.api.model.Application)8 API (org.wso2.micro.gateway.tests.common.model.API)8 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)7 ExportedApplication (org.wso2.carbon.apimgt.rest.api.store.v1.models.ExportedApplication)7 KeyValidationInfo (org.wso2.micro.gateway.tests.common.KeyValidationInfo)7 MockAPIPublisher (org.wso2.micro.gateway.tests.common.MockAPIPublisher)7 Test (org.junit.Test)6 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)6 ApplicationKeysDTO (org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationKeysDTO)6 ApplicationDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationDTO)6 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)5 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)5 ApplicationCreationResponse (org.wso2.carbon.apimgt.core.workflow.ApplicationCreationResponse)5