Skip to content

Commit

Permalink
Quarkus 1.7.0.Final + IBM api test fix (#776)
Browse files Browse the repository at this point in the history
* Upgrade Quarkus to 1.7.0.Final

* Fix all schemas check - missing paging.

* Make sure request is only used in http context.
  • Loading branch information
alesj authored Aug 20, 2020
1 parent ef4bad0 commit 02b4714
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 74 deletions.
110 changes: 76 additions & 34 deletions app/src/main/java/io/apicurio/registry/rest/ArtifactsResourceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,29 @@
import io.apicurio.registry.metrics.ResponseErrorLivenessCheck;
import io.apicurio.registry.metrics.ResponseTimeoutReadinessCheck;
import io.apicurio.registry.metrics.RestMetricsApply;
import io.apicurio.registry.rest.beans.*;
import io.apicurio.registry.rest.beans.ArtifactMetaData;
import io.apicurio.registry.rest.beans.EditableMetaData;
import io.apicurio.registry.rest.beans.IfExistsType;
import io.apicurio.registry.rest.beans.Rule;
import io.apicurio.registry.rest.beans.UpdateState;
import io.apicurio.registry.rest.beans.VersionMetaData;
import io.apicurio.registry.rules.RuleApplicationType;
import io.apicurio.registry.rules.RulesService;
import io.apicurio.registry.search.client.SearchClient;
import io.apicurio.registry.search.common.Search;
import io.apicurio.registry.storage.*;
import io.apicurio.registry.types.*;
import io.apicurio.registry.storage.ArtifactAlreadyExistsException;
import io.apicurio.registry.storage.ArtifactMetaDataDto;
import io.apicurio.registry.storage.ArtifactNotFoundException;
import io.apicurio.registry.storage.ArtifactVersionMetaDataDto;
import io.apicurio.registry.storage.EditableArtifactMetaDataDto;
import io.apicurio.registry.storage.RegistryStorage;
import io.apicurio.registry.storage.RuleConfigurationDto;
import io.apicurio.registry.storage.StoredArtifact;
import io.apicurio.registry.types.ArtifactMediaTypes;
import io.apicurio.registry.types.ArtifactState;
import io.apicurio.registry.types.ArtifactType;
import io.apicurio.registry.types.Current;
import io.apicurio.registry.types.RuleType;
import io.apicurio.registry.util.ArtifactIdGenerator;
import io.apicurio.registry.util.ArtifactTypeUtil;
import io.apicurio.registry.util.ContentTypeUtil;
Expand Down Expand Up @@ -57,7 +73,13 @@
import java.util.concurrent.CompletionStage;
import java.util.function.Supplier;

import static io.apicurio.registry.metrics.MetricIDs.*;
import static io.apicurio.registry.metrics.MetricIDs.REST_CONCURRENT_REQUEST_COUNT;
import static io.apicurio.registry.metrics.MetricIDs.REST_CONCURRENT_REQUEST_COUNT_DESC;
import static io.apicurio.registry.metrics.MetricIDs.REST_GROUP_TAG;
import static io.apicurio.registry.metrics.MetricIDs.REST_REQUEST_COUNT;
import static io.apicurio.registry.metrics.MetricIDs.REST_REQUEST_COUNT_DESC;
import static io.apicurio.registry.metrics.MetricIDs.REST_REQUEST_RESPONSE_TIME;
import static io.apicurio.registry.metrics.MetricIDs.REST_REQUEST_RESPONSE_TIME_DESC;
import static org.eclipse.microprofile.metrics.MetricUnits.MILLISECONDS;

/**
Expand Down Expand Up @@ -95,24 +117,31 @@ public class ArtifactsResourceImpl implements ArtifactsResource, Headers {

private static final int GET_ARTIFACT_IDS_LIMIT = 10000;

/**
* Make sure this is ONLY used when request instance is active.
* e.g. in actual http request
*/
private String getContentType() {
return request.getContentType();
}

/**
* Figures out the artifact type in the following order of precedent:
* <p>
* 1) The provided X-Registry-ArtifactType header
* 2) A hint provided in the Content-Type header
* 3) Determined from the content itself
*
* @param content the content
* @param content the content
* @param xArtifactType the artifact type
* @param request http request
* @param ct content type from request API
*/
private static ArtifactType determineArtifactType(ContentHandle content, ArtifactType xArtifactType, HttpServletRequest request) {
private static ArtifactType determineArtifactType(ContentHandle content, ArtifactType xArtifactType, String ct) {
ArtifactType artifactType = xArtifactType;
if (artifactType == null) {
artifactType = getArtifactTypeFromContentType(request);
artifactType = getArtifactTypeFromContentType(ct);
if (artifactType == null) {
String contentType = request.getContentType();
artifactType = ArtifactTypeUtil.discoverType(content, contentType);
artifactType = ArtifactTypeUtil.discoverType(content, ct);
}
}
return artifactType;
Expand All @@ -121,10 +150,9 @@ private static ArtifactType determineArtifactType(ContentHandle content, Artifac
/**
* Tries to figure out the artifact type by analyzing the content-type.
*
* @param request http request
* @param contentType the content type header
*/
private static ArtifactType getArtifactTypeFromContentType(HttpServletRequest request) {
String contentType = request.getHeader("Content-Type");
private static ArtifactType getArtifactTypeFromContentType(String contentType) {
if (contentType != null && contentType.contains(MediaType.APPLICATION_JSON) && contentType.indexOf(';') != -1) {
String[] split = contentType.split(";");
if (split.length > 1) {
Expand Down Expand Up @@ -176,32 +204,39 @@ private CompletionStage<ArtifactMetaData> indexArtifact(String artifactId, Conte
}
}

private CompletionStage<ArtifactMetaData> handleIfExists(ArtifactType artifactType,
String artifactId, IfExistsType ifExists, ContentHandle content) {
private CompletionStage<ArtifactMetaData> handleIfExists(
ArtifactType artifactType,
String artifactId,
IfExistsType ifExists,
ContentHandle content,
String ct) {
final ArtifactMetaData artifactMetaData = getArtifactMetaData(artifactId);

switch (ifExists) {
case UPDATE:
return updateArtifactInternal(artifactId, artifactType, content);
return updateArtifactInternal(artifactId, artifactType, content, ct);
case RETURN:
return CompletableFuture.completedFuture(artifactMetaData);
case RETURN_OR_UPDATE:
return handleIfExistsReturnOrUpdate(artifactId, artifactType, content);
return handleIfExistsReturnOrUpdate(artifactId, artifactType, content, ct);
default:
throw new ArtifactAlreadyExistsException(artifactId);
}
}

private CompletionStage<ArtifactMetaData> handleIfExistsReturnOrUpdate(String artifactId,
ArtifactType artifactType, ContentHandle content) {
private CompletionStage<ArtifactMetaData> handleIfExistsReturnOrUpdate(
String artifactId,
ArtifactType artifactType,
ContentHandle content,
String ct) {
try {
ArtifactMetaDataDto mdDto = this.storage.getArtifactMetaData(artifactId, content);
ArtifactMetaData md = DtoUtil.dtoToMetaData(artifactId, artifactType, mdDto);
return CompletableFuture.completedFuture(md);
} catch (ArtifactNotFoundException nfe) {
// This is OK - we'll update the artifact if there is no matching content already there.
}
return updateArtifactInternal(artifactId, artifactType, content);
return updateArtifactInternal(artifactId, artifactType, content, ct);
}

@Override
Expand Down Expand Up @@ -245,11 +280,12 @@ public void updateArtifactVersionState(Integer version, String artifactId, Updat
public void testUpdateArtifact(String artifactId, ArtifactType xRegistryArtifactType, InputStream data) {
Objects.requireNonNull(artifactId);
ContentHandle content = ContentHandle.create(data);
if (ContentTypeUtil.isApplicationYaml(request)) {
String ct = getContentType();
if (ContentTypeUtil.isApplicationYaml(ct)) {
content = ContentTypeUtil.yamlToJson(content);
}

ArtifactType artifactType = determineArtifactType(content, xRegistryArtifactType, request);
ArtifactType artifactType = determineArtifactType(content, xRegistryArtifactType, ct);
rulesService.applyRules(artifactId, artifactType, content, RuleApplicationType.UPDATE);
}

Expand All @@ -258,20 +294,21 @@ public void testUpdateArtifact(String artifactId, ArtifactType xRegistryArtifact
*/
@Override
public CompletionStage<ArtifactMetaData> createArtifact(ArtifactType xRegistryArtifactType,
String xRegistryArtifactId, IfExistsType ifExists, InputStream data) {
String xRegistryArtifactId, IfExistsType ifExists, InputStream data) {
ContentHandle content = ContentHandle.create(data);
String ct = getContentType();
final ContentHandle finalContent = content;
try {
String artifactId = xRegistryArtifactId;

if (artifactId == null || artifactId.trim().isEmpty()) {
artifactId = idGenerator.generate();
}
if (ContentTypeUtil.isApplicationYaml(request)) {
if (ContentTypeUtil.isApplicationYaml(ct)) {
content = ContentTypeUtil.yamlToJson(content);
}

ArtifactType artifactType = determineArtifactType(content, xRegistryArtifactType, request);
ArtifactType artifactType = determineArtifactType(content, xRegistryArtifactType, ct);
rulesService.applyRules(artifactId, artifactType, content, RuleApplicationType.CREATE);
final String finalArtifactId = artifactId;
return storage.createArtifact(artifactId, artifactType, content)
Expand All @@ -285,12 +322,12 @@ public CompletionStage<ArtifactMetaData> createArtifact(ArtifactType xRegistryAr
throw new CompletionException(t);
})
.thenCompose(amd -> amd == null ?
handleIfExists(xRegistryArtifactType, xRegistryArtifactId, ifExists, finalContent) :
handleIfExists(xRegistryArtifactType, xRegistryArtifactId, ifExists, finalContent, ct) :
CompletableFuture.completedFuture(DtoUtil.dtoToMetaData(finalArtifactId, artifactType, amd))
)
.thenCompose(amdd -> indexArtifact(finalArtifactId, finalContent, amdd));
} catch (ArtifactAlreadyExistsException ex) {
return handleIfExists(xRegistryArtifactType, xRegistryArtifactId, ifExists, content)
return handleIfExists(xRegistryArtifactType, xRegistryArtifactId, ifExists, content, ct)
.thenCompose(amdd -> indexArtifact(xRegistryArtifactId, finalContent, amdd));
}
}
Expand Down Expand Up @@ -320,13 +357,17 @@ public Response getLatestArtifact(String artifactId) {
return builder.build();
}

private CompletionStage<ArtifactMetaData> updateArtifactInternal(String artifactId, ArtifactType xRegistryArtifactType, ContentHandle content) {
private CompletionStage<ArtifactMetaData> updateArtifactInternal(
String artifactId,
ArtifactType xRegistryArtifactType,
ContentHandle content,
String ct) {
Objects.requireNonNull(artifactId);
if (ContentTypeUtil.isApplicationYaml(request)) {
if (ContentTypeUtil.isApplicationYaml(ct)) {
content = ContentTypeUtil.yamlToJson(content);
}

ArtifactType artifactType = determineArtifactType(content, xRegistryArtifactType, request);
ArtifactType artifactType = determineArtifactType(content, xRegistryArtifactType, ct);
rulesService.applyRules(artifactId, artifactType, content, RuleApplicationType.UPDATE);
return storage.updateArtifact(artifactId, artifactType, content).thenApply(dto -> DtoUtil.dtoToMetaData(artifactId, artifactType, dto));
}
Expand All @@ -337,7 +378,7 @@ private CompletionStage<ArtifactMetaData> updateArtifactInternal(String artifact
@Override
public CompletionStage<ArtifactMetaData> updateArtifact(String artifactId, ArtifactType xRegistryArtifactType, InputStream data) {
ContentHandle content = ContentHandle.create(data);
return updateArtifactInternal(artifactId, xRegistryArtifactType, content)
return updateArtifactInternal(artifactId, xRegistryArtifactType, content, getContentType())
.thenCompose(amdd -> indexArtifact(artifactId, content, amdd));
}

Expand Down Expand Up @@ -365,11 +406,12 @@ public List<Long> listArtifactVersions(String artifactId) {
public CompletionStage<VersionMetaData> createArtifactVersion(String artifactId, ArtifactType xRegistryArtifactType, InputStream data) {
Objects.requireNonNull(artifactId);
ContentHandle content = ContentHandle.create(data);
if (ContentTypeUtil.isApplicationYaml(request)) {
String ct = getContentType();
if (ContentTypeUtil.isApplicationYaml(ct)) {
content = ContentTypeUtil.yamlToJson(content);
}

ArtifactType artifactType = determineArtifactType(content, xRegistryArtifactType, request);
ArtifactType artifactType = determineArtifactType(content, xRegistryArtifactType, ct);
rulesService.applyRules(artifactId, artifactType, content, RuleApplicationType.UPDATE);
final ContentHandle finalContent = content;
return storage.updateArtifact(artifactId, artifactType, content)
Expand Down Expand Up @@ -477,7 +519,7 @@ public ArtifactMetaData getArtifactMetaData(String artifactId) {
@Override
public ArtifactMetaData getArtifactMetaDataByContent(String artifactId, InputStream data) {
ContentHandle content = ContentHandle.create(data);
if (ContentTypeUtil.isApplicationYaml(request)) {
if (ContentTypeUtil.isApplicationYaml(getContentType())) {
content = ContentTypeUtil.yamlToJson(content);
}

Expand Down
19 changes: 8 additions & 11 deletions app/src/main/java/io/apicurio/registry/util/ContentTypeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@

package io.apicurio.registry.util;

import javax.servlet.http.HttpServletRequest;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;

import io.apicurio.registry.content.ContentHandle;

/**
Expand All @@ -38,10 +35,10 @@ public final class ContentTypeUtil {

/**
* Returns true if the Content-Type of the inbound request is "application/json".
* @param request
*
* @param ct content type
*/
public static final boolean isApplicationJson(HttpServletRequest request) {
String ct = request.getContentType();
public static boolean isApplicationJson(String ct) {
if (ct == null) {
return false;
}
Expand All @@ -50,17 +47,17 @@ public static final boolean isApplicationJson(HttpServletRequest request) {

/**
* Returns true if the Content-Type of the inbound request is "application/x-yaml".
* @param request
*
* @param ct content type
*/
public static final boolean isApplicationYaml(HttpServletRequest request) {
String ct = request.getContentType();
public static boolean isApplicationYaml(String ct) {
if (ct == null) {
return false;
}
return ct.contains(CT_APPLICATION_YAML);
}
public static final ContentHandle yamlToJson(ContentHandle yaml) {

public static ContentHandle yamlToJson(ContentHandle yaml) {
try {
JsonNode root = yamlMapper.readTree(yaml.stream());
return ContentHandle.create(jsonMapper.writerWithDefaultPrettyPrinter().writeValueAsBytes(root));
Expand Down
Loading

0 comments on commit 02b4714

Please sign in to comment.