Skip to content

Commit

Permalink
Eliminate SonarQube warnings, "invalid" link type (#11)
Browse files Browse the repository at this point in the history
and introduce "invalid" link type to mark invalid links
  • Loading branch information
stefanseifert authored Jan 20, 2024
1 parent c0337f7 commit 98187cb
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 16 deletions.
3 changes: 3 additions & 0 deletions changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
<body>

<release version="2.0.0" date="not released">
<action type="add" dev="sseifert">
Introduce link type "invalid" to mark invalid links.
</action>
<action type="remove" dev="sseifert" issue="7">
Remove deprecated functionality.
</action>
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/io/wcm/handler/link/LinkArgs.java
Original file line number Diff line number Diff line change
Expand Up @@ -326,10 +326,9 @@ public String toString() {
* Custom clone-method for {@link LinkArgs}
* @return the cloned {@link LinkArgs}
*/
// CHECKSTYLE:OFF
@Override
@SuppressWarnings({ "java:S2975", "java:S1182", "checkstyle:SuperCloneCheck" }) // ignore clone warnings
public LinkArgs clone() { //NOPMD
// CHECKSTYLE:ON
LinkArgs clone = new LinkArgs();

clone.urlMode = this.urlMode;
Expand Down
25 changes: 13 additions & 12 deletions src/main/java/io/wcm/handler/link/impl/LinkHandlerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import io.wcm.handler.link.spi.LinkMarkupBuilder;
import io.wcm.handler.link.spi.LinkProcessor;
import io.wcm.handler.link.spi.LinkType;
import io.wcm.handler.link.type.InvalidLinkType;
import io.wcm.sling.commons.adapter.AdaptTo;
import io.wcm.sling.models.annotations.AemObject;
import io.wcm.wcm.commons.component.ComponentPropertyResolverFactory;
Expand Down Expand Up @@ -91,7 +92,11 @@ public final class LinkHandlerImpl implements LinkHandler {
* @return Link metadata (never null)
*/
@NotNull
@SuppressWarnings({ "null", "unused" })
@SuppressWarnings({
"null", "unused",
"java:S6541", "java:S3776", "java:S2589", // ignore complexity
"java:S112" // runtime exception
})
@SuppressFBWarnings({ "CORRECTNESS", "STYLE" })
Link processRequest(@NotNull LinkRequest linkRequest) {

Expand All @@ -108,6 +113,9 @@ Link processRequest(@NotNull LinkRequest linkRequest) {
break;
}
}
if (linkType == null) {
linkType = AdaptTo.notNull(adaptable, InvalidLinkType.class);
}
Link link = new Link(linkType, linkRequest);

// preprocess link before resolving
Expand All @@ -123,11 +131,9 @@ Link processRequest(@NotNull LinkRequest linkRequest) {
}

// resolve link
if (linkType != null) {
link = linkType.resolveLink(link);
if (link == null) {
throw new RuntimeException("LinkType '" + linkType + "' returned null, page '" + (currentPage != null ? currentPage.getPath() : "-") + "'.");
}
link = linkType.resolveLink(link);
if (link == null) {
throw new RuntimeException("LinkType '" + linkType + "' returned null, page '" + (currentPage != null ? currentPage.getPath() : "-") + "'.");
}

// if link is invalid - check if a fallback link property is set and try resolution with it
Expand Down Expand Up @@ -172,12 +178,7 @@ Link processRequest(@NotNull LinkRequest linkRequest) {

@Override
public Link invalid() {
// build invalid link with first link type
Class<? extends LinkType> linkTypeClass = linkHandlerConfig.getLinkTypes().stream().findFirst().orElse(null);
if (linkTypeClass == null) {
throw new RuntimeException("No link types defined.");
}
LinkType linkType = AdaptTo.notNull(adaptable, linkTypeClass);
LinkType linkType = AdaptTo.notNull(adaptable, InvalidLinkType.class);
return new Link(linkType, new LinkRequest(null, null, null));
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/io/wcm/handler/link/spi/LinkHandlerConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,13 @@ public abstract class LinkHandlerConfig implements ContextAwareService {
/**
* Default content root path.
*/
@SuppressWarnings("java:S1075") // no file path
public static final String DEFAULT_ROOT_PATH_CONTENT = "/content";

/**
* Default media/asset root path.
*/
@SuppressWarnings("java:S1075") // no file path
public static final String DEFAULT_ROOT_PATH_MEDIA = "/content/dam";

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public final class ExternalLinkType extends LinkType {
* Matches all strings that seem to have a proper URL scheme - e.g. starting with http://, https://, mailto:, tel:
* It also allows anchor links staring with #
*/
private static final Pattern EXTERNALIZED_PATTERN = Pattern.compile("^(([^/]+:|//)|#).*$");
private static final Pattern EXTERNALIZED_PATTERN = Pattern.compile("^([^/]+:|//|#).+?");

/**
* @return Link type ID (is stored as identifier in repository)
Expand Down
69 changes: 69 additions & 0 deletions src/main/java/io/wcm/handler/link/type/InvalidLinkType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* #%L
* wcm.io
* %%
* Copyright (C) 2024 wcm.io
* %%
* 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.
* #L%
*/
package io.wcm.handler.link.type;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.Model;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.osgi.annotation.versioning.ProviderType;

import io.wcm.handler.link.Link;
import io.wcm.handler.link.spi.LinkType;

/**
* This link type is used for invalid links. It must not be registered explicitly.
*/
@Model(adaptables = {
SlingHttpServletRequest.class, Resource.class
})
@ProviderType
public final class InvalidLinkType extends LinkType {

/**
* Link type ID
*/
public static final @NotNull String ID = "invalid";

/**
* @return Link type ID (is stored as identifier in repository)
*/
@Override
public @NotNull String getId() {
return ID;
}

@Override
public @Nullable String getPrimaryLinkRefProperty() {
return null;
}

@Override
public boolean accepts(@NotNull String linkRef) {
return false;
}

@Override
public @NotNull Link resolveLink(@NotNull Link link) {
return link;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ public final class InternalLinkResolver {
* @param options Options
* @return true if link is acceptable
*/
@SuppressWarnings({
"java:S1172", // options is unused, but may be needed in the future
"java:S1126" // keep separate if statements for better readability
})
public boolean acceptPage(@Nullable Page page, @NotNull InternalLinkResolverOptions options) {
if (page == null) {
return false;
Expand Down Expand Up @@ -120,6 +124,7 @@ public boolean acceptPage(@Nullable Page page, @NotNull InternalLinkResolverOpti
* @param options Options to influence the link resolution process
* @return Resolved link object
*/
@SuppressWarnings("java:S3776") // ignore complexity
public @NotNull Link resolveLink(@NotNull Link link, @NotNull InternalLinkResolverOptions options) {
LinkRequest linkRequest = link.getLinkRequest();
ValueMap props = linkRequest.getResourceProperties();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void increaseCount() {
*/
public void decreaseCount() {
if (this.count == 0) {
throw new RuntimeException("Cannot decrease, counter is already 0.");
throw new IllegalStateException("Cannot decrease, counter is already 0.");
}
this.count--;
if (this.count == 0) {
Expand Down
60 changes: 60 additions & 0 deletions src/test/java/io/wcm/handler/link/type/InvalidLinkTypeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* #%L
* wcm.io
* %%
* Copyright (C) 2024 wcm.io
* %%
* 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.
* #L%
*/
package io.wcm.handler.link.type;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

import org.apache.sling.api.adapter.Adaptable;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import io.wcm.handler.link.Link;
import io.wcm.handler.link.LinkRequest;
import io.wcm.handler.link.spi.LinkType;
import io.wcm.handler.link.testcontext.AppAemContext;
import io.wcm.sling.commons.adapter.AdaptTo;
import io.wcm.testing.mock.aem.junit5.AemContext;
import io.wcm.testing.mock.aem.junit5.AemContextExtension;

@ExtendWith(AemContextExtension.class)
class InvalidLinkTypeTest {

final AemContext context = AppAemContext.newAemContext();

protected Adaptable adaptable() {
return context.request();
}

@Test
void testInvalidLink() {
LinkType underTest = AdaptTo.notNull(adaptable(), InvalidLinkType.class);

assertEquals(InvalidLinkType.ID, underTest.getId());
assertNull(underTest.getPrimaryLinkRefProperty());
assertFalse(underTest.accepts(new LinkRequest(null, null, null)));

Link link = underTest.resolveLink(new Link(underTest, new LinkRequest(null, null, null)));
assertNotNull(link);
assertFalse(link.isValid());
}
}

0 comments on commit 98187cb

Please sign in to comment.