Search in sources :

Example 71 with Label

use of org.wso2.carbon.apimgt.core.models.Label in project carbon-apimgt by wso2.

the class KubernetesGatewayImplTestCase method testRemoveContainerBasedGateway.

@Test
public void testRemoveContainerBasedGateway() throws Exception {
    OpenShiftClient openShiftClient = Mockito.mock(OpenShiftClient.class, Mockito.RETURNS_DEEP_STUBS);
    KubernetesGatewayImpl kubernetesGateway = getKubernetesGatewayImpl(openShiftClient);
    NonNamespaceOperation nonNamespaceOperation = Mockito.mock(NonNamespaceOperation.class);
    BaseOperation baseOperation = Mockito.mock(BaseOperation.class);
    Mockito.when(openShiftClient.services().inNamespace(NAMESPACE)).thenReturn(nonNamespaceOperation);
    Mockito.when(nonNamespaceOperation.withLabel(Mockito.anyString(), Mockito.anyString())).thenReturn(baseOperation);
    Mockito.when(baseOperation.delete()).thenReturn(true);
    Mockito.when(openShiftClient.extensions().deployments().inNamespace(NAMESPACE)).thenReturn(nonNamespaceOperation);
    Mockito.when(openShiftClient.extensions().ingresses().inNamespace(NAMESPACE)).thenReturn(nonNamespaceOperation);
    API api = SampleTestObjectCreator.createDefaultAPI().build();
    kubernetesGateway.removeContainerBasedGateway("label", api);
    Mockito.verify(openShiftClient, Mockito.times(2)).services();
    Mockito.verify(openShiftClient, Mockito.times(4)).extensions();
}
Also used : OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) API(org.wso2.carbon.apimgt.core.models.API) BaseOperation(io.fabric8.kubernetes.client.dsl.base.BaseOperation) NonNamespaceOperation(io.fabric8.kubernetes.client.dsl.NonNamespaceOperation) Test(org.junit.Test)

Example 72 with Label

use of org.wso2.carbon.apimgt.core.models.Label in project carbon-apimgt by wso2.

the class GatewaysApiServiceImpl method gatewaysRegisterPost.

/**
 * Register gateway
 *
 * @param body        RegistrationDTO
 * @param contentType Content-Type header value
 * @return Registration summary details
 * @throws NotFoundException If failed to register gateway
 */
@Override
public Response gatewaysRegisterPost(RegistrationDTO body, String contentType, Request request) throws NotFoundException {
    try {
        LabelInfoDTO labelInfoDTO = body.getLabelInfo();
        if (labelInfoDTO != null) {
            APIMgtAdminService adminService = RestApiUtil.getAPIMgtAdminService();
            String overwriteLabels = labelInfoDTO.getOverwriteLabels();
            List<Label> labels = MappingUtil.convertToLabels(labelInfoDTO.getLabelList());
            adminService.registerGatewayLabels(labels, overwriteLabels);
            RegistrationSummary registrationSummary = adminService.getRegistrationSummary();
            return Response.ok().entity(MappingUtil.toRegistrationSummaryDTO(registrationSummary)).build();
        } else {
            String errorMessage = "Label information cannot be null";
            APIMgtResourceNotFoundException e = new APIMgtResourceNotFoundException(errorMessage, ExceptionCodes.LABEL_INFORMATION_CANNOT_BE_NULL);
            HashMap<String, String> paramList = new HashMap<String, String>();
            org.wso2.carbon.apimgt.rest.api.common.dto.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 registering the gateway";
        HashMap<String, String> paramList = new HashMap<String, String>();
        org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : HashMap(java.util.HashMap) Label(org.wso2.carbon.apimgt.core.models.Label) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException) LabelInfoDTO(org.wso2.carbon.apimgt.rest.api.core.dto.LabelInfoDTO) APIMgtAdminService(org.wso2.carbon.apimgt.core.api.APIMgtAdminService) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) RegistrationSummary(org.wso2.carbon.apimgt.core.models.RegistrationSummary)

Example 73 with Label

use of org.wso2.carbon.apimgt.core.models.Label in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisApiIdWsdlGet.

/**
 * Retrieves the WSDL of the particular API. If the WSDL is added as a single file/URL, the text content of the WSDL
 * will be retrived. If the WSDL is added as an archive, the binary content of the archive will be retrieved.
 *
 * @param apiId           UUID of API
 * @param labelName       Name of the label
 * @param ifNoneMatch     If-None-Match header value
 * @param ifModifiedSince If-Modified-Since header value
 * @param request         msf4j request
 * @return WSDL archive/file content
 * @throws NotFoundException
 */
@Override
public Response apisApiIdWsdlGet(String apiId, String labelName, String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    WSDLArchiveInfo wsdlArchiveInfo = null;
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        String wsdlString;
        if (!apiStore.isWSDLExists(apiId)) {
            if (log.isDebugEnabled()) {
                log.debug("WSDL has no content for API: " + apiId);
            }
            return Response.noContent().build();
        }
        if (StringUtils.isBlank(labelName)) {
            if (log.isDebugEnabled()) {
                log.debug("Label not provided since retrieving WSDL archive for default label. API: " + apiId);
            }
            labelName = APIMgtConstants.LabelConstants.DEFAULT;
        }
        boolean isWSDLArchiveExists = apiStore.isWSDLArchiveExists(apiId);
        if (log.isDebugEnabled()) {
            log.debug("API has WSDL archive?: " + isWSDLArchiveExists);
        }
        if (isWSDLArchiveExists) {
            wsdlArchiveInfo = apiStore.getAPIWSDLArchive(apiId, labelName);
            if (log.isDebugEnabled()) {
                log.debug("Successfully retrieved WSDL archive for API: " + apiId);
            }
            // wsdlArchiveInfo will not be null all the time so no need null check
            File archive = new File(wsdlArchiveInfo.getAbsoluteFilePath());
            return Response.ok(archive).header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_TYPE).header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + wsdlArchiveInfo.getFileName() + "\"").build();
        } else {
            wsdlString = apiStore.getAPIWSDL(apiId, labelName);
            if (log.isDebugEnabled()) {
                log.debug("Successfully retrieved WSDL for API: " + apiId);
            }
            return Response.ok(wsdlString).header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN).build();
        }
    } catch (APIManagementException e) {
        Map<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error("Error while getting WSDL for API:" + apiId + " and label:" + labelName, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    } finally {
    // Commented below since MSFJ fails to reply when the files are already deleted. Need to fix this properly
    /*
            if (wsdlArchiveInfo != null) {
                try {
                    APIFileUtils.deleteDirectory(wsdlArchiveInfo.getLocation());
                } catch (APIMgtDAOException e) {
                    //This is not a blocker. Give a warning and continue
                    log.warn("Error occured while deleting processed WSDL artifacts folder : " + wsdlArchiveInfo
                            .getLocation());
                }
            }*/
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) WSDLArchiveInfo(org.wso2.carbon.apimgt.core.models.WSDLArchiveInfo) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 74 with Label

use of org.wso2.carbon.apimgt.core.models.Label in project carbon-apimgt by wso2.

the class LabelDAOImpl method getLabelIdByNameAndType.

/**
 * @see LabelDAO#getLabelIdByNameAndType(String, String)
 */
@Override
public String getLabelIdByNameAndType(String name, String type) throws APIMgtDAOException {
    final String query = "SELECT LABEL_ID FROM AM_LABELS WHERE NAME = ? AND TYPE_NAME = ?";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(query)) {
        statement.setString(1, name);
        statement.setString(2, type);
        try (ResultSet rs = statement.executeQuery()) {
            if (rs.next()) {
                return rs.getString("LABEL_ID");
            } else {
                return null;
            }
        }
    } catch (SQLException e) {
        String message = "Error while retrieving label ID of label [label Name] " + name;
        throw new APIMgtDAOException(message, e, ExceptionCodes.LABEL_NOT_FOUND);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 75 with Label

use of org.wso2.carbon.apimgt.core.models.Label in project carbon-apimgt by wso2.

the class LabelDAOImpl method deleteLabel.

/**
 * @see LabelDAO#deleteLabel(String)
 */
@Override
public void deleteLabel(String labelId) throws APIMgtDAOException {
    final String query = "DELETE FROM AM_LABELS WHERE LABEL_ID = ?";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(query)) {
        try {
            connection.setAutoCommit(false);
            statement.setString(1, labelId);
            statement.execute();
            connection.commit();
        } catch (SQLException e) {
            connection.rollback();
            String message = DAOUtil.DAO_ERROR_PREFIX + "deleting the label [label id] " + labelId;
            throw new APIMgtDAOException(message, e);
        } finally {
            connection.setAutoCommit(DAOUtil.isAutoCommit());
        }
    } catch (SQLException e) {
        String message = DAOUtil.DAO_ERROR_PREFIX + "deleting the label [label id] " + labelId;
        throw new APIMgtDAOException(message, e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Aggregations

Label (org.wso2.carbon.apimgt.core.models.Label)65 ArrayList (java.util.ArrayList)52 Test (org.testng.annotations.Test)45 LabelDAO (org.wso2.carbon.apimgt.core.dao.LabelDAO)32 API (org.wso2.carbon.apimgt.core.models.API)29 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)28 SQLException (java.sql.SQLException)21 PreparedStatement (java.sql.PreparedStatement)20 HashMap (java.util.HashMap)16 Connection (java.sql.Connection)15 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)14 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)13 Test (org.junit.Test)12 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)11 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)11 Map (java.util.Map)10 BeforeTest (org.testng.annotations.BeforeTest)9 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)8 Response (javax.ws.rs.core.Response)8 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)8