Skip to content

Commit

Permalink
fix: route reference check (#416)
Browse files Browse the repository at this point in the history
* chore: fix method to check if a url is a
 route reference

* chore: add route repository mock
bean

* chore: add route repository mock
bean

* chore: refactor method to get UUID

* chore: remove unused import

* chore: fix
IndexOutOfBoundsException
  • Loading branch information
omarsilva1 authored Aug 31, 2022
1 parent ad3743d commit 1031815
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,20 @@
* 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.
*
* Contributors:
* sovity GmbH
*
*/
package io.dataspaceconnector.common.net;

import io.dataspaceconnector.common.exception.UUIDFormatException;
import io.dataspaceconnector.common.util.UUIDUtils;
import io.dataspaceconnector.config.BasePath;
import io.dataspaceconnector.model.artifact.Artifact;
import io.dataspaceconnector.repository.RouteRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
Expand All @@ -30,6 +38,7 @@
*/
@Component
@RequiredArgsConstructor
@Log4j2
public final class ApiReferenceHelper {

/**
Expand All @@ -39,16 +48,24 @@ public final class ApiReferenceHelper {
private String defaultBaseUrl;

/**
* Checks whether a URL is a route reference by checking whether the URL starts with the path
* to this connector's routes API. The full path to the routes API consists of the application's
* base URL and the path segment for the routes API.
* Route repository used to check if a given url is a route.
*/
private final RouteRepository routeRepository;

/**
* Checks whether a URL is a route reference by searching for
* the uuid of the url in the route repository.
*
* @param url The URL to check.
* @return True, if the URL is a route reference; false otherwise.
*/
public boolean isRouteReference(final URL url) {
final var routesApiUrl = getBaseUrl() + BasePath.ROUTES;
return url.toString().startsWith(routesApiUrl);
try {
final var uuid = UUIDUtils.uuidFromUrl(url);
return routeRepository.existsById(uuid);
} catch (UUIDFormatException exception) {
return false;
}
}

/**
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/io/dataspaceconnector/common/util/UUIDUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import io.dataspaceconnector.common.exception.UUIDFormatException;

import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
Expand Down Expand Up @@ -108,6 +110,23 @@ public static UUID uuidFromUri(final URI uri, final int index) throws UUIDFormat
}
}

/**
* Extracts a UUID from a URL. If more than one UUID is found the last UUID is returned. See
* also {@link #uuidFromUri}.
*
* @param url The URL from which the UUID should be extracted.
* @return the extracted UUID.
* @throws UUIDFormatException if the URL does not contain a parsable UUID.
*/
public static UUID uuidFromUrl(final URL url) throws UUIDFormatException {
try {
return uuidFromUri(url.toURI(), -1);
} catch (URISyntaxException | IndexOutOfBoundsException exception) {
throw new UUIDFormatException("No uuid could be found in the uri.", exception);
}
}


/**
* Generates a unique UUID, if it does not already exist.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import io.dataspaceconnector.model.endpoint.Endpoint;
import io.dataspaceconnector.model.endpoint.GenericEndpoint;
import io.dataspaceconnector.model.route.Route;
import io.dataspaceconnector.repository.RouteRepository;
import lombok.SneakyThrows;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -63,6 +64,9 @@ public class IdsAppRouteBuilderTest {
@MockBean
private SelfLinkHelper selfLinkHelper;

@MockBean
private RouteRepository routeRepository;

private final URI endpointDocumentation = URI.create("https://documentation.com");

private final UUID uuid = UUID.randomUUID();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import io.dataspaceconnector.model.artifact.ArtifactDesc;
import io.dataspaceconnector.model.artifact.ArtifactFactory;
import io.dataspaceconnector.model.base.Entity;
import io.dataspaceconnector.repository.DapsRepository;
import io.dataspaceconnector.repository.RouteRepository;
import lombok.SneakyThrows;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -54,6 +56,7 @@ public class IdsArtifactBuilderTest {
@MockBean
private SelfLinkHelper selfLinkHelper;


private final ZonedDateTime date = ZonedDateTime.now(ZoneOffset.UTC);

private final UUID uuid = UUID.randomUUID();
Expand Down

0 comments on commit 1031815

Please sign in to comment.