use of io.gravitee.rest.api.model.parameters.Key in project gravitee-management-rest-api by gravitee-io.
the class ApplicationServiceImpl method update.
@Override
public ApplicationEntity update(String applicationId, UpdateApplicationEntity updateApplicationEntity) {
try {
LOGGER.debug("Update application {}", applicationId);
if (updateApplicationEntity.getGroups() != null && !updateApplicationEntity.getGroups().isEmpty()) {
// throw a NotFoundException if the group doesn't exist
groupService.findByIds(updateApplicationEntity.getGroups());
}
Optional<Application> optApplicationToUpdate = applicationRepository.findById(applicationId);
if (!optApplicationToUpdate.isPresent()) {
throw new ApplicationNotFoundException(applicationId);
}
if (ApplicationStatus.ARCHIVED.equals(optApplicationToUpdate.get().getStatus())) {
throw new ApplicationArchivedException(optApplicationToUpdate.get().getName());
}
// Check that only one settings is defined
if (updateApplicationEntity.getSettings().getApp() != null && updateApplicationEntity.getSettings().getoAuthClient() != null) {
throw new InvalidApplicationTypeException();
}
// Check that a type is defined
if (updateApplicationEntity.getSettings().getApp() == null && updateApplicationEntity.getSettings().getoAuthClient() == null) {
throw new InvalidApplicationTypeException();
}
// Update application metadata
Map<String, String> metadata = new HashMap<>();
// Update a simple application
if (optApplicationToUpdate.get().getType() == ApplicationType.SIMPLE && updateApplicationEntity.getSettings().getApp() != null) {
// If clientId is set, check for uniqueness
String clientId = updateApplicationEntity.getSettings().getApp().getClientId();
if (clientId != null && !clientId.trim().isEmpty()) {
LOGGER.debug("Check that client_id is unique among all applications");
final Set<Application> applications = applicationRepository.findAllByEnvironment(GraviteeContext.getCurrentEnvironment(), ApplicationStatus.ACTIVE);
final Optional<Application> byClientId = applications.stream().filter(app -> app.getMetadata() != null && clientId.equals(app.getMetadata().get("client_id"))).findAny();
if (byClientId.isPresent() && !byClientId.get().getId().equals(optApplicationToUpdate.get().getId())) {
LOGGER.error("An application already exists with the same client_id");
throw new ClientIdAlreadyExistsException(clientId);
}
}
} else {
// Check that client registration is enabled
checkClientRegistrationEnabled();
checkClientSettings(updateApplicationEntity.getSettings().getoAuthClient());
// Update an OAuth client
final String registrationPayload = optApplicationToUpdate.get().getMetadata().get("registration_payload");
if (registrationPayload != null) {
ClientRegistrationResponse registrationResponse = clientRegistrationService.update(registrationPayload, updateApplicationEntity);
if (registrationResponse != null) {
try {
metadata.put("client_id", registrationResponse.getClientId());
metadata.put("registration_payload", mapper.writeValueAsString(registrationResponse));
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
}
}
Application application = convert(updateApplicationEntity);
application.setId(applicationId);
application.setEnvironmentId(optApplicationToUpdate.get().getEnvironmentId());
application.setStatus(ApplicationStatus.ACTIVE);
application.setType(optApplicationToUpdate.get().getType());
application.setCreatedAt(optApplicationToUpdate.get().getCreatedAt());
application.setUpdatedAt(new Date());
metadata.forEach((key, value) -> application.getMetadata().put(key, value));
Application updatedApplication = applicationRepository.update(application);
// Audit
auditService.createApplicationAuditLog(updatedApplication.getId(), Collections.emptyMap(), APPLICATION_UPDATED, updatedApplication.getUpdatedAt(), optApplicationToUpdate.get(), updatedApplication);
// Set correct client_id for all subscriptions
SubscriptionQuery subQuery = new SubscriptionQuery();
subQuery.setApplication(applicationId);
subQuery.setStatuses(Collections.singleton(SubscriptionStatus.ACCEPTED));
subscriptionService.search(subQuery).forEach(new Consumer<SubscriptionEntity>() {
@Override
public void accept(SubscriptionEntity subscriptionEntity) {
UpdateSubscriptionEntity updateSubscriptionEntity = new UpdateSubscriptionEntity();
updateSubscriptionEntity.setId(subscriptionEntity.getId());
updateSubscriptionEntity.setStartingAt(subscriptionEntity.getStartingAt());
updateSubscriptionEntity.setEndingAt(subscriptionEntity.getEndingAt());
subscriptionService.update(updateSubscriptionEntity, application.getMetadata().get("client_id"));
}
});
return convert(Collections.singleton(updatedApplication)).iterator().next();
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to update application {}", applicationId, ex);
throw new TechnicalManagementException(String.format("An error occurs while trying to update application %s", applicationId), ex);
}
}
use of io.gravitee.rest.api.model.parameters.Key in project gravitee-management-rest-api by gravitee-io.
the class ParameterServiceTest method shouldFindAllKeysWithFilterMapper.
@Test
public void shouldFindAllKeysWithFilterMapper() throws TechnicalException {
final Key p1key = PORTAL_TOP_APIS;
final Key p2key = PORTAL_ANALYTICS_ENABLED;
final Key p3key = PORTAL_ANALYTICS_TRACKINGID;
final Parameter parameter1 = new Parameter();
parameter1.setKey(PORTAL_TOP_APIS.key());
parameter1.setValue("api1;api2 ;; api1");
final Parameter parameter2 = new Parameter();
parameter2.setKey(PORTAL_ANALYTICS_ENABLED.key());
parameter2.setValue("api3;api4 ;; api5");
final Parameter parameter3 = new Parameter();
parameter3.setKey(PORTAL_ANALYTICS_TRACKINGID.key());
when(parameterRepository.findByKeys(Arrays.asList(PORTAL_TOP_APIS.key(), PORTAL_ANALYTICS_ENABLED.key(), PORTAL_ANALYTICS_TRACKINGID.key()), "DEFAULT", ParameterReferenceType.ENVIRONMENT)).thenReturn(Arrays.asList(parameter1, parameter2, parameter3));
final Map<String, List<String>> values = parameterService.findAll(Arrays.asList(p1key, p2key, p3key), value -> value.trim(), value -> !value.isEmpty(), io.gravitee.rest.api.model.parameters.ParameterReferenceType.ENVIRONMENT);
assertEquals(asList("api1", "api2", "api1"), values.get(p1key.key()));
assertEquals(asList("api3", "api4", "api5"), values.get(p2key.key()));
assertTrue(values.get(p3key.key()).isEmpty());
}
use of io.gravitee.rest.api.model.parameters.Key in project gravitee-management-rest-api by gravitee-io.
the class ParameterServiceTest method shouldFindAllKeysWithFilterFromEnvVar.
@Test
public void shouldFindAllKeysWithFilterFromEnvVar() throws TechnicalException {
final Key p1key = API_LABELS_DICTIONARY;
final Key p2key = PORTAL_ANALYTICS_ENABLED;
final Key p3key = PORTAL_ANALYTICS_TRACKINGID;
final Key p4key = PORTAL_APIKEY_HEADER;
final Parameter parameter1 = new Parameter();
parameter1.setKey(API_LABELS_DICTIONARY.key());
parameter1.setValue("api1;api2;api1");
final Parameter parameter2 = new Parameter();
parameter2.setKey(PORTAL_ANALYTICS_ENABLED.key());
parameter2.setValue("api3;api4;;api5");
final Parameter parameter3 = new Parameter();
parameter3.setKey(PORTAL_ANALYTICS_TRACKINGID.key());
final Parameter parameter4 = new Parameter();
parameter4.setKey(PORTAL_APIKEY_HEADER.key());
List<Parameter> parametersFromRepository = new ArrayList<>();
parametersFromRepository.add(parameter2);
parametersFromRepository.add(parameter3);
when(parameterRepository.findByKeys(Arrays.asList(PORTAL_ANALYTICS_ENABLED.key(), PORTAL_ANALYTICS_TRACKINGID.key()), "DEFAULT", ParameterReferenceType.ENVIRONMENT)).thenReturn(parametersFromRepository);
when(environment.containsProperty(API_LABELS_DICTIONARY.key())).thenReturn(true);
when(environment.getProperty(API_LABELS_DICTIONARY.key())).thenReturn("api1,api12");
when(environment.containsProperty(PORTAL_APIKEY_HEADER.key())).thenReturn(true);
when(environment.getProperty(PORTAL_APIKEY_HEADER.key())).thenReturn("header");
final Map<String, List<String>> values = parameterService.findAll(Arrays.asList(p1key, p2key, p3key, p4key), value -> value, value -> !value.isEmpty(), io.gravitee.rest.api.model.parameters.ParameterReferenceType.ENVIRONMENT);
assertEquals(asList("api1", "api12"), values.get(p1key.key()));
assertEquals(asList("api3", "api4", "api5"), values.get(p2key.key()));
assertTrue(values.get(p3key.key()).isEmpty());
assertEquals("header", values.get(p4key.key()).get(0));
}
Aggregations