use of org.wso2.carbon.apimgt.api.APIConsumer in project carbon-apimgt by wso2.
the class ApplicationsApiServiceImpl method applicationsGet.
/**
* Retrieves all applications that qualifies for the search query
*
* @param query Search query
* @param limit Max number of applications to return
* @param offset Starting position of pagination
* @param ifNoneMatch If-None-Match header value
* @param request msf4j request object
* @return A list of qualifying application DTOs as the response
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response applicationsGet(String query, Integer limit, Integer offset, String ifNoneMatch, Request request) throws NotFoundException {
ApplicationListDTO applicationListDTO = null;
String username = RestApiUtil.getLoggedInUsername(request);
limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
try {
APIStore apiConsumer = RestApiUtil.getConsumer(username);
List<Application> allMatchedApps = new ArrayList<>();
if (StringUtils.isBlank(query)) {
allMatchedApps = apiConsumer.getApplications(username);
} else {
Application application = apiConsumer.getApplicationByName(username, query);
if (application != null) {
allMatchedApps = new ArrayList<>();
allMatchedApps.add(application);
}
}
// allMatchedApps are already sorted to application name
applicationListDTO = ApplicationMappingUtil.fromApplicationsToDTO(allMatchedApps, limit, offset);
ApplicationMappingUtil.setPaginationParams(applicationListDTO, limit, offset, allMatchedApps.size());
} catch (APIManagementException e) {
String errorMessage = "Error while retrieving applications";
HashMap<String, String> paramList = new HashMap<String, String>();
paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_NAME, query);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
return Response.ok().entity(applicationListDTO).build();
}
use of org.wso2.carbon.apimgt.api.APIConsumer in project carbon-apimgt by wso2.
the class ApplicationsApiServiceImpl method applicationsApplicationIdPut.
/**
* Updates an existing application
*
* @param applicationId application Id
* @param body Application details to be updated
* @param ifMatch If-Match header value
* @param ifUnmodifiedSince If-Unmodified-Since header value
* @param request msf4j request object
* @return Updated application details as the response
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response applicationsApplicationIdPut(String applicationId, ApplicationDTO body, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
ApplicationDTO updatedApplicationDTO = null;
String username = RestApiUtil.getLoggedInUsername(request);
try {
APIStore apiConsumer = RestApiUtil.getConsumer(username);
String existingFingerprint = applicationsApplicationIdGetFingerprint(applicationId, null, null, request);
if (!StringUtils.isEmpty(ifMatch) && !StringUtils.isEmpty(existingFingerprint) && !ifMatch.contains(existingFingerprint)) {
return Response.status(Response.Status.PRECONDITION_FAILED).build();
}
Application application = ApplicationMappingUtil.fromDTOtoApplication(body, username);
if (!ApplicationStatus.APPLICATION_APPROVED.equals(application.getStatus())) {
String errorMessage = "Application " + applicationId + " is not active";
ExceptionCodes exceptionCode = ExceptionCodes.APPLICATION_INACTIVE;
APIManagementException e = new APIManagementException(errorMessage, exceptionCode);
Map<String, String> paramList = new HashMap<>();
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();
}
WorkflowResponse updateResponse = apiConsumer.updateApplication(applicationId, application);
if (WorkflowStatus.REJECTED == updateResponse.getWorkflowStatus()) {
String errorMessage = "Update request for application " + applicationId + " is rejected";
ExceptionCodes exceptionCode = ExceptionCodes.WORKFLOW_REJCECTED;
APIManagementException e = new APIManagementException(errorMessage, exceptionCode);
Map<String, String> paramList = new HashMap<>();
paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_ID, applicationId);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
// retrieves the updated application and send as the response
Application updatedApplication = apiConsumer.getApplication(applicationId, username);
updatedApplicationDTO = ApplicationMappingUtil.fromApplicationToDTO(updatedApplication);
String newFingerprint = applicationsApplicationIdGetFingerprint(applicationId, null, null, request);
// be in either pending or approved state) send back the workflow response
if (ApplicationStatus.APPLICATION_ONHOLD.equals(updatedApplication.getStatus())) {
WorkflowResponseDTO workflowResponse = MiscMappingUtil.fromWorkflowResponseToDTO(updateResponse);
URI location = new URI(RestApiConstants.RESOURCE_PATH_APPLICATIONS + "/" + applicationId);
return Response.status(Response.Status.ACCEPTED).header(RestApiConstants.LOCATION_HEADER, location).entity(workflowResponse).build();
}
return Response.ok().entity(updatedApplicationDTO).header(HttpHeaders.ETAG, "\"" + newFingerprint + "\"").build();
} catch (APIManagementException e) {
String errorMessage = "Error while updating application: " + body.getName();
HashMap<String, String> paramList = new HashMap<String, String>();
paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_NAME, body.getName());
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
} catch (URISyntaxException e) {
String errorMessage = "Error while adding location header in response for application : " + body.getName();
Map<String, String> paramList = new HashMap<>();
paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_NAME, body.getName());
ErrorHandler errorHandler = ExceptionCodes.LOCATION_HEADER_INCORRECT;
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorHandler, paramList);
log.error(errorMessage, e);
return Response.status(errorHandler.getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.carbon.apimgt.api.APIConsumer in project carbon-apimgt by wso2.
the class ApplicationUtils method isUserOwnerOfApplication.
/**
* check whether current logged in user is the owner of the application
*
* @param applicationId Application id
* @param username loged in user
* @return true if current logged in consumer is the owner of the specified application
*/
public static boolean isUserOwnerOfApplication(int applicationId, String username) throws APIManagementException {
APIConsumer apiConsumer = APIManagerFactory.getInstance().getAPIConsumer(username);
Application application = apiConsumer.getApplicationById(applicationId);
return isUserOwnerOfApplication(application, username);
}
use of org.wso2.carbon.apimgt.api.APIConsumer in project carbon-apimgt by wso2.
the class APIConsumerImplTest method testAddComment.
@Test
public void testAddComment() throws APIManagementException {
APIConsumerImpl apiConsumer = new APIConsumerImplWrapper(apiMgtDAO);
APIIdentifier apiIdentifier = new APIIdentifier(API_PROVIDER, SAMPLE_API_NAME, SAMPLE_API_VERSION);
Mockito.when(apiMgtDAO.addComment(apiIdentifier, "testComment", "testUser")).thenReturn(1111);
apiConsumer.addComment(apiIdentifier, "testComment", "testUser");
Mockito.verify(apiMgtDAO, Mockito.times(1)).addComment(apiIdentifier, "testComment", "testUser");
}
use of org.wso2.carbon.apimgt.api.APIConsumer in project carbon-apimgt by wso2.
the class APIConsumerImplTest method testGetTagsWithAttributes.
@Test
public void testGetTagsWithAttributes() throws Exception {
Registry userRegistry = Mockito.mock(Registry.class);
APIConsumerImpl apiConsumer = new APIConsumerImplWrapper(apiMgtDAO, apiPersistenceInstance);
System.setProperty(CARBON_HOME, "");
PowerMockito.mockStatic(GovernanceUtils.class);
UserRegistry userRegistry1 = Mockito.mock(UserRegistry.class);
Mockito.when(registryService.getGovernanceUserRegistry(Mockito.anyString(), Mockito.anyInt())).thenReturn(userRegistry1);
Mockito.when(registryService.getGovernanceSystemRegistry(Mockito.anyInt())).thenReturn(userRegistry1);
List<TermData> list = new ArrayList<TermData>();
TermData termData = new TermData("testTerm", 10);
list.add(termData);
Mockito.when(GovernanceUtils.getTermDataList(Mockito.anyMap(), Mockito.anyString(), Mockito.anyString(), Mockito.anyBoolean())).thenReturn(list);
ResourceDO resourceDO = Mockito.mock(ResourceDO.class);
Resource resource = new ResourceImpl("dw", resourceDO);
resource.setContent("testContent");
Mockito.when(userRegistry1.resourceExists(Mockito.anyString())).thenReturn(true);
Mockito.when(userRegistry1.get(Mockito.anyString())).thenReturn(resource);
assertNotNull(apiConsumer.getTagsWithAttributes("testDomain"));
}
Aggregations