Search in sources :

Example 81 with Label

use of org.wso2.carbon.apimgt.api.model.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 82 with Label

use of org.wso2.carbon.apimgt.api.model.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 83 with Label

use of org.wso2.carbon.apimgt.api.model.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)

Example 84 with Label

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

the class LabelDAOImpl method isLabelsExists.

/**
 * Checks if any labels added to DB and already available
 *
 * @return true if there are any labels available in the system
 * @throws APIMgtDAOException If an error occurs while checking labels existence
 */
private static boolean isLabelsExists(String name, String type) throws APIMgtDAOException {
    final String query = "SELECT * 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 true;
            }
        }
    } catch (SQLException e) {
        String message = "Error while retrieving label [label type] " + type;
        throw new APIMgtDAOException(message, e, ExceptionCodes.LABEL_NOT_FOUND);
    }
    return false;
}
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 85 with Label

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

the class LabelDAOImpl method addLabel.

/**
 * Add a new label
 *
 * @param label the label to ADD
 * @return {@link Label} a label object with the newly added label ID
 * @throws APIMgtDAOException if error occurs while accessing data layer
 */
public static Label addLabel(Label label) throws APIMgtDAOException {
    final String query = "INSERT INTO AM_LABELS (LABEL_ID, NAME, TYPE_NAME) VALUES (?,?,?)";
    String labelId = null;
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(query)) {
        connection.setAutoCommit(false);
        labelId = UUID.randomUUID().toString();
        statement.setString(1, labelId);
        statement.setString(2, label.getName());
        statement.setString(3, label.getType().toUpperCase(Locale.ENGLISH));
        statement.executeUpdate();
        connection.commit();
        if (!label.getAccessUrls().isEmpty()) {
            insertAccessUrlMappings(labelId, label.getAccessUrls());
        }
        return new Label.Builder().id(labelId).name(label.getName()).description(label.getDescription()).accessUrls(label.getAccessUrls()).type(label.getType()).build();
    } catch (SQLException e) {
        String message = "Error occured while trying to add label " + labelId;
        throw new APIMgtDAOException(message, e, ExceptionCodes.LABEL_ADDING_FAILED);
    }
}
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)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