Skip to content

Commit

Permalink
feat(service): create validate api metadata domain service
Browse files Browse the repository at this point in the history
  • Loading branch information
jourdiw committed Mar 21, 2024
1 parent b110ece commit 2c292d6
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 40 deletions.
@@ -0,0 +1,112 @@
/*
* Copyright © 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.apim.core.api.domain_service;

import io.gravitee.apim.core.DomainService;
import io.gravitee.apim.core.api.crud_service.ApiCrudService;
import io.gravitee.apim.core.api.exception.DuplicateApiMetadataKeyException;
import io.gravitee.apim.core.api.exception.DuplicateApiMetadataNameException;
import io.gravitee.apim.core.api.exception.InvalidApiMetadataValueException;
import io.gravitee.apim.core.api.model.Api;
import io.gravitee.apim.core.api.query_service.ApiMetadataQueryService;
import io.gravitee.apim.core.documentation.model.ApiFreemarkerTemplate;
import io.gravitee.apim.core.membership.domain_service.ApiPrimaryOwnerDomainService;
import io.gravitee.apim.core.metadata.crud_service.MetadataCrudService;
import io.gravitee.apim.core.metadata.model.Metadata;
import jakarta.mail.internet.InternetAddress;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;

@AllArgsConstructor
@DomainService
public class ValidateApiMetadataDomainService {

private final ApiMetadataQueryService metadataQueryService;
private final MetadataCrudService metadataCrudService;
private final ApiPrimaryOwnerDomainService apiPrimaryOwnerDomainService;
private final ApiMetadataDecoderDomainService apiMetadataDecoderDomainService;

public void validateUniqueKey(String apiId, String key) {
this.metadataCrudService.findById(apiId, Metadata.ReferenceType.API, key)
.ifPresent(m -> {
throw new DuplicateApiMetadataKeyException(apiId, key);
});
}

public void validateUniqueName(String apiId, String name) {
this.metadataQueryService.findApiMetadata(apiId)
.values()
.forEach(val -> {
if (val.getName().equalsIgnoreCase(name)) {
throw new DuplicateApiMetadataNameException(apiId, name);
}
});
}

public void validateValueByFormat(Api api, String organizationId, String value, Metadata.MetadataFormat format) {
var valueToCheck = Objects.nonNull(value) && value.startsWith("${") ? this.getDecodedValue(api, organizationId, value) : value;

try {
switch (format) {
case BOOLEAN:
Boolean.valueOf(valueToCheck);
break;
case URL:
new URL(valueToCheck);
break;
case MAIL:
final InternetAddress email = new InternetAddress(valueToCheck);
email.validate();
break;
case DATE:
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
sdf.setLenient(false);
sdf.parse(valueToCheck);
break;
case NUMERIC:
Double.valueOf(valueToCheck);
break;
}
} catch (Exception e) {
throw new InvalidApiMetadataValueException(value, format.name());
}
}

private String getDecodedValue(Api api, String organizationId, String value) {
var metadata =
this.metadataQueryService.findApiMetadata(api.getId())
.entrySet()
.stream()
.collect(
Collectors.toMap(
Map.Entry::getKey,
entry -> entry.getValue().getValue() != null ? entry.getValue().getValue() : entry.getValue().getDefaultValue()
)
);

var apiTemplate = new ApiFreemarkerTemplate(
api,
metadata,
apiPrimaryOwnerDomainService.getApiPrimaryOwner(organizationId, api.getId())
);

return this.apiMetadataDecoderDomainService.decodeMetadataValue(value, apiTemplate);
}
}
@@ -0,0 +1,25 @@
/*
* Copyright © 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.apim.core.api.exception;

import io.gravitee.apim.core.exception.ValidationDomainException;

public class DuplicateApiMetadataKeyException extends ValidationDomainException {

public DuplicateApiMetadataKeyException(String apiId, String key) {
super("Key [" + key + "] already exists for API [" + apiId + "]");
}
}
@@ -0,0 +1,25 @@
/*
* Copyright © 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.apim.core.api.exception;

import io.gravitee.apim.core.exception.ValidationDomainException;

public class DuplicateApiMetadataNameException extends ValidationDomainException {

public DuplicateApiMetadataNameException(String apiId, String name) {
super("Name [" + name + "] already exists for API [" + apiId + "]");
}
}
@@ -0,0 +1,25 @@
/*
* Copyright © 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.apim.core.api.exception;

import io.gravitee.apim.core.exception.ValidationDomainException;

public class InvalidApiMetadataValueException extends ValidationDomainException {

public InvalidApiMetadataValueException(String value, String format) {
super("Invalid value [" + value + "] for format [" + format + "]");
}
}
Expand Up @@ -15,46 +15,7 @@
*/
package inmemory.spring;

import inmemory.AccessPointQueryServiceInMemory;
import inmemory.ApiCrudServiceInMemory;
import inmemory.ApiKeyCrudServiceInMemory;
import inmemory.ApiKeyQueryServiceInMemory;
import inmemory.ApiMetadataQueryServiceInMemory;
import inmemory.ApiQueryServiceInMemory;
import inmemory.ApplicationCrudServiceInMemory;
import inmemory.AuditCrudServiceInMemory;
import inmemory.AuditMetadataQueryServiceInMemory;
import inmemory.AuditQueryServiceInMemory;
import inmemory.ConnectionLogsCrudServiceInMemory;
import inmemory.EndpointPluginQueryServiceInMemory;
import inmemory.EntrypointPluginQueryServiceInMemory;
import inmemory.EnvironmentCrudServiceInMemory;
import inmemory.EventCrudInMemory;
import inmemory.EventQueryServiceInMemory;
import inmemory.FlowCrudServiceInMemory;
import inmemory.GroupQueryServiceInMemory;
import inmemory.IndexerInMemory;
import inmemory.InstallationAccessQueryServiceInMemory;
import inmemory.InstanceQueryServiceInMemory;
import inmemory.LicenseCrudServiceInMemory;
import inmemory.MembershipCrudServiceInMemory;
import inmemory.MembershipQueryServiceInMemory;
import inmemory.MessageLogCrudServiceInMemory;
import inmemory.NoopSwaggerOpenApiResolver;
import inmemory.NoopTemplateResolverDomainService;
import inmemory.PageCrudServiceInMemory;
import inmemory.PageQueryServiceInMemory;
import inmemory.PageRevisionCrudServiceInMemory;
import inmemory.ParametersDomainServiceInMemory;
import inmemory.ParametersQueryServiceInMemory;
import inmemory.PlanCrudServiceInMemory;
import inmemory.PlanQueryServiceInMemory;
import inmemory.PolicyPluginQueryServiceInMemory;
import inmemory.RoleQueryServiceInMemory;
import inmemory.SubscriptionCrudServiceInMemory;
import inmemory.SubscriptionQueryServiceInMemory;
import inmemory.TriggerNotificationDomainServiceInMemory;
import inmemory.UserCrudServiceInMemory;
import inmemory.*;
import io.gravitee.apim.infra.query_service.audit.AuditEventQueryServiceImpl;
import org.mockito.Mockito;
import org.springframework.context.annotation.Bean;
Expand Down Expand Up @@ -267,4 +228,9 @@ public NoopTemplateResolverDomainService noopTemplateResolverDomainService() {
public NoopSwaggerOpenApiResolver noopSwaggerOpenApiResolver() {
return new NoopSwaggerOpenApiResolver();
}

@Bean
public MetadataCrudServiceInMemory metadataCrudService() {
return new MetadataCrudServiceInMemory();
}
}

0 comments on commit 2c292d6

Please sign in to comment.