Search in sources :

Example 6 with SimpleApplicationSettings

use of io.gravitee.rest.api.model.application.SimpleApplicationSettings in project gravitee-management-rest-api by gravitee-io.

the class ApplicationsResource method createApplication.

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Permissions({ @Permission(value = RolePermission.ENVIRONMENT_APPLICATION, acls = RolePermissionAction.CREATE) })
public Response createApplication(@Valid @NotNull(message = "Input must not be null.") ApplicationInput applicationInput) {
    NewApplicationEntity newApplicationEntity = new NewApplicationEntity();
    newApplicationEntity.setDescription(applicationInput.getDescription());
    newApplicationEntity.setGroups(applicationInput.getGroups() != null ? new HashSet<>(applicationInput.getGroups()) : new HashSet<>());
    newApplicationEntity.setName(applicationInput.getName());
    newApplicationEntity.setPicture(applicationInput.getPicture());
    final io.gravitee.rest.api.portal.rest.model.ApplicationSettings settings = applicationInput.getSettings();
    ApplicationSettings newApplicationEntitySettings = new ApplicationSettings();
    if (settings == null || (settings.getApp() == null && settings.getOauth() == null)) {
        newApplicationEntity.setSettings(newApplicationEntitySettings);
    } else {
        final io.gravitee.rest.api.portal.rest.model.SimpleApplicationSettings simpleAppInput = settings.getApp();
        if (simpleAppInput != null) {
            SimpleApplicationSettings sas = new SimpleApplicationSettings();
            sas.setClientId(simpleAppInput.getClientId());
            sas.setType(simpleAppInput.getType());
            newApplicationEntitySettings.setApp(sas);
        }
        final io.gravitee.rest.api.portal.rest.model.OAuthClientSettings oauthAppInput = settings.getOauth();
        if (oauthAppInput != null) {
            OAuthClientSettings ocs = new OAuthClientSettings();
            ocs.setApplicationType(oauthAppInput.getApplicationType());
            ocs.setGrantTypes(oauthAppInput.getGrantTypes());
            ocs.setRedirectUris(oauthAppInput.getRedirectUris());
            newApplicationEntitySettings.setoAuthClient(ocs);
        }
    }
    newApplicationEntity.setSettings(newApplicationEntitySettings);
    ApplicationEntity createdApplicationEntity = applicationService.create(newApplicationEntity, getAuthenticatedUser());
    return Response.created(this.getLocationHeader(createdApplicationEntity.getId())).entity(applicationMapper.convert(createdApplicationEntity, uriInfo)).build();
}
Also used : NewApplicationEntity(io.gravitee.rest.api.model.NewApplicationEntity) ApplicationSettings(io.gravitee.rest.api.model.application.ApplicationSettings) SimpleApplicationSettings(io.gravitee.rest.api.model.application.SimpleApplicationSettings) SimpleApplicationSettings(io.gravitee.rest.api.model.application.SimpleApplicationSettings) OAuthClientSettings(io.gravitee.rest.api.model.application.OAuthClientSettings) NewApplicationEntity(io.gravitee.rest.api.model.NewApplicationEntity) ApplicationEntity(io.gravitee.rest.api.model.ApplicationEntity) Permissions(io.gravitee.rest.api.portal.rest.security.Permissions)

Example 7 with SimpleApplicationSettings

use of io.gravitee.rest.api.model.application.SimpleApplicationSettings in project gravitee-management-rest-api by gravitee-io.

the class ApplicationService_UpdateTest method shouldUpdate.

@Test
public void shouldUpdate() throws TechnicalException {
    ApplicationSettings settings = new ApplicationSettings();
    SimpleApplicationSettings clientSettings = new SimpleApplicationSettings();
    clientSettings.setClientId(CLIENT_ID);
    settings.setApp(clientSettings);
    when(existingApplication.getSettings()).thenReturn(settings);
    when(applicationRepository.findById(APPLICATION_ID)).thenReturn(Optional.of(application));
    when(application.getName()).thenReturn(APPLICATION_NAME);
    when(application.getStatus()).thenReturn(ApplicationStatus.ACTIVE);
    when(existingApplication.getName()).thenReturn(APPLICATION_NAME);
    when(existingApplication.getDescription()).thenReturn("My description");
    when(application.getType()).thenReturn(ApplicationType.SIMPLE);
    when(applicationRepository.update(any())).thenReturn(application);
    when(roleService.findPrimaryOwnerRoleByOrganization(any(), any())).thenReturn(mock(RoleEntity.class));
    MembershipEntity po = new MembershipEntity();
    po.setMemberId(USER_NAME);
    po.setMemberType(MembershipMemberType.USER);
    po.setReferenceId(APPLICATION_ID);
    po.setReferenceType(MembershipReferenceType.APPLICATION);
    po.setRoleId("APPLICATION_PRIMARY_OWNER");
    when(membershipService.getMembershipsByReferencesAndRole(any(), any(), any())).thenReturn(Collections.singleton(po));
    final ApplicationEntity applicationEntity = applicationService.update(APPLICATION_ID, existingApplication);
    verify(applicationRepository).update(argThat(application -> APPLICATION_NAME.equals(application.getName()) && application.getUpdatedAt() != null));
    assertNotNull(applicationEntity);
    assertEquals(APPLICATION_NAME, applicationEntity.getName());
}
Also used : RoleEntity(io.gravitee.rest.api.model.RoleEntity) ArgumentMatchers(org.mockito.ArgumentMatchers) Mock(org.mockito.Mock) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) ClientRegistrationService(io.gravitee.rest.api.service.configuration.application.ClientRegistrationService) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException) Sets(org.mockito.internal.util.collections.Sets) UserService(io.gravitee.rest.api.service.UserService) UpdateApplicationEntity(io.gravitee.rest.api.model.UpdateApplicationEntity) Map(java.util.Map) ApplicationRepository(io.gravitee.repository.management.api.ApplicationRepository) MembershipMemberType(io.gravitee.rest.api.model.MembershipMemberType) ApplicationSettings(io.gravitee.rest.api.model.application.ApplicationSettings) ParameterService(io.gravitee.rest.api.service.ParameterService) InjectMocks(org.mockito.InjectMocks) MembershipEntity(io.gravitee.rest.api.model.MembershipEntity) Assert.assertNotNull(org.junit.Assert.assertNotNull) AuditService(io.gravitee.rest.api.service.AuditService) ClientIdAlreadyExistsException(io.gravitee.rest.api.service.exceptions.ClientIdAlreadyExistsException) Test(org.junit.Test) SimpleApplicationSettings(io.gravitee.rest.api.model.application.SimpleApplicationSettings) RoleEntity(io.gravitee.rest.api.model.RoleEntity) Mockito(org.mockito.Mockito) MembershipReferenceType(io.gravitee.rest.api.model.MembershipReferenceType) SubscriptionService(io.gravitee.rest.api.service.SubscriptionService) io.gravitee.repository.management.model(io.gravitee.repository.management.model) Optional(java.util.Optional) ApplicationNotFoundException(io.gravitee.rest.api.service.exceptions.ApplicationNotFoundException) Collections(java.util.Collections) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Assert.assertEquals(org.junit.Assert.assertEquals) ApplicationEntity(io.gravitee.rest.api.model.ApplicationEntity) ApplicationServiceImpl(io.gravitee.rest.api.service.impl.ApplicationServiceImpl) ApplicationSettings(io.gravitee.rest.api.model.application.ApplicationSettings) SimpleApplicationSettings(io.gravitee.rest.api.model.application.SimpleApplicationSettings) SimpleApplicationSettings(io.gravitee.rest.api.model.application.SimpleApplicationSettings) MembershipEntity(io.gravitee.rest.api.model.MembershipEntity) UpdateApplicationEntity(io.gravitee.rest.api.model.UpdateApplicationEntity) ApplicationEntity(io.gravitee.rest.api.model.ApplicationEntity) Test(org.junit.Test)

Example 8 with SimpleApplicationSettings

use of io.gravitee.rest.api.model.application.SimpleApplicationSettings in project gravitee-management-rest-api by gravitee-io.

the class ApplicationResource method updateApplication.

@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Update an application", notes = "User must have APPLICATION_DEFINITION[UPDATE] permission to update an application.")
@ApiResponses({ @ApiResponse(code = 200, message = "Updated application", response = ApplicationEntity.class), @ApiResponse(code = 500, message = "Internal server error") })
@Permissions({ @Permission(value = RolePermission.APPLICATION_DEFINITION, acls = RolePermissionAction.UPDATE) })
public ApplicationEntity updateApplication(@Valid @NotNull(message = "An application must be provided") final UpdateApplicationEntity updatedApplication) {
    // To preserve backward compatibility, ensure that we have at least default settings for simple application type
    if (updatedApplication.getSettings() == null || (updatedApplication.getSettings().getoAuthClient() == null && updatedApplication.getSettings().getApp() == null)) {
        ApplicationSettings settings = new ApplicationSettings();
        SimpleApplicationSettings simpleAppSettings = new SimpleApplicationSettings();
        simpleAppSettings.setType(updatedApplication.getType());
        simpleAppSettings.setClientId(updatedApplication.getClientId());
        updatedApplication.setSettings(settings);
    }
    return applicationService.update(application, updatedApplication);
}
Also used : ApplicationSettings(io.gravitee.rest.api.model.application.ApplicationSettings) SimpleApplicationSettings(io.gravitee.rest.api.model.application.SimpleApplicationSettings) SimpleApplicationSettings(io.gravitee.rest.api.model.application.SimpleApplicationSettings) Permissions(io.gravitee.rest.api.management.rest.security.Permissions)

Example 9 with SimpleApplicationSettings

use of io.gravitee.rest.api.model.application.SimpleApplicationSettings in project gravitee-management-rest-api by gravitee-io.

the class ApplicationServiceImpl method getSettings.

private ApplicationSettings getSettings(Application application) {
    final ApplicationSettings settings = new ApplicationSettings();
    if (application.getType() == ApplicationType.SIMPLE) {
        SimpleApplicationSettings simpleSettings = new SimpleApplicationSettings();
        if (application.getMetadata() != null) {
            if (application.getMetadata().get("client_id") != null) {
                simpleSettings.setClientId(application.getMetadata().get("client_id"));
            }
            if (application.getMetadata().get("type") != null) {
                simpleSettings.setType(application.getMetadata().get("type"));
            }
        }
        settings.setApp(simpleSettings);
    } else {
        OAuthClientSettings clientSettings = new OAuthClientSettings();
        if (application.getMetadata() != null) {
            try {
                final String registrationPayload = application.getMetadata().get("registration_payload");
                if (registrationPayload != null) {
                    final ClientRegistrationResponse registrationResponse = mapper.readValue(registrationPayload, ClientRegistrationResponse.class);
                    clientSettings.setClientId(registrationResponse.getClientId());
                    clientSettings.setClientSecret(registrationResponse.getClientSecret());
                    clientSettings.setClientUri(registrationResponse.getClientUri());
                    clientSettings.setApplicationType(registrationResponse.getApplicationType());
                    clientSettings.setLogoUri(registrationResponse.getLogoUri());
                    clientSettings.setResponseTypes(registrationResponse.getResponseTypes());
                    clientSettings.setRedirectUris(registrationResponse.getRedirectUris());
                    clientSettings.setGrantTypes(registrationResponse.getGrantTypes());
                }
                Iterator<ClientRegistrationProviderEntity> clientRegistrationProviderIte = clientRegistrationService.findAll().iterator();
                if (clientRegistrationProviderIte.hasNext()) {
                    clientSettings.setRenewClientSecretSupported(clientRegistrationProviderIte.next().isRenewClientSecretSupport());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        settings.setoAuthClient(clientSettings);
    }
    return settings;
}
Also used : ApplicationSettings(io.gravitee.rest.api.model.application.ApplicationSettings) SimpleApplicationSettings(io.gravitee.rest.api.model.application.SimpleApplicationSettings) SimpleApplicationSettings(io.gravitee.rest.api.model.application.SimpleApplicationSettings) OAuthClientSettings(io.gravitee.rest.api.model.application.OAuthClientSettings) ClientRegistrationResponse(io.gravitee.rest.api.service.impl.configuration.application.registration.client.register.ClientRegistrationResponse) ClientRegistrationProviderEntity(io.gravitee.rest.api.model.configuration.application.registration.ClientRegistrationProviderEntity) UuidString(io.gravitee.rest.api.service.common.UuidString) IOException(java.io.IOException)

Example 10 with SimpleApplicationSettings

use of io.gravitee.rest.api.model.application.SimpleApplicationSettings in project gravitee-management-rest-api by gravitee-io.

the class ApplicationService_CreateTest method shouldNotCreateForUserBecauseTechnicalException.

@Test(expected = TechnicalManagementException.class)
public void shouldNotCreateForUserBecauseTechnicalException() throws TechnicalException {
    ApplicationSettings settings = new ApplicationSettings();
    SimpleApplicationSettings clientSettings = new SimpleApplicationSettings();
    clientSettings.setClientId(CLIENT_ID);
    settings.setApp(clientSettings);
    when(newApplication.getSettings()).thenReturn(settings);
    when(applicationRepository.findAllByEnvironment("DEFAULT", ApplicationStatus.ACTIVE)).thenThrow(TechnicalException.class);
    applicationService.create(newApplication, USER_NAME);
}
Also used : ApplicationSettings(io.gravitee.rest.api.model.application.ApplicationSettings) SimpleApplicationSettings(io.gravitee.rest.api.model.application.SimpleApplicationSettings) SimpleApplicationSettings(io.gravitee.rest.api.model.application.SimpleApplicationSettings) Test(org.junit.Test)

Aggregations

ApplicationSettings (io.gravitee.rest.api.model.application.ApplicationSettings)15 SimpleApplicationSettings (io.gravitee.rest.api.model.application.SimpleApplicationSettings)15 Test (org.junit.Test)8 ApplicationEntity (io.gravitee.rest.api.model.ApplicationEntity)7 OAuthClientSettings (io.gravitee.rest.api.model.application.OAuthClientSettings)4 HashMap (java.util.HashMap)4 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)3 NewApplicationEntity (io.gravitee.rest.api.model.NewApplicationEntity)3 UpdateApplicationEntity (io.gravitee.rest.api.model.UpdateApplicationEntity)3 Application (io.gravitee.rest.api.portal.rest.model.Application)3 UserService (io.gravitee.rest.api.service.UserService)3 ApplicationRepository (io.gravitee.repository.management.api.ApplicationRepository)2 io.gravitee.repository.management.model (io.gravitee.repository.management.model)2 Permissions (io.gravitee.rest.api.management.rest.security.Permissions)2 MembershipEntity (io.gravitee.rest.api.model.MembershipEntity)2 MembershipMemberType (io.gravitee.rest.api.model.MembershipMemberType)2 MembershipReferenceType (io.gravitee.rest.api.model.MembershipReferenceType)2 RoleEntity (io.gravitee.rest.api.model.RoleEntity)2 UserEntity (io.gravitee.rest.api.model.UserEntity)2 Permissions (io.gravitee.rest.api.portal.rest.security.Permissions)2