@Override
    public ApiVersionBean createApiVersion(String organizationId, String apiId,
            NewApiVersionBean bean) throws ApiNotFoundException, NotAuthorizedException,
            InvalidVersionException, ApiVersionAlreadyExistsException {
        if (!securityContext.hasPermission(PermissionType.apiEdit, organizationId))
            throw ExceptionFactory.notAuthorizedException();
        FieldValidator.validateVersion(bean.getVersion());

        ApiVersionBean newVersion;
        try {
            GatewaySummaryBean gateway = getSingularGateway();

            storage.beginTx();
            ApiBean api = storage.getApi(organizationId, apiId);
            if (api == null) {
                throw ExceptionFactory.apiNotFoundException(apiId);
            }

            if (storage.getApiVersion(organizationId, apiId, bean.getVersion()) != null) {
                throw ExceptionFactory.apiVersionAlreadyExistsException(apiId, bean.getVersion());
            }

            newVersion = createApiVersionInternal(bean, api, gateway);
            storage.commitTx();
        } catch (AbstractRestException e) {
            storage.rollbackTx();
            throw e;
        } catch (Exception e) {
            storage.rollbackTx();
            throw new SystemErrorException(e);
        }

        if (bean.isClone() && bean.getCloneVersion() != null) {
            try {
                ApiVersionBean cloneSource = getApiVersion(organizationId, apiId, bean.getCloneVersion());

                // Clone primary attributes of the API version unless those attributes
                // were included in the NewApiVersionBean.  In other words, information
                // sent as part of the "create version" payload take precedence over the
                // cloned attributes.
                UpdateApiVersionBean updatedApi = new UpdateApiVersionBean();
                if (bean.getEndpoint() == null) {
                    updatedApi.setEndpoint(cloneSource.getEndpoint());
                }
                if (bean.getEndpointType() == null) {
                    updatedApi.setEndpointType(cloneSource.getEndpointType());
                }
                if (bean.getEndpointContentType() == null) {
                    updatedApi.setEndpointContentType(cloneSource.getEndpointContentType());
                }
                updatedApi.setEndpointProperties(cloneSource.getEndpointProperties());
                updatedApi.setGateways(cloneSource.getGateways());
                if (bean.getPlans() == null) {
                    updatedApi.setPlans(cloneSource.getPlans());
                }
                if (bean.getPublicAPI() == null) {
                    updatedApi.setPublicAPI(cloneSource.isPublicAPI());
                }
                if (bean.getParsePayload() == null) {
                    updatedApi.setParsePayload(bean.getParsePayload());
                }
                newVersion = updateApiVersion(organizationId, apiId, bean.getVersion(), updatedApi);

                if (bean.getDefinitionUrl() == null) {
                    // Clone the API definition document
                    InputStream definition = null;
                    try {
                        Response response = getApiDefinition(organizationId, apiId, bean.getCloneVersion());
                        definition = (InputStream) response.getEntity();
                        storeApiDefinition(organizationId, apiId, newVersion.getVersion(),
                                cloneSource.getDefinitionType(), definition);
                    } catch (ApiDefinitionNotFoundException svnfe) {
                        // This is ok - it just means the API doesn't have one, so do nothing.
                    } catch (Exception sdnfe) {
                        log.error("Unable to create response", sdnfe); //$NON-NLS-1$
                    } finally {
                        IOUtils.closeQuietly(definition);
                    }
                }

                // Clone all API policies
                List<PolicySummaryBean> policies = listApiPolicies(organizationId, apiId, bean.getCloneVersion());
                for (PolicySummaryBean policySummary : policies) {
                    PolicyBean policy = getApiPolicy(organizationId, apiId, bean.getCloneVersion(), policySummary.getId());
                    NewPolicyBean npb = new NewPolicyBean();
                    npb.setDefinitionId(policy.getDefinition().getId());
                    npb.setConfiguration(policy.getConfiguration());
                    createApiPolicy(organizationId, apiId, newVersion.getVersion(), npb);
                }
            } catch (Exception e) {
                // TODO it's ok if the clone fails - we did our best
                if (e != null) {
                    Throwable t = e;
                    e = (Exception) t;
                }
            }
        }

        return newVersion;
    }
@ Override public ApiVersionBean createApiVersion ( String organizationId , String apiId , NewApiVersionBean bean ) throws ApiNotFoundException , NotAuthorizedException , InvalidVersionException , ApiVersionAlreadyExistsException { if ( ! securityContext . hasPermission ( PermissionType . apiEdit , organizationId ) ) throw ExceptionFactory . notAuthorizedException ( ) ; FieldValidator . validateVersion ( bean . getVersion ( ) ) ; ApiVersionBean newVersion ; try { GatewaySummaryBean gateway = getSingularGateway ( ) ; storage . beginTx ( ) ; ApiBean api = storage . getApi ( organizationId , apiId ) ; if ( api == null ) { throw ExceptionFactory . apiNotFoundException ( apiId ) ; } if ( storage . getApiVersion ( organizationId , apiId , bean . getVersion ( ) ) != null ) { throw ExceptionFactory . apiVersionAlreadyExistsException ( apiId , bean . getVersion ( ) ) ; } newVersion = createApiVersionInternal ( bean , api , gateway ) ; storage . commitTx ( ) ; } catch ( AbstractRestException e ) { storage . rollbackTx ( ) ; throw e ; } catch ( Exception e ) { storage . rollbackTx ( ) ; throw new SystemErrorException ( e ) ; } if ( bean . isClone ( ) && bean . getCloneVersion ( ) != null ) { try { ApiVersionBean cloneSource = getApiVersion ( organizationId , apiId , bean . getCloneVersion ( ) ) ; UpdateApiVersionBean updatedApi = new UpdateApiVersionBean ( ) ; if ( bean . getEndpoint ( ) == null ) { updatedApi . setEndpoint ( cloneSource . getEndpoint ( ) ) ; } if ( bean . getEndpointType ( ) == null ) { updatedApi . setEndpointType ( cloneSource . getEndpointType ( ) ) ; } if ( bean . getEndpointContentType ( ) == null ) { updatedApi . setEndpointContentType ( cloneSource . getEndpointContentType ( ) ) ; } updatedApi . setEndpointProperties ( cloneSource . getEndpointProperties ( ) ) ; updatedApi . setGateways ( cloneSource . getGateways ( ) ) ; if ( bean . getPlans ( ) == null ) { updatedApi . setPlans ( cloneSource . getPlans ( ) ) ; } if ( bean . getPublicAPI ( ) == null ) { updatedApi . setPublicAPI ( cloneSource . isPublicAPI ( ) ) ; } if ( bean . getParsePayload ( ) == null ) { updatedApi . setParsePayload ( bean . getParsePayload ( ) ) ; } newVersion = updateApiVersion ( organizationId , apiId , bean . getVersion ( ) , updatedApi ) ; if ( bean . getDefinitionUrl ( ) == null ) { InputStream definition = null ; try { Response response = getApiDefinition ( organizationId , apiId , bean . getCloneVersion ( ) ) ; definition = ( InputStream ) response . getEntity ( ) ; storeApiDefinition ( organizationId , apiId , newVersion . getVersion ( ) , cloneSource . getDefinitionType ( ) , definition ) ; } catch ( ApiDefinitionNotFoundException svnfe ) { } catch ( Exception sdnfe ) { log . error ( "Unable to create response" , sdnfe ) ; } finally { IOUtils . closeQuietly ( definition ) ; } } List < PolicySummaryBean > policies = listApiPolicies ( organizationId , apiId , bean . getCloneVersion ( ) ) ; for ( PolicySummaryBean policySummary : policies ) { PolicyBean policy = getApiPolicy ( organizationId , apiId , bean . getCloneVersion ( ) , policySummary . getId ( ) ) ; NewPolicyBean npb = new NewPolicyBean ( ) ; npb . setDefinitionId ( policy . getDefinition ( ) . getId ( ) ) ; npb . setConfiguration ( policy . getConfiguration ( ) ) ; createApiPolicy ( organizationId , apiId , newVersion . getVersion ( ) , npb ) ; } } catch ( Exception e ) { if ( e != null ) { Throwable t = e ; e = ( Exception ) t ; } } } return newVersion ; }
@see io.apiman.manager.api.rest.contract.IOrganizationResource#createApiVersion(java.lang.String, java.lang.String, io.apiman.manager.api.beans.apis.NewApiVersionBean)