Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions bom/artifacts/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@
<artifactId>unomi-json-schema-rest</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.unomi</groupId>
<artifactId>unomi-json-schema-shell-commands</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.unomi</groupId>
<artifactId>unomi-rest</artifactId>
Expand Down
1 change: 1 addition & 0 deletions extensions/json-schema/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<modules>
<module>services</module>
<module>rest</module>
<module>shell-commands</module>
</modules>

</project>
105 changes: 105 additions & 0 deletions extensions/json-schema/shell-commands/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to You 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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.apache.unomi</groupId>
<artifactId>unomi-json-schema-root</artifactId>
<version>3.1.0-SNAPSHOT</version>
</parent>

<artifactId>unomi-json-schema-shell-commands</artifactId>
<name>Apache Unomi :: Extension :: JSON Schema :: Shell Commands</name>
<description>Shell commands for managing JSON schemas in Apache Unomi</description>
<packaging>bundle</packaging>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.unomi</groupId>
<artifactId>unomi-bom</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<!-- Unomi dependencies -->
<dependency>
<groupId>org.apache.unomi</groupId>
<artifactId>unomi-json-schema-services</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.unomi</groupId>
<artifactId>unomi-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.unomi</groupId>
<artifactId>shell-dev-commands</artifactId>
<scope>provided</scope>
</dependency>

<!-- Standard API Dependencies -->
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.service.component.annotations</artifactId>
<scope>provided</scope>
</dependency>

<!-- Libraries -->
<dependency>
<groupId>org.apache.karaf.shell</groupId>
<artifactId>org.apache.karaf.shell.core</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<configuration>
<instructions>
<Karaf-Commands>*</Karaf-Commands>
<Import-Package>
org.apache.karaf.shell.api.action,
org.apache.karaf.shell.api.action.lifecycle,
org.apache.karaf.shell.support.completers,
org.apache.unomi.api,
org.apache.unomi.api.services,
org.apache.unomi.api.tenants,
org.apache.unomi.schema.api,
*
</Import-Package>
<_dsannotations>*</_dsannotations>
<_dsannotations-options>inherit</_dsannotations-options>
<_metatypeannotations>*</_metatypeannotations>
<_metatypeannotations-options>version;nested</_metatypeannotations-options>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.unomi.shell.commands.schema;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.unomi.api.PartialList;
import org.apache.unomi.api.query.Query;
import org.apache.unomi.api.tenants.TenantService;
import org.apache.unomi.schema.api.JsonSchemaWrapper;
import org.apache.unomi.schema.api.SchemaService;
import org.apache.unomi.shell.dev.services.BaseCrudCommand;
import org.apache.unomi.shell.dev.services.CrudCommand;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

import java.util.*;
import java.util.stream.Collectors;

@Component(service = CrudCommand.class, immediate = true)
public class SchemaCrudCommand extends BaseCrudCommand {

private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private static final List<String> PROPERTY_NAMES = List.of(
"$id", "self.target", "self.name", "self.extends", "properties", "required", "allOf"
);
private static final List<String> TARGET_TYPES = List.of(
"events", "profiles", "sessions", "rules", "segments"
);

@Reference
private SchemaService schemaService;

@Reference
private TenantService tenantService;

@Override
public String getObjectType() {
return "schema";
}

@Override
public String create(Map<String, Object> properties) {
try {
String schema = OBJECT_MAPPER.writeValueAsString(properties);
schemaService.saveSchema(schema);
return properties.get("$id").toString();
} catch (JsonProcessingException e) {
throw new RuntimeException("Error processing JSON schema", e);
}
}

@Override
public Map<String, Object> read(String id) {
try {
JsonSchemaWrapper schema = schemaService.getSchema(id);
if (schema == null) {
return null;
}
Map<String, Object> result = new HashMap<>();
result.put("id", schema.getItemId());
result.put("name", schema.getName());
result.put("target", schema.getTarget());
result.put("tenantId", schema.getTenantId());
if (schema.getExtendsSchemaId() != null) {
result.put("extends", schema.getExtendsSchemaId());
}
result.put("schema", OBJECT_MAPPER.readValue(schema.getSchema(), Map.class));
return result;
} catch (JsonProcessingException e) {
throw new RuntimeException("Error reading JSON schema", e);
}
}

@Override
public void update(String id, Map<String, Object> properties) {
try {
// Ensure the ID matches
properties.put("$id", id);
String schema = OBJECT_MAPPER.writeValueAsString(properties);
schemaService.saveSchema(schema);
} catch (JsonProcessingException e) {
throw new RuntimeException("Error updating JSON schema", e);
}
}

@Override
public void delete(String id) {
schemaService.deleteSchema(id);
}

@Override
public String getPropertiesHelp() {
return "Required properties:\n" +
"- $id: Schema ID (URI)\n" +
"- self.target: Target type (e.g. \"events\", \"profiles\", \"sessions\", \"rules\", \"segments\")\n" +
"- self.name: Schema name\n" +
"\n" +
"Optional properties:\n" +
"- self.extends: ID of schema to extend\n" +
"- properties: JSON Schema properties\n" +
"- required: List of required properties\n" +
"- allOf: List of schemas to extend";
}

@Override
public List<String> completePropertyNames(String prefix) {
return PROPERTY_NAMES.stream()
.filter(name -> name.startsWith(prefix))
.collect(Collectors.toList());
}

@Override
public List<String> completePropertyValue(String propertyName, String prefix) {
if ("self.target".equals(propertyName)) {
return TARGET_TYPES.stream()
.filter(type -> type.startsWith(prefix))
.collect(Collectors.toList());
} else if ("self.extends".equals(propertyName)) {
return new ArrayList<>(schemaService.getInstalledJsonSchemaIds()).stream()
.filter(id -> id.startsWith(prefix))
.collect(Collectors.toList());
}
return List.of();
}

@Override
protected String[] getHeadersWithoutTenant() {
return new String[] {
"ID",
"Target",
"Name",
"Extends"
};
}

@Override
protected PartialList<?> getItems(Query query) {
List<JsonSchemaWrapper> schemas = new ArrayList<>();
Set<String> schemaIds = schemaService.getInstalledJsonSchemaIds();
for (String schemaId : schemaIds) {
JsonSchemaWrapper schema = schemaService.getSchema(schemaId);
if (schema != null) {
schemas.add(schema);
}
}
int totalSize = schemas.size();
int start = 0;
int end = Math.min(query.getLimit(), totalSize);
return new PartialList<JsonSchemaWrapper>(schemas.subList(start, end), start, end, totalSize, PartialList.Relation.EQUAL);
}

@Override
protected Comparable[] buildRow(Object item) {
JsonSchemaWrapper schema = (JsonSchemaWrapper) item;
return new Comparable[] {
schema.getItemId(),
schema.getTarget(),
schema.getName(),
schema.getExtendsSchemaId() != null ? schema.getExtendsSchemaId() : ""
};
}
}
8 changes: 8 additions & 0 deletions itests/src/test/java/org/apache/unomi/itests/AllITs.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.unomi.itests.migration.Migrate16xToCurrentVersionIT;
import org.apache.unomi.itests.graphql.*;
import org.apache.unomi.itests.migration.MigrationIT;
import org.apache.unomi.itests.shell.*;
import org.junit.runner.RunWith;
import org.junit.runners.Suite.SuiteClasses;

Expand Down Expand Up @@ -65,6 +66,13 @@
SendEventActionIT.class,
ScopeIT.class,
V2CompatibilityModeIT.class,
CrudCommandsIT.class,
CacheCommandsIT.class,
TailCommandsIT.class,
SchedulerCommandsIT.class,
TenantCommandsIT.class,
RuleStatisticsCommandsIT.class,
OtherCommandsIT.class,
HealthCheckIT.class,
LegacyQueryBuilderMappingIT.class,
})
Expand Down
Loading
Loading