Skip to content

Commit

Permalink
eliminate code warnings with updated null annotations, add additional…
Browse files Browse the repository at this point in the history
… null checks
  • Loading branch information
stefanseifert committed Jan 17, 2024
1 parent 86ccddf commit 65513f1
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ public boolean isValid() {
@JsonSerialize(using = LinkHtmlAttributesSerializer.class)
@JsonProperty("attributes")
public @NotNull Map getHtmlAttributes() {
return link.getAnchorAttributes();
Map<String, String> result = link.getAnchorAttributes();
if (result == null) {
result = Map.of();
}
return result;
}

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

import io.wcm.handler.link.Link;
import io.wcm.handler.media.Media;
import io.wcm.handler.media.Rendition;
import io.wcm.wcm.core.components.impl.models.helpers.ImageAreaV1Impl;
import io.wcm.wcm.core.components.impl.models.v3.ImageV3Impl;
import io.wcm.wcm.core.components.impl.servlets.ImageWidthProxyServlet;
Expand Down Expand Up @@ -89,7 +90,11 @@ protected List<ImageArea> buildAreas() {
*/
@Override
protected List<Long> buildRenditionWidths() {
long maxWidth = media.getRendition().getWidth();
Rendition rendition = media.getRendition();
if (rendition == null) {
return List.of();
}
long maxWidth = rendition.getWidth();
String[] configuredWidths = currentStyle.get(PN_DESIGN_ALLOWED_RENDITION_WIDTHS, new String[0]);
return Arrays.stream(configuredWidths)
.map(NumberUtils::toLong)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,9 @@ private boolean isCurrent(@NotNull Page page, @NotNull Link link) {
}

private boolean currentPageIsRedirectTarget(@NotNull Link link) {
return link.getTargetPage() != null
&& StringUtils.equals(getCurrentPage().getPath(), link.getTargetPage().getPath());
Page targetPage = link.getTargetPage();
return targetPage != null
&& StringUtils.equals(getCurrentPage().getPath(), targetPage.getPath());
}

protected NavigationItem newNavigationItem(@NotNull Page page, @NotNull Link link,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,9 @@ private void activate() {

// resolve media and properties from DAM asset
media = buildMedia(altFromAsset);
Rendition rendition = media.getRendition();

if (media.isValid() && !media.getRendition().isImage()) {
if (media.isValid() && (rendition == null || !rendition.isImage())) {
// no image asset selected (cannot be rendered) - set to invalid
media = mediaHandler.invalid();
}
Expand Down Expand Up @@ -213,9 +214,13 @@ private void initPropertiesFromDamAsset(ValueMap properties) {
* @return Widths
*/
protected List<Long> buildRenditionWidths() {
double primaryRatio = media.getRendition().getRatio();
Rendition rendition = media.getRendition();
if (rendition == null) {
return List.of();
}
double primaryRatio = rendition.getRatio();
return media.getRenditions().stream()
.filter(rendition -> Ratio.matches(rendition.getRatio(), primaryRatio))
.filter(item -> Ratio.matches(item.getRatio(), primaryRatio))
.map(Rendition::getWidth)
.distinct()
.sorted()
Expand All @@ -228,7 +233,7 @@ protected List<Long> buildRenditionWidths() {
*/
protected String buildSrcPattern() {
Rendition rendition = media.getRendition();
if (!rendition.isImage() || rendition.isVectorImage()) {
if (rendition == null || !rendition.isImage() || rendition.isVectorImage()) {
return null;
}
UriTemplate uriTempalte = rendition.getUriTemplate(UriTemplateType.SCALE_WIDTH);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import io.wcm.handler.media.Media;
import io.wcm.handler.media.MediaHandler;
import io.wcm.handler.media.MediaNameConstants;
import io.wcm.handler.media.Rendition;
import io.wcm.sling.commons.adapter.AdaptTo;
import io.wcm.sling.models.annotations.AemObject;
import io.wcm.wcm.core.components.impl.models.helpers.AbstractComponentImpl;
Expand Down Expand Up @@ -126,8 +127,9 @@ private void activate() {
.mediaHandlerProperty("data-cmp-hook-image", "image")
.mediaHandlerProperty(MediaNameConstants.PROP_CSS_CLASS, "cmp-wcmio-responsiveimage__image")
.buildMedia();
Rendition rendition = media.getRendition();

if (media.isValid() && !media.getRendition().isImage()) {
if (media.isValid() && (rendition == null || !rendition.isImage())) {
// no image asset selected (cannot be rendered) - set to invalid
media = mediaHandler.invalid();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.apache.sling.commons.mime.MimeTypeService;
import org.apache.sling.servlets.annotations.SlingServletResourceTypes;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
Expand All @@ -27,6 +28,7 @@
import io.wcm.handler.media.MediaArgs;
import io.wcm.handler.media.MediaBuilder;
import io.wcm.handler.media.MediaHandler;
import io.wcm.handler.media.Rendition;
import io.wcm.handler.media.impl.ImageFileServlet;
import io.wcm.sling.commons.adapter.AdaptTo;
import io.wcm.wcm.commons.contenttype.ContentType;
Expand Down Expand Up @@ -78,15 +80,17 @@ protected void doGet(@NotNull SlingHttpServletRequest request, @NotNull SlingHtt
}

// if media url used ImageFileServlet forward request
if (usesImageFileServlet(media.getUrl())) {
log.debug("Forward to {}", media.getUrl());
request.getRequestDispatcher(media.getUrl()).forward(request, response);
String url = media.getUrl();
Rendition rendition = media.getRendition();
if (usesImageFileServlet(url)) {
log.debug("Forward to {}", url);
request.getRequestDispatcher(url).forward(request, response);
}
else {
else if (rendition != null) {
// otherwise it points directly to a binary in repository, stream it directly
log.debug("Stream binary content from {}", media.getRendition().getPath());
log.debug("Stream binary content from {}", rendition.getPath());
response.setContentType(getMimeType(request));
Resource resource = AdaptTo.notNull(media.getRendition(), Resource.class);
Resource resource = AdaptTo.notNull(rendition, Resource.class);
try (InputStream is = resource.adaptTo(InputStream.class)) {
BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
IOUtils.copy(is, bos);
Expand All @@ -113,7 +117,7 @@ private Media buildMedia(SlingHttpServletRequest request, long width) {
return builder.build();
}

private boolean usesImageFileServlet(String mediaUrl) {
private boolean usesImageFileServlet(@Nullable String mediaUrl) {
return StringUtils.contains(mediaUrl, "." + ImageFileServlet.SELECTOR + ".");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,14 @@ void setUp() {
}

@Test
@SuppressWarnings("unused")
void testInvalid() {
LinkWrapper underTest = new LinkWrapper(linkHandler.invalid());

assertFalse(underTest.isValid());
assertNull(underTest.getURL());
assertNull(underTest.getMappedURL());
assertNull(underTest.getExternalizedURL());
assertNull(underTest.getHtmlAttributes());
assertEquals(Map.of(), underTest.getHtmlAttributes());
assertNull(underTest.getReference());
assertNotNull(underTest.getLinkObject());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ void testInvalidAssetReference() {
}

@Test
@SuppressWarnings("null")
void testWithAssetImage() {
context.currentResource(context.create().resource(page.getContentResource().getPath() + "/image",
PROPERTY_RESOURCE_TYPE, RESOURCE_TYPE,
Expand Down Expand Up @@ -289,6 +290,7 @@ void testWithNoImageAsset() {
}

@Test
@SuppressWarnings("null")
void testDisplayPopupTitle() {
context.currentResource(context.create().resource(page.getContentResource().getPath() + "/image",
PROPERTY_RESOURCE_TYPE, RESOURCE_TYPE,
Expand Down Expand Up @@ -349,6 +351,7 @@ void testUUID_Disabled() {
}

@Test
@SuppressWarnings("null")
void testAreas() {
context.currentResource(context.create().resource(page.getContentResource().getPath() + "/image",
PROPERTY_RESOURCE_TYPE, RESOURCE_TYPE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ void testInvalidImage() {
}

@Test
@SuppressWarnings("null")
void testComponentImage() {
Resource component = context.create().resource(page1, "comp1",
PROPERTY_RESOURCE_TYPE, COMPONENT_RESOURCE_TYPE,
Expand All @@ -119,6 +120,7 @@ void testComponentImage() {
}

@Test
@SuppressWarnings("null")
void testComponentImage_Decorative() {
Resource component = context.create().resource(page1, "comp1",
PROPERTY_RESOURCE_TYPE, COMPONENT_RESOURCE_TYPE,
Expand All @@ -135,6 +137,7 @@ void testComponentImage_Decorative() {
}

@Test
@SuppressWarnings("null")
void testComponentImage_Decorative_Policy() {
context.contentPolicyMapping(COMPONENT_RESOURCE_TYPE,
PN_MEDIA_IS_DECORATIVE_STANDARD, true);
Expand Down Expand Up @@ -182,6 +185,7 @@ void testComponentImageFromPage_NoFeatureImage() {
}

@Test
@SuppressWarnings("null")
void testComponentImageFromPage() {
Resource component = context.create().resource(page1, "comp1",
PROPERTY_RESOURCE_TYPE, COMPONENT_RESOURCE_TYPE,
Expand All @@ -205,6 +209,7 @@ void testComponentImageFromPage() {
}

@Test
@SuppressWarnings("null")
void testComponentImageFromPage_FallbackCurrentPage() {
Resource component = context.create().resource(page1, "comp1",
PROPERTY_RESOURCE_TYPE, COMPONENT_RESOURCE_TYPE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import io.wcm.handler.media.Rendition;
import io.wcm.testing.mock.aem.junit5.AemContext;
import io.wcm.wcm.core.components.models.mixin.LinkMixin;
import io.wcm.wcm.core.components.models.mixin.MediaMixin;
Expand Down Expand Up @@ -116,7 +117,8 @@ public static void assertValidMedia(Object object, String mediaUrl) {
MediaMixin mediaMixin = (MediaMixin)object;
assertTrue(mediaMixin.isMediaValid());
assertEquals(mediaUrl, mediaMixin.getMediaURL());
if (mediaMixin.getMediaObject().getRendition().isImage()) {
Rendition rendition = mediaMixin.getMediaObject().getRendition();
if (rendition != null && rendition.isImage()) {
assertNotNull(mediaMixin.getMediaMarkup());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static io.wcm.wcm.core.components.testcontext.AppAemContext.ROOT_LEVEL;

import org.apache.sling.api.resource.Resource;
import org.jetbrains.annotations.Nullable;

import io.wcm.handler.url.spi.UrlHandlerConfig;

Expand All @@ -31,7 +32,7 @@
class UrlHandlerConfigImpl extends UrlHandlerConfig {

@Override
public int getSiteRootLevel(Resource contextResource) {
public int getSiteRootLevel(@Nullable Resource contextResource) {
return ROOT_LEVEL;
}

Expand Down

0 comments on commit 65513f1

Please sign in to comment.