Search in sources :

Example 71 with Environment

use of org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.environmentspecificproperty.Environment in project carbon-apimgt by wso2.

the class APIControllerUtil method handleEndpointSecurityConfigs.

/**
 * This method will be used to add Endpoint security related environment parameters to imported Api object.
 *
 * @param envParams      Env params object with required parameters
 * @param endpointConfig Endpoint config object to be updated
 * @throws APIManagementException If an error occurs when setting security env parameters
 */
private static void handleEndpointSecurityConfigs(JsonObject envParams, JsonObject endpointConfig) throws APIManagementException {
    // If the user has set (either true or false) the enabled field under security in the params file,
    // the following code should be executed.
    JsonObject security = envParams.getAsJsonObject(ImportExportConstants.ENDPOINT_SECURITY_FIELD);
    if (security == null) {
        return;
    }
    String[] endpointTypes = { APIConstants.ENDPOINT_SECURITY_PRODUCTION, APIConstants.ENDPOINT_SECURITY_SANDBOX };
    for (String endpointType : endpointTypes) {
        if (security.has(endpointType)) {
            JsonObject endpointSecurityDetails = security.get(endpointType).getAsJsonObject();
            if (endpointSecurityDetails.has(APIConstants.ENDPOINT_SECURITY_ENABLED) && (endpointSecurityDetails.get(APIConstants.ENDPOINT_SECURITY_ENABLED) != null || !endpointSecurityDetails.get(APIConstants.ENDPOINT_SECURITY_ENABLED).isJsonNull())) {
                boolean securityEnabled = endpointSecurityDetails.get(APIConstants.ENDPOINT_SECURITY_ENABLED).getAsBoolean();
                // Set endpoint security details to API
                if (securityEnabled) {
                    String endpointSecurityType;
                    if (endpointSecurityDetails.has(APIConstants.ENDPOINT_SECURITY_TYPE) && (endpointSecurityDetails.get(APIConstants.ENDPOINT_SECURITY_TYPE) != null || !endpointSecurityDetails.get(APIConstants.ENDPOINT_SECURITY_TYPE).isJsonNull())) {
                        // Check whether the type is defined in the params file
                        JsonElement type = endpointSecurityDetails.get(APIConstants.ENDPOINT_SECURITY_TYPE);
                        endpointSecurityType = type.getAsString();
                    } else {
                        throw new APIManagementException("You have enabled endpoint security but the type is not found " + "in the params file. Please specify type field and continue...", ExceptionCodes.ERROR_READING_PARAMS_FILE);
                    }
                    // Setup security type (basic, digest or oauth)
                    endpointSecurityDetails.remove(APIConstants.ENDPOINT_SECURITY_TYPE);
                    if (StringUtils.equals(endpointSecurityType.toLowerCase(), APIConstants.ENDPOINT_SECURITY_TYPE_DIGEST)) {
                        endpointSecurityDetails.addProperty(APIConstants.ENDPOINT_SECURITY_TYPE, APIConstants.ENDPOINT_SECURITY_TYPE_DIGEST.toUpperCase());
                        validateEndpointSecurityUsernamePassword(endpointSecurityDetails);
                    } else if (StringUtils.equals(endpointSecurityType.toLowerCase(), APIConstants.ENDPOINT_SECURITY_TYPE_BASIC)) {
                        endpointSecurityDetails.addProperty(APIConstants.ENDPOINT_SECURITY_TYPE, APIConstants.ENDPOINT_SECURITY_TYPE_BASIC.toUpperCase());
                        validateEndpointSecurityUsernamePassword(endpointSecurityDetails);
                    } else if (StringUtils.equals(endpointSecurityType.toLowerCase(), APIConstants.ENDPOINT_SECURITY_TYPE_OAUTH)) {
                        endpointSecurityDetails.addProperty(APIConstants.ENDPOINT_SECURITY_TYPE, APIConstants.ENDPOINT_SECURITY_TYPE_OAUTH.toUpperCase());
                        validateEndpointSecurityOauth(endpointSecurityDetails);
                    } else {
                        // If the type is not either basic or digest, return an error
                        throw new APIManagementException("Invalid endpoint security type found in the params file. " + "Should be either basic, digest or oauth. " + "Please specify correct security types field and continue...", ExceptionCodes.ERROR_READING_PARAMS_FILE);
                    }
                } else {
                    endpointSecurityDetails.addProperty(APIConstants.ENDPOINT_SECURITY_TYPE, ImportExportConstants.ENDPOINT_NONE_SECURITY_TYPE);
                }
            }
        } else {
            // Even though the security field is defined, if either production/sandbox is not defined
            // under that,set endpoint security to none. Otherwise the security will be blank if you
            // check from the UI.
            JsonObject endpointSecurityForNotDefinedEndpointType = new JsonObject();
            endpointSecurityForNotDefinedEndpointType.addProperty(APIConstants.ENDPOINT_SECURITY_TYPE, ImportExportConstants.ENDPOINT_NONE_SECURITY_TYPE);
            endpointSecurityForNotDefinedEndpointType.addProperty(APIConstants.ENDPOINT_SECURITY_ENABLED, Boolean.FALSE);
            security.add(endpointType, endpointSecurityForNotDefinedEndpointType);
        }
    }
    endpointConfig.add(APIConstants.ENDPOINT_SECURITY, security);
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject)

Example 72 with Environment

use of org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.environmentspecificproperty.Environment in project carbon-apimgt by wso2.

the class APIControllerUtil method handleEndpointValues.

/**
 * This method will production and sandbox endpoint values.
 *
 * @param endpointConfigs           Endpoint configurations to be updated
 * @param updatedEndpointParams     Updated endpoint parameters object
 * @param defaultProductionEndpoint Default production endpoint json object
 * @param defaultSandboxEndpoint    Default sandbox endpoint json object
 */
private static void handleEndpointValues(JsonObject endpointConfigs, JsonObject updatedEndpointParams, JsonObject defaultProductionEndpoint, JsonObject defaultSandboxEndpoint) throws APIManagementException {
    // check api params file to get provided endpoints
    if (endpointConfigs == null) {
        updatedEndpointParams.add(ImportExportConstants.PRODUCTION_ENDPOINTS_PROPERTY, defaultProductionEndpoint);
        updatedEndpointParams.add(ImportExportConstants.SANDBOX_ENDPOINTS_PROPERTY, defaultSandboxEndpoint);
    } else {
        // handle production endpoints
        if (endpointConfigs.get(ImportExportConstants.PRODUCTION_ENDPOINTS_JSON_PROPERTY) != null) {
            updatedEndpointParams.add(ImportExportConstants.PRODUCTION_ENDPOINTS_PROPERTY, endpointConfigs.get(ImportExportConstants.PRODUCTION_ENDPOINTS_JSON_PROPERTY));
        }
        // handle sandbox endpoints
        if (endpointConfigs.get(ImportExportConstants.SANDBOX_ENDPOINTS_JSON_PROPERTY) != null) {
            updatedEndpointParams.add(ImportExportConstants.SANDBOX_ENDPOINTS_PROPERTY, endpointConfigs.get(ImportExportConstants.SANDBOX_ENDPOINTS_JSON_PROPERTY));
        }
        if (updatedEndpointParams.get(ImportExportConstants.SANDBOX_ENDPOINTS_PROPERTY) == null && updatedEndpointParams.get(ImportExportConstants.PRODUCTION_ENDPOINTS_PROPERTY) == null) {
            throw new APIManagementException("Please specify production sandbox or endpoints for the environment and continue...", ExceptionCodes.ERROR_READING_PARAMS_FILE);
        } else if ((updatedEndpointParams.get(ImportExportConstants.SANDBOX_ENDPOINTS_PROPERTY) != null) && (updatedEndpointParams.get(ImportExportConstants.SANDBOX_ENDPOINTS_PROPERTY).isJsonNull())) {
            if ((updatedEndpointParams.get(ImportExportConstants.PRODUCTION_ENDPOINTS_PROPERTY) != null) && updatedEndpointParams.get(ImportExportConstants.PRODUCTION_ENDPOINTS_PROPERTY).isJsonNull()) {
                throw new APIManagementException("Please specify production or sandbox endpoints for the environment and continue...", ExceptionCodes.ERROR_READING_PARAMS_FILE);
            }
        }
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException)

Example 73 with Environment

use of org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.environmentspecificproperty.Environment in project carbon-apimgt by wso2.

the class EnvironmentMappingUtil method fromEnvironmentCollectionToDTO.

/**
 * Converts a List object of SubscribedAPIs into a DTO.
 *
 * @param environmentCollection a collection of Environment objects
 * @return EnvironmentListDTO object containing EnvironmentDTOs
 */
public static EnvironmentListDTO fromEnvironmentCollectionToDTO(Collection<Environment> environmentCollection) {
    EnvironmentListDTO environmentListDTO = new EnvironmentListDTO();
    List<EnvironmentDTO> environmentDTOs = environmentListDTO.getList();
    if (environmentDTOs == null) {
        environmentDTOs = new ArrayList<>();
        environmentListDTO.setList(environmentDTOs);
    }
    for (Environment environment : environmentCollection) {
        environmentDTOs.add(fromEnvironmentToDTO(environment));
    }
    environmentListDTO.setCount(environmentDTOs.size());
    return environmentListDTO;
}
Also used : EnvironmentListDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.EnvironmentListDTO) EnvironmentDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.EnvironmentDTO) ExternalEnvironment(org.wso2.carbon.apimgt.impl.ExternalEnvironment) Environment(org.wso2.carbon.apimgt.api.model.Environment)

Example 74 with Environment

use of org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.environmentspecificproperty.Environment in project carbon-apimgt by wso2.

the class APITemplateBuilderImpl method getConfigStringForPrototypeScriptAPI.

@Override
public String getConfigStringForPrototypeScriptAPI(Environment environment) throws APITemplateException {
    StringWriter writer = new StringWriter();
    try {
        // build the context for template and apply the necessary decorators
        ConfigContext configcontext = new APIConfigContext(this.api);
        configcontext = new TransportConfigContext(configcontext, api);
        configcontext = new ResourceConfigContext(configcontext, api);
        configcontext = new EndpointBckConfigContext(configcontext, api);
        configcontext = new EndpointConfigContext(configcontext, api);
        configcontext = new SecurityConfigContext(configcontext, api);
        configcontext = new JwtConfigContext(configcontext);
        configcontext = new ResponseCacheConfigContext(configcontext, api);
        configcontext = new HandlerConfigContex(configcontext, handlers);
        configcontext = new EnvironmentConfigContext(configcontext, environment);
        configcontext = new TemplateUtilContext(configcontext);
        // @todo: this validation might be better to do when the builder is initialized.
        configcontext.validate();
        VelocityContext context = configcontext.getContext();
        context.internalGetKeys();
        /*  first, initialize velocity engine  */
        VelocityEngine velocityengine = new VelocityEngine();
        APIUtil.initializeVelocityContext(velocityengine);
        velocityengine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, CarbonUtils.getCarbonHome());
        initVelocityEngine(velocityengine);
        Template t = velocityengine.getTemplate(this.getPrototypeTemplatePath());
        t.merge(context, writer);
    } catch (Exception e) {
        log.error("Velocity Error", e);
        throw new APITemplateException("Velocity Error", e);
    }
    return writer.toString();
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) VelocityContext(org.apache.velocity.VelocityContext) UserStoreException(org.wso2.carbon.user.api.UserStoreException) RegistryException(org.wso2.carbon.registry.api.RegistryException) APITemplateException(org.wso2.carbon.apimgt.impl.template.APITemplateException) Template(org.apache.velocity.Template) StringWriter(java.io.StringWriter) APITemplateException(org.wso2.carbon.apimgt.impl.template.APITemplateException)

Example 75 with Environment

use of org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.environmentspecificproperty.Environment in project carbon-apimgt by wso2.

the class WebSubConfigContextTest method testWithSecretConfigContextForAPI.

@Test
public void testWithSecretConfigContextForAPI() throws Exception {
    API api = new API(new APIIdentifier("admin", "websubAPI", "1.0.0"));
    api.setStatus(APIConstants.CREATED);
    api.setContextTemplate("/websub");
    api.setTransports(Constants.TRANSPORT_HTTP);
    api.setEndpointSecured(false);
    api.setUriTemplates(setAPIUriTemplates());
    api.setType(APIConstants.APITransportType.WEBSUB.toString());
    WebsubSubscriptionConfiguration webSubConfig = new WebsubSubscriptionConfiguration(true, "9207975e1fef9c41fab41645f81dbf0f", "SHA1", "x-hub-signature");
    api.setWebsubSubscriptionConfiguration(webSubConfig);
    Environment environment = new Environment();
    environment.setType("production");
    config = Mockito.mock(APIManagerConfiguration.class);
    APIManagerConfigurationService apiManagerConfigurationService = new APIManagerConfigurationServiceImpl(config);
    ServiceReferenceHolder.getInstance().setAPIManagerConfigurationService(apiManagerConfigurationService);
    String templatePath = System.getProperty("user.dir") + File.separator + "src" + File.separator + "test" + File.separator + "resources" + File.separator;
    System.setProperty("carbon.home", templatePath);
    APITemplateBuilderImpl apiTemplateBuilder = new APITemplateBuilderImpl(api, null, null);
    String updatedTemplate = apiTemplateBuilder.getConfigStringForTemplate(environment);
    Assert.assertTrue("The websub velocity template is not updated correctly", updatedTemplate.contains("generated_signature"));
}
Also used : APIManagerConfiguration(org.wso2.carbon.apimgt.impl.APIManagerConfiguration) APIManagerConfigurationService(org.wso2.carbon.apimgt.impl.APIManagerConfigurationService) APIManagerConfigurationServiceImpl(org.wso2.carbon.apimgt.impl.APIManagerConfigurationServiceImpl) Environment(org.wso2.carbon.apimgt.api.model.Environment) API(org.wso2.carbon.apimgt.api.model.API) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) WebsubSubscriptionConfiguration(org.wso2.carbon.apimgt.api.model.WebsubSubscriptionConfiguration) APITemplateBuilderImpl(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.APITemplateBuilderImpl) Test(org.junit.Test)

Aggregations

Environment (org.wso2.carbon.apimgt.api.model.Environment)67 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)53 ArrayList (java.util.ArrayList)35 HashMap (java.util.HashMap)25 API (org.wso2.carbon.apimgt.api.model.API)22 IOException (java.io.IOException)19 APIRevisionDeployment (org.wso2.carbon.apimgt.api.model.APIRevisionDeployment)19 Map (java.util.Map)15 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)13 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)12 JsonObject (com.google.gson.JsonObject)10 PreparedStatement (java.sql.PreparedStatement)10 SQLException (java.sql.SQLException)10 HashSet (java.util.HashSet)10 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)10 SolaceAdminApis (org.wso2.carbon.apimgt.solace.SolaceAdminApis)10 Gson (com.google.gson.Gson)9 JSONObject (org.json.simple.JSONObject)9 Test (org.junit.Test)9 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)9