use of io.gravitee.rest.api.service.swagger.converter.extension.XGraviteeIODefinition in project gravitee-management-rest-api by gravitee-io.
the class OAIToAPIConverter method visit.
@Override
public SwaggerApiEntity visit(OpenAPI oai) {
final SwaggerApiEntity apiEntity = new SwaggerApiEntity();
// Name
apiEntity.setName(oai.getInfo().getTitle());
// Description
apiEntity.setDescription(oai.getInfo().getDescription() == null ? "Description of " + apiEntity.getName() : oai.getInfo().getDescription());
// Version
apiEntity.setVersion(oai.getInfo().getVersion());
fill(apiEntity, oai);
// Use X-Gravitee to add information in API
XGraviteeIODefinition xGraviteeIODefinition = null;
if (oai.getExtensions() != null && oai.getExtensions().get(X_GRAVITEEIO_DEFINITION_VENDOR_EXTENSION) != null) {
xGraviteeIODefinition = new ObjectMapper().convertValue(oai.getExtensions().get(X_GRAVITEEIO_DEFINITION_VENDOR_EXTENSION), XGraviteeIODefinition.class);
}
// Proxy
Proxy proxy = new Proxy();
String defaultEndpoint = null;
// Proxy - Endpoints
if (!oai.getServers().isEmpty()) {
List<String> evaluatedServerUrl = mapServersToEndpoint(oai.getServers());
EndpointGroup defaultGroup = new EndpointGroup();
defaultGroup.setName("default-group");
if (evaluatedServerUrl == null) {
defaultGroup.setEndpoints(singleton(new HttpEndpoint("default", defaultEndpoint)));
} else if (evaluatedServerUrl.size() == 1) {
defaultEndpoint = evaluatedServerUrl.get(0);
defaultGroup.setEndpoints(singleton(new HttpEndpoint("default", defaultEndpoint)));
} else {
defaultEndpoint = evaluatedServerUrl.get(0);
defaultGroup.setEndpoints(new HashSet<>());
for (int i = 0; i < evaluatedServerUrl.size(); i++) {
defaultGroup.getEndpoints().add(new HttpEndpoint("server" + (i + 1), evaluatedServerUrl.get(i)));
}
}
proxy.setGroups(singleton(defaultGroup));
apiEntity.setProxy(proxy);
}
// Proxy - Context Path / Virtual Host
if (xGraviteeIODefinition != null && xGraviteeIODefinition.getVirtualHosts() != null) {
proxy.setVirtualHosts(xGraviteeIODefinition.getVirtualHosts().stream().map(vHost -> new VirtualHost(vHost.getHost(), vHost.getPath(), (vHost.getOverrideEntrypoint() != null ? vHost.getOverrideEntrypoint() : false))).collect(Collectors.toList()));
} else {
String contextPath = null;
if (defaultEndpoint != null) {
contextPath = URI.create(defaultEndpoint).getPath();
}
if (contextPath == null || contextPath.equals("/")) {
contextPath = apiEntity.getName().replaceAll("\\s+", "").toLowerCase();
}
proxy.setVirtualHosts(singletonList(new VirtualHost(contextPath)));
}
apiEntity.setProxy(proxy);
// Add xGraviteeIODefinition config
if (xGraviteeIODefinition != null) {
// Categories
if (xGraviteeIODefinition.getCategories() != null && !xGraviteeIODefinition.getCategories().isEmpty()) {
apiEntity.setCategories(new HashSet<>(xGraviteeIODefinition.getCategories()));
}
// Groups
if (xGraviteeIODefinition.getGroups() != null && !xGraviteeIODefinition.getGroups().isEmpty()) {
// Groups in schema are group name. Replace them by id
Set<String> groupIdsToImport = xGraviteeIODefinition.getGroups().stream().flatMap(group -> groupService.findByName(group).stream()).map(GroupEntity::getId).collect(Collectors.toSet());
apiEntity.setGroups(groupIdsToImport);
}
// Labels
if (xGraviteeIODefinition.getLabels() != null && !xGraviteeIODefinition.getLabels().isEmpty()) {
apiEntity.setLabels(xGraviteeIODefinition.getLabels());
}
// Metadata
if (xGraviteeIODefinition.getMetadata() != null && !xGraviteeIODefinition.getMetadata().isEmpty()) {
final List<ApiMetadataEntity> apiMetadataEntities = xGraviteeIODefinition.getMetadata().stream().map(metadata -> {
ApiMetadataEntity apiMetadata = new ApiMetadataEntity();
apiMetadata.setKey(IdGenerator.generate(metadata.getName()));
apiMetadata.setName(metadata.getName());
apiMetadata.setValue(metadata.getValue());
apiMetadata.setFormat(metadata.getFormat() != null ? MetadataFormat.valueOf(metadata.getFormat().name()) : MetadataFormat.STRING);
return apiMetadata;
}).collect(Collectors.toList());
apiEntity.setMetadata(apiMetadataEntities);
}
// Picture
if (xGraviteeIODefinition.getPicture() != null && !StringUtils.isEmpty(xGraviteeIODefinition.getPicture())) {
if (xGraviteeIODefinition.getPicture().matches(PICTURE_REGEX)) {
apiEntity.setPicture(xGraviteeIODefinition.getPicture());
}
}
// Properties
if (xGraviteeIODefinition.getProperties() != null && !xGraviteeIODefinition.getProperties().isEmpty()) {
Properties properties = new Properties();
properties.setProperties(xGraviteeIODefinition.getProperties().stream().map(prop -> new Property(prop.getKey(), prop.getValue())).collect(Collectors.toList()));
apiEntity.setProperties(properties);
}
// Tags
if (xGraviteeIODefinition.getTags() != null && !xGraviteeIODefinition.getTags().isEmpty()) {
final Map<String, String> tagMap = tagService.findByReference(GraviteeContext.getCurrentOrganization(), TagReferenceType.ORGANIZATION).stream().collect(toMap(TagEntity::getId, TagEntity::getName));
final Set<String> tagIdToAdd = xGraviteeIODefinition.getTags().stream().map(tag -> findTagIdByName(tagMap, tag)).filter(Objects::nonNull).collect(Collectors.toSet());
if (tagIdToAdd != null && !tagIdToAdd.isEmpty()) {
apiEntity.setTags(tagIdToAdd);
}
}
// Visibility
if (xGraviteeIODefinition.getVisibility() != null) {
apiEntity.setVisibility(Visibility.valueOf(xGraviteeIODefinition.getVisibility().name()));
}
}
return apiEntity;
}
Aggregations