Search in sources :

Example 16 with VirtualHost

use of io.gravitee.definition.model.VirtualHost in project gravitee-management-rest-api by gravitee-io.

the class ApiDuplicatorServiceImpl method duplicate.

@Override
public ApiEntity duplicate(final ApiEntity apiEntity, final DuplicateApiEntity duplicateApiEntity, String organizationId, String environmentId) {
    requireNonNull(apiEntity, "Missing ApiEntity");
    final String apiId = apiEntity.getId();
    LOGGER.debug("Duplicate API {}", apiId);
    final UpdateApiEntity newApiEntity = convert(apiEntity);
    final Proxy proxy = apiEntity.getProxy();
    proxy.setVirtualHosts(singletonList(new VirtualHost(duplicateApiEntity.getContextPath())));
    newApiEntity.setProxy(proxy);
    newApiEntity.setVersion(duplicateApiEntity.getVersion() == null ? apiEntity.getVersion() : duplicateApiEntity.getVersion());
    if (duplicateApiEntity.getFilteredFields().contains("groups")) {
        newApiEntity.setGroups(null);
    } else {
        newApiEntity.setGroups(apiEntity.getGroups());
    }
    final ApiEntity duplicatedApi = apiService.createWithApiDefinition(newApiEntity, getAuthenticatedUsername(), null);
    if (!duplicateApiEntity.getFilteredFields().contains("members")) {
        final Set<MembershipEntity> membershipsToDuplicate = membershipService.getMembershipsByReference(io.gravitee.rest.api.model.MembershipReferenceType.API, apiId);
        RoleEntity primaryOwnerRole = roleService.findPrimaryOwnerRoleByOrganization(organizationId, RoleScope.API);
        if (primaryOwnerRole != null) {
            String primaryOwnerRoleId = primaryOwnerRole.getId();
            membershipsToDuplicate.forEach(membership -> {
                String roleId = membership.getRoleId();
                if (!primaryOwnerRoleId.equals(roleId)) {
                    membershipService.addRoleToMemberOnReference(io.gravitee.rest.api.model.MembershipReferenceType.API, duplicatedApi.getId(), membership.getMemberType(), membership.getMemberId(), roleId);
                }
            });
        }
    }
    if (!duplicateApiEntity.getFilteredFields().contains("pages")) {
        final List<PageEntity> pages = pageService.search(new PageQuery.Builder().api(apiId).build(), true, environmentId);
        pageService.duplicatePages(pages, environmentId, duplicatedApi.getId());
    }
    if (!duplicateApiEntity.getFilteredFields().contains("plans")) {
        final Set<PlanEntity> plans = planService.findByApi(apiId);
        planService.duplicatePlans(plans, environmentId, duplicatedApi.getId());
    }
    return duplicatedApi;
}
Also used : UpdateApiEntity(io.gravitee.rest.api.model.api.UpdateApiEntity) PageQuery(io.gravitee.rest.api.model.documentation.PageQuery) UpdateApiEntity(io.gravitee.rest.api.model.api.UpdateApiEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) DuplicateApiEntity(io.gravitee.rest.api.model.api.DuplicateApiEntity) UuidString(io.gravitee.rest.api.service.common.UuidString) Proxy(io.gravitee.definition.model.Proxy) VirtualHost(io.gravitee.definition.model.VirtualHost)

Example 17 with VirtualHost

use of io.gravitee.definition.model.VirtualHost in project gravitee-management-rest-api by gravitee-io.

the class VirtualHostServiceTest method validate_nullIsNotASubDomain.

@Test
public void validate_nullIsNotASubDomain() {
    VirtualHost vhost = new VirtualHost();
    vhost.setHost(null);
    vhost.setPath("/validVhostPath");
    EnvironmentEntity environmentEntity = new EnvironmentEntity();
    environmentEntity.setDomainRestrictions(Arrays.asList("test.gravitee.io", "other.gravitee.io"));
    when(environmentService.findById(any())).thenReturn(environmentEntity);
    final Collection<VirtualHost> virtualHosts = virtualHostService.sanitizeAndValidate(Collections.singletonList(vhost));
    Assert.assertEquals(1, virtualHosts.size());
    Assert.assertEquals("test.gravitee.io", virtualHosts.iterator().next().getHost());
}
Also used : EnvironmentEntity(io.gravitee.rest.api.model.EnvironmentEntity) VirtualHost(io.gravitee.definition.model.VirtualHost) Test(org.junit.Test)

Example 18 with VirtualHost

use of io.gravitee.definition.model.VirtualHost in project gravitee-management-rest-api by gravitee-io.

the class VirtualHostServiceTest method validate_hostSubDomainOfDomainConstraint.

@Test
public void validate_hostSubDomainOfDomainConstraint() {
    VirtualHost vhost = getValidVirtualHost();
    String domainConstraint = vhost.getHost();
    vhost.setHost("level2.level1." + domainConstraint);
    EnvironmentEntity environmentEntity = new EnvironmentEntity();
    environmentEntity.setDomainRestrictions(Collections.singletonList(domainConstraint));
    when(environmentService.findById(any())).thenReturn(environmentEntity);
    virtualHostService.sanitizeAndValidate(Collections.singletonList(vhost));
}
Also used : EnvironmentEntity(io.gravitee.rest.api.model.EnvironmentEntity) VirtualHost(io.gravitee.definition.model.VirtualHost) Test(org.junit.Test)

Example 19 with VirtualHost

use of io.gravitee.definition.model.VirtualHost in project gravitee-management-rest-api by gravitee-io.

the class VirtualHostServiceTest method shouldFail_create_existingPath.

@Test(expected = ApiContextPathAlreadyExistsException.class)
public void shouldFail_create_existingPath() {
    Api api1 = createMock("mock1", "/context");
    when(apiRepository.search(null)).thenReturn(Collections.singletonList(api1));
    virtualHostService.sanitizeAndValidate(Collections.singletonList(new VirtualHost("/context")));
}
Also used : Api(io.gravitee.repository.management.model.Api) VirtualHost(io.gravitee.definition.model.VirtualHost) Test(org.junit.Test)

Example 20 with VirtualHost

use of io.gravitee.definition.model.VirtualHost in project gravitee-management-rest-api by gravitee-io.

the class VirtualHostServiceTest method getValidVirtualHost.

private VirtualHost getValidVirtualHost() {
    VirtualHost vhost = new VirtualHost();
    vhost.setHost("valid.host.gravitee.io");
    vhost.setPath("/validVhostPath");
    return vhost;
}
Also used : VirtualHost(io.gravitee.definition.model.VirtualHost)

Aggregations

VirtualHost (io.gravitee.definition.model.VirtualHost)25 Test (org.junit.Test)17 Api (io.gravitee.repository.management.model.Api)10 Proxy (io.gravitee.definition.model.Proxy)7 EnvironmentEntity (io.gravitee.rest.api.model.EnvironmentEntity)6 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)6 UpdateApiEntity (io.gravitee.rest.api.model.api.UpdateApiEntity)3 Date (java.util.Date)3 PrimaryOwnerEntity (io.gravitee.rest.api.model.PrimaryOwnerEntity)2 UserEntity (io.gravitee.rest.api.model.UserEntity)2 Api (io.gravitee.rest.api.portal.rest.model.Api)2 RatingSummary (io.gravitee.rest.api.portal.rest.model.RatingSummary)2 Before (org.junit.Before)2 RatingSummaryEntity (io.gravitee.rest.api.model.RatingSummaryEntity)1 ApiEntrypointEntity (io.gravitee.rest.api.model.api.ApiEntrypointEntity)1 DuplicateApiEntity (io.gravitee.rest.api.model.api.DuplicateApiEntity)1 PageQuery (io.gravitee.rest.api.model.documentation.PageQuery)1 User (io.gravitee.rest.api.portal.rest.model.User)1 UuidString (io.gravitee.rest.api.service.common.UuidString)1 InvalidVirtualHostException (io.gravitee.rest.api.service.exceptions.InvalidVirtualHostException)1