Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move some dereferencing logic to hub-core and implement dereferencing from the ws modules (for sharing) #1721

Merged
merged 6 commits into from
Mar 10, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

import io.apicurio.datamodels.Library;
import io.apicurio.datamodels.core.models.Document;
import io.apicurio.hub.core.content.AbsoluteReferenceResolver;
import io.apicurio.hub.core.content.SharedReferenceResolver;
import io.apicurio.hub.core.exceptions.UnresolvableReferenceException;

/**
* Used to take a {@link Document} and convert it to its dereferenced
Expand All @@ -40,21 +43,28 @@ public class ContentDereferencer {
private AbsoluteReferenceResolver absoluteResolver;
@Inject
private InternalReferenceResolver apicurioResolver;
@Inject
private SharedReferenceResolver sharedReferenceResolver;

@PostConstruct
void init() {
Library.addReferenceResolver(apicurioResolver);
Library.addReferenceResolver(absoluteResolver);
Library.addReferenceResolver(sharedReferenceResolver);
}

/**
* Called to dereference the given content. Content must be in JSON format.
* @param content
* @throws IOException
*/
public String dereference(String content) throws IOException {
public String dereference(String content) throws UnresolvableReferenceException {
Document doc = Library.readDocumentFromJSONString(content);
doc = Library.dereferenceDocument(doc);
try {
doc = Library.dereferenceDocument(doc, true);
} catch (Exception e) {
throw new UnresolvableReferenceException(e);
}
return Library.writeDocumentToJSONString(doc);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import io.apicurio.hub.api.security.ISecurityContext;
import io.apicurio.hub.core.beans.ApiDesignContent;
import io.apicurio.hub.core.content.AbstractReferenceResolver;
import io.apicurio.hub.core.exceptions.NotFoundException;
import io.apicurio.hub.core.storage.IStorage;
import io.apicurio.hub.core.storage.StorageException;
Expand Down Expand Up @@ -54,7 +55,7 @@ public InternalReferenceResolver() {
}

/**
* @see io.apicurio.hub.api.content.AbstractReferenceResolver#accepts(java.net.URI)
* @see io.apicurio.hub.core.content.AbstractReferenceResolver#accepts(java.net.URI)
*/
@Override
protected boolean accepts(URI uri) {
Expand All @@ -63,7 +64,7 @@ protected boolean accepts(URI uri) {
}

/**
* @see io.apicurio.hub.api.content.AbstractReferenceResolver#fetchUriContent(java.net.URI)
* @see io.apicurio.hub.core.content.AbstractReferenceResolver#fetchUriContent(java.net.URI)
*/
@Override
protected String fetchUriContent(URI referenceUri) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
import io.apicurio.hub.core.beans.CodegenProject;
import io.apicurio.hub.core.beans.CodegenProjectType;
import io.apicurio.hub.core.beans.Contributor;
import io.apicurio.hub.core.beans.DereferenceControlResult;
import io.apicurio.hub.core.beans.FormatType;
import io.apicurio.hub.core.beans.Invitation;
import io.apicurio.hub.core.beans.LinkedAccountType;
Expand All @@ -116,11 +117,13 @@
import io.apicurio.hub.core.cmd.OaiCommandException;
import io.apicurio.hub.core.cmd.OaiCommandExecutor;
import io.apicurio.hub.core.config.HubConfiguration;
import io.apicurio.hub.core.content.ContentAccessibilityAsserter;
import io.apicurio.hub.core.editing.IEditingSessionManager;
import io.apicurio.hub.core.exceptions.AccessDeniedException;
import io.apicurio.hub.core.exceptions.ApiValidationException;
import io.apicurio.hub.core.exceptions.NotFoundException;
import io.apicurio.hub.core.exceptions.ServerError;
import io.apicurio.hub.core.exceptions.UnresolvableReferenceException;
import io.apicurio.hub.core.integrations.ApicurioEventType;
import io.apicurio.hub.core.integrations.EventsService;
import io.apicurio.hub.core.storage.IStorage;
Expand Down Expand Up @@ -156,6 +159,8 @@ public class DesignsResource implements IDesignsResource {
@Inject
private ContentDereferencer dereferencer;
@Inject
private ContentAccessibilityAsserter accessibilityAsserter;
@Inject
private IEditingSessionManager editingSessionManager;
@Inject
private IMicrocksConnector microcks;
Expand Down Expand Up @@ -1125,7 +1130,7 @@ private String getApiContent(String designId, FormatType format, boolean derefer
}

return content;
} catch (StorageException | OaiCommandException | IOException e) {
} catch (StorageException | OaiCommandException | IOException | UnresolvableReferenceException e) {
throw new ServerError(e);
}
}
Expand Down Expand Up @@ -1440,7 +1445,7 @@ private String generateAndPublishProject(CodegenProject project, boolean updateO
} else {
throw new ServerError("Unsupported project type: " + project.getType());
}
} catch (IOException | SourceConnectorException e) {
} catch (IOException | SourceConnectorException | UnresolvableReferenceException e) {
throw new ServerError(e);
}
}
Expand Down Expand Up @@ -1585,6 +1590,15 @@ public SharingConfiguration configureSharing(String designId, UpdateSharingConfi
if (!this.authorizationService.hasOwnerPermission(currentUser, designId)) {
throw new NotFoundException();
}

if (SharingLevel.DOCUMENTATION == config.getLevel()) {
final ApiDesignContent document = this.storage.getLatestContentDocument(currentUser.getLogin(), designId);
final String content = document.getDocument();
final DereferenceControlResult dereferenceControlResult = accessibilityAsserter.isDereferenceable(content);
if (!dereferenceControlResult.isSuccess()) {
throw new ServerError(dereferenceControlResult.getError());
}
}

this.storage.setSharingConfig(designId, uuid, config.getLevel());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import io.apicurio.datamodels.openapi.v3.models.Oas30Document;
import io.apicurio.datamodels.openapi.v3.models.Oas30Response;
import io.apicurio.datamodels.openapi.v3.models.Oas30SchemaDefinition;
import io.apicurio.hub.core.content.AbsoluteReferenceResolver;

/**
* @author eric.wittmann@gmail.com
Expand All @@ -70,7 +71,7 @@ public static void tearDown() {
}

/**
* Test method for {@link io.apicurio.hub.api.content.AbsoluteReferenceResolver#resolveRef(java.lang.String, io.apicurio.datamodels.core.models.Node)}.
* Test method for {@link io.apicurio.hub.core.content.AbsoluteReferenceResolver#resolveRef(java.lang.String, io.apicurio.datamodels.core.models.Node)}.
*/
@Test
public void testResolveRef_OpenAPI3() {
Expand All @@ -95,7 +96,7 @@ public void testResolveRef_OpenAPI3() {
}

/**
* Test method for {@link io.apicurio.hub.api.content.AbsoluteReferenceResolver#resolveRef(java.lang.String, io.apicurio.datamodels.core.models.Node)}.
* Test method for {@link io.apicurio.hub.core.content.AbsoluteReferenceResolver#resolveRef(java.lang.String, io.apicurio.datamodels.core.models.Node)}.
*/
@Test
public void testResolveRef_OpenAPI2() {
Expand All @@ -119,7 +120,7 @@ public void testResolveRef_OpenAPI2() {
}

/**
* Test method for {@link io.apicurio.hub.api.content.AbsoluteReferenceResolver#resolveRef(java.lang.String, io.apicurio.datamodels.core.models.Node)}.
* Test method for {@link io.apicurio.hub.core.content.AbsoluteReferenceResolver#resolveRef(java.lang.String, io.apicurio.datamodels.core.models.Node)}.
*/
@Test
public void testResolveRef_AsyncAPI2() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2019 JBoss Inc
*
* Licensed 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 io.apicurio.hub.core.beans;

/**
* @author c.desc2@gmail.com
*/
public class DereferenceControlResult {

private boolean success = true;
private String error;

/**
* Constructor.
*/
public DereferenceControlResult() {
}

/**
* @return the success
*/
public boolean isSuccess() {
return success;
}

/**
* @param success the success to set
*/
public void setSuccess(boolean success) {
this.success = success;
}

/**
* @return the error
*/
public String getError() {
return error;
}

/**
* @param error the error to set
*/
public void setError(String error) {
this.error = error;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package io.apicurio.hub.api.content;
package io.apicurio.hub.core.content;

import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -63,7 +63,7 @@ public AbsoluteReferenceResolver() {
}

/**
* @see io.apicurio.hub.api.content.AbstractReferenceResolver#accepts(java.net.URI)
* @see AbstractReferenceResolver#accepts(java.net.URI)
*/
@Override
protected boolean accepts(URI uri) {
Expand All @@ -72,7 +72,7 @@ protected boolean accepts(URI uri) {
}

/**
* @see io.apicurio.hub.api.content.AbstractReferenceResolver#fetchUriContent(java.net.URI)
* @see AbstractReferenceResolver#fetchUriContent(java.net.URI)
*/
@Override
protected String fetchUriContent(URI referenceUri) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package io.apicurio.hub.api.content;
package io.apicurio.hub.core.content;

import java.io.IOException;
import java.net.MalformedURLException;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2019 JBoss Inc
*
* Licensed 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 io.apicurio.hub.core.content;

import io.apicurio.datamodels.Library;
import io.apicurio.datamodels.core.models.Document;
import io.apicurio.datamodels.core.util.ReferenceResolverChain;
import io.apicurio.hub.core.beans.DereferenceControlResult;

import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

/**
* Used to take a {@link Document} and check if it can be accessed from the hub-core package
* @author c.desc2@gmail.com
*/
@ApplicationScoped
public class ContentAccessibilityAsserter {

@Inject
private AbsoluteReferenceResolver absoluteResolver;
@Inject
private SharedReferenceResolver sharedReferenceResolver;

private final ReferenceResolverChain referenceResolverChain = new ReferenceResolverChain();

@PostConstruct
void init() {
referenceResolverChain.addResolver(absoluteResolver);
referenceResolverChain.addResolver(sharedReferenceResolver);
}

/**
* Checks whether the minimal reference resolvers can handle a document. Content must be in JSON format.
* @param content
* @return
*/
public DereferenceControlResult isDereferenceable(String content) {
final DereferenceControlResult res = new DereferenceControlResult();
try {
Document doc = Library.readDocumentFromJSONString(content);
Library.dereferenceDocument(doc, referenceResolverChain, true);
res.setSuccess(true);
} catch (Exception e) {
res.setError(e.getMessage());
res.setSuccess(false);
}
return res;
}

}
Loading