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;
}
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;
}
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;
}
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();
}
}
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;
}
Aggregations