Search in sources :

Example 11 with Parameter

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

the class CommonThrottleMappingUtil method fromDTOToQueryParameterCondition.

/**
 * Converts a Query Parameter Condition DTO object into a model object
 *
 * @param dto Query Parameter Condition DTO object
 * @return Query Parameter Condition model object derived from Query Parameter Condition DTO
 */
public static QueryParameterCondition fromDTOToQueryParameterCondition(ThrottleConditionDTO dto) {
    QueryParameterCondition queryParameterCondition = new QueryParameterCondition();
    queryParameterCondition = updateFieldsFromDTOToCondition(dto, queryParameterCondition);
    queryParameterCondition.setParameter(dto.getQueryParameterCondition().getParameterName());
    queryParameterCondition.setValue(dto.getQueryParameterCondition().getParameterValue());
    return queryParameterCondition;
}
Also used : QueryParameterCondition(org.wso2.carbon.apimgt.core.models.policy.QueryParameterCondition)

Example 12 with Parameter

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

the class ApisApiServiceImpl method apisApiIdSdksLanguageGet.

/**
 * Generates an SDK for a API with provided ID and language
 *
 * @param apiId   API ID
 * @param language   SDK language
 * @param request msf4j request object
 * @return ZIP file for the generated SDK
 * @throws NotFoundException if failed to find method implementation
 */
@Override
public Response apisApiIdSdksLanguageGet(String apiId, String language, Request request) throws NotFoundException {
    if (StringUtils.isBlank(apiId) || StringUtils.isBlank(language)) {
        String errorMessage = "API ID or language is not valid";
        log.error(errorMessage);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorMessage, 400L, errorMessage);
        return Response.status(Response.Status.BAD_REQUEST).entity(errorDTO).build();
    }
    String userName = RestApiUtil.getLoggedInUsername(request);
    ApiStoreSdkGenerationManager sdkGenerationManager = new ApiStoreSdkGenerationManager();
    if (!sdkGenerationManager.getSdkGenLanguages().containsKey(language)) {
        String errorMessage = "Specified language parameter doesn't exist";
        log.error(errorMessage);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorMessage, 400L, errorMessage);
        return Response.status(Response.Status.BAD_REQUEST).entity(errorDTO).build();
    }
    String tempZipFilePath;
    try {
        tempZipFilePath = sdkGenerationManager.generateSdkForApi(apiId, language, userName);
    } catch (ApiStoreSdkGenerationException e) {
        String errorMessage = "Error while generating SDK for requested language";
        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();
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving API for SDK generation " + 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();
    }
    File sdkZipFile = new File(tempZipFilePath);
    return Response.ok().entity(sdkZipFile).header("Content-Disposition", "attachment; filename=\"" + sdkZipFile.getName() + "\"").build();
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) ApiStoreSdkGenerationManager(org.wso2.carbon.apimgt.core.impl.ApiStoreSdkGenerationManager) File(java.io.File) ApiStoreSdkGenerationException(org.wso2.carbon.apimgt.core.exception.ApiStoreSdkGenerationException)

Example 13 with Parameter

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

the class LabelInfoApiServiceImpl method labelInfoGet.

/**
 * Get label information for labels provided.
 *
 * @param labels          List of labels
 * @param ifNoneMatch     If-None-Match header value
 * @param ifModifiedSince If-Modified-Since header value
 * @param request         msf4j request object
 * @return Label List
 * @throws NotFoundException If failed to get the label values
 */
@Override
public Response labelInfoGet(String labels, String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    LabelListDTO labelListDTO;
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        if (labels != null) {
            List<String> labelNames = Arrays.asList(labels.split(","));
            List<Label> labelList = apiStore.getLabelInfo(labelNames, username);
            labelListDTO = LabelMappingUtil.toLabelListDTO(labelList);
        } else {
            // mandatory parameters not provided
            String errorMessage = "Labels parameter should be provided";
            ErrorHandler errorHandler = ExceptionCodes.PARAMETER_NOT_PROVIDED;
            ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorHandler);
            log.error(errorMessage);
            return Response.status(errorHandler.getHttpStatusCode()).entity(errorDTO).build();
        }
    } catch (APIManagementException e) {
        String errorMessage = "Error occurred while retrieving label information";
        HashMap<String, String> paramList = new HashMap<String, String>();
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
    return Response.ok().entity(labelListDTO).build();
}
Also used : ErrorHandler(org.wso2.carbon.apimgt.core.exception.ErrorHandler) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) Label(org.wso2.carbon.apimgt.core.models.Label) LabelListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.LabelListDTO) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 14 with Parameter

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

the class ApplicationMappingUtil method fromApplicationsToDTO.

/**
 * Converts an Application[] array into a corresponding ApplicationListDTO
 *
 * @param applications array of Application objects
 * @param limit limit parameter
 * @param offset starting index
 * @return ApplicationListDTO object corresponding to Application[] array
 */
public static ApplicationListDTO fromApplicationsToDTO(List<Application> applications, int limit, int offset) {
    ApplicationListDTO applicationListDTO = new ApplicationListDTO();
    List<ApplicationInfoDTO> applicationInfoDTOs = applicationListDTO.getList();
    if (applicationInfoDTOs == null) {
        applicationInfoDTOs = new ArrayList<>();
        applicationListDTO.setList(applicationInfoDTOs);
    }
    // identifying the proper start and end indexes
    int start = offset < applications.size() && offset >= 0 ? offset : Integer.MAX_VALUE;
    int end = offset + limit - 1 <= applications.size() - 1 ? offset + limit - 1 : applications.size() - 1;
    for (int i = start; i <= end; i++) {
        applicationInfoDTOs.add(fromApplicationToInfoDTO(applications.get(i)));
    }
    applicationListDTO.setCount(applicationInfoDTOs.size());
    return applicationListDTO;
}
Also used : ApplicationInfoDTO(org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationInfoDTO) ApplicationListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationListDTO)

Example 15 with Parameter

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

the class DefaultKeyManagerImpl method updateApplication.

@Override
public OAuthApplicationInfo updateApplication(OAuthApplicationInfo oAuthApplicationInfo) throws KeyManagementException {
    if (log.isDebugEnabled()) {
        log.debug("Updating OAuth2 application with : " + oAuthApplicationInfo.toString());
    }
    String applicationName = oAuthApplicationInfo.getClientName();
    String keyType = (String) oAuthApplicationInfo.getParameter(KeyManagerConstants.APP_KEY_TYPE);
    if (keyType != null) {
        // Derive oauth2 app name based on key type and user input for app name
        applicationName = applicationName + '_' + keyType;
    }
    DCRClientInfo dcrClientInfo = new DCRClientInfo();
    dcrClientInfo.setClientName(applicationName);
    dcrClientInfo.setClientId(oAuthApplicationInfo.getClientId());
    dcrClientInfo.setClientSecret(oAuthApplicationInfo.getClientSecret());
    dcrClientInfo.addCallbackUrl(oAuthApplicationInfo.getCallBackURL());
    dcrClientInfo.setGrantTypes(oAuthApplicationInfo.getGrantTypes());
    Response response = dcrmServiceStub.updateApplication(dcrClientInfo, dcrClientInfo.getClientId());
    if (response == null) {
        throw new KeyManagementException("Error occurred while updating DCR application. Response is null", ExceptionCodes.OAUTH2_APP_UPDATE_FAILED);
    }
    if (response.status() == APIMgtConstants.HTTPStatusCodes.SC_200_OK) {
        // 200 - Success
        try {
            OAuthApplicationInfo oAuthApplicationInfoResponse = getOAuthApplicationInfo(response);
            // setting original parameter list
            oAuthApplicationInfoResponse.setParameters(oAuthApplicationInfo.getParameters());
            if (log.isDebugEnabled()) {
                log.debug("OAuth2 application updated: " + oAuthApplicationInfoResponse.toString());
            }
            return oAuthApplicationInfoResponse;
        } catch (IOException e) {
            throw new KeyManagementException("Error occurred while parsing the DCR application update response " + "message.", e, ExceptionCodes.OAUTH2_APP_UPDATE_FAILED);
        }
    } else if (response.status() == APIMgtConstants.HTTPStatusCodes.SC_400_BAD_REQUEST) {
        // 400 - Known Error
        try {
            DCRError error = (DCRError) new GsonDecoder().decode(response, DCRError.class);
            throw new KeyManagementException("Error occurred while updating DCR application. Error: " + error.getError() + ". Error Description: " + error.getErrorDescription() + ". Status Code: " + response.status(), ExceptionCodes.OAUTH2_APP_UPDATE_FAILED);
        } catch (IOException e) {
            throw new KeyManagementException("Error occurred while parsing the DCR error message.", e, ExceptionCodes.OAUTH2_APP_UPDATE_FAILED);
        }
    } else {
        // Unknown Error
        throw new KeyManagementException("Error occurred while updating DCR application. Error: " + response.body().toString() + " Status Code: " + response.status(), ExceptionCodes.OAUTH2_APP_UPDATE_FAILED);
    }
}
Also used : OAuth2IntrospectionResponse(org.wso2.carbon.apimgt.core.auth.dto.OAuth2IntrospectionResponse) Response(feign.Response) DCRError(org.wso2.carbon.apimgt.core.auth.dto.DCRError) OAuthApplicationInfo(org.wso2.carbon.apimgt.core.models.OAuthApplicationInfo) GsonDecoder(feign.gson.GsonDecoder) IOException(java.io.IOException) DCRClientInfo(org.wso2.carbon.apimgt.core.auth.dto.DCRClientInfo) KeyManagementException(org.wso2.carbon.apimgt.core.exception.KeyManagementException)

Aggregations

HashMap (java.util.HashMap)35 ArrayList (java.util.ArrayList)32 Parameter (org.apache.axis2.description.Parameter)14 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)14 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)13 JSONDecoder (org.wso2.charon3.core.encoder.JSONDecoder)11 BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)11 CharonException (org.wso2.charon3.core.exceptions.CharonException)11 InternalErrorException (org.wso2.charon3.core.exceptions.InternalErrorException)11 NotFoundException (org.wso2.charon3.core.exceptions.NotFoundException)11 SCIMResponse (org.wso2.charon3.core.protocol.SCIMResponse)11 SCIMResourceTypeSchema (org.wso2.charon3.core.schema.SCIMResourceTypeSchema)11 List (java.util.List)10 Map (java.util.Map)10 ConstantExpressionExecutor (org.wso2.siddhi.core.executor.ConstantExpressionExecutor)9 IOException (java.io.IOException)8 PreparedStatement (java.sql.PreparedStatement)8 ResultSet (java.sql.ResultSet)8 Test (org.testng.annotations.Test)8 BInvokableSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol)8