1
1
package io .apicurio .registry .ccompat .rest .v7 .impl ;
2
2
3
+ import io .apicurio .common .apps .util .Pair ;
3
4
import io .apicurio .registry .ccompat .dto .SchemaReference ;
4
5
import io .apicurio .registry .ccompat .rest .error .ConflictException ;
5
6
import io .apicurio .registry .ccompat .rest .error .UnprocessableEntityException ;
19
20
import io .apicurio .registry .storage .dto .ContentWrapperDto ;
20
21
import io .apicurio .registry .storage .dto .EditableArtifactMetaDataDto ;
21
22
import io .apicurio .registry .storage .dto .EditableVersionMetaDataDto ;
23
+ import io .apicurio .registry .storage .dto .SearchedArtifactDto ;
22
24
import io .apicurio .registry .storage .dto .StoredArtifactVersionDto ;
23
25
import io .apicurio .registry .storage .error .ArtifactNotFoundException ;
24
26
import io .apicurio .registry .storage .error .RuleNotFoundException ;
31
33
import io .apicurio .registry .types .provider .ArtifactTypeUtilProvider ;
32
34
import io .apicurio .registry .types .provider .ArtifactTypeUtilProviderFactory ;
33
35
import jakarta .inject .Inject ;
36
+ import jakarta .ws .rs .BadRequestException ;
34
37
import org .apache .avro .AvroTypeException ;
35
38
import org .apache .avro .SchemaParseException ;
36
39
import org .apache .commons .codec .digest .DigestUtils ;
@@ -63,7 +66,37 @@ public abstract class AbstractResource {
63
66
@ Inject
64
67
ArtifactTypeUtilProviderFactory factory ;
65
68
66
- protected ArtifactVersionMetaDataDto createOrUpdateArtifact (String subject , String schema ,
69
+ protected String toSubjectWithGroupConcat (String groupId , String artifactId ) {
70
+ return (groupId == null ? "" : groupId ) + cconfig .groupConcatSeparator + artifactId ;
71
+ }
72
+
73
+ protected String toSubjectWithGroupConcat (SearchedArtifactDto dto ) {
74
+ return toSubjectWithGroupConcat (dto .getGroupId (), dto .getArtifactId ());
75
+ }
76
+
77
+ private Pair <String , String > toGAFromGroupConcatSubject (String subject ) {
78
+ int sepIdx = subject .indexOf (cconfig .groupConcatSeparator );
79
+ if (sepIdx < 1 ) {
80
+ throw new BadRequestException ("Invalid subject format. Should be: groupId"
81
+ + cconfig .groupConcatSeparator + "artifactId" );
82
+ }
83
+ String groupId = subject .substring (0 , sepIdx );
84
+ String artifactId = subject .substring (sepIdx + cconfig .groupConcatSeparator .length ());
85
+ return new Pair <>(groupId , artifactId );
86
+ }
87
+
88
+ protected GA getGA (String groupId , String artifactId ) {
89
+ String gid = groupId ;
90
+ String aid = artifactId ;
91
+ if (cconfig .groupConcatEnabled ) {
92
+ Pair <String , String > ga = toGAFromGroupConcatSubject (artifactId );
93
+ gid = ga .getLeft ();
94
+ aid = ga .getRight ();
95
+ }
96
+ return new GA (gid , aid );
97
+ }
98
+
99
+ protected ArtifactVersionMetaDataDto createOrUpdateArtifact (String artifactId , String schema ,
67
100
String artifactType , List <SchemaReference > references , String groupId ) {
68
101
ArtifactVersionMetaDataDto res ;
69
102
final List <ArtifactReferenceDto > parsedReferences = parseReferences (references , groupId );
@@ -80,9 +113,9 @@ protected ArtifactVersionMetaDataDto createOrUpdateArtifact(String subject, Stri
80
113
contentType = ContentTypes .APPLICATION_PROTOBUF ;
81
114
}
82
115
83
- if (!doesArtifactExist (subject , groupId )) {
116
+ if (!doesArtifactExist (artifactId , groupId )) {
84
117
TypedContent typedSchemaContent = TypedContent .create (schemaContent , contentType );
85
- rulesService .applyRules (groupId , subject , artifactType , typedSchemaContent ,
118
+ rulesService .applyRules (groupId , artifactId , artifactType , typedSchemaContent ,
86
119
RuleApplicationType .CREATE , artifactReferences , resolvedReferences );
87
120
88
121
EditableArtifactMetaDataDto artifactMetaData = EditableArtifactMetaDataDto .builder ().build ();
@@ -91,15 +124,15 @@ protected ArtifactVersionMetaDataDto createOrUpdateArtifact(String subject, Stri
91
124
ContentWrapperDto firstVersionContent = ContentWrapperDto .builder ().content (schemaContent )
92
125
.contentType (contentType ).references (parsedReferences ).build ();
93
126
94
- res = storage .createArtifact (groupId , subject , artifactType , artifactMetaData , null ,
127
+ res = storage .createArtifact (groupId , artifactId , artifactType , artifactMetaData , null ,
95
128
firstVersionContent , firstVersionMetaData , null , false ).getValue ();
96
129
} else {
97
130
TypedContent typedSchemaContent = TypedContent .create (schemaContent , contentType );
98
- rulesService .applyRules (groupId , subject , artifactType , typedSchemaContent ,
131
+ rulesService .applyRules (groupId , artifactId , artifactType , typedSchemaContent ,
99
132
RuleApplicationType .UPDATE , artifactReferences , resolvedReferences );
100
133
ContentWrapperDto versionContent = ContentWrapperDto .builder ().content (schemaContent )
101
134
.contentType (contentType ).references (parsedReferences ).build ();
102
- res = storage .createArtifactVersion (groupId , subject , null , artifactType , versionContent ,
135
+ res = storage .createArtifactVersion (groupId , artifactId , null , artifactType , versionContent ,
103
136
EditableVersionMetaDataDto .builder ().build (), List .of (), false );
104
137
}
105
138
} catch (RuleViolationException ex ) {
@@ -112,7 +145,7 @@ protected ArtifactVersionMetaDataDto createOrUpdateArtifact(String subject, Stri
112
145
return res ;
113
146
}
114
147
115
- protected ArtifactVersionMetaDataDto lookupSchema (String groupId , String subject , String schema ,
148
+ protected ArtifactVersionMetaDataDto lookupSchema (String groupId , String artifactId , String schema ,
116
149
List <SchemaReference > schemaReferences , String schemaType , boolean normalize ) {
117
150
// FIXME simplify logic
118
151
try {
@@ -126,7 +159,7 @@ protected ArtifactVersionMetaDataDto lookupSchema(String groupId, String subject
126
159
127
160
if (cconfig .canonicalHashModeEnabled .get () || normalize ) {
128
161
try {
129
- amd = storage .getArtifactVersionMetaDataByContent (groupId , subject , true ,
162
+ amd = storage .getArtifactVersionMetaDataByContent (groupId , artifactId , true ,
130
163
typedSchemaContent , artifactReferences );
131
164
} catch (ArtifactNotFoundException ex ) {
132
165
if (type .equals (ArtifactType .AVRO )) {
@@ -137,9 +170,9 @@ protected ArtifactVersionMetaDataDto lookupSchema(String groupId, String subject
137
170
// exception.
138
171
// This approach only works for schema types with dereference support (for now, only
139
172
// Avro in the ccompat API).
140
- amd = storage .getArtifactVersions (groupId , subject ).stream ().filter (version -> {
173
+ amd = storage .getArtifactVersions (groupId , artifactId ).stream ().filter (version -> {
141
174
StoredArtifactVersionDto artifactVersion = storage
142
- .getArtifactVersionContent (groupId , subject , version );
175
+ .getArtifactVersionContent (groupId , artifactId , version );
143
176
TypedContent typedArtifactVersion = TypedContent
144
177
.create (artifactVersion .getContent (), artifactVersion .getContentType ());
145
178
Map <String , TypedContent > artifactVersionReferences = storage
@@ -149,17 +182,17 @@ protected ArtifactVersionMetaDataDto lookupSchema(String groupId, String subject
149
182
.dereference (typedArtifactVersion , artifactVersionReferences )
150
183
.getContent ().content ());
151
184
return dereferencedExistingContentSha .equals (DigestUtils .sha256Hex (schema ));
152
- }).findAny ()
153
- . map ( version -> storage .getArtifactVersionMetaData (groupId , subject , version ))
185
+ }).findAny (). map (
186
+ version -> storage .getArtifactVersionMetaData (groupId , artifactId , version ))
154
187
.orElseThrow (() -> ex );
155
188
} else {
156
189
throw ex ;
157
190
}
158
191
}
159
192
160
193
} else {
161
- amd = storage .getArtifactVersionMetaDataByContent (groupId , subject , false , typedSchemaContent ,
162
- artifactReferences );
194
+ amd = storage .getArtifactVersionMetaDataByContent (groupId , artifactId , false ,
195
+ typedSchemaContent , artifactReferences );
163
196
}
164
197
165
198
return amd ;
@@ -193,18 +226,18 @@ protected Map<String, TypedContent> resolveReferences(List<SchemaReference> refe
193
226
return resolvedReferences ;
194
227
}
195
228
196
- protected boolean isArtifactActive (String subject , String groupId ) {
197
- long count = storage .countActiveArtifactVersions (groupId , subject );
229
+ protected boolean isArtifactActive (String artifactId , String groupId ) {
230
+ long count = storage .countActiveArtifactVersions (groupId , artifactId );
198
231
return count > 0 ;
199
232
}
200
233
201
- protected String getLatestArtifactVersionForSubject (String subject , String groupId ) {
234
+ protected String getLatestArtifactVersionForSubject (String artifactId , String groupId ) {
202
235
try {
203
- GAV latestGAV = storage .getBranchTip (new GA (groupId , subject ), BranchId .LATEST ,
236
+ GAV latestGAV = storage .getBranchTip (new GA (groupId , artifactId ), BranchId .LATEST ,
204
237
RetrievalBehavior .SKIP_DISABLED_LATEST );
205
238
return latestGAV .getRawVersionId ();
206
239
} catch (ArtifactNotFoundException ex ) {
207
- throw new VersionNotFoundException (groupId , subject , "latest" );
240
+ throw new VersionNotFoundException (groupId , artifactId , "latest" );
208
241
}
209
242
}
210
243
0 commit comments