Skip to content

Commit 8f034e0

Browse files
committed
chore: sonarcloud fixes
1 parent f319a33 commit 8f034e0

4 files changed

Lines changed: 36 additions & 40 deletions

File tree

openpdf-core/src/main/java/org/openpdf/text/html/HtmlWriter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ public void open() {
329329
super.open();
330330
try {
331331
writeComment(Document.getVersion());
332-
writeComment("CreationDate: " + new Date().toString());
332+
writeComment("CreationDate: " + new Date());
333333
addTabs(1);
334334
writeEnd(HtmlTags.HEAD);
335335
addTabs(1);
@@ -467,7 +467,7 @@ protected void writeJavaScript(Header header) throws IOException {
467467
addTabs(2);
468468
writeStart(HtmlTags.SCRIPT);
469469
write(HtmlTags.LANGUAGE, HtmlTags.JAVASCRIPT);
470-
if (markup.size() > 0) {
470+
if (!markup.isEmpty()) {
471471
/* JavaScript reference example:
472472
*
473473
* <script language="JavaScript" src="/myPath/MyFunctions.js"/>
@@ -629,7 +629,7 @@ protected void write(Element element, int indent) throws IOException {
629629
if (attributes != null && attributes.get(Chunk.NEWPAGE) != null) {
630630
return;
631631
}
632-
boolean tag = isOtherFont(chunk.getFont()) || markup.size() > 0;
632+
boolean tag = isOtherFont(chunk.getFont()) || !markup.isEmpty();
633633
if (tag) {
634634
// start span tag
635635
addTabs(indent);

openpdf-core/src/main/java/org/openpdf/text/pdf/OcspClientBouncyCastle.java

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
import java.math.BigInteger;
109109
import java.net.HttpURLConnection;
110110
import java.net.Proxy;
111+
import java.net.URI;
111112
import java.net.URL;
112113
import java.security.Provider;
113114
import java.security.Security;
@@ -153,6 +154,7 @@ public class OcspClientBouncyCastle implements OcspClient {
153154
* HTTP proxy used to access the OCSP URL
154155
*/
155156
private Proxy proxy;
157+
private static final Random RANDOM_INSTANCE = new Random();
156158

157159
/**
158160
* Creates an instance of an OcspClient that will be using BouncyCastle.
@@ -185,10 +187,6 @@ private static OCSPReq generateOCSPRequest(X509Certificate issuerCert,
185187
Security.addProvider(prov);
186188

187189
// Generate the id for the certificate we are looking for
188-
// OJO... Modificacion de
189-
// Felix--------------------------------------------------
190-
// CertificateID id = new CertificateID(CertificateID.HASH_SHA1, issuerCert,
191-
// serialNumber);
192190
// Example from
193191
// http://grepcode.com/file/repo1.maven.org/maven2/org.bouncycastle/bcmail-jdk16/1.46/org/bouncycastle/cert/ocsp/test/OCSPTest.java
194192
DigestCalculatorProvider digCalcProv = new JcaDigestCalculatorProviderBuilder()
@@ -203,19 +201,10 @@ private static OCSPReq generateOCSPRequest(X509Certificate issuerCert,
203201

204202
gen.addRequest(id);
205203

206-
// create details for nonce extension
207-
// Vector oids = new Vector();
208-
// Vector values = new Vector();
209-
// oids.add(OCSPObjectIdentifiers.id_pkix_ocsp_nonce);
210-
// values.add(new X509Extension(false, new DEROctetString(new
211-
// DEROctetString(PdfEncryption.createDocumentId()).getEncoded())));
212-
// gen.setRequestExtensions(new X509Extensions(oids, values));
213-
214204
// Add nonce extension
215205
ExtensionsGenerator extGen = new ExtensionsGenerator();
216206
byte[] nonce = new byte[16];
217-
Random rand = new Random();
218-
rand.nextBytes(nonce);
207+
RANDOM_INSTANCE.nextBytes(nonce);
219208

220209
extGen.addExtension(OCSPObjectIdentifiers.id_pkix_ocsp_nonce, false,
221210
new DEROctetString(nonce));
@@ -236,7 +225,7 @@ public byte[] getEncoded() {
236225
OCSPReq request = generateOCSPRequest(rootCert,
237226
checkCert.getSerialNumber());
238227
byte[] array = request.getEncoded();
239-
URL urlt = new URL(url);
228+
URL urlt = new URI(url).toURL();
240229
Proxy tmpProxy = proxy == null ? Proxy.NO_PROXY : proxy;
241230
HttpURLConnection con = (HttpURLConnection) urlt.openConnection(tmpProxy);
242231
con.setRequestProperty("Content-Type", "application/ocsp-request");

openpdf-core/src/main/java/org/openpdf/text/pdf/PdfReader.java

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,13 @@ public class PdfReader implements PdfViewerPreferences, Closeable {
9292
static final PdfName[] pageInhCandidates = {PdfName.MEDIABOX,
9393
PdfName.ROTATE, PdfName.RESOURCES, PdfName.CROPBOX};
9494

95-
private static final byte[] endstream = PdfEncodings
96-
.convertToBytes("endstream", null);
95+
private static final String ENDSTREAM = "endstream";
96+
private static final byte[] endstreamBytes = PdfEncodings
97+
.convertToBytes(ENDSTREAM, null);
9798
private static final byte[] endobj = PdfEncodings.convertToBytes("endobj", null);
99+
private static final String FALSE = "false";
100+
private static final String ILLEGAL_LENGTH_VALUE = "illegal.length.value";
101+
public static final String BAD_USER_PASSWORD = "bad.user.password";
98102
private final PdfViewerPreferencesImp viewerPreferences = new PdfViewerPreferencesImp();
99103
protected PRTokeniser tokens;
100104
// Each xref pair is a position
@@ -1482,12 +1486,12 @@ private void readDecryptedDocObj() throws IOException {
14821486
o = enc.get(PdfName.LENGTH);
14831487
if (!o.isNumber()) {
14841488
throw new InvalidPdfException(
1485-
MessageLocalization.getComposedMessage("illegal.length.value"));
1489+
MessageLocalization.getComposedMessage(ILLEGAL_LENGTH_VALUE));
14861490
}
14871491
lengthValue = ((PdfNumber) o).intValue();
14881492
if (lengthValue > 128 || lengthValue < 40 || lengthValue % 8 != 0) {
14891493
throw new InvalidPdfException(
1490-
MessageLocalization.getComposedMessage("illegal.length.value"));
1494+
MessageLocalization.getComposedMessage(ILLEGAL_LENGTH_VALUE));
14911495
}
14921496
cryptoMode = PdfWriter.STANDARD_ENCRYPTION_128;
14931497
break;
@@ -1513,14 +1517,14 @@ private void readDecryptedDocObj() throws IOException {
15131517
.getComposedMessage("no.compatible.encryption.found"));
15141518
}
15151519
PdfObject em = enc.get(PdfName.ENCRYPTMETADATA);
1516-
if (em != null && em.toString().equals("false")) {
1520+
if (em != null && em.toString().equals(FALSE)) {
15171521
cryptoMode |= PdfWriter.DO_NOT_ENCRYPT_METADATA;
15181522
}
15191523
break;
15201524
case 6:
15211525
cryptoMode = PdfWriter.ENCRYPTION_AES_256_V3;
15221526
em = enc.get(PdfName.ENCRYPTMETADATA);
1523-
if (em != null && em.toString().equals("false")) {
1527+
if (em != null && em.toString().equals(FALSE)) {
15241528
cryptoMode |= PdfWriter.DO_NOT_ENCRYPT_METADATA;
15251529
}
15261530
break;
@@ -1548,12 +1552,12 @@ private void readDecryptedDocObj() throws IOException {
15481552
o = enc.get(PdfName.LENGTH);
15491553
if (!o.isNumber()) {
15501554
throw new InvalidPdfException(
1551-
MessageLocalization.getComposedMessage("illegal.length.value"));
1555+
MessageLocalization.getComposedMessage(ILLEGAL_LENGTH_VALUE));
15521556
}
15531557
lengthValue = ((PdfNumber) o).intValue();
15541558
if (lengthValue > 128 || lengthValue < 40 || lengthValue % 8 != 0) {
15551559
throw new InvalidPdfException(
1556-
MessageLocalization.getComposedMessage("illegal.length.value"));
1560+
MessageLocalization.getComposedMessage(ILLEGAL_LENGTH_VALUE));
15571561
}
15581562
cryptoMode = PdfWriter.STANDARD_ENCRYPTION_128;
15591563
recipients = (PdfArray) enc.get(PdfName.RECIPIENTS);
@@ -1582,7 +1586,7 @@ private void readDecryptedDocObj() throws IOException {
15821586
.getComposedMessage("no.compatible.encryption.found"));
15831587
}
15841588
PdfObject em = dic.get(PdfName.ENCRYPTMETADATA);
1585-
if (em != null && em.toString().equals("false")) {
1589+
if (em != null && em.toString().equals(FALSE)) {
15861590
cryptoMode |= PdfWriter.DO_NOT_ENCRYPT_METADATA;
15871591
}
15881592

@@ -1634,7 +1638,7 @@ private void readDecryptedDocObj() throws IOException {
16341638
if (!equalsArray(uValue, decrypt.userKey,
16351639
(rValue == 3 || rValue == 4) ? 16 : 32)) {
16361640
throw new BadPasswordException(
1637-
MessageLocalization.getComposedMessage("bad.user.password"));
1641+
MessageLocalization.getComposedMessage(BAD_USER_PASSWORD));
16381642
}
16391643
} else {
16401644
ownerPasswordUsed = true;
@@ -1684,13 +1688,13 @@ decrypt it (revision 6 and later) - ISO 32000-2 section 7.6.4.3.3 */
16841688
// analog of step c of Algorithm 2.A for user password
16851689
hashAlg2B = decrypt.hashAlg2B(password, Arrays.copyOfRange(uValue, 32, 40), null);
16861690
if (!equalsArray(hashAlg2B, uValue, 32)) {
1687-
throw new BadPasswordException(MessageLocalization.getComposedMessage("bad.user.password"));
1691+
throw new BadPasswordException(MessageLocalization.getComposedMessage(BAD_USER_PASSWORD));
16881692
}
16891693
// step e of Algorithm 2.A
16901694
decrypt.setupByUserPassword(documentID, password, uValue, ueValue, oValue, oeValue, pValue);
16911695
// step f of Algorithm 2.A
16921696
if (!decrypt.decryptAndCheckPerms(permsValue)) {
1693-
throw new BadPasswordException(MessageLocalization.getComposedMessage("bad.user.password"));
1697+
throw new BadPasswordException(MessageLocalization.getComposedMessage(BAD_USER_PASSWORD));
16941698
}
16951699
}
16961700
pValue = decrypt.permissions;
@@ -1981,7 +1985,7 @@ private void checkPRStreamLength(PRStream stream) throws IOException {
19811985
String line = tokens.readString(20);
19821986
if (!line.startsWith("\nendstream")
19831987
&& !line.startsWith("\r\nendstream")
1984-
&& !line.startsWith("\rendstream") && !line.startsWith("endstream")) {
1988+
&& !line.startsWith("\rendstream") && !line.startsWith(ENDSTREAM)) {
19851989
calc = true;
19861990
}
19871991
}
@@ -1996,14 +2000,14 @@ private void checkPRStreamLength(PRStream stream) throws IOException {
19962000
if (!tokens.readLineSegment(tline)) {
19972001
break;
19982002
}
1999-
if (equalsn(tline, endstream)) {
2003+
if (equalsn(tline, endstreamBytes)) {
20002004
streamLength = pos - start;
20012005
break;
20022006
}
20032007
if (equalsn(tline, endobj)) {
20042008
tokens.seek(pos - 16);
20052009
String s = tokens.readString(16);
2006-
int index = s.indexOf("endstream");
2010+
int index = s.indexOf(ENDSTREAM);
20072011
if (index >= 0) {
20082012
pos = pos - 16 + index;
20092013
}
@@ -2523,7 +2527,7 @@ protected PdfObject readPRObject() throws IOException {
25232527
return new PdfBoolean(true);
25242528
} // else
25252529
return PdfBoolean.PDFTRUE;
2526-
} else if ("false".equals(sv)) {
2530+
} else if (FALSE.equals(sv)) {
25272531
if (readDepth == 0) {
25282532
return new PdfBoolean(false);
25292533
} // else
@@ -2985,6 +2989,7 @@ public int shuffleSubsetNames() {
29852989
*
29862990
* @return the subset prefix
29872991
*/
2992+
@SuppressWarnings("java:S2245") // weak random is here ok
29882993
private String createRandomSubsetPrefix() {
29892994
String s = "";
29902995
for (int k = 0; k < 6; ++k) {

pdf-toolbox/src/main/java/org/openpdf/tools/Executable.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
*/
6060
public class Executable {
6161

62+
public static final String OS_NAME = "os.name";
6263
/**
6364
* The path to Acrobat Reader.
6465
*/
@@ -77,7 +78,7 @@ public class Executable {
7778
private static Process action(final String fileName,
7879
String parameters, boolean waitForTermination) throws IOException {
7980
Process process = null;
80-
if (parameters.trim().length() > 0) {
81+
if (!parameters.trim().isEmpty()) {
8182
parameters = " " + parameters.trim();
8283
} else {
8384
parameters = "";
@@ -94,7 +95,7 @@ private static Process action(final String fileName,
9495
"cmd /c start acrord32", parameters, " \"", fileName, "\""));
9596
}
9697
} else if (isMac()) {
97-
if (parameters.trim().length() == 0) {
98+
if (parameters.trim().isEmpty()) {
9899
process = Runtime.getRuntime().exec(
99100
new String[]{"/usr/bin/open", fileName});
100101
} else {
@@ -271,6 +272,7 @@ public static Process printDocumentSilent(File file) throws IOException {
271272
* @param url the URL you want to open in the browser
272273
* @throws IOException on error
273274
*/
275+
@SuppressWarnings("java:S4036") // PATH is under user-control
274276
public static void launchBrowser(String url) throws IOException {
275277
try {
276278
if (isMac()) {
@@ -305,7 +307,7 @@ public static void launchBrowser(String url) throws IOException {
305307
* @return true if the current os is Windows
306308
*/
307309
public static boolean isWindows() {
308-
String os = System.getProperty("os.name").toLowerCase();
310+
String os = System.getProperty(OS_NAME).toLowerCase();
309311
return os.contains("windows") || os.contains("nt");
310312
}
311313

@@ -315,7 +317,7 @@ public static boolean isWindows() {
315317
* @return true if the current os is Windows
316318
*/
317319
public static boolean isWindows9X() {
318-
String os = System.getProperty("os.name").toLowerCase();
320+
String os = System.getProperty(OS_NAME).toLowerCase();
319321
return os.equals("windows 95") || os.equals("windows 98");
320322
}
321323

@@ -325,7 +327,7 @@ public static boolean isWindows9X() {
325327
* @return true if the current os is Apple
326328
*/
327329
public static boolean isMac() {
328-
String os = System.getProperty("os.name").toLowerCase();
330+
String os = System.getProperty(OS_NAME).toLowerCase();
329331
return os.contains("mac");
330332
}
331333

@@ -335,7 +337,7 @@ public static boolean isMac() {
335337
* @return true if the current os is Linux
336338
*/
337339
public static boolean isLinux() {
338-
String os = System.getProperty("os.name").toLowerCase();
340+
String os = System.getProperty(OS_NAME).toLowerCase();
339341
return os.contains("linux");
340342
}
341343
}

0 commit comments

Comments
 (0)