Skip to content

Commit c7e76e0

Browse files
committed
adding debugging information about products RND-367
1 parent 6fe3bce commit c7e76e0

File tree

6 files changed

+404
-11
lines changed

6 files changed

+404
-11
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
3+
This file is part of the iText (R) project.
4+
Copyright (c) 1998-2017 iText Group NV
5+
Authors: Bruno Lowagie, Paulo Soares, et al.
6+
7+
This program is free software; you can redistribute it and/or modify
8+
it under the terms of the GNU Affero General Public License version 3
9+
as published by the Free Software Foundation with the addition of the
10+
following permission added to Section 15 as permitted in Section 7(a):
11+
FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
12+
ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT
13+
OF THIRD PARTY RIGHTS
14+
15+
This program is distributed in the hope that it will be useful, but
16+
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17+
or FITNESS FOR A PARTICULAR PURPOSE.
18+
See the GNU Affero General Public License for more details.
19+
You should have received a copy of the GNU Affero General Public License
20+
along with this program; if not, see http://www.gnu.org/licenses or write to
21+
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22+
Boston, MA, 02110-1301 USA, or download the license from the following URL:
23+
http://itextpdf.com/terms-of-use/
24+
25+
The interactive user interfaces in modified source and object code versions
26+
of this program must display Appropriate Legal Notices, as required under
27+
Section 5 of the GNU Affero General Public License.
28+
29+
In accordance with Section 7(b) of the GNU Affero General Public License,
30+
a covered work must retain the producer line in every PDF that is created
31+
or manipulated using iText.
32+
33+
You can be released from the requirements of the license by purchasing
34+
a commercial license. Buying such a license is mandatory as soon as you
35+
develop commercial activities involving the iText software without
36+
disclosing the source code of your own applications.
37+
These activities include: offering paid services to customers as an ASP,
38+
serving PDFs on the fly in a web application, shipping iText with a closed
39+
source product.
40+
41+
For more information, please contact iText Software Corp. at this
42+
43+
*/
44+
package com.itextpdf.kernel;
45+
46+
/**
47+
* Describes an iText 7 add on. An add on should register itself to a PdfDocument object if it
48+
* wants to be included in the debugging information.
49+
*/
50+
public class ProductInfo {
51+
private String name;
52+
private int major;
53+
private int minor;
54+
private int patch;
55+
private boolean snapshot;
56+
57+
/**
58+
* Instantiates a ProductInfo object.
59+
*
60+
* @param name name of the add on
61+
* @param major major version of the add on
62+
* @param minor minor version of the add on
63+
* @param patch patch number of the add on
64+
* @param snapshot whether the version of this add on is a snapshot build or not
65+
*/
66+
public ProductInfo(String name, int major, int minor, int patch, boolean snapshot) {
67+
this.name = name;
68+
this.major = major;
69+
this.minor = minor;
70+
this.patch = patch;
71+
this.snapshot = snapshot;
72+
}
73+
74+
public String getName() {
75+
return name;
76+
}
77+
78+
public int getMajor() {
79+
return major;
80+
}
81+
82+
public int getMinor() {
83+
return minor;
84+
}
85+
86+
public int getPatch() {
87+
return patch;
88+
}
89+
90+
public boolean isSnapshot() {
91+
return snapshot;
92+
}
93+
94+
@Override
95+
public String toString() {
96+
return name + "-" + major + "." + minor + "." + patch + ( snapshot ? "-SNAPSHOT" : "" );
97+
}
98+
99+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
3+
This file is part of the iText (R) project.
4+
Copyright (c) 1998-2017 iText Group NV
5+
Authors: Bruno Lowagie, Paulo Soares, et al.
6+
7+
This program is free software; you can redistribute it and/or modify
8+
it under the terms of the GNU Affero General Public License version 3
9+
as published by the Free Software Foundation with the addition of the
10+
following permission added to Section 15 as permitted in Section 7(a):
11+
FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
12+
ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT
13+
OF THIRD PARTY RIGHTS
14+
15+
This program is distributed in the hope that it will be useful, but
16+
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17+
or FITNESS FOR A PARTICULAR PURPOSE.
18+
See the GNU Affero General Public License for more details.
19+
You should have received a copy of the GNU Affero General Public License
20+
along with this program; if not, see http://www.gnu.org/licenses or write to
21+
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22+
Boston, MA, 02110-1301 USA, or download the license from the following URL:
23+
http://itextpdf.com/terms-of-use/
24+
25+
The interactive user interfaces in modified source and object code versions
26+
of this program must display Appropriate Legal Notices, as required under
27+
Section 5 of the GNU Affero General Public License.
28+
29+
In accordance with Section 7(b) of the GNU Affero General Public License,
30+
a covered work must retain the producer line in every PDF that is created
31+
or manipulated using iText.
32+
33+
You can be released from the requirements of the license by purchasing
34+
a commercial license. Buying such a license is mandatory as soon as you
35+
develop commercial activities involving the iText software without
36+
disclosing the source code of your own applications.
37+
These activities include: offering paid services to customers as an ASP,
38+
serving PDFs on the fly in a web application, shipping iText with a closed
39+
source product.
40+
41+
For more information, please contact iText Software Corp. at this
42+
43+
*/
44+
package com.itextpdf.kernel.pdf;
45+
46+
import com.itextpdf.kernel.ProductInfo;
47+
48+
import java.util.Collection;
49+
import java.util.HashSet;
50+
import java.util.Set;
51+
52+
/**
53+
* Data container for debugging information. This class keeps a record of every registered product that
54+
* was involved in the creation of a certain PDF file. This information can then be used to log to the
55+
* logger or to the file.
56+
*/
57+
public class FingerPrint {
58+
59+
private Set<ProductInfo> productInfoSet;
60+
61+
/**
62+
* Default constructor. Initializes the productInfoSet.
63+
*/
64+
public FingerPrint() {
65+
this.productInfoSet = new HashSet<>();
66+
}
67+
68+
/**
69+
* Registers a product to be added to the fingerprint or other debugging info.
70+
*
71+
* @param productInfo ProductInfo to be added
72+
* @return true if the fingerprint did not already contain the specified element
73+
*/
74+
public boolean registerProduct(final ProductInfo productInfo) {
75+
return this.productInfoSet.add(productInfo);
76+
}
77+
78+
/**
79+
* Returns the registered products.
80+
*
81+
* @return registered products.
82+
*/
83+
public Collection<ProductInfo> getProducts() {
84+
return this.productInfoSet;
85+
}
86+
87+
}

kernel/src/main/java/com/itextpdf/kernel/pdf/PdfDocument.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ This file is part of the iText (R) project.
4848
import com.itextpdf.io.source.ByteUtils;
4949
import com.itextpdf.io.source.RandomAccessFileOrArray;
5050
import com.itextpdf.kernel.PdfException;
51+
import com.itextpdf.kernel.ProductInfo;
5152
import com.itextpdf.kernel.Version;
5253
import com.itextpdf.kernel.crypto.BadPasswordException;
5354
import com.itextpdf.kernel.events.EventDispatcher;
@@ -76,8 +77,6 @@ This file is part of the iText (R) project.
7677
import com.itextpdf.kernel.xmp.XMPMetaFactory;
7778
import com.itextpdf.kernel.xmp.options.PropertyOptions;
7879
import com.itextpdf.kernel.xmp.options.SerializeOptions;
79-
import org.slf4j.Logger;
80-
import org.slf4j.LoggerFactory;
8180

8281
import java.io.Closeable;
8382
import java.io.IOException;
@@ -96,6 +95,9 @@ This file is part of the iText (R) project.
9695
import java.util.Set;
9796
import java.util.concurrent.atomic.AtomicLong;
9897

98+
import org.slf4j.Logger;
99+
import org.slf4j.LoggerFactory;
100+
99101
/**
100102
* Main enter point to work with PDF document.
101103
*/
@@ -174,6 +176,7 @@ public class PdfDocument implements IEventDispatcher, Closeable, Serializable {
174176
* List of indirect objects used in the document.
175177
*/
176178
final PdfXrefTable xref = new PdfXrefTable();
179+
protected FingerPrint fingerPrint;
177180

178181
protected final StampingProperties properties;
179182

@@ -1575,6 +1578,25 @@ public PdfFont addFont(PdfFont font) {
15751578
return font;
15761579
}
15771580

1581+
/**
1582+
* Registers a product for debugging purposes.
1583+
*
1584+
* @param productInfo product to be registered.
1585+
* @return true if the product hadn't been registered before.
1586+
*/
1587+
public boolean registerProduct(final ProductInfo productInfo) {
1588+
return this.fingerPrint.registerProduct(productInfo);
1589+
}
1590+
1591+
/**
1592+
* Returns the object containing the registered products.
1593+
*
1594+
* @return fingerprint object
1595+
*/
1596+
public FingerPrint getFingerPrint() {
1597+
return fingerPrint;
1598+
}
1599+
15781600
/**
15791601
* Gets list of indirect references.
15801602
*
@@ -1642,6 +1664,8 @@ protected void flushObject(PdfObject pdfObject, boolean canBeInObjStm) throws IO
16421664
* or {@code null} otherwise
16431665
*/
16441666
protected void open(PdfVersion newPdfVersion) {
1667+
this.fingerPrint = new FingerPrint();
1668+
16451669
try {
16461670
if (reader != null) {
16471671
reader.pdfDocument = this;

kernel/src/main/java/com/itextpdf/kernel/pdf/PdfXrefTable.java

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,15 @@ This file is part of the iText (R) project.
4545

4646
import com.itextpdf.io.LogMessageConstant;
4747
import com.itextpdf.io.source.ByteUtils;
48+
import com.itextpdf.io.util.MessageFormatUtil;
49+
import com.itextpdf.kernel.ProductInfo;
4850
import com.itextpdf.kernel.Version;
51+
import org.slf4j.Logger;
52+
import org.slf4j.LoggerFactory;
4953

5054
import java.io.IOException;
5155
import java.io.Serializable;
52-
import com.itextpdf.io.util.MessageFormatUtil;
53-
import java.util.ArrayList;
54-
import java.util.List;
55-
import java.util.Map;
56-
import java.util.TreeMap;
57-
import java.util.TreeSet;
58-
import org.slf4j.Logger;
59-
import org.slf4j.LoggerFactory;
56+
import java.util.*;
6057

6158
class PdfXrefTable implements Serializable {
6259

@@ -362,7 +359,7 @@ protected void writeXrefTableAndTrailer(PdfDocument document, PdfObject fileId,
362359
writer.write(document.getTrailer());
363360
writer.write('\n');
364361
}
365-
writeKeyInfo(writer);
362+
writeKeyInfo(document);
366363
writer.writeString("startxref\n").
367364
writeLong(startxref).
368365
writeString("\n%%EOF\n");
@@ -380,6 +377,31 @@ void clear() {
380377
count = 1;
381378
}
382379

380+
/**
381+
* Convenience method to write the fingerprint preceding the trailer.
382+
* The fingerprint contains information on iText products used in the generation or manipulation
383+
* of an outputted PDF file.
384+
*
385+
* @param document pdfDocument to write the fingerprint to
386+
*/
387+
protected static void writeKeyInfo(PdfDocument document) {
388+
PdfWriter writer = document.getWriter();
389+
FingerPrint fingerPrint = document.getFingerPrint();
390+
391+
String platform = "";
392+
Version version = Version.getInstance();
393+
String k = version.getKey();
394+
if (k == null) {
395+
k = "iText";
396+
}
397+
writer.writeString(MessageFormatUtil.format("%{0}-{1}{2}\n", k, version.getRelease(), platform));
398+
399+
for (ProductInfo productInfo : fingerPrint.getProducts() ) {
400+
writer.writeString(MessageFormatUtil.format("%{0}\n", productInfo));
401+
}
402+
}
403+
404+
@Deprecated
383405
protected static void writeKeyInfo(PdfWriter writer) throws IOException {
384406
String platform = "";
385407
Version version = Version.getInstance();

0 commit comments

Comments
 (0)