Search in sources :

Example 1 with APIStore

use of org.wso2.carbon.apimgt.api.model.APIStore in project carbon-apimgt by wso2.

the class APIManagerFactory method newConsumer.

private APIStore newConsumer(String username) throws APIManagementException {
    // }
    try {
        APIStoreImpl userAwareAPIStore = new APIStoreImpl(username, getIdentityProvider(), getKeyManager(), DAOFactory.getApiDAO(), DAOFactory.getApplicationDAO(), DAOFactory.getAPISubscriptionDAO(), DAOFactory.getPolicyDAO(), DAOFactory.getTagDAO(), DAOFactory.getLabelDAO(), DAOFactory.getWorkflowDAO(), new GatewaySourceGeneratorImpl(), new APIGatewayPublisherImpl());
        // Register all the observers which need to observe 'Store' component
        userAwareAPIStore.registerObserver(new EventLogger());
        userAwareAPIStore.registerObserver(new FunctionTrigger(DAOFactory.getFunctionDAO(), new RestCallUtilImpl()));
        return userAwareAPIStore;
    } catch (APIMgtDAOException e) {
        log.error("Couldn't Create API Consumer", e);
        throw new APIMgtDAOException("Couldn't Create API Consumer", ExceptionCodes.APIMGT_DAO_EXCEPTION);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)

Example 2 with APIStore

use of org.wso2.carbon.apimgt.api.model.APIStore in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisApiIdGet.

/**
 * Get API of given ID
 *
 * @param apiId           API ID
 * @param ifNoneMatch     If-None-Match header value
 * @param ifModifiedSince If-Modified-Since header value
 * @param request         msf4j request object
 * @return API of the given ID
 * @throws NotFoundException If failed to get the API
 */
@Override
public Response apisApiIdGet(String apiId, String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
    APIDTO apiToReturn = null;
    try {
        String username = RestApiUtil.getLoggedInUsername(request);
        APIStore apiStore = RestApiUtil.getConsumer(username);
        String existingFingerprint = apisApiIdGetFingerprint(apiId, ifNoneMatch, ifModifiedSince, request);
        if (!StringUtils.isEmpty(ifNoneMatch) && !StringUtils.isEmpty(existingFingerprint) && ifNoneMatch.contains(existingFingerprint)) {
            return Response.notModified().build();
        }
        API api = apiStore.getAPIbyUUID(apiId);
        boolean isWSDLExists = apiStore.isWSDLExists(apiId);
        apiToReturn = APIMappingUtil.toAPIDTO(api);
        if (isWSDLExists) {
            String wsdlUri = RestApiConstants.WSDL_URI_TEMPLATE.replace(RestApiConstants.APIID_PARAM, api.getId());
            apiToReturn.setWsdlUri(wsdlUri);
        }
        return Response.ok().entity(apiToReturn).header(HttpHeaders.ETAG, "\"" + existingFingerprint + "\"").build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving API : " + apiId;
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : APIDTO(org.wso2.carbon.apimgt.rest.api.store.dto.APIDTO) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) API(org.wso2.carbon.apimgt.core.models.API) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 3 with APIStore

use of org.wso2.carbon.apimgt.api.model.APIStore in project carbon-apimgt by wso2.

the class ApplicationsApiServiceImpl method applicationsApplicationIdKeysKeyTypePut.

/**
 * Update grant types/callback URL
 *
 * @param applicationId Application Id
 * @param keyType       Key Type (Production | Sandbox)
 * @param body          Grant type and callback URL information
 * @param request       msf4j request object
 * @return Updated Key Information
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response applicationsApplicationIdKeysKeyTypePut(String applicationId, String keyType, ApplicationKeysDTO body, Request request) throws NotFoundException {
    try {
        String username = RestApiUtil.getLoggedInUsername(request);
        APIStore apiConsumer = RestApiUtil.getConsumer(username);
        OAuthApplicationInfo oAuthApp = apiConsumer.updateGrantTypesAndCallbackURL(applicationId, keyType, body.getSupportedGrantTypes(), body.getCallbackUrl());
        ApplicationKeysDTO appKeys = ApplicationKeyMappingUtil.fromApplicationKeysToDTO(oAuthApp);
        return Response.ok().entity(appKeys).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error occurred while updating '" + keyType + "'-type grant types/callback url of " + "application: " + applicationId;
        Map<String, String> paramList = new HashMap<>();
        paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_ID, applicationId);
        paramList.put(APIMgtConstants.ExceptionsConstants.KEY_TYPE, keyType);
        paramList.put(APIMgtConstants.ExceptionsConstants.CALLBACK_URL, body.getCallbackUrl());
        String grantTypes = null;
        if (body.getSupportedGrantTypes() != null) {
            grantTypes = String.join(",", body.getSupportedGrantTypes());
        }
        paramList.put(APIMgtConstants.ExceptionsConstants.GRANT_TYPES, grantTypes);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : ApplicationKeysDTO(org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationKeysDTO) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) OAuthApplicationInfo(org.wso2.carbon.apimgt.core.models.OAuthApplicationInfo) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) HashMap(java.util.HashMap) Map(java.util.Map) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 4 with APIStore

use of org.wso2.carbon.apimgt.api.model.APIStore in project carbon-apimgt by wso2.

the class ApplicationsApiServiceImpl method applicationsApplicationIdMapKeysPost.

/**
 * Map application keys
 *
 * @param applicationId   Application ID
 * @param body            Application key mapping information
 * @param request         msf4j request object
 * @return Generated application key detials
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response applicationsApplicationIdMapKeysPost(String applicationId, ApplicationKeyMappingRequestDTO body, Request request) throws NotFoundException {
    try {
        String username = RestApiUtil.getLoggedInUsername(request);
        APIStore apiConsumer = RestApiUtil.getConsumer(username);
        OAuthApplicationInfo oAuthApp = apiConsumer.mapApplicationKeys(applicationId, body.getKeyType().name(), body.getConsumerKey(), body.getConsumerSecret());
        ApplicationKeysDTO appKeys = ApplicationKeyMappingUtil.fromApplicationKeysToDTO(oAuthApp);
        return Response.ok().entity(appKeys).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error occurred while mapping application keys with application: " + applicationId;
        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();
    }
}
Also used : ApplicationKeysDTO(org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationKeysDTO) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) OAuthApplicationInfo(org.wso2.carbon.apimgt.core.models.OAuthApplicationInfo) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) HashMap(java.util.HashMap) Map(java.util.Map) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 5 with APIStore

use of org.wso2.carbon.apimgt.api.model.APIStore in project carbon-apimgt by wso2.

the class ApplicationsApiServiceImpl method applicationsPost.

/**
 * Adds a new application
 *
 * @param body        Application details to be added
 * @param request     msf4j request object
 * @return 201 Response if successful
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response applicationsPost(ApplicationDTO body, Request request) throws NotFoundException {
    URI location = null;
    ApplicationDTO createdApplicationDTO = null;
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIStore apiConsumer = RestApiUtil.getConsumer(username);
        Application application = ApplicationMappingUtil.fromDTOtoApplication(body, username);
        ApplicationCreationResponse applicationResponse = apiConsumer.addApplication(application);
        String applicationUUID = applicationResponse.getApplicationUUID();
        Application createdApplication = apiConsumer.getApplication(applicationUUID, username);
        createdApplicationDTO = ApplicationMappingUtil.fromApplicationToDTO(createdApplication);
        location = new URI(RestApiConstants.RESOURCE_PATH_APPLICATIONS + "/" + createdApplicationDTO.getApplicationId());
        // be in either pending or approved state) send back the workflow response
        if (ApplicationStatus.APPLICATION_ONHOLD.equals(createdApplication.getStatus())) {
            WorkflowResponseDTO workflowResponse = MiscMappingUtil.fromWorkflowResponseToDTO(applicationResponse.getWorkflowResponse());
            return Response.status(Response.Status.ACCEPTED).header(RestApiConstants.LOCATION_HEADER, location).entity(workflowResponse).build();
        }
    } catch (APIManagementException e) {
        String errorMessage = "Error while adding new 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();
        HashMap<String, String> paramList = new HashMap<String, String>();
        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();
    }
    // .build()
    return Response.status(Response.Status.CREATED).header(RestApiConstants.LOCATION_HEADER, location).entity(createdApplicationDTO).build();
}
Also used : ApplicationDTO(org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationDTO) ErrorHandler(org.wso2.carbon.apimgt.core.exception.ErrorHandler) ApplicationCreationResponse(org.wso2.carbon.apimgt.core.workflow.ApplicationCreationResponse) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) WorkflowResponseDTO(org.wso2.carbon.apimgt.rest.api.store.dto.WorkflowResponseDTO) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) Application(org.wso2.carbon.apimgt.core.models.Application) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Aggregations

APIStore (org.wso2.carbon.apimgt.core.api.APIStore)226 Test (org.testng.annotations.Test)109 Test (org.junit.Test)98 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)96 BeforeTest (org.testng.annotations.BeforeTest)93 Response (javax.ws.rs.core.Response)90 Request (org.wso2.msf4j.Request)87 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)72 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)56 API (org.wso2.carbon.apimgt.core.models.API)51 HashMap (java.util.HashMap)49 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)49 ArrayList (java.util.ArrayList)47 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)47 Application (org.wso2.carbon.apimgt.core.models.Application)46 ApplicationDAO (org.wso2.carbon.apimgt.core.dao.ApplicationDAO)38 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)32 SQLException (java.sql.SQLException)31 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)31 GeneralWorkflowResponse (org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse)29