-
Notifications
You must be signed in to change notification settings - Fork 276
/
Copy pathIdsResourceImpl.java
139 lines (124 loc) · 6.13 KB
/
IdsResourceImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package io.apicurio.registry.rest.v3;
import io.apicurio.common.apps.logging.Logged;
import io.apicurio.registry.auth.Authorized;
import io.apicurio.registry.auth.AuthorizedLevel;
import io.apicurio.registry.auth.AuthorizedStyle;
import io.apicurio.registry.content.ContentHandle;
import io.apicurio.registry.content.TypedContent;
import io.apicurio.registry.metrics.health.liveness.ResponseErrorLivenessCheck;
import io.apicurio.registry.metrics.health.readiness.ResponseTimeoutReadinessCheck;
import io.apicurio.registry.rest.HeadersHack;
import io.apicurio.registry.rest.v3.beans.ArtifactReference;
import io.apicurio.registry.rest.v3.beans.HandleReferencesType;
import io.apicurio.registry.rest.v3.shared.CommonResourceOperations;
import io.apicurio.registry.storage.dto.ArtifactVersionMetaDataDto;
import io.apicurio.registry.storage.dto.ContentWrapperDto;
import io.apicurio.registry.storage.dto.StoredArtifactVersionDto;
import io.apicurio.registry.storage.error.ArtifactNotFoundException;
import io.apicurio.registry.storage.error.ContentNotFoundException;
import io.apicurio.registry.types.ArtifactMediaTypes;
import io.apicurio.registry.types.ReferenceType;
import io.apicurio.registry.types.VersionState;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.interceptor.Interceptors;
import jakarta.ws.rs.core.Response;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Collectors;
@ApplicationScoped
@Interceptors({ ResponseErrorLivenessCheck.class, ResponseTimeoutReadinessCheck.class })
@Logged
public class IdsResourceImpl extends AbstractResourceImpl implements IdsResource {
@Inject
CommonResourceOperations common;
private void checkIfDeprecated(Supplier<VersionState> stateSupplier, String artifactId, String version,
Response.ResponseBuilder builder) {
HeadersHack.checkIfDeprecated(stateSupplier, null, artifactId, version, builder);
}
/**
* @see io.apicurio.registry.rest.v3.IdsResource#getContentById(long)
*/
@Override
@Authorized(style = AuthorizedStyle.None, level = AuthorizedLevel.Read)
public Response getContentById(long contentId) {
ContentWrapperDto dto = storage.getContentById(contentId);
if (dto.getContentHash() != null && dto.getContentHash().startsWith("draft:")) {
throw new ContentNotFoundException(contentId);
}
ContentHandle content = dto.getContent();
Response.ResponseBuilder builder = Response.ok(content, ArtifactMediaTypes.BINARY);
return builder.build();
}
/**
* @see io.apicurio.registry.rest.v3.IdsResource#getContentByGlobalId(long,
* io.apicurio.registry.rest.v3.beans.HandleReferencesType)
*/
@Override
@Authorized(style = AuthorizedStyle.GlobalId, level = AuthorizedLevel.Read)
public Response getContentByGlobalId(long globalId, HandleReferencesType references,
Boolean returnArtifactType) {
ArtifactVersionMetaDataDto metaData = storage.getArtifactVersionMetaData(globalId);
if (VersionState.DISABLED.equals(metaData.getState())
|| VersionState.DRAFT.equals(metaData.getState())) {
throw new ArtifactNotFoundException(null, String.valueOf(globalId));
}
if (references == null) {
references = HandleReferencesType.PRESERVE;
}
StoredArtifactVersionDto artifact = storage.getArtifactVersionContent(globalId);
TypedContent contentToReturn = TypedContent.create(artifact.getContent(), artifact.getContentType());
contentToReturn = handleContentReferences(references, metaData.getArtifactType(), contentToReturn,
artifact.getReferences());
Response.ResponseBuilder builder = Response.ok(contentToReturn.getContent(),
contentToReturn.getContentType());
if (returnArtifactType != null && returnArtifactType) {
builder.header("X-Registry-ArtifactType", metaData.getArtifactType());
}
checkIfDeprecated(metaData::getState, metaData.getArtifactId(), metaData.getVersion(), builder);
return builder.build();
}
/**
* @see io.apicurio.registry.rest.v3.IdsResource#getContentByHash(java.lang.String)
*/
@Override
@Authorized(style = AuthorizedStyle.None, level = AuthorizedLevel.Read)
public Response getContentByHash(String contentHash) {
ContentHandle content = storage.getContentByHash(contentHash).getContent();
Response.ResponseBuilder builder = Response.ok(content, ArtifactMediaTypes.BINARY);
return builder.build();
}
/**
* @see io.apicurio.registry.rest.v3.IdsResource#referencesByContentHash(java.lang.String)
*/
@Override
public List<ArtifactReference> referencesByContentHash(String contentHash) {
return common.getReferencesByContentHash(contentHash);
}
/**
* @see io.apicurio.registry.rest.v3.IdsResource#referencesByContentId(long)
*/
@Override
public List<ArtifactReference> referencesByContentId(long contentId) {
ContentWrapperDto artifact = storage.getContentById(contentId);
return artifact.getReferences().stream().map(V3ApiUtil::referenceDtoToReference)
.collect(Collectors.toList());
}
/**
* @see io.apicurio.registry.rest.v3.IdsResource#referencesByGlobalId(long,
* io.apicurio.registry.types.ReferenceType)
*/
@Override
public List<ArtifactReference> referencesByGlobalId(long globalId, ReferenceType refType) {
if (refType == ReferenceType.OUTBOUND || refType == null) {
StoredArtifactVersionDto artifact = storage.getArtifactVersionContent(globalId);
return artifact.getReferences().stream().map(V3ApiUtil::referenceDtoToReference)
.collect(Collectors.toList());
} else {
ArtifactVersionMetaDataDto vmd = storage.getArtifactVersionMetaData(globalId);
return storage
.getInboundArtifactReferences(vmd.getGroupId(), vmd.getArtifactId(), vmd.getVersion())
.stream().map(V3ApiUtil::referenceDtoToReference).collect(Collectors.toList());
}
}
}