Skip to content

Commit

Permalink
Silly sonar issues in Aspose, AWS and CMIS (frankframework#6915)
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsm5 authored May 30, 2024
1 parent 441c9ed commit 898e663
Show file tree
Hide file tree
Showing 17 changed files with 72 additions and 246 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public class NetStorageSender extends HttpSenderBase {
public static final String MTIME_PARAM_KEY = "mtime";
public static final String HASHVALUE_PARAM_KEY = "hashValue";

private static final String PATH_SEPARATOR = "/";

private @Getter Action action = null;
public enum Action {
DU, DIR, DELETE, UPLOAD, MKDIR, RMDIR, RENAME, MTIME, DOWNLOAD;
Expand Down Expand Up @@ -144,15 +146,15 @@ public void configure() throws ConfigurationException {
*/
@Override
protected URI getURI(String path) throws URISyntaxException {
if (!path.startsWith("/")) path = "/" + path;
if (!path.startsWith(PATH_SEPARATOR)) path = PATH_SEPARATOR + path;
String url = getUrl() + getCpCode();

if(getRootDir() != null)
url += getRootDir();

url += path;

if(url.endsWith("/")) //The path should never end with a '/'
if(url.endsWith(PATH_SEPARATOR)) //The path should never end with a '/'
url = url.substring(0, url.length() -1);

return new URIBuilder(url).build();
Expand Down Expand Up @@ -187,7 +189,7 @@ public HttpRequestBase getMethod(URI uri, Message message, ParameterValueList pa
}

setMethodType(request.getMethodType()); //For logging purposes
if(log.isDebugEnabled()) log.debug("opening ["+request.getMethodType()+"] connection to ["+uri+"] with action ["+getAction()+"]");
log.debug("opening [{}] connection to [{}] with action [{}]", request::getMethodType, () -> uri, this::getAction);

NetStorageCmsSigner signer = new NetStorageCmsSigner(uri, accessTokenCf, getSignType());
request.sign(signer);
Expand All @@ -197,7 +199,7 @@ public HttpRequestBase getMethod(URI uri, Message message, ParameterValueList pa

@Override
public Message extractResult(HttpResponseHandler responseHandler, PipeLineSession session) throws SenderException, IOException {
int statusCode = responseHandler.getStatusLine().getStatusCode();
final int statusCode = responseHandler.getStatusLine().getStatusCode();

boolean ok = false;
if (StringUtils.isNotEmpty(getResultStatusCodeSessionKey())) {
Expand Down Expand Up @@ -228,9 +230,7 @@ public Message extractResult(HttpResponseHandler responseHandler, PipeLineSessio
statuscode.setValue(statusCode + "");
result.addSubElement(statuscode);

String responseString = getResponseBodyAsString(responseHandler, true);
responseString = XmlUtils.skipDocTypeDeclaration(responseString.trim());
responseString = XmlUtils.skipXmlDeclaration(responseString);
final String responseString = getSanitizedResponseBodyAsString(responseHandler);

if (statusCode == HttpURLConnection.HTTP_OK) {
XmlBuilder message = new XmlBuilder("message");
Expand Down Expand Up @@ -258,21 +258,27 @@ public Message extractResult(HttpResponseHandler responseHandler, PipeLineSessio
message.setValue(responseString);
result.addSubElement(message);

log.warn("Unexpected Response from Server: %d %s\n%s".formatted(
statusCode, responseString, responseHandler.getHeaderFields()));
log.warn("Unexpected Response from Server: {} {}\n{}", () -> statusCode, () -> responseString, responseHandler::getHeaderFields);
}
}

return Message.asMessage(result.toXML());
}

private String getSanitizedResponseBodyAsString(HttpResponseHandler responseHandler) throws IOException {
String responseString = getResponseBodyAsString(responseHandler, true);
responseString = XmlUtils.skipDocTypeDeclaration(responseString.trim());
responseString = XmlUtils.skipXmlDeclaration(responseString);
return responseString;
}

/**
* When an exception occurs and the response cannot be parsed, we do not want to throw a 'missing response' exception.
* Since this method is used when handling exceptions, silently return null, to avoid NPE's and IOExceptions
*/
public String getResponseBodyAsString(HttpResponseHandler responseHandler, boolean throwIOExceptionWhenParsingResponse) throws IOException {
String charset = responseHandler.getCharset();
if (log.isDebugEnabled()) log.debug(getLogPrefix()+"response body uses charset ["+charset+"]");
log.debug("response body uses charset [{}]", charset);

Message response = responseHandler.getResponseMessage();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2019-2023 WeAreFrank!
Copyright 2019 - 2024 WeAreFrank!
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -25,7 +25,6 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.mutable.MutableInt;
import org.frankframework.configuration.ConfigurationException;
import org.frankframework.configuration.ConfigurationWarning;
import org.frankframework.configuration.ConfigurationWarnings;
import org.frankframework.core.PipeLineSession;
import org.frankframework.core.PipeRunException;
Expand Down Expand Up @@ -68,7 +67,6 @@ public class PdfPipe extends FixedForwardPipe {
private @Getter String conversionResultDocumentSessionKey = "documents";
private @Getter String conversionResultFilesSessionKey = "pdfConversionResultFiles";
private @Getter String charset = null;
private AsposeFontManager fontManager;
private @Getter boolean unpackDefaultFonts = false;
private @Getter boolean loadExternalResources = false;

Expand Down Expand Up @@ -122,7 +120,7 @@ public void configure() throws ConfigurationException {
}
}

fontManager = new AsposeFontManager(getFontsDirectory());
AsposeFontManager fontManager = new AsposeFontManager(getFontsDirectory());
try {
fontManager.load(isUnpackDefaultFonts());
} catch (IOException e) {
Expand Down Expand Up @@ -239,12 +237,6 @@ public void setConversionResultFilesSessionKey(String conversionResultFilesSessi
this.conversionResultFilesSessionKey = conversionResultFilesSessionKey;
}

@Deprecated
@ConfigurationWarning("attribute 'fileNameToAttachSessionKey' is replaced with 'filenameToAttachSessionKey'")
public void setFileNameToAttachSessionKey(String filenameToAttachSessionKey) {
this.filenameToAttachSessionKey = filenameToAttachSessionKey;
}

/**
* session key that contains the filename to be attached. Only used when the action is set to 'combine'
* @ff.default defaultFileNameToAttachSessionKey
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.List;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.imageio.ImageIO;
import jakarta.annotation.Nonnull;

import org.apache.commons.io.FileUtils;
import org.frankframework.configuration.ConfigurationException;
Expand All @@ -54,16 +52,17 @@
import org.frankframework.testutil.TestFileUtils;
import org.frankframework.util.LogUtil;
import org.frankframework.util.MessageUtils;
import org.springframework.util.MimeType;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.util.MimeType;

import com.testautomationguru.utility.CompareMode;
import com.testautomationguru.utility.ImageUtil;
import com.testautomationguru.utility.PDFUtil;

import jakarta.annotation.Nonnull;

/**
* Executes defined tests against the PdfPipe to ensure the correct working of this pipe.
*
Expand All @@ -75,8 +74,6 @@ public class PdfPipeTest extends PipeTestBase<PdfPipe> {
private static final String REGEX_TIMESTAMP_IGNORE = "(?<=Timestamp:).*(?=\" n)";
private static final String[] REGEX_IGNORES = {REGEX_PATH_IGNORE, REGEX_TIMESTAMP_IGNORE};

private static final TimeZone TEST_TZ = TimeZone.getTimeZone("Europe/Amsterdam");

private Path pdfOutputLocation;

@Override
Expand Down Expand Up @@ -135,7 +132,7 @@ public void expectSuccessfulConversion(String pipeName, String fileToConvert, St
assertNotNull(expectedFileUrl, "cannot find expected file ["+expectedFile+"]");
File file = new File(expectedFileUrl.toURI());
String expectedFilePath = file.getPath();
log.debug("converted relative path ["+expectedFile+"] to absolute file ["+expectedFilePath+"]");
log.debug("converted relative path [{}] to absolute file [{}]", expectedFile, expectedFilePath);

PDFUtil pdfUtil = createPdfUtil(CompareMode.VISUAL_MODE);
double compare = pdfUtil.compare(convertedFilePath, expectedFilePath);
Expand Down Expand Up @@ -291,7 +288,6 @@ public void dot2Pdf() throws Exception {

@Test
public void emlFromGroupmailbox2Pdf() throws Exception {
// assumeTrue(TestAssertions.isTimeZone(TEST_TZ), "This test only runs for Europe/Amsterdam due to the time being in the output PDF");
expectSuccessfulConversion("EmlFromGroupmailbox", "/PdfPipe/eml-from-groupmailbox.eml", "/PdfPipe/xml-results/eml-from-groupmailbox.xml", "/PdfPipe/results/eml-from-groupmailbox.pdf");
assertTrue(session.containsKey("documents"));
assertTrue(session.containsKey("pdfConversionResultFiles1"));
Expand Down Expand Up @@ -508,7 +504,6 @@ public void mailWithPdfAttachment() throws Exception {

@Test
public void mailWithWordAttachment() throws Exception {
// assumeTrue(TestAssertions.isTimeZone(TEST_TZ), "This test only runs for Europe/Amsterdam due to the time being in the output PDF");
expectSuccessfulConversion("mailWithWordAttachment", "/PdfPipe/MailWithAttachments/mailWithWordAttachment.msg", "/PdfPipe/xml-results/mailWithWordAttachment.xml", "/PdfPipe/results/mailWithWordAttachment.pdf");
assertTrue(session.containsKey("documents"));
assertTrue(session.containsKey("pdfConversionResultFiles1"));
Expand All @@ -518,7 +513,6 @@ public void mailWithWordAttachment() throws Exception {

@Test
public void mailWithAttachmentSaveSeparateFiles() throws Exception {
// assumeTrue(TestAssertions.isTimeZone(TEST_TZ), "This test only runs for Europe/Amsterdam due to the time being in the output PDF");
pipe.setSaveSeparate(true);

expectSuccessfulConversion("mailWithAttachmentSaveSeparateFiles", "/PdfPipe/MailWithAttachments/mailWithWordAttachment.msg", "/PdfPipe/xml-results/mailWithWordAttachmentSaveSeparate.xml", "/PdfPipe/results/mailWithWordAttachment.pdf");
Expand All @@ -532,7 +526,6 @@ public void mailWithAttachmentSaveSeparateFiles() throws Exception {

@Test
public void mailWithAttachmentDifferentSessionKeyNames() throws Exception {
// assumeTrue(TestAssertions.isTimeZone(TEST_TZ), "This test only runs for Europe/Amsterdam due to the time being in the output PDF");
pipe.setSaveSeparate(true);
pipe.setConversionResultDocumentSessionKey("output");
pipe.setConversionResultFilesSessionKey("pdf");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.NotImplementedException;
import org.apache.commons.lang3.StringUtils;
import org.frankframework.aws.AwsUtil;
import org.frankframework.configuration.ConfigurationException;
import org.frankframework.doc.Mandatory;
import org.frankframework.stream.Message;
import org.frankframework.util.CredentialFactory;
import org.frankframework.util.FileUtils;
import org.frankframework.util.StreamUtil;
import org.frankframework.util.StringUtil;

import com.amazonaws.AmazonServiceException;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
Expand All @@ -52,18 +64,6 @@

import jakarta.annotation.Nullable;
import lombok.Getter;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.input.NullInputStream;
import org.apache.commons.lang3.NotImplementedException;
import org.apache.commons.lang3.StringUtils;
import org.frankframework.aws.AwsUtil;
import org.frankframework.configuration.ConfigurationException;
import org.frankframework.doc.Mandatory;
import org.frankframework.stream.Message;
import org.frankframework.util.CredentialFactory;
import org.frankframework.util.FileUtils;
import org.frankframework.util.StreamUtil;
import org.frankframework.util.StringUtil;


public class AmazonS3FileSystem extends FileSystemBase<S3Object> implements IWritableFileSystem<S3Object> {
Expand Down Expand Up @@ -388,7 +388,7 @@ public void createFolder(String folder) throws FileSystemException {
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(0);
metadata.setContentType("binary/octet-stream");
InputStream emptyContent = NullInputStream.nullInputStream();
InputStream emptyContent = InputStream.nullInputStream();

PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, folderName, emptyContent, metadata);
s3Client.putObject(putObjectRequest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,7 @@ public ConnectionFactory createConnectionFactory() {
ProviderConfiguration providerConfiguration = new ProviderConfiguration();

SqsClient client = createSqsClient();

SQSConnectionFactory connectionFactory = new SQSConnectionFactory(providerConfiguration, client);

return connectionFactory;
return new SQSConnectionFactory(providerConfiguration, client);
}

// A dirty workaround to create queues, should use JmsMessagingSource#createDestination
Expand Down
96 changes: 2 additions & 94 deletions aws/src/main/java/org/frankframework/senders/AmazonS3Sender.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,110 +15,18 @@
*/
package org.frankframework.senders;

import com.amazonaws.services.s3.model.S3Object;

import org.frankframework.filesystem.AmazonS3FileSystem;
import org.frankframework.filesystem.AmazonS3FileSystemDelegator;

import org.frankframework.filesystem.FileSystemSender;

import com.amazonaws.services.s3.model.S3Object;

/**
* Sender to work with Amazon S3.
*/
public class AmazonS3Sender extends FileSystemSender<S3Object, AmazonS3FileSystem> implements AmazonS3FileSystemDelegator {

// * <p><b>S3 File System specific Actions:</b></p>
// * <p>The <code>createBucket</code> action requires bucket name as input. Bucket region must be specified.</p>
// * <p>The <code>deleteBucket</code> action requires bucket name as input. </p>
// * <p>The <code>copy</code> action requires the destinationFileName parameter to be set which should contain the name of the destination file. Destination bucket name must be specified. </p>
// * <p>The <code>restore</code> action restores an archived copy of an object back into Amazon S3, requires object name as input. Tier must be specified. </p>

// private List<FileSystemAction> specificActions = Arrays.asList(FileSystemAction.CREATEBUCKET,FileSystemAction.DELETEBUCKET,FileSystemAction.RESTORE,FileSystemAction.COPYS3OBJECT);

public AmazonS3Sender() {
setFileSystem(new AmazonS3FileSystem());
// addActions(specificActions);
}

// @Override
// public void configure() throws ConfigurationException {
// super.configure();
// if (getActionEnum()==FileSystemAction.CREATEBUCKET && getFileSystem().isForceGlobalBucketAccessEnabled()
// && (StringUtils.isEmpty(getFileSystem().getBucketRegion())
// || !AmazonS3FileSystem.AVAILABLE_REGIONS.contains(getFileSystem().getBucketRegion()))) {
// throw new ConfigurationException(" invalid bucketRegion [" + getFileSystem().getBucketRegion()
// + "] please use following supported regions " + AmazonS3FileSystem.AVAILABLE_REGIONS.toString());
// }
// if (getActionEnum()==FileSystemAction.COPY) {
// if (StringUtils.isEmpty(getFileSystem().getDestinationBucketName())
// || !BucketNameUtils.isValidV2BucketName(getFileSystem().getDestinationBucketName()))
// throw new ConfigurationException(
// " invalid or empty destinationBucketName [" + getFileSystem().getDestinationBucketName()
// + "] please visit AWS to see correct bucket naming");
// if (getParameterList().findParameter("destinationFileName") == null)
// throw new ConfigurationException(" destinationFileName parameter requires to be present to perform ["
// + getActionEnum() + "] action");
// if (getFileSystem().isStorageClassEnabled() && (StringUtils.isEmpty(getFileSystem().getStorageClass())
// || !AmazonS3FileSystem.STORAGE_CLASSES.contains(getFileSystem().getStorageClass())))
// throw new ConfigurationException(" invalid storage class [" + getFileSystem().getStorageClass()
// + "] please use following supported storage classes "
// + AmazonS3FileSystem.STORAGE_CLASSES.toString());
// }
// else if (getActionEnum()==FileSystemAction.RESTORE && (StringUtils.isEmpty(getFileSystem().getTier())
// || !AmazonS3FileSystem.TIERS.contains(getFileSystem().getTier()))) {
// throw new ConfigurationException(
// " invalid tier when restoring an object from Amazon S3 Glacier, please use one of the following supported tiers: "
// + AmazonS3FileSystem.TIERS.toString());
// }
// }

// @Override
// public PipeRunResult sendMessage(Message message, PipeLineSession session, IForwardTarget next) throws SenderException, TimeOutException {
//
// String result = null;
// String fileName;
// try {
// fileName = message.asString();
// } catch (IOException e) {
// throw new SenderException(e);
// }
//
// ParameterValueList pvl = null;
// if (getParameterList() != null) {
// try {
// pvl = getParameterList().getValues(message, session);
// } catch (ParameterException e) {
// throw new SenderException(getLogPrefix() + "Sender [" + getName() + "] caught exception evaluating parameters", e);
// }
// }

// switch(getActionEnum()) {
// case CREATEBUCKET:
// result = getFileSystem().createBucket(getFileSystem().getBucketName(), getFileSystem().isBucketExistsThrowException());
// break;
// case DELETEBUCKET:
// result = getFileSystem().deleteBucket();
// break;
// case COPY:
// if (pvl.getParameterValue("destinationFileName") != null) {
// if (pvl.getParameterValue("destinationFileName").getValue() != null) {
// String destinationFileName = pvl.getParameterValue("destinationFileName").getValue().toString();
// result = getFileSystem().copyObject(fileName, destinationFileName);
// } else {
// throw new SenderException(getLogPrefix() + " no value in destinationFileName parameter found, please assing value to the parameter to perfom [copy] action");
// }
// } else {
// throw new SenderException(getLogPrefix() + " no destinationFileName parameter found, it must be used to perform [copy] action");
// }
// break;
// case RESTORE:
// result = getFileSystem().restoreObject(fileName);
// break;
// default:
// return super.sendMessage(message, session, next);
// }

// return super.sendMessage(message, session, next);
// }

}
Loading

0 comments on commit 898e663

Please sign in to comment.