Skip to content

Commit

Permalink
Eliminate SonarQube warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanseifert committed Jan 19, 2024
1 parent 1c67cd8 commit af865e4
Show file tree
Hide file tree
Showing 42 changed files with 163 additions and 99 deletions.
1 change: 1 addition & 0 deletions src/main/java/io/wcm/handler/media/CropDimension.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ public String toString() {
* @return Crop dimension instance
* @throws IllegalArgumentException if crop string syntax is invalid
*/
@SuppressWarnings("java:S1192") // ignore duplicated strings
public static @NotNull CropDimension fromCropString(@NotNull String cropString) {
if (StringUtils.isEmpty(cropString)) {
throw new IllegalArgumentException("Invalid crop string: '" + cropString + "'.");
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/wcm/handler/media/MediaArgs.java
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,7 @@ public boolean equals(Object obj) {
}

@Override
@SuppressWarnings("java:S3776") // ignore complexity
public String toString() {
ToStringBuilder sb = new ToStringBuilder(this, ToStringStyle.NO_CLASS_NAME_STYLE);
if (mediaFormatOptions != null && mediaFormatOptions.length > 0) {
Expand Down Expand Up @@ -851,10 +852,9 @@ public String toString() {
* Custom clone-method for {@link MediaArgs}
* @return the cloned {@link MediaArgs}
*/
// CHECKSTYLE:OFF
@Override
@SuppressWarnings({ "java:S2975", "java:S1182", "checkstyle:SuperCloneCheck" }) // ignore clone warnings
public MediaArgs clone() { //NOPMD
// CHECKSTYLE:ON
MediaArgs clone = new MediaArgs();

clone.mediaFormatOptions = ArrayUtils.clone(this.mediaFormatOptions);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/wcm/handler/media/format/MediaFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public final class MediaFormat implements Comparable<MediaFormat> {
private String ratioDisplayString;
private String combinedTitle;

//CHECKSTYLE:OFF
@SuppressWarnings({ "java:S107", "checkstyle:ParameterNumberCheck" }) // ignore parameter count
MediaFormat(String name, String label, String description,
long width, long minWidth, long maxWidth, long height, long minHeight, long maxHeight, long minWidthHeight,
double ratio, double ratioWidth, double ratioHeight, long fileSizeMax, String[] extensions,
Expand All @@ -93,7 +93,6 @@ public final class MediaFormat implements Comparable<MediaFormat> {
this.ranking = ranking;
this.properties = properties;
}
//CHECKSTYLE:ON

/**
* @return Media format name
Expand Down Expand Up @@ -479,6 +478,7 @@ public Dimension getMinDimension() {
* @return User-friendly combined title of current media format name and dimension.
*/
@JsonIgnore
@SuppressWarnings({ "java:S3776", "java:S6541" }) // ignore complexity
String getCombinedTitle() {
if (combinedTitle == null) {
StringBuilder sb = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public MediaFormat getMediaFormat(@NotNull String mediaFormatName) {
* @return Matching media formats, sorted by size (biggest first), ranking, name
*/
@Override
@SuppressWarnings({ "java:S3776", "java:S1066" }) //ignore complexity
public @NotNull SortedSet<MediaFormat> getSameBiggerMediaFormats(@NotNull MediaFormat mediaFormatRequested, boolean filterRenditionGroup) {
SortedSet<MediaFormat> matchingFormats = new TreeSet<>(new MediaFormatSizeRankingComparator());

Expand Down Expand Up @@ -163,6 +164,7 @@ public MediaFormat getMediaFormat(@NotNull String mediaFormatName) {
* @return Matching media formats, sorted by size (biggest first), ranking, name
*/
@Override
@SuppressWarnings({ "java:S3776", "java:S1066" }) //ignore complexity
public @NotNull SortedSet<MediaFormat> getSameSmallerMediaFormats(@NotNull MediaFormat mediaFormatRequested, boolean filterRenditionGroup) {
SortedSet<MediaFormat> matchingFormats = new TreeSet<>(new MediaFormatSizeRankingComparator());

Expand Down Expand Up @@ -290,6 +292,7 @@ public MediaFormat detectMediaFormat(@Nullable String extension, long fileSize,
* @return Matching media formats sorted by their ranking or an empty list if no matching format was found
*/
@Override
@SuppressWarnings("java:S3776") //ignore complexity
public @NotNull SortedSet<MediaFormat> detectMediaFormats(@Nullable String extension, long fileSize, long width, long height) {

// sort media formats by ranking
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
*/
package io.wcm.handler.media.impl;

import static io.wcm.handler.media.impl.MediaFileServletConstants.HEADER_CONTENT_DISPOSITION;
import static io.wcm.handler.media.impl.MediaFileServletConstants.SELECTOR_DOWNLOAD;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand Down Expand Up @@ -47,16 +50,6 @@
abstract class AbstractMediaFileServlet extends SlingSafeMethodsServlet {
private static final long serialVersionUID = 1L;

/**
* Selector for forcing a "save-as" dialog in the browser
*/
public static final String SELECTOR_DOWNLOAD = "download_attachment";

/**
* Content disposition header
*/
public static final String HEADER_CONTENT_DISPOSITION = "Content-Disposition";

@Override
@SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE")
protected void doGet(@NotNull SlingHttpServletRequest request, @NotNull SlingHttpServletResponse response) throws ServletException, IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import static com.day.cq.commons.jcr.JcrConstants.JCR_MIMETYPE;
import static com.day.cq.commons.jcr.JcrConstants.JCR_PRIMARYTYPE;
import static com.day.cq.commons.jcr.JcrConstants.NT_FILE;
import static io.wcm.handler.media.impl.AbstractMediaFileServlet.HEADER_CONTENT_DISPOSITION;
import static io.wcm.handler.media.impl.MediaFileServletConstants.HEADER_CONTENT_DISPOSITION;
import static org.apache.sling.api.servlets.HttpConstants.METHOD_GET;

import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
* Optional support for Content-Disposition header ("download_attachment").
*/
@Component(service = Servlet.class, immediate = true, property = {
"sling.servlet.extensions=" + MediaFileServlet.EXTENSION,
"sling.servlet.extensions=" + MediaFileServletConstants.EXTENSION,
"sling.servlet.selectors=" + ImageFileServlet.SELECTOR,
"sling.servlet.resourceTypes=" + JcrConstants.NT_FILE,
"sling.servlet.resourceTypes=" + JcrConstants.NT_RESOURCE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ else if (imageQualityPercentage != null) {
}
}
if (contentDispositionAttachment) {
result.append(".").append(AbstractMediaFileServlet.SELECTOR_DOWNLOAD);
result.append(".").append(MediaFileServletConstants.SELECTOR_DOWNLOAD);
}

return result.toString();
Expand Down
14 changes: 3 additions & 11 deletions src/main/java/io/wcm/handler/media/impl/MediaFileServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,15 @@
* Optional support for Content-Disposition header ("download_attachment").
*/
@Component(service = Servlet.class, immediate = true, property = {
"sling.servlet.extensions=" + MediaFileServlet.EXTENSION,
"sling.servlet.selectors=" + MediaFileServlet.SELECTOR,
"sling.servlet.extensions=" + MediaFileServletConstants.EXTENSION,
"sling.servlet.selectors=" + MediaFileServletConstants.SELECTOR,
"sling.servlet.resourceTypes=" + JcrConstants.NT_FILE,
"sling.servlet.resourceTypes=" + JcrConstants.NT_RESOURCE,
"sling.servlet.methods=" + HttpConstants.METHOD_GET
})
public final class MediaFileServlet extends AbstractMediaFileServlet {
private static final long serialVersionUID = 1L;

/**
* Selector
*/
public static final String SELECTOR = "media_file";

/**
* Extension
*/
public static final String EXTENSION = "file";
// inherits all functionality

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* #%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.media.impl;

/**
* Constants for {@link MediaFileServlet} and {@link ImageFileServlet}.
*/
public final class MediaFileServletConstants {

/**
* Selector for forcing a "save-as" dialog in the browser
*/
public static final String SELECTOR_DOWNLOAD = "download_attachment";

/**
* Content disposition header
*/
public static final String HEADER_CONTENT_DISPOSITION = "Content-Disposition";

/**
* Selector
*/
public static final String SELECTOR = "media_file";

/**
* Extension
*/
public static final String EXTENSION = "file";

private MediaFileServletConstants() {
// constants only
}

}
9 changes: 7 additions & 2 deletions src/main/java/io/wcm/handler/media/impl/MediaHandlerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,13 @@ public final class MediaHandlerImpl implements MediaHandler {
* @param mediaRequest Media request
* @return Media metadata (never null)
*/
@SuppressWarnings({ "null", "unused" })
@SuppressFBWarnings({ "CORRECTNESS", "STYLE" })
@NotNull
@SuppressWarnings({
"null", "unused",
"java:S3776", "java:S6541", // ignore complexity
"java:S112" // allow runtime exception
})
@SuppressFBWarnings({ "CORRECTNESS", "STYLE" })
Media processRequest(@NotNull final MediaRequest mediaRequest) {

// detect media source
Expand Down Expand Up @@ -286,6 +290,7 @@ private boolean resolveDownloadMediaFormats(MediaArgs mediaArgs) {
}

@Override
@SuppressWarnings("java:S112") // allow runtime exception
public Media invalid() {
// build invalid media with first media source
Class<? extends MediaSource> mediaSourceClass = mediaHandlerConfig.getSources().stream().findFirst().orElse(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ && getWcmMode() != WCMMode.DISABLED
&& mediaArgs.isDummyImage();
}

@SuppressWarnings({ "null", "unused" })
@SuppressWarnings({ "null", "unused", "java:S2589" })
@SuppressFBWarnings("STYLE")
@Override
public HtmlElement<?> build(@NotNull Media media) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ private MediaMarkupBuilderUtil() {
* @param request Servlet request
* @param mediaHandlerConfig Media handler config (can be null, but should not be null)
*/
@SuppressWarnings("null")
@SuppressWarnings({
"null",
"java:S3776" // ignore complexity
})
@SuppressFBWarnings("NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE")
public static void addDiffDecoration(@NotNull HtmlElement<?> mediaElement, @NotNull Resource resource,
@NotNull String refProperty, @NotNull SlingHttpServletRequest request, @Nullable MediaHandlerConfig mediaHandlerConfig) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public final HtmlElement<?> build(@NotNull Media media) {
* @param media Media metadata
* @return <code>img</code> element with properties or null if media metadata is invalid
*/
@SuppressWarnings("java:S3776") // ignore complexity
protected @Nullable HtmlElement<?> getPictureElement(@NotNull Media media) {
PictureSource[] pictureSources = media.getMediaRequest().getMediaArgs().getPictureSources();

Expand Down Expand Up @@ -165,6 +166,7 @@ public final HtmlElement<?> build(@NotNull Media media) {
* @param media Media metadata
* @return <code>img</code> element with properties or null if media metadata is invalid
*/
@SuppressWarnings({ "java:S3776", "java:S2589" }) // ignore complexity
protected @Nullable HtmlElement<?> getImageElement(@NotNull Media media) {
Image img = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ else if (media instanceof ResourceMedia) {
return ((ResourceMedia)media).getMetadata();
}
log.warn("Invalid 'media' parameter passed to MediaPlaceholder model. "
+ "Expected: " + Media.class.getName() + ", actual: " + media.getClass().getName());
+ "Expected: {}, actual: {}", Media.class.getName(), media.getClass().getName());
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@ public boolean accepts(@Nullable String mediaRef) {
}

@Override
@SuppressWarnings("null")
@SuppressWarnings({
"null",
"java:S3776" // ignore complexity
})
@SuppressFBWarnings("NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE")
public @NotNull Media resolveMedia(@NotNull Media media) {
String mediaRef = getMediaRef(media.getMediaRequest(), mediaHandlerConfig);
Expand Down Expand Up @@ -180,7 +183,7 @@ else if (StringUtils.isNotEmpty(mediaRef)) {
}

@Override
@SuppressWarnings("null")
@SuppressWarnings({ "null", "java:S2589" })
public void enableMediaDrop(@NotNull HtmlElement element, @NotNull MediaRequest mediaRequest) {
if (wcmMode == WCMMode.DISABLED || wcmMode == null) {
return;
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/io/wcm/handler/mediasource/dam/impl/DamAsset.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,29 @@
*/
public final class DamAsset extends SlingAdaptable implements Asset {

private final com.day.cq.dam.api.Asset damAsset;
private final com.day.cq.dam.api.Asset asset;
private final CropDimension cropDimension;
private final Integer rotation;
private final MediaArgs defaultMediaArgs;
private final DamContext damContext;

/**
* @param media Media metadata
* @param damAsset DAM asset
* @param asset DAM asset
* @param mediaHandlerConfig Media handler config
* @param dynamicMediaSupportService Dynamic media support service
* @param webOptimizedImageDeliveryService Web optimized image delivery service
* @param adaptable Adaptable from current context
*/
public DamAsset(Media media, com.day.cq.dam.api.Asset damAsset, MediaHandlerConfig mediaHandlerConfig,
public DamAsset(Media media, com.day.cq.dam.api.Asset asset, MediaHandlerConfig mediaHandlerConfig,
DynamicMediaSupportService dynamicMediaSupportService,
WebOptimizedImageDeliveryService webOptimizedImageDeliveryService,
Adaptable adaptable) {
this.damAsset = damAsset;
this.cropDimension = rescaleCropDimension(damAsset, media.getCropDimension());
this.asset = asset;
this.cropDimension = rescaleCropDimension(asset, media.getCropDimension());
this.rotation = media.getRotation();
this.defaultMediaArgs = media.getMediaRequest().getMediaArgs();
this.damContext = new DamContext(damAsset, defaultMediaArgs, mediaHandlerConfig,
this.damContext = new DamContext(asset, defaultMediaArgs, mediaHandlerConfig,
dynamicMediaSupportService, webOptimizedImageDeliveryService, adaptable);
}

Expand Down Expand Up @@ -107,7 +107,7 @@ public String getTitle() {
* @return Single value
*/
private @Nullable String getPropertyAwareOfArray(@NotNull String propertyName) {
Object valueObject = damAsset.getMetadataValueFromJcr(propertyName);
Object valueObject = asset.getMetadataValueFromJcr(propertyName);
String value = null;
if (valueObject != null) {
if (valueObject instanceof Object[]) {
Expand Down Expand Up @@ -146,7 +146,7 @@ public String getDescription() {

@Override
public @NotNull ValueMap getProperties() {
return new ValueMapDecorator(damAsset.getMetadata());
return new ValueMapDecorator(asset.getMetadata());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class DamRendition extends SlingAdaptable implements Rendition {
* @param mediaArgs Media args
* @param damContext DAM context objects
*/
@SuppressWarnings("java:S3776") // ignore complexity
DamRendition(CropDimension cropDimension, Integer rotation, MediaArgs mediaArgs, DamContext damContext) {
this.damContext = damContext;
this.mediaArgs = mediaArgs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import io.wcm.handler.media.UriTemplateType;
import io.wcm.handler.media.impl.ImageFileServlet;
import io.wcm.handler.media.impl.ImageFileServletSelector;
import io.wcm.handler.media.impl.MediaFileServlet;
import io.wcm.handler.media.impl.MediaFileServletConstants;
import io.wcm.handler.mediasource.dam.impl.dynamicmedia.DynamicMediaPath;
import io.wcm.handler.mediasource.dam.impl.dynamicmedia.NamedDimension;
import io.wcm.handler.mediasource.dam.impl.dynamicmedia.SmartCrop;
Expand Down Expand Up @@ -99,7 +99,7 @@ private static String buildUriTemplateDam(@NotNull UriTemplateType type, @NotNul
MediaArgs mediaArgs = damContext.getMediaArgs();
String mediaPath = RenditionMetadata.buildMediaPath(rendition.getPath()
+ "." + ImageFileServletSelector.build(DUMMY_WIDTH, DUMMY_HEIGHT, cropDimension, rotation, imageQualityPercentage, false)
+ "." + MediaFileServlet.EXTENSION,
+ "." + MediaFileServletConstants.EXTENSION,
ImageFileServlet.getImageFileName(damContext.getAsset().getName(), mediaArgs.getEnforceOutputFileExtension()));
UrlHandler urlHandler = AdaptTo.notNull(damContext, UrlHandler.class);
String url = urlHandler.get(mediaPath).urlMode(mediaArgs.getUrlMode())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ private boolean isSizeMatchingRequest(MediaArgs mediaArgs, String[] requestedFil
* @param mediaArgs Media args
* @return Rendition or null if none found
*/
@SuppressWarnings("java:S3776") // ignore complexity
private RenditionMetadata getExactMatchRendition(final Set<RenditionMetadata> candidates, MediaArgs mediaArgs) {
MediaFormat[] mediaFormats = mediaArgs.getMediaFormats();
// check for fixed width and/or height request
Expand Down
Loading

0 comments on commit af865e4

Please sign in to comment.