Search in sources :

Example 21 with APIManagementException

use of org.wso2.carbon.apimgt.core.exception.APIManagementException in project carbon-apimgt by wso2.

the class ServiceDiscovererKubernetes method resolveToken.

/**
 * Get the token after decrypting using {@link FileEncryptionUtility#readFromEncryptedFile(java.lang.String)}
 *
 * @return service account token
 * @throws ServiceDiscoveryException if an error occurs while resolving the token
 */
private String resolveToken(String encryptedTokenFileName) throws ServiceDiscoveryException {
    String token;
    try {
        String externalSATokenFilePath = System.getProperty(FileEncryptionUtility.CARBON_HOME) + FileEncryptionUtility.SECURITY_DIR + File.separator + encryptedTokenFileName;
        token = FileEncryptionUtility.getInstance().readFromEncryptedFile(externalSATokenFilePath);
    } catch (APIManagementException e) {
        String msg = "Error occurred while resolving externally stored token";
        throw new ServiceDiscoveryException(msg, e, ExceptionCodes.ERROR_INITIALIZING_SERVICE_DISCOVERY);
    }
    return StringUtils.replace(token, "\n", "");
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ServiceDiscoveryException(org.wso2.carbon.apimgt.core.exception.ServiceDiscoveryException)

Example 22 with APIManagementException

use of org.wso2.carbon.apimgt.core.exception.APIManagementException in project carbon-apimgt by wso2.

the class BundleActivator method start.

@Activate
protected void start(BundleContext bundleContext) {
    try {
        // Set default timestamp to UTC
        java.util.TimeZone.setDefault(java.util.TimeZone.getTimeZone("Etc/UTC"));
        Context ctx = jndiContextManager.newInitialContext();
        DataSource dataSourceAMDB = new DataSourceImpl((HikariDataSource) ctx.lookup("java:comp/env/jdbc/WSO2AMDB"));
        DAOUtil.initialize(dataSourceAMDB);
        boolean isAnalyticsEnabled = ServiceReferenceHolder.getInstance().getAPIMConfiguration().getAnalyticsConfigurations().isEnabled();
        if (isAnalyticsEnabled) {
            DataSource dataSourceStatDB = new DataSourceImpl((HikariDataSource) ctx.lookup("java:comp/env/jdbc/WSO2AMSTATSDB"));
            DAOUtil.initializeAnalyticsDataSource(dataSourceStatDB);
        }
        WorkflowExtensionsConfigBuilder.build(configProvider);
        ServiceDiscoveryConfigBuilder.build(configProvider);
        ContainerBasedGatewayConfigBuilder.build(configProvider);
        BrokerManager.start();
        Broker broker = new BrokerImpl();
        BrokerUtil.initialize(broker);
    } catch (NamingException e) {
        log.error("Error occurred while jndi lookup", e);
    }
    // deploying default policies
    try {
        ThrottlerUtil.addDefaultAdvancedThrottlePolicies();
        if (log.isDebugEnabled()) {
            log.debug("Checked default throttle policies successfully");
        }
    } catch (APIManagementException e) {
        log.error("Error occurred while deploying default policies", e);
    }
    // securing files
    try {
        boolean fileEncryptionEnabled = ServiceReferenceHolder.getInstance().getAPIMConfiguration().getFileEncryptionConfigurations().isEnabled();
        if (fileEncryptionEnabled) {
            FileEncryptionUtility fileEncryptionUtility = FileEncryptionUtility.getInstance();
            fileEncryptionUtility.init();
            fileEncryptionUtility.encryptFiles();
        }
    } catch (APIManagementException e) {
        log.error("Error occurred while encrypting files", e);
    }
}
Also used : Context(javax.naming.Context) BundleContext(org.osgi.framework.BundleContext) BrokerImpl(org.wso2.carbon.apimgt.core.impl.BrokerImpl) Broker(org.wso2.carbon.apimgt.core.api.Broker) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) FileEncryptionUtility(org.wso2.carbon.apimgt.core.impl.FileEncryptionUtility) DataSourceImpl(org.wso2.carbon.apimgt.core.dao.impl.DataSourceImpl) NamingException(javax.naming.NamingException) DataSource(org.wso2.carbon.apimgt.core.dao.impl.DataSource) HikariDataSource(com.zaxxer.hikari.HikariDataSource) Activate(org.osgi.service.component.annotations.Activate)

Example 23 with APIManagementException

use of org.wso2.carbon.apimgt.core.exception.APIManagementException in project carbon-apimgt by wso2.

the class RestCallUtilImpl method postRequest.

/**
 * {@inheritDoc}
 */
@Override
public HttpResponse postRequest(URI uri, MediaType acceptContentType, List<String> cookies, Entity entity, MediaType payloadContentType, Map<String, String> headers) throws APIManagementException {
    if (uri == null) {
        throw new IllegalArgumentException("The URI must not be null");
    }
    if (entity == null) {
        throw new IllegalArgumentException("Entity must not be null");
    }
    if (payloadContentType == null) {
        throw new IllegalArgumentException("Payload content type must not be null");
    }
    HttpURLConnection httpConnection = null;
    try {
        httpConnection = (HttpURLConnection) uri.toURL().openConnection();
        httpConnection.setRequestMethod(APIMgtConstants.FunctionsConstants.POST);
        httpConnection.setRequestProperty(APIMgtConstants.FunctionsConstants.CONTENT_TYPE, payloadContentType.toString());
        httpConnection.setDoOutput(true);
        if (acceptContentType != null) {
            httpConnection.setRequestProperty(APIMgtConstants.FunctionsConstants.ACCEPT, acceptContentType.toString());
        }
        if (cookies != null && !cookies.isEmpty()) {
            for (String cookie : cookies) {
                httpConnection.addRequestProperty(APIMgtConstants.FunctionsConstants.COOKIE, cookie.split(";", 2)[0]);
            }
        }
        if (headers != null) {
            for (Map.Entry<String, String> header : headers.entrySet()) {
                httpConnection.addRequestProperty(header.getKey(), header.getValue());
            }
        }
        OutputStream outputStream = httpConnection.getOutputStream();
        outputStream.write(entity.getEntity().toString().getBytes(StandardCharsets.UTF_8));
        outputStream.flush();
        outputStream.close();
        return getResponse(httpConnection);
    } catch (IOException e) {
        throw new APIManagementException("Connection not established properly ", e);
    } finally {
        if (httpConnection != null) {
            httpConnection.disconnect();
        }
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) OutputStream(java.io.OutputStream) IOException(java.io.IOException) Map(java.util.Map)

Example 24 with APIManagementException

use of org.wso2.carbon.apimgt.core.exception.APIManagementException in project carbon-apimgt by wso2.

the class RestCallUtilImpl method rsaSignedFetchUserRequest.

/**
 * {@inheritDoc}
 */
@Override
public HttpResponse rsaSignedFetchUserRequest(URI uri, String username, String userTenantDomain, String rsaSignedToken, MediaType acceptContentType) throws APIManagementException {
    if (uri == null) {
        throw new IllegalArgumentException("The URI must not be null");
    }
    if (username == null) {
        throw new IllegalArgumentException("UserName must not be null");
    }
    if (userTenantDomain == null) {
        throw new IllegalArgumentException("User tenant domain must not be null");
    }
    if (rsaSignedToken == null) {
        throw new IllegalArgumentException("RSA signed token must not be null");
    }
    HttpURLConnection httpConnection = null;
    try {
        JSONObject loginInfoJsonObj = new JSONObject();
        loginInfoJsonObj.put(APIMgtConstants.FunctionsConstants.USERNAME, username);
        loginInfoJsonObj.put(APIMgtConstants.FunctionsConstants.USER_TENANT_DOMAIN, userTenantDomain);
        httpConnection = (HttpURLConnection) uri.toURL().openConnection();
        httpConnection.setRequestMethod(APIMgtConstants.FunctionsConstants.POST);
        httpConnection.setRequestProperty(APIMgtConstants.FunctionsConstants.CONTENT_TYPE, MediaType.APPLICATION_JSON);
        httpConnection.setDoOutput(true);
        httpConnection.setRequestProperty(APIMgtConstants.FunctionsConstants.RSA_SIGNED_TOKEN, rsaSignedToken);
        if (acceptContentType != null) {
            httpConnection.setRequestProperty(APIMgtConstants.FunctionsConstants.ACCEPT, acceptContentType.toString());
        }
        OutputStream outputStream = httpConnection.getOutputStream();
        outputStream.write(loginInfoJsonObj.toString().getBytes(StandardCharsets.UTF_8));
        outputStream.flush();
        outputStream.close();
        return getResponse(httpConnection);
    } catch (IOException e) {
        throw new APIManagementException("Connection not established properly ", e);
    } finally {
        if (httpConnection != null) {
            httpConnection.disconnect();
        }
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) JSONObject(org.json.simple.JSONObject) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) OutputStream(java.io.OutputStream) IOException(java.io.IOException)

Example 25 with APIManagementException

use of org.wso2.carbon.apimgt.core.exception.APIManagementException in project carbon-apimgt by wso2.

the class WSO2ISKeyManagerImpl method revokeAccessToken.

// TODO: Remove after revoke endpoint implementation done in key manager.
@Override
public void revokeAccessToken(String accessToken, String clientId, String clientSecret) throws KeyManagementException {
    log.debug("Revoking access token");
    Response response;
    try {
        response = oAuth2ServiceStubs.getRevokeServiceStub().revokeAccessToken(accessToken, clientId, clientSecret);
    } catch (APIManagementException e) {
        throw new KeyManagementException("Error occurred while revoking current access token", e, ExceptionCodes.ACCESS_TOKEN_REVOKE_FAILED);
    }
    if (response == null) {
        throw new KeyManagementException("Error occurred while revoking current access token. " + "Response is null", ExceptionCodes.ACCESS_TOKEN_REVOKE_FAILED);
    }
    if (response.status() == APIMgtConstants.HTTPStatusCodes.SC_200_OK) {
        if (log.isDebugEnabled()) {
            log.debug("Successfully revoked access token: " + accessToken);
        }
    } else {
        throw new KeyManagementException("Token revocation failed. HTTP error code: " + response.status() + " Error Response Body: " + response.body().toString(), ExceptionCodes.ACCESS_TOKEN_REVOKE_FAILED);
    }
}
Also used : Response(feign.Response) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) KeyManagementException(org.wso2.carbon.apimgt.core.exception.KeyManagementException)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)432 Test (org.testng.annotations.Test)353 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)233 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)202 API (org.wso2.carbon.apimgt.core.models.API)200 Test (org.junit.Test)164 Response (javax.ws.rs.core.Response)160 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)159 HashMap (java.util.HashMap)148 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)146 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)134 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)129 ArrayList (java.util.ArrayList)102 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)100 BeforeTest (org.testng.annotations.BeforeTest)82 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)80 Request (org.wso2.msf4j.Request)80 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)76 APILifecycleManager (org.wso2.carbon.apimgt.core.api.APILifecycleManager)75 GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)71