Search in sources :

Example 1 with ApiDefinitionBean

use of io.apiman.manager.api.beans.apis.ApiDefinitionBean in project apiman by apiman.

the class JpaStorage method getApiDefinition.

/**
 * {@inheritDoc}
 */
@Override
public InputStream getApiDefinition(String apiOrgId, String apiId, String apiVersion) throws StorageException {
    try {
        EntityManager entityManager = getActiveEntityManager();
        String jpql = "SELECT v from ApiDefinitionBean v " + "JOIN v.apiVersion av " + "JOIN av.api api " + "JOIN api.organization org " + "WHERE org.id = :apiOrgId " + "AND api.id = :apiId " + "AND av.version = :apiVersion ";
        TypedQuery<ApiDefinitionBean> query = entityManager.createQuery(jpql, ApiDefinitionBean.class).setParameter("apiOrgId", apiOrgId).setParameter("apiId", apiId).setParameter("apiVersion", apiVersion);
        ApiDefinitionBean apiDef = query.getSingleResult();
        return new ByteArrayInputStream(apiDef.getData());
    } catch (NoResultException e) {
        return null;
    } catch (Throwable t) {
        LOGGER.error(t.getMessage(), t);
        throw new StorageException(t);
    }
}
Also used : EntityManager(javax.persistence.EntityManager) ApiDefinitionBean(io.apiman.manager.api.beans.apis.ApiDefinitionBean) ByteArrayInputStream(java.io.ByteArrayInputStream) NoResultException(javax.persistence.NoResultException) StorageException(io.apiman.manager.api.core.exceptions.StorageException)

Example 2 with ApiDefinitionBean

use of io.apiman.manager.api.beans.apis.ApiDefinitionBean in project apiman by apiman.

the class JpaStorage method updateApiDefinition.

/**
 * {@inheritDoc}
 */
@Override
public void updateApiDefinition(ApiVersionBean version, InputStream definitionStream) throws StorageException {
    try {
        ApiDefinitionBean bean = super.get(version.getId(), ApiDefinitionBean.class);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        IOUtils.copy(definitionStream, baos);
        byte[] data = baos.toByteArray();
        if (bean != null) {
            bean.setData(data);
            super.update(bean);
        } else {
            bean = new ApiDefinitionBean();
            bean.setId(version.getId());
            bean.setData(data);
            bean.setApiVersion(version);
            super.create(bean);
        }
    } catch (IOException e) {
        throw new StorageException(e);
    }
}
Also used : ApiDefinitionBean(io.apiman.manager.api.beans.apis.ApiDefinitionBean) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) StorageException(io.apiman.manager.api.core.exceptions.StorageException)

Aggregations

ApiDefinitionBean (io.apiman.manager.api.beans.apis.ApiDefinitionBean)2 StorageException (io.apiman.manager.api.core.exceptions.StorageException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 EntityManager (javax.persistence.EntityManager)1 NoResultException (javax.persistence.NoResultException)1