Search in sources :

Example 66 with Label

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

the class OracleSQLStatements method prepareAttributeSearchStatementForStore.

/**
 * @see ApiDAOVendorSpecificStatements#prepareAttributeSearchStatementForStore(Connection connection, List, List,
 * Map, int, int)
 */
@Override
@SuppressFBWarnings({ "SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING", "OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE" })
public PreparedStatement prepareAttributeSearchStatementForStore(Connection connection, List<String> roles, List<String> labels, Map<String, String> attributeMap, int offset, int limit) throws APIMgtDAOException {
    StringBuilder roleListBuilder = new StringBuilder();
    roleListBuilder.append("?");
    for (int i = 0; i < roles.size() - 1; i++) {
        roleListBuilder.append(",?");
    }
    StringBuilder searchQuery = new StringBuilder();
    Iterator<Map.Entry<String, String>> entries = attributeMap.entrySet().iterator();
    while (entries.hasNext()) {
        Map.Entry<String, String> entry = entries.next();
        searchQuery.append("LOWER(");
        if (APIMgtConstants.TAG_SEARCH_TYPE_PREFIX.equalsIgnoreCase(entry.getKey())) {
            searchQuery.append(APIMgtConstants.TAG_NAME_COLUMN);
        } else if (APIMgtConstants.SUBCONTEXT_SEARCH_TYPE_PREFIX.equalsIgnoreCase(entry.getKey())) {
            searchQuery.append(APIMgtConstants.URL_PATTERN_COLUMN);
        } else {
            searchQuery.append(entry.getKey());
        }
        searchQuery.append(") LIKE ?");
        if (entries.hasNext()) {
            searchQuery.append(" AND ");
        }
    }
    // retrieve the attribute applicable for the search
    String searchAttribute = attributeMap.entrySet().iterator().next().getKey();
    // get the corresponding implementation based on the attribute to be searched
    String query = searchMap.get(searchAttribute).getStoreAttributeSearchQuery(roleListBuilder, searchQuery, offset, limit);
    query = "SELECT * FROM (SELECT A.*, rownum rnum FROM (Select * from ( " + query + " ) " + getStoreAPIsByLabelJoinQuery(labels) + " ORDER BY NAME) A WHERE rownum <= ?) " + "WHERE rnum >= ?";
    try {
        int queryIndex = 1;
        PreparedStatement statement = connection.prepareStatement(query);
        // include the attribute in the query (for APIs with public visibility)
        for (Map.Entry<String, String> entry : attributeMap.entrySet()) {
            statement.setString(queryIndex, '%' + entry.getValue().toLowerCase(Locale.ENGLISH) + '%');
            queryIndex++;
        }
        // include user roles in the query
        for (String role : roles) {
            statement.setString(queryIndex, role);
            queryIndex++;
        }
        // include the attribute in the query (for APIs with restricted visibility)
        for (Map.Entry<String, String> entry : attributeMap.entrySet()) {
            statement.setString(queryIndex, '%' + entry.getValue().toLowerCase(Locale.ENGLISH) + '%');
            queryIndex++;
        }
        for (String label : labels) {
            statement.setString(queryIndex, label);
            queryIndex++;
        }
        // setting 0 as the default offset based on store-api.yaml and Oracle specifications
        statement.setInt(queryIndex, (offset < 0) ? 0 : offset);
        queryIndex++;
        statement.setInt(queryIndex, limit);
        return statement;
    } catch (SQLException e) {
        String errorMsg = "Error occurred while searching APIs for attributes in the database.";
        log.error(errorMsg, e);
        throw new APIMgtDAOException(errorMsg, e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) HashMap(java.util.HashMap) Map(java.util.Map) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 67 with Label

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

the class WSDL20ProcessorImpl method getUpdatedWSDLPath.

/**
 * Updates the endpoints of all the WSDL files in the path based on the provided API (context) and Label (host).
 *
 * @param api Provided API object
 * @param label Provided label object
 * @return Updated WSDL file path
 * @throws APIMgtWSDLException Error while updating WSDL files
 */
@Override
public String getUpdatedWSDLPath(API api, Label label) throws APIMgtWSDLException {
    if (label != null) {
        for (Map.Entry<String, Description> entry : pathToDescriptionMap.entrySet()) {
            Description description = entry.getValue();
            if (log.isDebugEnabled()) {
                log.debug("Updating endpoints of WSDL: " + entry.getKey());
            }
            updateEndpoints(label.getAccessUrls(), api, description);
            if (log.isDebugEnabled()) {
                log.debug("Successfully updated endpoints of WSDL: " + entry.getKey());
            }
            try (FileOutputStream wsdlFileOutputStream = new FileOutputStream(new File(entry.getKey()))) {
                WSDLWriter writer = getWsdlFactoryInstance().newWSDLWriter();
                writer.writeWSDL(description.toElement(), wsdlFileOutputStream);
            } catch (IOException | WSDLException e) {
                throw new APIMgtWSDLException("Failed to create WSDL archive for API:" + api.getName() + ":" + api.getVersion() + " for label " + label.getName(), ExceptionCodes.ERROR_WHILE_CREATING_WSDL_ARCHIVE);
            }
        }
    }
    return wsdlArchiveExtractedPath;
}
Also used : Description(org.apache.woden.wsdl20.Description) APIMgtWSDLException(org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException) APIMgtWSDLException(org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException) WSDLException(org.apache.woden.WSDLException) FileOutputStream(java.io.FileOutputStream) WSDLWriter(org.apache.woden.WSDLWriter) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File)

Example 68 with Label

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

the class WSDL11ProcessorImpl method getUpdatedWSDLPath.

/**
 * Updates the endpoints of all the WSDL files in the path based on the provided API (context) and Label (host).
 *
 * @param api   Provided API object
 * @param label Provided label object
 * @return Updated WSDL file path
 * @throws APIMgtWSDLException Error while updating WSDL files
 */
@Override
public String getUpdatedWSDLPath(API api, Label label) throws APIMgtWSDLException {
    if (label != null) {
        for (Map.Entry<String, Definition> entry : pathToDefinitionMap.entrySet()) {
            Definition definition = entry.getValue();
            if (log.isDebugEnabled()) {
                log.debug("Updating endpoints of WSDL: " + entry.getKey());
            }
            updateEndpoints(label.getAccessUrls(), api, definition);
            if (log.isDebugEnabled()) {
                log.debug("Successfully updated endpoints of WSDL: " + entry.getKey());
            }
            try (FileOutputStream wsdlFileOutputStream = new FileOutputStream(new File(entry.getKey()))) {
                WSDLWriter writer = getWsdlFactoryInstance().newWSDLWriter();
                writer.writeWSDL(definition, wsdlFileOutputStream);
            } catch (IOException | WSDLException e) {
                throw new APIMgtWSDLException("Failed to create WSDL archive for API:" + api.getName() + ":" + api.getVersion() + " for label " + label.getName(), ExceptionCodes.ERROR_WHILE_CREATING_WSDL_ARCHIVE);
            }
        }
    }
    return wsdlArchiveExtractedPath;
}
Also used : APIMgtWSDLException(org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException) APIMgtWSDLException(org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException) WSDLException(javax.wsdl.WSDLException) FileOutputStream(java.io.FileOutputStream) Definition(javax.wsdl.Definition) WSDLWriter(javax.wsdl.xml.WSDLWriter) IOException(java.io.IOException) Map(java.util.Map) HashMap(java.util.HashMap) File(java.io.File)

Example 69 with Label

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

the class KubernetesGatewayImpl method removeContainerBasedGateway.

/**
 * @see ContainerBasedGatewayGenerator#removeContainerBasedGateway(String, API) (String)
 */
@Override
public void removeContainerBasedGateway(String label, API api) throws ContainerBasedGatewayException {
    try {
        client.services().inNamespace(namespace).withLabel(ContainerBasedGatewayConstants.GATEWAY, label).delete();
        client.extensions().deployments().inNamespace(namespace).withLabel(ContainerBasedGatewayConstants.GATEWAY, label).delete();
        client.extensions().ingresses().inNamespace(namespace).withLabel(ContainerBasedGatewayConstants.GATEWAY, label).delete();
        log.info(String.format("Completed deleting the container gateway related %s deployment, service and " + "ingress resources.", cmsType));
    } catch (KubernetesClientException e) {
        throw new ContainerBasedGatewayException("Error while removing container based gateway", e, ExceptionCodes.CONTAINER_GATEWAY_REMOVAL_FAILED);
    }
}
Also used : ContainerBasedGatewayException(org.wso2.carbon.apimgt.core.exception.ContainerBasedGatewayException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 70 with Label

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

the class APIGatewayPublisherImplTestCase method testDeleteAPIForException.

@Test(expected = GatewayException.class)
public void testDeleteAPIForException() throws GatewayException, ContainerBasedGatewayException {
    APIGatewayPublisherImpl apiGatewayPublisher = new APIGatewayPublisherImpl();
    Broker broker = Mockito.mock(BrokerImpl.class, Mockito.RETURNS_DEEP_STUBS);
    BrokerUtil.initialize(broker);
    ContainerBasedGatewayGenerator containerBasedGatewayGenerator = Mockito.mock(ContainerBasedGatewayGenerator.class);
    apiGatewayPublisher.setContainerBasedGatewayGenerator(containerBasedGatewayGenerator);
    Mockito.doThrow(ContainerBasedGatewayException.class).when(containerBasedGatewayGenerator).removeContainerBasedGateway(Mockito.any(), Mockito.any());
    List<String> labels = new ArrayList<>();
    labels.add("label");
    API api = SampleTestObjectCreator.createDefaultAPI().lifeCycleStatus(APIStatus.PUBLISHED.getStatus()).hasOwnGateway(true).labels(labels).build();
    apiGatewayPublisher.deleteAPI(api);
}
Also used : Broker(org.wso2.carbon.apimgt.core.api.Broker) ArrayList(java.util.ArrayList) API(org.wso2.carbon.apimgt.core.models.API) Test(org.junit.Test)

Aggregations

Label (org.wso2.carbon.apimgt.core.models.Label)65 ArrayList (java.util.ArrayList)60 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)26 PreparedStatement (java.sql.PreparedStatement)25 Connection (java.sql.Connection)20 HashMap (java.util.HashMap)18 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)14 Test (org.junit.Test)13 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)13 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)11 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)11 ResultSet (java.sql.ResultSet)10 IOException (java.io.IOException)9 Map (java.util.Map)9 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)9 BeforeTest (org.testng.annotations.BeforeTest)9