diff --git a/org.knime.core.tests/src/org/knime/core/util/hub/HubItemVersionPersistorTest.java b/org.knime.core.tests/src/org/knime/core/util/hub/HubItemVersionPersistorTest.java deleted file mode 100644 index 8567fb3de1..0000000000 --- a/org.knime.core.tests/src/org/knime/core/util/hub/HubItemVersionPersistorTest.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * ------------------------------------------------------------------------ - * - * Copyright by KNIME AG, Zurich, Switzerland - * Website: http://www.knime.com; Email: contact@knime.com - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License, Version 3, as - * published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, see . - * - * Additional permission under GNU GPL version 3 section 7: - * - * KNIME interoperates with ECLIPSE solely via ECLIPSE's plug-in APIs. - * Hence, KNIME and ECLIPSE are both independent programs and are not - * derived from each other. Should, however, the interpretation of the - * GNU GPL Version 3 ("License") under any applicable laws result in - * KNIME and ECLIPSE being a combined program, KNIME AG herewith grants - * you the additional permission to use and propagate KNIME together with - * ECLIPSE with only the license terms in place for ECLIPSE applying to - * ECLIPSE and the GNU GPL Version 3 applying for KNIME, provided the - * license terms of ECLIPSE themselves allow for the respective use and - * propagation of ECLIPSE together with KNIME. - * - * Additional permission relating to nodes for KNIME that extend the Node - * Extension (and in particular that are based on subclasses of NodeModel, - * NodeDialog, and NodeView) and that only interoperate with KNIME through - * standard APIs ("Nodes"): - * Nodes are deemed to be separate and independent programs and to not be - * covered works. Notwithstanding anything to the contrary in the - * License, the License does not apply to Nodes, you are not required to - * license Nodes under the License, and you are granted a license to - * prepare and propagate Nodes, in each case even if such Nodes are - * propagated with or for interoperation with KNIME. The owner of a Node - * may freely choose the license terms applicable to such Node, including - * when such Node is propagated with or for interoperation with KNIME. - * --------------------------------------------------------------------- - * - * History - * 20 Sept 2023 (carlwitt): created - */ -package org.knime.core.util.hub; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; - -import java.util.List; -import java.util.Optional; - -import org.junit.jupiter.api.Test; -import org.knime.core.node.InvalidSettingsException; -import org.knime.core.node.config.base.SimpleConfig; - -/** - * @author Carl Witt, KNIME AG, Zurich, Switzerland - */ -class HubItemVersionPersistorTest { - - @Test - void testSaveLoad() throws InvalidSettingsException { - // given test versions - var versions = List.of(HubItemVersion.of(5), HubItemVersion.currentState(), HubItemVersion.latestVersion()); - - // when writing to node settings - var settings = - List.of(new SimpleConfig("fixed"), new SimpleConfig("current state"), new SimpleConfig("latest version")); - for (int i = 0; i < versions.size(); i++) { - var config = settings.get(i); - HubItemVersionPersistor.save(versions.get(i), config); - } - - // then loaded settings should be equal to the test versions - for (int i = 0; i < versions.size(); i++) { - var config = settings.get(i); - var loadedVersion = HubItemVersionPersistor.load(config).get(); - assertEquals(versions.get(i), loadedVersion, "loaded version should be equal to the test version"); - } - } - - @Test - void loadSettingsWithoutVersion() throws InvalidSettingsException { - // given a node settings without a version - var config = new SimpleConfig("no version"); - - // when loading the version - var loadedVersion = HubItemVersionPersistor.load(config); - - // then the version should be empty - assertEquals(Optional.empty(), loadedVersion); - } - - @Test - void loadInvalidVersion() { - // given an invalid version - var config = new SimpleConfig("invalid version"); - config.addString(HubItemVersionPersistor.CONFIG_KEY, "invalid version"); - - // when loading the version - // then expect an invalid settings exception - var exception = assertThrows(InvalidSettingsException.class, () -> HubItemVersionPersistor.load(config)); - assertEquals("Invalid hub item version invalid version. Must be LATEST_STATE or LATEST_VERSION or an integer " - + "version number > 0.", exception.getMessage()); - } - -} diff --git a/org.knime.core.tests/src/org/knime/core/util/hub/HubItemVersionTest.java b/org.knime.core.tests/src/org/knime/core/util/hub/HubItemVersionTest.java deleted file mode 100644 index 9f745ab3d0..0000000000 --- a/org.knime.core.tests/src/org/knime/core/util/hub/HubItemVersionTest.java +++ /dev/null @@ -1,281 +0,0 @@ -/* - * ------------------------------------------------------------------------ - * - * Copyright by KNIME AG, Zurich, Switzerland - * Website: http://www.knime.com; Email: contact@knime.com - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License, Version 3, as - * published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, see . - * - * Additional permission under GNU GPL version 3 section 7: - * - * KNIME interoperates with ECLIPSE solely via ECLIPSE's plug-in APIs. - * Hence, KNIME and ECLIPSE are both independent programs and are not - * derived from each other. Should, however, the interpretation of the - * GNU GPL Version 3 ("License") under any applicable laws result in - * KNIME and ECLIPSE being a combined program, KNIME AG herewith grants - * you the additional permission to use and propagate KNIME together with - * ECLIPSE with only the license terms in place for ECLIPSE applying to - * ECLIPSE and the GNU GPL Version 3 applying for KNIME, provided the - * license terms of ECLIPSE themselves allow for the respective use and - * propagation of ECLIPSE together with KNIME. - * - * Additional permission relating to nodes for KNIME that extend the Node - * Extension (and in particular that are based on subclasses of NodeModel, - * NodeDialog, and NodeView) and that only interoperate with KNIME through - * standard APIs ("Nodes"): - * Nodes are deemed to be separate and independent programs and to not be - * covered works. Notwithstanding anything to the contrary in the - * License, the License does not apply to Nodes, you are not required to - * license Nodes under the License, and you are granted a license to - * prepare and propagate Nodes, in each case even if such Nodes are - * propagated with or for interoperation with KNIME. The owner of a Node - * may freely choose the license terms applicable to such Node, including - * when such Node is propagated with or for interoperation with KNIME. - * --------------------------------------------------------------------- - * - * History - * 26 Jun 2023 (carlwitt): created - */ -package org.knime.core.util.hub; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.net.MalformedURLException; -import java.net.URI; -import java.net.URISyntaxException; -import java.net.URL; -import java.nio.charset.StandardCharsets; -import java.util.List; -import java.util.Optional; - -import org.apache.hc.core5.http.NameValuePair; -import org.apache.hc.core5.http.message.BasicNameValuePair; -import org.apache.hc.core5.net.URIBuilder; -import org.apache.hc.core5.net.WWWFormCodec; -import org.junit.Test; -import org.knime.core.node.workflow.TemplateUpdateUtil.LinkType; - -/** - * Tests for {@link HubItemVersion}. - * - * @author Carl Witt, KNIME AG, Zurich, Switzerland - */ -public class HubItemVersionTest { - - /** - * Test that the utility constructors create the expected objects. - */ - @Test - public void testUtilityConstructors() { - assertEquals(new HubItemVersion(LinkType.LATEST_VERSION, null), HubItemVersion.latestVersion(), - "Utility constructor for latest version is faulty."); - assertEquals(new HubItemVersion(LinkType.LATEST_STATE, null), HubItemVersion.currentState(), - "Utility constructor for latest state is faulty."); - assertEquals(new HubItemVersion(LinkType.FIXED_VERSION, 3), HubItemVersion.of(3), - "Utility constructor for fixed version is faulty."); - } - - /** - * Example input: knime://My-KNIME-Hub/*02j3f023j?someParameter=12&version=3¶m=4
- * Example output: HubItemVersion(LinkType.FIXED_VERSION, 3) - * - * @throws MalformedURLException - */ - @Test - public void testExtractVersionNumberWhenNotPresent() throws MalformedURLException { - // given a URI/URL without a version number - final URI uri = URI.create("knime://SomeMountPoint/some/path?someParameter=12"); - final URL url = new URL("https://www.knime.com"); - - // when extracting the version number - var uriVersion = HubItemVersion.of(uri); - var urlVersion = HubItemVersion.of(url); - - // then the version is empty - assertEquals(Optional.empty(), uriVersion, "version should be empty uri: " + uri); - assertEquals(Optional.empty(), urlVersion, "version should be empty url: " + url); - } - - /** - * Example input: knime://My-KNIME-Hub/*02j3f023j?someParameter=12&version=3¶m=4
- * Example output: HubItemVersion(LinkType.FIXED_VERSION, 3) - * - * @throws MalformedURLException - */ - @Test - public void testExtractVersionNumberFixed() throws MalformedURLException { - // given a URI/URL with a version number - final URI uri = URI.create("knime://My-KNIME-Hub/*02j3f023j?multiValueParam=4,4&version=3&otherParam=book"); - final URL url = - new URL("https://www.hub.knime.com/repo/some/item?multiValueParam=4,4&version=3&otherParam=book"); - - // when extracting the version number - var uriVersion = HubItemVersion.of(uri).get(); - var urlVersion = HubItemVersion.of(url).get(); - - // then the link type is fixed version and the version number is 3 - final var version3 = HubItemVersion.of(3); - assertEquals(version3, uriVersion, "version 3 should be returned for uri: " + uri); - assertEquals(version3, urlVersion, "version 3 should be returned for url: " + url); - } - - /** - * Example input: knime://My-KNIME-Hub/*02j3f023j?someParameter=12&version=latest-state¶m=4
- * Example output: HubItemVersion(LinkType.LATEST_STATE, null) - * - * @throws MalformedURLException - */ - @Test - public void testExtractVersionNumberLatestState() throws MalformedURLException { - // given a URI/URL with latest version - final URI uri = URI.create("knime://My-KNIME-Hub/*02j3f023j?someParameter=12&version=most-recent¶m=4"); - final URL url = - new URL("https://www.hub.knime.com/repo/some/item?someParameter=12&version=most-recent¶m=4"); - - // when extracting the version number - var uriVersion = HubItemVersion.of(uri).get(); - var urlVersion = HubItemVersion.of(url).get(); - - // then the link type is latest version and the version number is null - final var latest = HubItemVersion.latestVersion(); - assertEquals(latest, uriVersion, "latest version should be returned for uri: " + uri); - assertEquals(latest, urlVersion, "latest version should be returned for url: " + url); - } - - /** - * Example input: knime://My-KNIME-Hub/*02j3f023j?someParameter=12&version=3¶m=4
- * Example output: knime://My-KNIME-Hub/*02j3f023j?someParameter=12¶m=4 - */ - @Test - public void testApplyCurrentStateRemovesVersion() { - // given a URI/URL with version set - final URI uri = URI.create("knime://My-KNIME-Hub/*02j3f023j?someParameter=12&version=3¶m=4"); - - // when setting to current state - final var appliedUri = HubItemVersion.currentState().applyTo(uri); - - // then the version is removed - assertEquals(URI.create("knime://My-KNIME-Hub/*02j3f023j?someParameter=12¶m=4"), appliedUri, - "version should be removed from URI"); - } - - /** - * Example input: knime://SomeMountPoint/some/path?someParameter=12
- * Example output: unchanged - */ - @Test - public void testApplyCurrentStateToCurrentStateLeavesUnchanged() { - // given a URI/URL without version set - final URI uri = URI.create("knime://SomeMountPoint/some/path?someParameter=12"); - - // when setting to current state - var appliedUri = HubItemVersion.currentState().applyTo(uri); - - // then the result is unchanged - assertEquals(uri, appliedUri, "URI should be unchanged."); - } - - /** - * Example input: knime://My-KNIME-Hub/*02j3f023j?someParameter=12&version=3¶m=4
- * Example output: knime://My-KNIME-Hub/*02j3f023j?someParameter=12&version=4¶m=4 - */ - @Test - public void testApplyFixedVersionUpdatesVersion() { - // given a URI with version set - URI withVersion3 = URI.create("knime://My-KNIME-Hub/*02j3f023j?param=12&version=3¶m=4"); - // when setting a different version - var withVersion4 = HubItemVersion.of(4).applyTo(withVersion3); - // then the version is removed - assertEquals(URI.create("knime://My-KNIME-Hub/*02j3f023j?param=12&version=4¶m=4"), withVersion4, - "version should be updated in URI"); - } - - /** - * Example input: knime://SomeMountPoint/some/path
- * Example output: knime://SomeMountPoint/some/path?version=4 - */ - @Test - public void testApplyFixedVersionAddsVersion() { - // given a URI without version set - URI withoutVersion = URI.create("knime://SomeMountPoint/some/path"); - // when setting a version - var withVersion4 = HubItemVersion.of(4).applyTo(withoutVersion); - // then the version is added - assertEquals(URI.create("knime://SomeMountPoint/some/path?version=4"), withVersion4, - "version should be added to URI"); - } - - /** - * Example input: knime://My-KNIME-Hub/*02j3f023j?someParameter=12&version=3¶m=4
- * Example output: knime://My-KNIME-Hub/*02j3f023j?someParameter=12&version=most-recent¶m=4 - */ - @Test - public void testApplyLatestVersionReplacesFixedVersion() { - // given a URI with version set - URI withVersion3 = URI.create("knime://My-KNIME-Hub/*02j3f023j?someParameter=12&version=3¶m=4"); - // when setting to latest version - var withVersionLatest = HubItemVersion.latestVersion().applyTo(withVersion3); - // then the version is set to most-recent - assertEquals(URI.create("knime://My-KNIME-Hub/*02j3f023j?someParameter=12&version=most-recent¶m=4"), - withVersionLatest, "version should be set to most-recent in URI"); - } - - // no migration from space version anymore - - /** - * Example input: knime://My-KNIME-Hub/*02j3f023j
- * Example output: knime://My-KNIME-Hub/*02j3f023j?version=3&spaceVersion=3 - * - * @throws URISyntaxException - */ - @Test - public void testAddToUriAddsBothParameters() throws URISyntaxException { - // given a fixed version - final var version = HubItemVersion.of(3); - final var builder = new URIBuilder(URI.create("knime://My-KNIME-Hub/*02j3f023j?someParameter=12¶m=4")); - - // when adding to a URI - version.addVersionToURI(builder); - final var result = builder.build(); - - // then the resulting URI has both parameters - final List params = WWWFormCodec.parse(result.getQuery(), StandardCharsets.UTF_8); - assertTrue(params.contains(new BasicNameValuePair(LinkType.VERSION_QUERY_PARAM, "3")), "URI should have query parameter version=3"); - // No support for space version _creation_ anymore - assertFalse(params.contains(new BasicNameValuePair("spaceVersion", "3")), "URI should have query parameter spaceVersion=3"); - } - - /** - * Test that null inputs are handled correctly. They are quite common. - */ - @Test - public void testNullInputs() { - // given null - URI nonExistent = null; - - // no migration from space version anymore - - // when applying, we expect an illegal argument exception - final var version = HubItemVersion.of(1); - assertThrows(IllegalArgumentException.class, () -> version.applyTo(nonExistent), - "Null URI should not be applicable."); - - // when parsing, we expect an illegal argument exception - assertThrows(IllegalArgumentException.class, () -> HubItemVersion.of(nonExistent), - "Null URI should not be parsable."); - } - -} diff --git a/org.knime.core.tests/src/org/knime/core/util/urlresolve/AnalyticsPlatformLocalUrlResolverTest.java b/org.knime.core.tests/src/org/knime/core/util/urlresolve/AnalyticsPlatformLocalUrlResolverTest.java index a17ce5a6db..2358e32164 100644 --- a/org.knime.core.tests/src/org/knime/core/util/urlresolve/AnalyticsPlatformLocalUrlResolverTest.java +++ b/org.knime.core.tests/src/org/knime/core/util/urlresolve/AnalyticsPlatformLocalUrlResolverTest.java @@ -80,7 +80,7 @@ import org.knime.core.node.workflow.contextv2.AnalyticsPlatformExecutorInfo; import org.knime.core.node.workflow.contextv2.WorkflowContextV2; import org.knime.core.util.exception.ResourceAccessException; -import org.knime.core.util.hub.HubItemVersion; +import org.knime.core.util.hub.ItemVersion; /** * Tests for {@link AnalyticsPlatformLocalUrlResolver}. For more generic tests see {@link KnimeUrlResolverTest}. @@ -212,7 +212,7 @@ void testResolveNodeRelativeLocal() throws Exception { @ParameterizedTest @MethodSource("org.knime.core.util.urlresolve.URLMethodSources#nodeRelativeInScope()") void testResolveNodeRelativeLocalNotSaved(final URL unversioned, final URL withVersion, final URL bothVersions, - @SuppressWarnings("unused") final HubItemVersion version) throws Exception { + @SuppressWarnings("unused") final ItemVersion version) throws Exception { withNodeContext(null, () -> { final var exc = assertThrows(ResourceAccessException.class, () -> m_resolver.resolve(unversioned)); assertEquals("Workflow must be saved before node-relative URLs can be used", exc.getLocalizedMessage()); @@ -260,7 +260,7 @@ class Versioning { "org.knime.core.util.urlresolve.URLMethodSources#workflowRelativeLeavingScope()", "org.knime.core.util.urlresolve.URLMethodSources#workflowRelativeInScope()"}) void testResolveRelativeIgnoresItemVersion(final URL unversioned, final URL withVersion, final URL bothVersions, - @SuppressWarnings("unused") final HubItemVersion version) + @SuppressWarnings("unused") final ItemVersion version) throws ResourceAccessException, MalformedURLException { // given a local workflow // when resolving a URL with a version diff --git a/org.knime.core.tests/src/org/knime/core/util/urlresolve/AnalyticsPlatformTempCopyUrlResolverTest.java b/org.knime.core.tests/src/org/knime/core/util/urlresolve/AnalyticsPlatformTempCopyUrlResolverTest.java index 951c58a3bd..6fbbb14f65 100644 --- a/org.knime.core.tests/src/org/knime/core/util/urlresolve/AnalyticsPlatformTempCopyUrlResolverTest.java +++ b/org.knime.core.tests/src/org/knime/core/util/urlresolve/AnalyticsPlatformTempCopyUrlResolverTest.java @@ -81,7 +81,7 @@ import org.knime.core.util.KnimeUrlType; import org.knime.core.util.auth.SimpleTokenAuthenticator; import org.knime.core.util.exception.ResourceAccessException; -import org.knime.core.util.hub.HubItemVersion; +import org.knime.core.util.hub.ItemVersion; import org.knime.core.util.urlresolve.URLMethodSources.Context; import org.knime.core.util.urlresolve.URLMethodSources.WorkspaceType; /** @@ -250,7 +250,7 @@ class Versioning { "org.knime.core.util.urlresolve.URLMethodSources#workflowRelativeLeavingScope()" }) void testResolveRelativeItemVersion(final URL unversioned, final URL withVersion, final URL bothVersions, - final HubItemVersion version) throws ResourceAccessException, MalformedURLException { + final ItemVersion version) throws ResourceAccessException, MalformedURLException { var resolvedWithoutVersion = m_resolver.resolve(unversioned); for (final var url : new URL[] { withVersion, bothVersions }) { var resolved = m_resolver.resolve(url); diff --git a/org.knime.core.tests/src/org/knime/core/util/urlresolve/HubExecutorUrlResolverTest.java b/org.knime.core.tests/src/org/knime/core/util/urlresolve/HubExecutorUrlResolverTest.java index b0abe0bf59..cdc5a3436c 100644 --- a/org.knime.core.tests/src/org/knime/core/util/urlresolve/HubExecutorUrlResolverTest.java +++ b/org.knime.core.tests/src/org/knime/core/util/urlresolve/HubExecutorUrlResolverTest.java @@ -76,7 +76,7 @@ import org.knime.core.node.workflow.contextv2.WorkflowContextV2; import org.knime.core.util.auth.SimpleTokenAuthenticator; import org.knime.core.util.exception.ResourceAccessException; -import org.knime.core.util.hub.HubItemVersion; +import org.knime.core.util.hub.ItemVersion; /** * Tests for {@link HubExecutorUrlResolver}, currently only with a focus on item version handling. @@ -137,7 +137,7 @@ void popNodeContext() { "org.knime.core.util.urlresolve.URLMethodSources#spaceRelative()" }) void testResolveWithVersionParameter(final URL unversioned, final URL withItemOrSpaceVersion, final URL withBoth, - final HubItemVersion version) throws ResourceAccessException, MalformedURLException { + final ItemVersion version) throws ResourceAccessException, MalformedURLException { final var resolvedPlain = m_resolver.resolve(unversioned); var resolved = m_resolver.resolve(withItemOrSpaceVersion); @@ -147,10 +147,10 @@ void testResolveWithVersionParameter(final URL unversioned, final URL withItemOr if (version == null || version.isVersioned()) { assertNotEquals(resolvedPlain, resolved, "Should not ignore version parameter"); assertNotEquals(resolvedPlain, resolvedWithBoth, "Should not ignore version parameter(s)"); - final var fromEither = HubItemVersion.of(resolved); + final var fromEither = URLResolverUtil.parseVersion(resolved.getQuery()); assertEquals(version, fromEither.orElse(null), "Has correct version when specifying either query parameter"); - final var fromBoth = HubItemVersion.of(resolvedWithBoth); + final var fromBoth = URLResolverUtil.parseVersion(resolvedWithBoth.getQuery()); assertEquals(version, fromBoth.orElse(null), "Has correct version when specifying both query parameters"); } } @@ -160,7 +160,7 @@ void testResolveWithVersionParameter(final URL unversioned, final URL withItemOr "org.knime.core.util.urlresolve.URLMethodSources#workflowRelativeInScope()" }) void testResolveWorkflowRelative(final URL unversioned, final URL withVersion, final URL withBoth, - @SuppressWarnings("unused") final HubItemVersion version) throws ResourceAccessException { + @SuppressWarnings("unused") final ItemVersion version) throws ResourceAccessException { assertThat(m_resolver.resolve(unversioned)).hasProtocol("file"); assertThrows(ResourceAccessException.class, () -> m_resolver.resolve(withVersion), @@ -174,7 +174,7 @@ void testResolveWorkflowRelative(final URL unversioned, final URL withVersion, f "org.knime.core.util.urlresolve.URLMethodSources#nodeRelativeInScope()" }) void testResolveNodeRelative(final URL unversioned, final URL withVersion, final URL withBoth, - @SuppressWarnings("unused") final HubItemVersion version) { + @SuppressWarnings("unused") final ItemVersion version) { final var exc = assertThrows(ResourceAccessException.class, () -> m_resolver.resolve(unversioned)); org.junit.Assert.assertEquals("Workflow must be saved before node-relative URLs can be used", exc.getLocalizedMessage()); diff --git a/org.knime.core.tests/src/org/knime/core/util/urlresolve/RemoteExecutorUrlResolverTest.java b/org.knime.core.tests/src/org/knime/core/util/urlresolve/RemoteExecutorUrlResolverTest.java index 8edd5d9059..8454379e59 100644 --- a/org.knime.core.tests/src/org/knime/core/util/urlresolve/RemoteExecutorUrlResolverTest.java +++ b/org.knime.core.tests/src/org/knime/core/util/urlresolve/RemoteExecutorUrlResolverTest.java @@ -70,7 +70,7 @@ import org.knime.core.node.workflow.contextv2.WorkflowContextV2; import org.knime.core.util.auth.SimpleTokenAuthenticator; import org.knime.core.util.exception.ResourceAccessException; -import org.knime.core.util.hub.HubItemVersion; +import org.knime.core.util.hub.ItemVersion; import org.knime.core.util.urlresolve.URLMethodSources.Context; import org.knime.core.util.urlresolve.URLMethodSources.WorkspaceType; @@ -181,14 +181,14 @@ void testResolveWorkfowRelativeURIToServerMountpointURI() throws Exception { "org.knime.core.util.urlresolve.URLMethodSources#spaceRelative()" }) void testVersionedUrls(final URL unversioned, final URL versioned, final URL bothVersions, - final HubItemVersion version) throws ResourceAccessException { + final ItemVersion version) throws ResourceAccessException { final var resolvedPlain = m_resolver.resolve(unversioned); for (final var url : new URL[] { versioned, bothVersions }) { final var resolved = m_resolver.resolve(url); - final var isVersioned = !HubItemVersion.currentState().equals(version); + final var isVersioned = version.isVersioned(); if (isVersioned) { assertNotEquals(resolvedPlain, resolved, "Version should not be ignored"); - final var fromUrl = HubItemVersion.of(resolved); + final var fromUrl = URLResolverUtil.parseVersion(resolved.getQuery()); assertEquals(version, fromUrl.orElseThrow(), "Has correct version"); } } @@ -226,13 +226,13 @@ void createContext() throws URISyntaxException { "org.knime.core.util.urlresolve.URLMethodSources#mountpointAbsolute()", }) void testVersionedUrls(final URL unversioned, final URL versioned, final URL bothVersions, - final HubItemVersion version) throws ResourceAccessException { + final ItemVersion version) throws ResourceAccessException { final var resolvedPlain = m_resolver.resolve(unversioned); for (final var url : new URL[] { versioned, bothVersions }) { final var resolved = m_resolver.resolve(url); - if (!HubItemVersion.currentState().equals(version)) { + if (version.isVersioned()) { assertNotEquals(resolvedPlain, resolved, "Version should not be ignored"); - final var fromUrl = HubItemVersion.of(resolved); + final var fromUrl = URLResolverUtil.parseVersion(resolved.getQuery()); assertEquals(version, fromUrl.orElseThrow(), "Has correct version"); } diff --git a/org.knime.core.tests/src/org/knime/core/util/urlresolve/ServerExecutorUrlResolverTest.java b/org.knime.core.tests/src/org/knime/core/util/urlresolve/ServerExecutorUrlResolverTest.java index 50ade501ec..6c1dffd81c 100644 --- a/org.knime.core.tests/src/org/knime/core/util/urlresolve/ServerExecutorUrlResolverTest.java +++ b/org.knime.core.tests/src/org/knime/core/util/urlresolve/ServerExecutorUrlResolverTest.java @@ -70,7 +70,7 @@ import org.knime.core.node.workflow.contextv2.WorkflowContextV2; import org.knime.core.util.auth.SimpleTokenAuthenticator; import org.knime.core.util.exception.ResourceAccessException; -import org.knime.core.util.hub.HubItemVersion; +import org.knime.core.util.hub.ItemVersion; /** * Tests for {@link ServerExecutorUrlResolver}, currently only with a focus on item version handling. * @@ -256,7 +256,7 @@ void testResolveMountpointRelativeToServer() throws Exception { "org.knime.core.util.urlresolve.URLMethodSources#spaceRelative()" }) void testResolveForbidden(@SuppressWarnings("unused") final URL unversioned, final URL withVersion, - final URL withBoth, @SuppressWarnings("unused") final HubItemVersion version) { + final URL withBoth, @SuppressWarnings("unused") final ItemVersion version) { for (final var url : new URL[] { withVersion, withBoth }) { final var ex = assertThrows(ResourceAccessException.class, () -> m_resolver.resolve(url), "Must not contain version query parameter"); @@ -269,7 +269,7 @@ void testResolveForbidden(@SuppressWarnings("unused") final URL unversioned, fin "org.knime.core.util.urlresolve.URLMethodSources#nodeRelativeInScope()" }) void testResolveNodeRelativeNoItemVersion(@SuppressWarnings("unused") final URL unversioned, final URL withVersion, - final URL withBoth, @SuppressWarnings("unused") final HubItemVersion version) { + final URL withBoth, @SuppressWarnings("unused") final ItemVersion version) { for (final var url : new URL[] { withVersion, withBoth }) { final var ex = assertThrows(ResourceAccessException.class, () -> m_resolver.resolve(url), "Must not contain version query parameter"); diff --git a/org.knime.core.tests/src/org/knime/core/util/urlresolve/URLMethodSources.java b/org.knime.core.tests/src/org/knime/core/util/urlresolve/URLMethodSources.java index 5a8b897e3a..a2f021b7da 100644 --- a/org.knime.core.tests/src/org/knime/core/util/urlresolve/URLMethodSources.java +++ b/org.knime.core.tests/src/org/knime/core/util/urlresolve/URLMethodSources.java @@ -61,7 +61,7 @@ import org.junit.jupiter.params.provider.MethodSource; import org.knime.core.node.workflow.contextv2.WorkflowContextV2; import org.knime.core.util.auth.SimpleTokenAuthenticator; -import org.knime.core.util.hub.HubItemVersion; +import org.knime.core.util.hub.ItemVersion; /** * Utility class to provide {@link MethodSource method sources} to parameterized test methods. @@ -296,19 +296,19 @@ static Stream mountpointAbsolute() { Arguments.of("knime://My-Knime-Hub/Users/john/Private/test.txt", "knime://My-Knime-Hub/Users/john/Private/test.txt?version=3", "knime://My-Knime-Hub/Users/john/Private/test.txt?version=3&spaceVersion=3", - HubItemVersion.of(3)), + ItemVersion.of(3)), Arguments.of("knime://My-Knime-Hub/Users/john/Private/test.txt", "knime://My-Knime-Hub/Users/john/Private/test.txt?spaceVersion=3", "knime://My-Knime-Hub/Users/john/Private/test.txt?version=3&spaceVersion=3", - HubItemVersion.of(3)), + ItemVersion.of(3)), Arguments.of("knime://My-Knime-Hub/Users/john/Private/test.txt", "knime://My-Knime-Hub/Users/john/Private/test.txt?version=most-recent", "knime://My-Knime-Hub/Users/john/Private/test.txt?version=most-recent&spaceVersion=latest", - HubItemVersion.latestVersion()), + ItemVersion.mostRecent()), Arguments.of("knime://My-Knime-Hub/Users/john/Private/test.txt", "knime://My-Knime-Hub/Users/john/Private/test.txt?version=current-state", "knime://My-Knime-Hub/Users/john/Private/test.txt?version=current-state&spaceVersion=-1", - HubItemVersion.currentState()) + ItemVersion.currentState()) ); } @@ -318,19 +318,19 @@ static Stream mountpointRelative() { Arguments.of("knime://knime.mountpoint/test.txt", "knime://knime.mountpoint/test.txt?version=3", "knime://knime.mountpoint/test.txt?version=3&spaceVersion=3", - HubItemVersion.of(3)), + ItemVersion.of(3)), Arguments.of("knime://knime.mountpoint/test.txt", "knime://knime.mountpoint/test.txt?spaceVersion=3", "knime://knime.mountpoint/test.txt?version=3&spaceVersion=3", - HubItemVersion.of(3)), + ItemVersion.of(3)), Arguments.of("knime://knime.mountpoint/test.txt", "knime://knime.mountpoint/test.txt?version=most-recent", "knime://knime.mountpoint/test.txt?version=most-recent&spaceVersion=latest", - HubItemVersion.latestVersion()), + ItemVersion.mostRecent()), Arguments.of("knime://knime.mountpoint/test.txt", "knime://knime.mountpoint/test.txt?version=current-state", "knime://knime.mountpoint/test.txt?version=current-state&spaceVersion=-1", - HubItemVersion.currentState()) + ItemVersion.currentState()) ); } @@ -340,19 +340,19 @@ static Stream spaceRelative() { Arguments.of("knime://knime.space/test.txt", "knime://knime.space/test.txt?version=3", "knime://knime.space/test.txt?version=3&spaceVersion=3", - HubItemVersion.of(3)), + ItemVersion.of(3)), Arguments.of("knime://knime.space/test.txt", "knime://knime.space/test.txt?spaceVersion=3", "knime://knime.space/test.txt?version=3&spaceVersion=3", - HubItemVersion.of(3)), + ItemVersion.of(3)), Arguments.of("knime://knime.space/test.txt", "knime://knime.space/test.txt?version=most-recent", "knime://knime.space/test.txt?version=most-recent&spaceVersion=latest", - HubItemVersion.latestVersion()), + ItemVersion.mostRecent()), Arguments.of("knime://knime.space/test.txt", "knime://knime.space/test.txt?version=current-state", "knime://knime.space/test.txt?version=current-state&spaceVersion=-1", - HubItemVersion.currentState()) + ItemVersion.currentState()) ); } @@ -362,19 +362,19 @@ static Stream workflowRelativeLeavingScope() { Arguments.of("knime://knime.workflow/../test.txt", "knime://knime.workflow/../test.txt?version=3", "knime://knime.workflow/../test.txt?version=3&spaceVersion=3", - HubItemVersion.of(3)), + ItemVersion.of(3)), Arguments.of("knime://knime.workflow/../test.txt", "knime://knime.workflow/../test.txt?spaceVersion=3", "knime://knime.workflow/../test.txt?version=3&spaceVersion=3", - HubItemVersion.of(3)), + ItemVersion.of(3)), Arguments.of("knime://knime.workflow/../test.txt", "knime://knime.workflow/../test.txt?version=most-recent", "knime://knime.workflow/../test.txt?version=most-recent&spaceVersion=latest", - HubItemVersion.latestVersion()), + ItemVersion.mostRecent()), Arguments.of("knime://knime.workflow/../test.txt", "knime://knime.workflow/../test.txt?version=current-state", "knime://knime.workflow/../test.txt?version=current-state&spaceVersion=-1", - HubItemVersion.currentState()) + ItemVersion.currentState()) ); } diff --git a/org.knime.core/src/eclipse/org/knime/core/node/workflow/MetaNodeTemplateInformation.java b/org.knime.core/src/eclipse/org/knime/core/node/workflow/MetaNodeTemplateInformation.java index f2d03bc5d0..c60aedc518 100644 --- a/org.knime.core/src/eclipse/org/knime/core/node/workflow/MetaNodeTemplateInformation.java +++ b/org.knime.core/src/eclipse/org/knime/core/node/workflow/MetaNodeTemplateInformation.java @@ -68,7 +68,7 @@ import org.knime.core.node.NodeSettingsWO; import org.knime.core.node.util.CheckUtils; import org.knime.core.util.LoadVersion; -import org.knime.core.util.hub.HubItemVersion; +import org.knime.core.util.hub.ItemVersion; import org.knime.core.util.urlresolve.URLResolverUtil; import org.knime.shared.workflow.def.TemplateInfoDef; @@ -294,8 +294,9 @@ public MetaNodeTemplateInformation createLink(final URI sourceURI, final boolean public MetaNodeTemplateInformation createLinkWithUpdatedSource(final URI newSource) throws InvalidSettingsException { // ifModifiedSince is set to null because always perform a download for components with a space version. - final var newLastModified = HubItemVersion.of(newSource).filter(HubItemVersion::isVersioned).isPresent() - ? Instant.EPOCH : null; + final var newLastModified = URLResolverUtil.parseVersion(newSource.getQuery()) // + .filter(ItemVersion::isVersioned) // + .isPresent() ? Instant.EPOCH : null; return createLinkWithUpdatedSource(newSource, newLastModified); } @@ -442,7 +443,7 @@ boolean isNewerOrNotEqualThan(final MetaNodeTemplateInformation other) { throw new IllegalStateException("Argument not a template or link: " + this); } - if (HubItemVersion.of(m_sourceURI).filter(HubItemVersion::isVersioned).isPresent()) { + if (URLResolverUtil.parseVersion(m_sourceURI.getQuery()).filter(ItemVersion::isVersioned).isPresent()) { return !m_timestamp.equals(other.m_timestamp); } else { return m_timestamp.isAfter(other.m_timestamp); diff --git a/org.knime.core/src/eclipse/org/knime/core/node/workflow/TemplateUpdateUtil.java b/org.knime.core/src/eclipse/org/knime/core/node/workflow/TemplateUpdateUtil.java index b136fb0c9c..4cb3fd01c5 100644 --- a/org.knime.core/src/eclipse/org/knime/core/node/workflow/TemplateUpdateUtil.java +++ b/org.knime.core/src/eclipse/org/knime/core/node/workflow/TemplateUpdateUtil.java @@ -91,113 +91,37 @@ public final class TemplateUpdateUtil { /** - * A link to a template on Hub can contain three different kinds of version information through a query parameter. - *
    - *
  • A specific item version can be referenced: {@code version=42}
  • - *
  • The link can point to the latest version: {@code version=most-recent}
  • - *
  • If no {@code version} query parameter is given or {@code version=current-state} the current state - * (i.e., the staging area) of the space is referenced.
  • - *
- * - * @since 5.0 - * @deprecated prefer {@link ItemVersion} for working with Hub item versions + * Hollowed-out enum, throws {@link UnsupportedOperationException} in every method. + * To be removed soon! */ @Deprecated(since = "5.5", forRemoval = true) public enum LinkType { - /** Specific version is selected, no updates will be available. */ - FIXED_VERSION(Object::toString, null, null), - - /** Latest version is selected. */ - LATEST_VERSION(v -> "most-recent", "most-recent", "latest"), - - /** Current state is selected, which includes unversioned changes. */ - LATEST_STATE(v -> null, "current-state", "-1"); + FIXED_VERSION(Object::toString, null, null), + LATEST_VERSION(v -> "most-recent", "most-recent", "latest"), + LATEST_STATE(v -> null, "current-state", "-1"); - private final Function m_paramString; - private final String m_identifier; - private final String m_legacyIdentifier; - - /** - * The key used in KNIME hub catalog service query parameters to specify the version of a repository item. - * Note that the hub execution service uses "itemVersion" instead. - * @since 5.1 - */ public static final String VERSION_QUERY_PARAM = "version"; - /** - * spaceVersion query parameter was used prior to item level versioning. Space versions do not exist anymore. - * However, when the KNIME hub migrated from space versions to item versions, item versions were created such - * that item version X matches the content in space version X. - * @since 5.1 - */ public static final String LEGACY_SPACE_VERSION_QUERY_PARAM = "spaceVersion"; - /** - * Creates a link type. - * - * @param paramString function to create the query parameter for the numeric version - * @param identifier identifier to denote a non-numeric version - * @param legacyIdentifier legacy identifier (space versioning) - */ LinkType(final Function paramString, final String identifier, final String legacyIdentifier) { - m_paramString = paramString; - m_identifier = identifier; - m_legacyIdentifier = legacyIdentifier; + throw new UnsupportedOperationException(); } - /** - * Creates the value for the {@code version} query parameter. - * - * @param version item version number, only used for {@link #FIXED_VERSION} - * @return parameter value, e.g. {@code "most-recent"}, {@code "current-state"} or {@code "123"} - */ public String getParameterString(final Integer version) { - return m_paramString.apply(version); + throw new UnsupportedOperationException(); } - /** - * @return the value used in query parameters to denote a specific link type. {@code null} for FIXED_VERSION. - * @since 5.1 - */ public String getIdentifier() { - return m_identifier; + throw new UnsupportedOperationException(); } - /** - * @return the value previously used in query parameters (for Space Versioning) to denote a specific link type. - * {@code null} for FIXED_VERSION. - * @since 5.1 - */ public String getLegacyIdentifier() { - return m_legacyIdentifier; - } - - /** - * @return a permanent identifier for this link type - * @since 5.2 - */ - @Override - public String toString() { - return switch (this) { - case LATEST_STATE -> "LATEST_STATE"; - case LATEST_VERSION -> "LATEST_VERSION"; - case FIXED_VERSION -> "FIXED_VERSION"; - }; + throw new UnsupportedOperationException(); } - /** - * @param s permanent identifier as returned by {@link #toString()} - * @return the link type for the given string representation, or {@link Optional#empty()} if the string does - * not match any identifier. - * @since 5.2 - */ public static Optional fromString(final String s) { - return switch (s) { - case "LATEST_STATE" -> Optional.of(LATEST_STATE); - case "LATEST_VERSION" -> Optional.of(LATEST_VERSION); - case "FIXED_VERSION" -> Optional.of(FIXED_VERSION); - default -> Optional.empty(); - }; + throw new UnsupportedOperationException(); } } diff --git a/org.knime.core/src/eclipse/org/knime/core/util/hub/HubItemVersion.java b/org.knime.core/src/eclipse/org/knime/core/util/hub/HubItemVersion.java index 06d5213419..b18fd800ee 100644 --- a/org.knime.core/src/eclipse/org/knime/core/util/hub/HubItemVersion.java +++ b/org.knime.core/src/eclipse/org/knime/core/util/hub/HubItemVersion.java @@ -49,275 +49,68 @@ package org.knime.core.util.hub; import java.net.URI; -import java.net.URISyntaxException; import java.net.URL; -import java.nio.charset.StandardCharsets; import java.util.Optional; import org.apache.hc.core5.net.URIBuilder; -import org.apache.hc.core5.net.WWWFormCodec; -import org.knime.core.node.util.CheckUtils; import org.knime.core.node.workflow.TemplateUpdateUtil.LinkType; -import org.knime.core.util.urlresolve.URLResolverUtil; /** - * Reference to a KNIME Hub item version. Provides utility methods for conversion to/from URI query parameters. - * - * @param linkType whether this refers to a fixed version, the latest version, or the staging area. Never {@code null}. - * @param versionNumber only for fixed version: the id of the version - * - * @author Carl Witt, KNIME AG, Zurich, Switzerland - * @since 5.1 - * @deprecated Use {@link ItemVersion} for version references and {@link URLResolverUtil} for URL query parameter handling - * @see ItemVersion - * @see URLResolverUtil + * Hollowed-out record, throws {@link UnsupportedOperationException} in every method. + * To be removed soon! */ @Deprecated(since = "5.5", forRemoval = true) public record HubItemVersion(LinkType linkType, Integer versionNumber) { - private static final HubItemVersion CURRENT_STATE = new HubItemVersion(LinkType.LATEST_STATE, null); - - private static final HubItemVersion MOST_RECENT = new HubItemVersion(LinkType.LATEST_VERSION, null); - - /** - * @param linkType whether this refers to a fixed version, the latest version, or the staging area. Never - * {@code null}. - * @param versionNumber only for fixed version: the id of the version - * @throws IllegalArgumentException if the link type is null or if the link type is not equal to fixed version but a - * version number is specified. - */ - public HubItemVersion { - CheckUtils.checkArgumentNotNull(linkType); - if(linkType == LinkType.FIXED_VERSION) { - CheckUtils.checkNotNull(versionNumber); - CheckUtils.checkArgument(versionNumber > 0, "Version number must be larger than zero, but was %d.", - versionNumber); - } else { - CheckUtils.checkArgument(versionNumber == null, - "Version number can only be specified for a fixed version."); - } - } - - /** - * @return {@code false} if this refers to content in the staging area, {@code true} if this refers to content - * in a specific version or the latest version. - */ public boolean isVersioned() { - return linkType != LinkType.LATEST_STATE; + throw new UnsupportedOperationException(); } - /** - * @return the value for the version query parameter, e.g., "most-recent" or "4". Empty if this refers to the - * staging area. - */ public Optional getQueryParameterValue() { - return Optional.ofNullable(linkType.getParameterString(versionNumber)); + throw new UnsupportedOperationException(); } - /** - * Adds the item version query parameter to the given uri or removes it if this represents an item's current state. - * - * @param uri to derive new URI from. Non-null. - * @return source URI, possibly with item version query parameter. - * @see URLResolverUtil#applyTo(ItemVersion, java.util.List) - */ public URI applyTo(final URI uri) { - CheckUtils.checkArgumentNotNull(uri); - final var builder = new URIBuilder(uri); - final var params = builder.getQueryParams(); - final var replaced = URLResolverUtil.applyTo(convert(this), params); - try { - return builder.setParameters(replaced).build(); - } catch (final URISyntaxException e) { - // we just modify the query parameters and these should not cause a URISyntaxException - throw new IllegalStateException(e); - } + throw new UnsupportedOperationException(); } - /** - * @return a to the staging area state - * @see ItemVersion#currentState() - */ public static HubItemVersion currentState() { - return CURRENT_STATE; + throw new UnsupportedOperationException(); } - /** - * @return a reference to the newest version state - * @see ItemVersion#mostRecent() - */ public static HubItemVersion latestVersion() { - return MOST_RECENT; + throw new UnsupportedOperationException(); } - /** - * @param versionNumber id of the version, larger than zero - * @return a reference to a fixed version state - * @see SpecificVersion#SpecificVersion(int) - */ public static HubItemVersion of(final int versionNumber) { - return new HubItemVersion(LinkType.FIXED_VERSION, versionNumber); + throw new UnsupportedOperationException(); } - /** - * Extracts the item version from a URI. - * - * For instance - *
    - *
  • "knime://My-KNIME-Hub/*Rllck6Bn2-EaOR6d?version=3" -> (FIXED_VERSION, 3)
  • - *
  • "knime://My-KNIME-Hub/*Rllck6Bn2-EaOR6d?version=most-recent" -> (LATEST_VERSION, null)
  • - *
- * - * @param knimeUrl KNIME URL. Non-null. - * @return the link type and item version. Item version is {@code null} for link types ≠ FIXED_VERSION - * @throws IllegalArgumentException if the given URL is null or the version cannot be determined - * @see URLResolverUtil#parseVersion(String) - */ public static Optional of(final URI knimeUrl) { - CheckUtils.checkArgumentNotNull(knimeUrl); - return fromQuery(knimeUrl.getQuery(), knimeUrl.toString()); + throw new UnsupportedOperationException(); } - /** - * Extracts the item version from a URL. - * - * For instance - *
    - *
  • "knime://My-KNIME-Hub/*Rllck6Bn2-EaOR6d?version=3" -> (FIXED_VERSION, 3)
  • - *
  • "knime://My-KNIME-Hub/*Rllck6Bn2-EaOR6d?version=most-recent" -> (LATEST_VERSION, null)
  • - *
- * - * @param knimeUrl KNIME URL. Non-null. - * @return the link type and item version. Item version is {@code null} for link types ≠ FIXED_VERSION - * @throws IllegalArgumentException if the given URI is null or the version cannot be determined - * @see URLResolverUtil#parseVersion(String) - */ public static Optional of(final URL knimeUrl) { - CheckUtils.checkArgumentNotNull(knimeUrl); - return fromQuery(knimeUrl.getQuery(), knimeUrl.toString()); - } - - private static Optional fromQuery(final String query, final String url) { // NOSONAR - if (query == null || query.isEmpty()) { - return Optional.empty(); - } - - HubItemVersion found = null; - for (final var param : WWWFormCodec.parse(query, StandardCharsets.UTF_8)) { - final var isItemVersion = LinkType.VERSION_QUERY_PARAM.equals(param.getName()); - if (isItemVersion) { - final var value = CheckUtils.checkArgumentNotNull(param.getValue(), - "version parameter can't be empty in URL '%s'", url); - - final HubItemVersion versionHere; - if (value.equals(LinkType.LATEST_STATE.getIdentifier())) { - versionHere = CURRENT_STATE; - } else if (value.equals(LinkType.LATEST_VERSION.getIdentifier())) { - versionHere = MOST_RECENT; - } else { - try { // NOSONAR - versionHere = HubItemVersion.of(Integer.parseInt(value)); - } catch (final NumberFormatException nfe) { - throw new IllegalArgumentException("Version specifier '" + value + "' not recognized in URL '" - + url + "'.", nfe); - } - } - - if (found != null && !found.equals(versionHere)) { - throw new IllegalArgumentException("Conflicting version parameters in URL " + url); - } - found = versionHere; - } - } - - return Optional.ofNullable(found); + throw new UnsupportedOperationException(); } - /** - * Migrates an URI that might contain a "Space Version" query parameter to item versioning. - * - * @param uri URI that might contain a spaceVersion query parameter. Nullable. - * @return URI that has no spaceVersion parameter. If the input has a spaceVersion=X, the output has version=X. If - * given {@code null}, {@code null} is returned. - */ - // kept for backwards compatibility public static URI migrateFromSpaceVersion(final URI uri) { - final var builder = new URIBuilder(uri); - final var params = URLResolverUtil.migrateFromSpaceVersion(uri.getQuery()); - builder.setCustomQuery(params); - try { - return builder.build(); - } catch (final URISyntaxException e) { - // we just modify the query parameters and these should not cause a URISyntaxException - throw new IllegalStateException(e); - } + throw new UnsupportedOperationException(); } - /** - * Adds the {@code version=[...]} query parameter to the given builder if this version needs them. - * - * @param uriBuilder URI builder - * @since 5.0 - */ public void addVersionToURI(final URIBuilder uriBuilder) { - addVersionToURI(uriBuilder, false); + throw new UnsupportedOperationException(); } - /** - * Adds the new {@code version=[...]} query parameter and optionally the legacy {@code spaceVersion=[...]} to the - * given builder if this version needs them. - * - * @param uriBuilder URI builder - * @param addSpaceVersion if {@code true}, the {@code spaceVersion=[...]} is also being added - * @since 5.3 - */ public void addVersionToURI(final URIBuilder uriBuilder, final boolean addSpaceVersion) { - if (isVersioned()) { - if (linkType == LinkType.LATEST_VERSION) { - uriBuilder.addParameter(LinkType.VERSION_QUERY_PARAM, LinkType.LATEST_VERSION.getIdentifier()); - if (addSpaceVersion) { - uriBuilder.addParameter(LinkType.LEGACY_SPACE_VERSION_QUERY_PARAM, - LinkType.LATEST_VERSION.getLegacyIdentifier()); - } - } else { - final var versionNo = versionNumber.toString(); - uriBuilder.addParameter(LinkType.VERSION_QUERY_PARAM, versionNo); - if (addSpaceVersion) { - uriBuilder.addParameter(LinkType.LEGACY_SPACE_VERSION_QUERY_PARAM, versionNo); - } - } - } + throw new UnsupportedOperationException(); } - /** - * Converts a {@link HubItemVersion} to an {@link ItemVersion}. - * - * @param version the version - * @return the corresponding {@link ItemVersion} - * @since 5.5 - */ public static ItemVersion convert(final HubItemVersion version) { - if (version == null) { - return null; - } - return switch (version.linkType) { - case LATEST_STATE -> ItemVersion.currentState(); - case LATEST_VERSION -> ItemVersion.mostRecent(); - case FIXED_VERSION -> new SpecificVersion(version.versionNumber()); - }; + throw new UnsupportedOperationException(); } - /** - * Converts an {@link ItemVersion} to a {@link HubItemVersion}. - * - * @param version the version - * @return the corresponding {@link HubItemVersion} - * @since 5.5 - */ public static HubItemVersion convert(final ItemVersion version) { - if (version == null) { - return null; - } - return version.match(() -> currentState(), () -> latestVersion(), sv -> of(sv)); + throw new UnsupportedOperationException(); } } \ No newline at end of file diff --git a/org.knime.core/src/eclipse/org/knime/core/util/hub/HubItemVersionPersistor.java b/org.knime.core/src/eclipse/org/knime/core/util/hub/HubItemVersionPersistor.java index 4a072ed470..a2c0a2845d 100644 --- a/org.knime.core/src/eclipse/org/knime/core/util/hub/HubItemVersionPersistor.java +++ b/org.knime.core/src/eclipse/org/knime/core/util/hub/HubItemVersionPersistor.java @@ -56,21 +56,10 @@ import org.knime.core.node.NodeDialogPane; import org.knime.core.node.config.base.ConfigBaseRO; import org.knime.core.node.config.base.ConfigBaseWO; -import org.knime.core.node.workflow.FlowVariable; -import org.knime.core.node.workflow.TemplateUpdateUtil.LinkType; -import org.knime.core.node.workflow.VariableType; /** - * Stores a hub item version in a configuration. Uses a string variable - *
    - *
  • in order to be compatible with the Version Creator node which currently outputs the created version (an integer) - * as string
  • - *
  • as an easy union type that can hold fixed versions (integers) and version references (enum constants)
  • - *
- * - * @author Carl Witt, KNIME AG, Zurich, Switzerland - * @since 5.2 - * @deprecated use {@link ItemVersion} and {@link ItemVersionStringPersistor} instead + * Hollowed-out class, throws {@link UnsupportedOperationException} in every method. + * To be removed soon! */ @Deprecated(since = "5.5", forRemoval = true) public final class HubItemVersionPersistor { @@ -78,76 +67,22 @@ public final class HubItemVersionPersistor { private HubItemVersionPersistor() { } - /** The settings key used to store the version information */ public static final String CONFIG_KEY = ItemVersionStringPersistor.CONFIG_KEY; - private static String toString(final HubItemVersion version) { - return Optional.ofNullable(version.versionNumber())// - .map(Object::toString)// - .orElse(version.linkType().toString()); - } - - private static HubItemVersion fromString(final String versionString) throws InvalidSettingsException { - final var linkType = LinkType.fromString(versionString).orElse(LinkType.FIXED_VERSION); - if (linkType == LinkType.FIXED_VERSION) { - try { - return HubItemVersion.of(Integer.parseInt(versionString)); - } catch (IllegalArgumentException e) { - throw new InvalidSettingsException( - "Invalid hub item version %s. Must be %s or %s or an integer version number > 0." - .formatted(versionString, LinkType.LATEST_STATE.toString(), LinkType.LATEST_VERSION.toString()), - e); - } - } else { - return new HubItemVersion(linkType, null); - } - } - - /** - * Stores either the version number, e.g., "2" or the link type, e.g., "LATEST_STATE" - * - * @param version to save - * @param config to save to - */ public static void save(final HubItemVersion version, final ConfigBaseWO config) { - config.addString(CONFIG_KEY, toString(version)); + throw new UnsupportedOperationException(); } - /** - * @param config to load from - * @return the loaded version or empty if the settings do not contain the required key - * @throws InvalidSettingsException if the settings contain broken hub item version data - */ public static Optional load(final ConfigBaseRO config) throws InvalidSettingsException { - if (!config.containsKey(CONFIG_KEY)) { - return Optional.empty(); - } - final var versionString = config.getString(CONFIG_KEY); - return Optional.of(fromString(versionString)); + throw new UnsupportedOperationException(); } - /** - * @param pane node dialog pane that manages the flow variables - * @return a model that can be listened to in order to reflect flow variable state in legacy dialogs - */ public static FlowVariableModel createFlowVariableModel(final NodeDialogPane pane) { - return pane.createFlowVariableModel(CONFIG_KEY, VariableType.StringType.INSTANCE); + throw new UnsupportedOperationException(); } - /** - * @param evt the change event - * @return the version that was set in the flow variable or empty if the event does not contain a flow variable - * @throws InvalidSettingsException if the flow variable contains an invalid value, i.e., invalid link type or - * non-parseable version number - */ public static Optional fromFlowVariableChangeEvent(final EventObject evt) throws InvalidSettingsException { - if (evt.getSource() instanceof FlowVariableModel fvm && fvm.isVariableReplacementEnabled()) { - final var versionString = fvm.getVariableValue().map(FlowVariable::getStringValue); - // can't do flatmap because of exception - return versionString.isPresent() ? Optional.of(HubItemVersionPersistor.fromString(versionString.get())) - : Optional.empty(); - } - return Optional.empty(); + throw new UnsupportedOperationException(); } } \ No newline at end of file diff --git a/org.knime.core/src/eclipse/org/knime/core/util/hub/ItemVersionStringPersistor.java b/org.knime.core/src/eclipse/org/knime/core/util/hub/ItemVersionStringPersistor.java index bb6ce09a86..6379dc93f4 100644 --- a/org.knime.core/src/eclipse/org/knime/core/util/hub/ItemVersionStringPersistor.java +++ b/org.knime.core/src/eclipse/org/knime/core/util/hub/ItemVersionStringPersistor.java @@ -110,13 +110,13 @@ private static String toString(final ItemVersion version) { */ private static ItemVersion fromString(final String versionString) throws InvalidSettingsException { if (CURRENT_STATE_LINK_TYPE.equals(versionString)) { - return CurrentState.getInstance(); + return ItemVersion.currentState(); } if (MOST_RECENT_LINK_TYPE.equals(versionString)) { - return MostRecent.getInstance(); + return ItemVersion.mostRecent(); } try { - return new SpecificVersion(Integer.parseUnsignedInt(versionString)); + return ItemVersion.of(Integer.parseUnsignedInt(versionString)); } catch (final NumberFormatException e) { throw new InvalidSettingsException( "Invalid Hub item version \"%s\". Valid values are \"%s\", \"%s\", or a non-negative integer value." diff --git a/org.knime.core/src/eclipse/org/knime/core/util/urlresolve/URLResolverUtil.java b/org.knime.core/src/eclipse/org/knime/core/util/urlresolve/URLResolverUtil.java index 325006da0a..aaf09142eb 100644 --- a/org.knime.core/src/eclipse/org/knime/core/util/urlresolve/URLResolverUtil.java +++ b/org.knime.core/src/eclipse/org/knime/core/util/urlresolve/URLResolverUtil.java @@ -58,10 +58,7 @@ import java.util.List; import java.util.Optional; import java.util.function.BiConsumer; -import java.util.function.IntConsumer; -import java.util.function.IntFunction; -import org.apache.commons.lang3.function.FailableCallable; import org.apache.hc.core5.http.NameValuePair; import org.apache.hc.core5.http.message.BasicNameValuePair; import org.apache.hc.core5.net.URIBuilder; @@ -69,10 +66,7 @@ import org.knime.core.node.util.CheckUtils; import org.knime.core.util.URIPathEncoder; import org.knime.core.util.exception.ResourceAccessException; -import org.knime.core.util.hub.CurrentState; import org.knime.core.util.hub.ItemVersion; -import org.knime.core.util.hub.MostRecent; -import org.knime.core.util.hub.SpecificVersion; import org.knime.core.util.pathresolve.URIToFileResolve; /** @@ -149,26 +143,15 @@ static URL toURL(final Path path) throws ResourceAccessException { private URLResolverUtil() { } - // utilities to work with item version - // from core LinkType.LATEST_VERSION.getIdentifier() private static final String MOST_RECENT_IDENTIFIER = "most-recent"; - private static final String LEGACY_SPACE_VERSION_MOST_RECENT = "latest"; - - // from core LinkType.LATEST_STATE.getIdentifier() - private static final String CURRENT_STATE_IDENTIFIER = "current-state"; - - private static final String LEGACY_SPACE_VERSION_CURRENT = "-1"; - // from core LinkType.VERSION_QUERY_PARAM private static final String VERSION_QUERY_PARAM_NAME = "version"; // from core LinkType.SPACE_VERSION_QUERY_PARAM, needed for backwards compatibility private static final String SPACE_VERSION_QUERY_PARAM_NAME = "spaceVersion"; - // these methods formerly lived in the HubItemVersion class - /** * Applies the given {@link ItemVersion} to the given query parameters by adding/replacing the version. In case the * given version is not {@link ItemVersion#isVersioned}, the parameter is removed. @@ -194,11 +177,8 @@ public static List applyTo(final ItemVersion version, final List< */ public static void addVersionQueryParameter(final ItemVersion version, final BiConsumer parameterConsumer) { - match(version, // - () -> { }, // nothing to do for current-state, like the old method - () -> parameterConsumer.accept(VERSION_QUERY_PARAM_NAME, MOST_RECENT_IDENTIFIER), // - v -> parameterConsumer.accept(VERSION_QUERY_PARAM_NAME, Integer.toString(v)) // - ); + getQueryParameterValue(version) // + .ifPresent(value -> parameterConsumer.accept(VERSION_QUERY_PARAM_NAME, value)); } /** @@ -261,7 +241,7 @@ private static Optional fromQueryParameters(final Collection fromQueryParameters(final Collection getQueryParameterValue(final ItemVersion version) { - return matchFn(version, // - Optional::empty, // + CheckUtils.checkArgumentNotNull(version, "Version cannot be null"); + return version.match( // + Optional::empty, // nothing to return for current-state, like the old method () -> Optional.of(MOST_RECENT_IDENTIFIER), // v -> Optional.of(Integer.toString(v)) // ); } - /** - * Matches one of the functions with the given non-{@code null} version and applies it. - * - * @param return type of the functions - * @param version {@code null}able version to apply functions to - * @param currentState function to apply if given current-state version - * @param mostRecent function to apply if given most-recent version - * @param versionFn function to apply if given fixed version number - * @return function return value or {@code null} if version was {@code null} - */ - private static T matchFn(final ItemVersion version, - final FailableCallable currentState, final FailableCallable mostRecent, - final IntFunction versionFn) throws E { - CheckUtils.checkArgumentNotNull(version, "Version cannot be null"); - // good candidate for pattern-matching switch in next Java version (preview in Java 17) - if (version instanceof CurrentState) { - return currentState.call(); - } - if (version instanceof MostRecent) { - return mostRecent.call(); - } - if (version instanceof SpecificVersion sv) { - return versionFn.apply(sv.version()); - } - throw new IllegalStateException("Unexpected item version class: \"%s\"".formatted(version.getClass())); - } - - /** - * Matches one of the given callbacks with the given non-{@code null} version and runs it. - * @param version {@code null}able version to apply functions to - * @param currentState callback to run if given current-state version - * @param mostRecent callback to run if given most-recent version - * @param versionConsumer callback to run if given fixed version number - */ - private static void match(final ItemVersion version, final Runnable currentState, - final Runnable mostRecent, final IntConsumer versionConsumer) { - CheckUtils.checkArgumentNotNull(version, "Version cannot be null"); - // good candidate for pattern-matching switch in next Java version (preview in Java 17) - if (version instanceof CurrentState) { - currentState.run(); - } else if (version instanceof MostRecent) { - mostRecent.run(); - } else if (version instanceof SpecificVersion sv) { - versionConsumer.accept(sv.version()); - } else { - throw new IllegalStateException("Unexpected item version class: \"%s\"".formatted(version.getClass())); - } - } - - /** - * Matches the given query parameter value potentially representing an {@link ItemVersion} to the proper - * {@link ItemVersion} if possible. - * - * @param itemVersionParamValue query parameter value to match to {@link ItemVersion} - * @return {@link ItemVersion} if it can be matched (parsed), otherwise {@link Optional#empty()} - */ - private static Optional matchVersionValue(final String itemVersionParamValue) { - return switch (itemVersionParamValue) { - case CURRENT_STATE_IDENTIFIER, LEGACY_SPACE_VERSION_CURRENT -> Optional.of(CurrentState.getInstance()); - case MOST_RECENT_IDENTIFIER, LEGACY_SPACE_VERSION_MOST_RECENT -> Optional.of(MostRecent.getInstance()); - default -> parseIntegerVersion(itemVersionParamValue); - }; - } - - private static Optional parseIntegerVersion(final String itemVersionParamValue) { - try { - return Optional.of(new SpecificVersion(Integer.parseUnsignedInt(itemVersionParamValue))); - } catch (final NumberFormatException e) { - throw new IllegalArgumentException( - "Cannot parse specific version from value: \"%s\"".formatted(itemVersionParamValue), e); - } - } }