Search in sources :

Example 16 with ResponseCode

use of org.wso2.carbon.apimgt.impl.certificatemgt.ResponseCode in project carbon-apimgt by wso2.

the class APIPublisherImpl method addApiFromDefinition.

@Override
public String addApiFromDefinition(HttpURLConnection urlConn) throws APIManagementException {
    try {
        urlConn.setDoOutput(true);
        urlConn.setRequestMethod(APIMgtConstants.HTTP_GET);
        urlConn.connect();
        int responseCode = urlConn.getResponseCode();
        if (responseCode == 200) {
            return addApiFromDefinition(urlConn.getInputStream());
        } else {
            throw new APIManagementException("Error while getting swagger resource from url : " + urlConn.getURL(), ExceptionCodes.API_DEFINITION_MALFORMED);
        }
    } catch (ProtocolException e) {
        String msg = "Protocol exception while getting the swagger resource from url";
        log.error(msg, e);
        throw new APIManagementException(msg, ExceptionCodes.API_DEFINITION_MALFORMED);
    } catch (IOException e) {
        String msg = "Error while getting the swagger resource from url";
        log.error(msg, e);
        throw new APIManagementException(msg, ExceptionCodes.API_DEFINITION_MALFORMED);
    }
}
Also used : ProtocolException(java.net.ProtocolException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) IOException(java.io.IOException) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint)

Example 17 with ResponseCode

use of org.wso2.carbon.apimgt.impl.certificatemgt.ResponseCode in project carbon-apimgt by wso2.

the class CertificateManagerImpl method deleteCertificateFromParentNode.

@Override
public ResponseCode deleteCertificateFromParentNode(String alias, String endpoint, int tenantId) {
    try {
        List<CertificateMetadataDTO> certificateMetadataDTOList = certificateMgtDAO.getCertificates(alias, null, tenantId);
        if (certificateMetadataDTOList != null && certificateMetadataDTOList.size() == 1) {
            CertificateMetadataDTO certificate = certificateMetadataDTOList.get(0);
            boolean removeFromDB = certificateMgtDAO.deleteCertificate(alias, endpoint, tenantId);
            if (removeFromDB) {
                ResponseCode responseCode = certificateMgtUtils.removeCertificateFromTrustStore(alias);
                if (responseCode == ResponseCode.INTERNAL_SERVER_ERROR) {
                    certificateMgtDAO.addCertificate(certificate.getCertificate(), alias, endpoint, tenantId);
                    log.error("Error removing the Certificate from Trust Store. Rolling back...");
                } else if (responseCode.getResponseCode() == ResponseCode.CERTIFICATE_NOT_FOUND.getResponseCode()) {
                    log.warn("The Certificate for Alias '" + alias + "' has been previously removed from " + "Trust Store. Hence DB entry is removed.");
                } else {
                    log.info("Certificate is successfully removed from the Publisher Trust Store with Alias '" + alias + "'");
                }
                return responseCode;
            } else {
                log.error("Failed to remove certificate from the data base. No certificate changes will be affected" + ".");
                return ResponseCode.INTERNAL_SERVER_ERROR;
            }
        }
    } catch (CertificateManagementException e) {
        log.error("Error persisting/ deleting certificate metadata. ", e);
        return ResponseCode.INTERNAL_SERVER_ERROR;
    } catch (CertificateAliasExistsException e) {
        return ResponseCode.ALIAS_EXISTS_IN_TRUST_STORE;
    }
    return ResponseCode.CERTIFICATE_NOT_FOUND;
}
Also used : CertificateMetadataDTO(org.wso2.carbon.apimgt.api.dto.CertificateMetadataDTO) CertificateAliasExistsException(org.wso2.carbon.apimgt.impl.certificatemgt.exceptions.CertificateAliasExistsException) CertificateManagementException(org.wso2.carbon.apimgt.impl.certificatemgt.exceptions.CertificateManagementException)

Example 18 with ResponseCode

use of org.wso2.carbon.apimgt.impl.certificatemgt.ResponseCode in project carbon-apimgt by wso2.

the class CertificateManagerImplTest method testRemoveFromPublisherInternalServerError.

@Test
public void testRemoveFromPublisherInternalServerError() throws CertificateManagementException {
    PowerMockito.stub(PowerMockito.method(CertificateMgtUtils.class, "removeCertificateFromTrustStore", String.class)).toReturn(ResponseCode.SUCCESS);
    Mockito.when(certificateMgtDAO.deleteCertificate(ALIAS, END_POINT, TENANT_ID)).thenReturn(false);
    List<CertificateMetadataDTO> certificateMetadataDTOList = new ArrayList<>();
    CertificateMetadataDTO certificateMetadataDTO = new CertificateMetadataDTO();
    certificateMetadataDTO.setAlias(ALIAS);
    certificateMetadataDTO.setEndpoint(END_POINT);
    certificateMetadataDTOList.add(certificateMetadataDTO);
    Mockito.when(certificateMgtDAO.getCertificates(ALIAS, null, TENANT_ID)).thenReturn(certificateMetadataDTOList);
    ResponseCode responseCode = certificateManager.deleteCertificateFromParentNode(ALIAS, END_POINT, TENANT_ID);
    Assert.assertEquals(ResponseCode.INTERNAL_SERVER_ERROR, responseCode);
}
Also used : CertificateMetadataDTO(org.wso2.carbon.apimgt.api.dto.CertificateMetadataDTO) ArrayList(java.util.ArrayList) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 19 with ResponseCode

use of org.wso2.carbon.apimgt.impl.certificatemgt.ResponseCode in project carbon-apimgt by wso2.

the class CertificateManagerImplTest method testRemoveFromPublisherWithInternalServerErrorWhenDeleting.

@Test
public void testRemoveFromPublisherWithInternalServerErrorWhenDeleting() {
    PowerMockito.stub(PowerMockito.method(CertificateMgtUtils.class, "removeCertificateFromTrustStore", String.class)).toReturn(ResponseCode.INTERNAL_SERVER_ERROR);
    try {
        Mockito.when(certificateMgtDAO.deleteCertificate("testRemoveFromPublisherWithInternalServerErrorWhenDeleting", "testRemoveFromPublisherWithInternalServerErrorWhenDeleting", TENANT_ID)).thenReturn(true);
        Mockito.when(certificateMgtDAO.addCertificate(BASE64_ENCODED_CERT, "testRemoveFromPublisherWithInternalServerErrorWhenDeleting", "testRemoveFromPublisherWithInternalServerErrorWhenDeleting", TENANT_ID)).thenReturn(true);
        CertificateMetadataDTO certificateMetadataDTO = new CertificateMetadataDTO();
        certificateMetadataDTO.setEndpoint("testRemoveFromPublisherWithInternalServerErrorWhenDeleting");
        certificateMetadataDTO.setCertificate(BASE64_ENCODED_CERT);
        certificateMetadataDTO.setAlias("testRemoveFromPublisherWithInternalServerErrorWhenDeleting");
        Mockito.when(certificateMgtDAO.getCertificates("testRemoveFromPublisherWithInternalServerErrorWhenDeleting", null, TENANT_ID)).thenReturn(Arrays.asList(certificateMetadataDTO));
    } catch (CertificateManagementException | CertificateAliasExistsException e) {
        e.printStackTrace();
    }
    ResponseCode responseCode = certificateManager.deleteCertificateFromParentNode("testRemoveFromPublisherWithInternalServerErrorWhenDeleting", "testRemoveFromPublisherWithInternalServerErrorWhenDeleting", TENANT_ID);
    Assert.assertEquals(ResponseCode.INTERNAL_SERVER_ERROR, responseCode);
}
Also used : CertificateMetadataDTO(org.wso2.carbon.apimgt.api.dto.CertificateMetadataDTO) CertificateAliasExistsException(org.wso2.carbon.apimgt.impl.certificatemgt.exceptions.CertificateAliasExistsException) CertificateManagementException(org.wso2.carbon.apimgt.impl.certificatemgt.exceptions.CertificateManagementException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 20 with ResponseCode

use of org.wso2.carbon.apimgt.impl.certificatemgt.ResponseCode in project carbon-apimgt by wso2.

the class CertificateManagerImplTest method testEmptyCertAddToGateway.

@Test
public void testEmptyCertAddToGateway() throws NoSuchFieldException, IllegalAccessException {
    CertificateMgtUtils certificateMgtUtils = CertificateMgtUtils.getInstance();
    ResponseCode responseCode = certificateMgtUtils.addCertificateToTrustStore("", "testalias");
    Assert.assertEquals(ResponseCode.INTERNAL_SERVER_ERROR.getResponseCode(), responseCode.getResponseCode());
}
Also used : CertificateMgtUtils(org.wso2.carbon.apimgt.impl.utils.CertificateMgtUtils) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

ResponseCode (org.wso2.carbon.apimgt.impl.certificatemgt.ResponseCode)18 Test (org.junit.Test)17 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)9 IOException (java.io.IOException)8 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)6 ArrayList (java.util.ArrayList)5 CertificateMetadataDTO (org.wso2.carbon.apimgt.api.dto.CertificateMetadataDTO)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 UserStoreException (org.wso2.carbon.user.api.UserStoreException)4 URI (java.net.URI)3 URISyntaxException (java.net.URISyntaxException)3 ContentDisposition (org.apache.cxf.jaxrs.ext.multipart.ContentDisposition)3 FaultGatewaysException (org.wso2.carbon.apimgt.api.FaultGatewaysException)3 API (org.wso2.carbon.apimgt.api.model.API)3 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)3 APIProduct (org.wso2.carbon.apimgt.api.model.APIProduct)3 APIProductIdentifier (org.wso2.carbon.apimgt.api.model.APIProductIdentifier)3 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)3 CertificateAliasExistsException (org.wso2.carbon.apimgt.impl.certificatemgt.exceptions.CertificateAliasExistsException)3 CertificateManagementException (org.wso2.carbon.apimgt.impl.certificatemgt.exceptions.CertificateManagementException)3