diff options
author | tknall <tknall@7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c> | 2010-03-16 12:07:29 +0000 |
---|---|---|
committer | tknall <tknall@7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c> | 2010-03-16 12:07:29 +0000 |
commit | 11b5950be66bcc9d6f0bb28d3fc9d211bc70f4d9 (patch) | |
tree | 5a48a33069a318e269245998ecf89b387f331f67 /src/main/java/at/gv | |
parent | da4926845267ca8bedf34917bd3bfb94aeafa153 (diff) | |
download | pdf-as-3-11b5950be66bcc9d6f0bb28d3fc9d211bc70f4d9.tar.gz pdf-as-3-11b5950be66bcc9d6f0bb28d3fc9d211bc70f4d9.tar.bz2 pdf-as-3-11b5950be66bcc9d6f0bb28d3fc9d211bc70f4d9.zip |
Catching OutOfMemory exceptions, returning appropriate error message/code
Binary signature: bug concerning indirect pdf objects fixed
SignaturePositioning improved (Signature position can be declared by String which is parsed)
Some more error codes (Out of memory, Invalid signature position)
iText utility for creation of pdf files added
ConfigUtils updated (destination of configuration to be extracted can now be chosen)
PDFASUtils updated (more tools)
WebApplication: Freetext pdf creation implemented
WebApplication: XSS security updates
git-svn-id: https://joinup.ec.europa.eu/svn/pdf-as/trunk@580 7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c
Diffstat (limited to 'src/main/java/at/gv')
8 files changed, 353 insertions, 81 deletions
diff --git a/src/main/java/at/gv/egiz/pdfas/api/sign/pos/SignaturePositioning.java b/src/main/java/at/gv/egiz/pdfas/api/sign/pos/SignaturePositioning.java index b9e2c8a..42b02eb 100644 --- a/src/main/java/at/gv/egiz/pdfas/api/sign/pos/SignaturePositioning.java +++ b/src/main/java/at/gv/egiz/pdfas/api/sign/pos/SignaturePositioning.java @@ -3,6 +3,8 @@ */
package at.gv.egiz.pdfas.api.sign.pos;
+import java.util.StringTokenizer;
+
import at.gv.egiz.pdfas.api.sign.pos.axis.AbsoluteAxisAlgorithm;
import at.gv.egiz.pdfas.api.sign.pos.axis.AutoAxisAlgorithm;
import at.gv.egiz.pdfas.api.sign.pos.axis.AxisAlgorithm;
@@ -10,6 +12,8 @@ import at.gv.egiz.pdfas.api.sign.pos.page.AbsolutePageAlgorithm; import at.gv.egiz.pdfas.api.sign.pos.page.AutoPageAlgorithm;
import at.gv.egiz.pdfas.api.sign.pos.page.NewPageAlgorithm;
import at.gv.egiz.pdfas.api.sign.pos.page.PageAlgorithm;
+import at.gv.egiz.pdfas.exceptions.ErrorCode;
+import at.knowcenter.wag.egov.egiz.exceptions.PDFDocumentException;
/**
* Defines how the signature positioning is to be performed.
@@ -186,4 +190,80 @@ public class SignaturePositioning this.footerLine = footerLine;
}
+ public SignaturePositioning() {
+ }
+
+ public SignaturePositioning(String position) throws PDFDocumentException {
+ if (position != null) {
+ StringTokenizer tokenizer = new StringTokenizer(position, ";");
+ while (tokenizer.hasMoreTokens()) {
+ String token = tokenizer.nextToken().replaceAll(" ", "");
+ String[] sToken = token.split(":");
+ if (sToken == null || sToken.length != 2 || sToken[0].length() != 1) {
+ throw new PDFDocumentException(ErrorCode.INVALID_SIGNATURE_POSITION, "Invalid signature position element: " + token);
+ }
+ char cmd = sToken[0].toLowerCase().charAt(0);
+ String value = sToken[1];
+ switch (cmd) {
+ case 'x':
+ if ("auto".equalsIgnoreCase(value)) {
+ this.setXAlgorithm(new AutoAxisAlgorithm());
+ } else {
+ try {
+ this.setXAlgorithm(new AbsoluteAxisAlgorithm(Float.parseFloat(value)));
+ } catch (NumberFormatException e) {
+ throw new PDFDocumentException(ErrorCode.INVALID_SIGNATURE_POSITION, "Invalid signature position element: " + token);
+ }
+ }
+ break;
+ case 'y':
+ if ("auto".equalsIgnoreCase(value)) {
+ this.setYAlgorithm(new AutoAxisAlgorithm());
+ } else {
+ try {
+ this.setYAlgorithm(new AbsoluteAxisAlgorithm(Float.parseFloat(value)));
+ } catch (NumberFormatException e) {
+ throw new PDFDocumentException(ErrorCode.INVALID_SIGNATURE_POSITION, "Invalid signature position element: " + token);
+ }
+ }
+ break;
+ case 'w':
+ if ("auto".equalsIgnoreCase(value)) {
+ this.setWidthAlgorithm(new AutoAxisAlgorithm());
+ } else {
+ try {
+ this.setWidthAlgorithm(new AbsoluteAxisAlgorithm(Float.parseFloat(value)));
+ } catch (NumberFormatException e) {
+ throw new PDFDocumentException(ErrorCode.INVALID_SIGNATURE_POSITION, "Invalid signature position element: " + token);
+ }
+ }
+ break;
+ case 'p':
+ if ("auto".equalsIgnoreCase(value)) {
+ this.setPageAlgorithm(new AutoPageAlgorithm());
+ } else if ("new".equalsIgnoreCase(value)) {
+ this.setPageAlgorithm(new NewPageAlgorithm());
+ } else {
+ try {
+ this.setPageAlgorithm(new AbsolutePageAlgorithm(Integer.parseInt(value)));
+ } catch (NumberFormatException e) {
+ throw new PDFDocumentException(ErrorCode.INVALID_SIGNATURE_POSITION, "Invalid signature position element: " + token);
+ }
+ }
+ break;
+ case 'f':
+ try {
+ this.setFooterLine(Float.parseFloat(value));
+ } catch (NumberFormatException e) {
+ throw new PDFDocumentException(ErrorCode.INVALID_SIGNATURE_POSITION, "Invalid signature position element: " + token);
+ }
+ break;
+ default:
+ throw new PDFDocumentException(ErrorCode.INVALID_SIGNATURE_POSITION, "Invalid signature position element: " + token);
+ }
+ }
+ }
+ }
+
+
}
diff --git a/src/main/java/at/gv/egiz/pdfas/exceptions/ErrorCode.java b/src/main/java/at/gv/egiz/pdfas/exceptions/ErrorCode.java index c53151f..9c2e440 100644 --- a/src/main/java/at/gv/egiz/pdfas/exceptions/ErrorCode.java +++ b/src/main/java/at/gv/egiz/pdfas/exceptions/ErrorCode.java @@ -16,6 +16,7 @@ public final class ErrorCode {
public static final int EXTERNAL_ERROR = 0;
public static final int UNKNOWN_ERROR = 6;
+ public static final int OUT_OF_MEMORY_ERROR = 7;
public static final int SETTING_NOT_FOUND = 100;
public static final int SETTINGS_EXCEPTION = 101;
@@ -31,6 +32,8 @@ public final class ErrorCode public static final int FONT_NOT_FOUND = 230;
public static final int DOCUMENT_IS_PROTECTED = 231;
+ public static final int INVALID_SIGNATURE_POSITION = 224;
+
public static final int SIGNATURE_COULDNT_BE_CREATED = 300;
public static final int SIGNED_TEXT_EMPTY = 301;
public static final int PROFILE_NOT_DEFINED = 302;
diff --git a/src/main/java/at/gv/egiz/pdfas/impl/api/PdfAsObject.java b/src/main/java/at/gv/egiz/pdfas/impl/api/PdfAsObject.java index 824416f..2ccc1b7 100644 --- a/src/main/java/at/gv/egiz/pdfas/impl/api/PdfAsObject.java +++ b/src/main/java/at/gv/egiz/pdfas/impl/api/PdfAsObject.java @@ -50,6 +50,7 @@ import at.gv.egiz.pdfas.utils.ConfigUtils; import at.knowcenter.wag.egov.egiz.PdfAS;
import at.knowcenter.wag.egov.egiz.PdfASID;
import at.knowcenter.wag.egov.egiz.cfg.SettingsReader;
+import at.knowcenter.wag.egov.egiz.exceptions.OutOfMemoryException;
import at.knowcenter.wag.egov.egiz.exceptions.PDFDocumentException;
import at.knowcenter.wag.egov.egiz.exceptions.PresentableException;
import at.knowcenter.wag.egov.egiz.exceptions.SignatureException;
@@ -240,20 +241,24 @@ public class PdfAsObject implements PdfAs TablePos pos = PosHelper.formTablePos(signParameters.getSignaturePositioning());
String connectorId = CommandlineConnectorChooser.chooseCommandlineConnectorForSign(signParameters.getSignatureDevice());
-
- SignatorInformation si = PdfAS.signCommandline(
- new PdfDataSourceAdapter(signParameters.getDocument()),
- new DataSinkAdapter(signParameters.getOutput()),
- signatorId,
- connectorId,
- signParameters.getSignatureProfileId(),
- signParameters.getSignatureKeyIdentifier(),
- pos,
- signParameters.getTimeStamperImpl()
- );
-
- return new SignResultImpl(signParameters.getOutput(), si.getSignSignatureObject().getX509Certificate(),
- new ActualSignaturePositionAdapter(si.getActualTablePos()), si.getNonTextualObjects());
+
+ try {
+ SignatorInformation si = PdfAS.signCommandline(
+ new PdfDataSourceAdapter(signParameters.getDocument()),
+ new DataSinkAdapter(signParameters.getOutput()),
+ signatorId,
+ connectorId,
+ signParameters.getSignatureProfileId(),
+ signParameters.getSignatureKeyIdentifier(),
+ pos,
+ signParameters.getTimeStamperImpl()
+ );
+ return new SignResultImpl(signParameters.getOutput(), si.getSignSignatureObject().getX509Certificate(),
+ new ActualSignaturePositionAdapter(si.getActualTablePos()), si.getNonTextualObjects());
+ } catch (java.lang.OutOfMemoryError e) {
+ throw new OutOfMemoryException(ErrorCode.OUT_OF_MEMORY_ERROR, "Insufficient memory allocated to virtual machine. Start Java with parameters \"-Xms128m -Xmx786m -XX:MaxPermSize=256m\".", e);
+ }
+
}
/**
@@ -364,31 +369,35 @@ public class PdfAsObject implements PdfAs }
assert inputDataSource != null;
- ExtractionStage es = new ExtractionStage();
- List signature_holders = es.extractSignatureHolders(inputDataSource, parameters);
-
-// List sigInfs = new ArrayList(signature_holders.size());
- List sigInfs = new ArrayList();
- List noSigs = new ArrayList();
- Iterator it = signature_holders.iterator();
- while (it.hasNext())
- {
- SignatureHolder sh = (SignatureHolder)it.next();
-
- if(sh instanceof NoSignatureHolder) {
- noSigs.add(sh);
- } else {
-
- SignatureInformation si = new SignatureInformationAdapter(sh);
- sigInfs.add(si);
- if (analyzeParameters.isReturnNonTextualObjects()) {
- si.setNonTextualObjects(doExtractNonTexualObjects(sh, (PdfDataSource) inputDataSource));
- }
-
- }
+ try {
+ ExtractionStage es = new ExtractionStage();
+ List signature_holders = es.extractSignatureHolders(inputDataSource, parameters);
+
+ // List sigInfs = new ArrayList(signature_holders.size());
+ List sigInfs = new ArrayList();
+ List noSigs = new ArrayList();
+ Iterator it = signature_holders.iterator();
+ while (it.hasNext())
+ {
+ SignatureHolder sh = (SignatureHolder)it.next();
+
+ if(sh instanceof NoSignatureHolder) {
+ noSigs.add(sh);
+ } else {
+
+ SignatureInformation si = new SignatureInformationAdapter(sh);
+ sigInfs.add(si);
+ if (analyzeParameters.isReturnNonTextualObjects()) {
+ si.setNonTextualObjects(doExtractNonTexualObjects(sh, (PdfDataSource) inputDataSource));
+ }
+
+ }
+ }
+ return new AnalyzeResultImpl(sigInfs, noSigs);
+ } catch (java.lang.OutOfMemoryError e) {
+ throw new OutOfMemoryException(ErrorCode.OUT_OF_MEMORY_ERROR, "Insufficient memory allocated to virtual machine. Start Java with parameters \"-Xms128m -Xmx786m -XX:MaxPermSize=256m\".", e);
}
- return new AnalyzeResultImpl(sigInfs, noSigs);
}
private List doExtractNonTexualObjects(SignatureHolder sh, PdfDataSource pdfDataSource) {
@@ -433,25 +442,30 @@ public class PdfAsObject implements PdfAs }
assert signature_holders.size() == signatures.size();
- List results = PdfAS.verifySignatureHolders(signature_holders, verifyAfterAnalysisParameters.getSignatureDevice(), verifyAfterAnalysisParameters.isReturnHashInputData(), verifyAfterAnalysisParameters.getVerificationTime());
-
- List vrs = new ArrayList(results.size());
-
- assert signature_holders.size() == results.size() : "Not all signatures were verified.";
-
- for (int i = 0; i < signature_holders.size(); i++)
- {
- SignatureResponse response = (SignatureResponse) results.get(i);
- SignatureHolder holder = (SignatureHolder) signature_holders.get(i);
-
- VerifyResult vr = new VerifyResultAdapter(response, holder, verifyAfterAnalysisParameters.getVerificationTime());
- vr.setNonTextualObjects( ((SignatureInformation) signatures.get(i)).getNonTextualObjects());
-
- vrs.add(vr);
+ try {
+ List results = PdfAS.verifySignatureHolders(signature_holders, verifyAfterAnalysisParameters.getSignatureDevice(), verifyAfterAnalysisParameters.isReturnHashInputData(), verifyAfterAnalysisParameters.getVerificationTime());
+
+ List vrs = new ArrayList(results.size());
+
+ assert signature_holders.size() == results.size() : "Not all signatures were verified.";
+
+ for (int i = 0; i < signature_holders.size(); i++)
+ {
+ SignatureResponse response = (SignatureResponse) results.get(i);
+ SignatureHolder holder = (SignatureHolder) signature_holders.get(i);
+
+ VerifyResult vr = new VerifyResultAdapter(response, holder, verifyAfterAnalysisParameters.getVerificationTime());
+ vr.setNonTextualObjects( ((SignatureInformation) signatures.get(i)).getNonTextualObjects());
+
+ vrs.add(vr);
+ }
+
+ VerifyResultsImpl verifyResults = new VerifyResultsImpl(vrs);
+ return verifyResults;
+ } catch (java.lang.OutOfMemoryError e) {
+ throw new OutOfMemoryException(ErrorCode.OUT_OF_MEMORY_ERROR, "Insufficient memory allocated to virtual machine. Start Java with parameters \"-Xms128m -Xmx786m -XX:MaxPermSize=256m\".", e);
}
-
- VerifyResultsImpl verifyResults = new VerifyResultsImpl(vrs);
- return verifyResults;
+
}
}
diff --git a/src/main/java/at/gv/egiz/pdfas/impl/signator/binary/BinarySignator_1_0_0.java b/src/main/java/at/gv/egiz/pdfas/impl/signator/binary/BinarySignator_1_0_0.java index 36d3764..bdb5fdb 100644 --- a/src/main/java/at/gv/egiz/pdfas/impl/signator/binary/BinarySignator_1_0_0.java +++ b/src/main/java/at/gv/egiz/pdfas/impl/signator/binary/BinarySignator_1_0_0.java @@ -17,6 +17,7 @@ */
package at.gv.egiz.pdfas.impl.signator.binary;
+import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -26,6 +27,7 @@ import java.util.HashMap; import java.util.Iterator;
import java.util.List;
+import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -40,6 +42,7 @@ import at.gv.egiz.pdfas.framework.signator.SignatorInformation; import at.gv.egiz.pdfas.impl.input.CompoundPdfDataSourceImpl;
import at.gv.egiz.pdfas.impl.signator.IncrementalUpdateHelper;
import at.gv.egiz.pdfas.utils.OgnlUtil;
+import at.gv.egiz.pdfas.utils.PDFASUtils;
import at.knowcenter.wag.egov.egiz.PdfAS;
import at.knowcenter.wag.egov.egiz.PdfASID;
import at.knowcenter.wag.egov.egiz.cfg.SettingsReader;
@@ -199,7 +202,7 @@ public class BinarySignator_1_0_0 implements Signator iui.invisibleKZString = invKZString;
String temp_string = iui.temp_ir_number + " " + iui.temp_ir_generation + " obj"; //$NON-NLS-1$//$NON-NLS-2$
- byte[] temp_bytes = temp_string.getBytes("US-ASCII"); //$NON-NLS-1$
+ byte[] temp_bytes = ArrayUtils.add(temp_string.getBytes("US-ASCII"), 0, (byte) 0x0A);
int temp_start = ByteArrayUtils.lastIndexOf(iui.signed_pdf, temp_bytes);
byte[] stream_bytes = new byte[] { '>', '>', 's', 't', 'r', 'e', 'a', 'm', 0x0A };
int stream_start = ByteArrayUtils.indexOf(iui.signed_pdf, temp_start, stream_bytes);
diff --git a/src/main/java/at/gv/egiz/pdfas/itext/IText.java b/src/main/java/at/gv/egiz/pdfas/itext/IText.java new file mode 100644 index 0000000..767dbbc --- /dev/null +++ b/src/main/java/at/gv/egiz/pdfas/itext/IText.java @@ -0,0 +1,111 @@ +package at.gv.egiz.pdfas.itext;
+
+import java.awt.color.ICC_Profile;
+import java.io.IOException;
+import java.io.OutputStream;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.io.output.ByteArrayOutputStream;
+import org.apache.log4j.Logger;
+
+import com.lowagie.text.Document;
+import com.lowagie.text.DocumentException;
+import com.lowagie.text.Font;
+import com.lowagie.text.PageSize;
+import com.lowagie.text.Paragraph;
+import com.lowagie.text.Rectangle;
+import com.lowagie.text.pdf.BaseFont;
+import com.lowagie.text.pdf.PdfArray;
+import com.lowagie.text.pdf.PdfDictionary;
+import com.lowagie.text.pdf.PdfICCBased;
+import com.lowagie.text.pdf.PdfName;
+import com.lowagie.text.pdf.PdfString;
+import com.lowagie.text.pdf.PdfWriter;
+
+public final class IText {
+
+ private IText() {
+ }
+
+ private static final Logger LOG = Logger.getLogger(IText.class);
+
+ private static final Font DEFAULT_FONT = new Font(Font.HELVETICA, 12, Font.NORMAL);
+ private static final String DEFAULT_TTF_FONT_RESOURCE = "DejaVuSansCondensed.ttf";
+ private static final String DEFAULT_ICC_PROFILE_RESOURCE = "srgb.profile";
+ private static final Rectangle DEFAULT_PAGE_SIZE = PageSize.A4;
+ private static final int[] DEFAULT_MARGINS_MM = { 25, 25, 25, 25 }; // top, right, bottom, left
+ public static final String DEFAULT_FILENAME = "text";
+
+ private static Font DEFAULT_TTF_FONT;
+ private static PdfICCBased DEFAULT_ICC_COLOR_SPACE;
+
+ static {
+ try {
+ byte[] ttfFontData = IOUtils.toByteArray(IText.class.getResourceAsStream(DEFAULT_TTF_FONT_RESOURCE));
+ DEFAULT_TTF_FONT = new Font(BaseFont.createFont(DEFAULT_TTF_FONT_RESOURCE, BaseFont.WINANSI, true, true, ttfFontData, null));
+ } catch (Exception e) {
+ LOG.error(e);
+ DEFAULT_TTF_FONT = DEFAULT_FONT;
+ }
+ try {
+ byte[] iccData = IOUtils.toByteArray(IText.class.getResourceAsStream(DEFAULT_ICC_PROFILE_RESOURCE));
+ DEFAULT_ICC_COLOR_SPACE = new PdfICCBased(ICC_Profile.getInstance(iccData));
+ DEFAULT_ICC_COLOR_SPACE.remove(PdfName.ALTERNATE);
+ } catch (Exception e) {
+ LOG.error(e);
+ DEFAULT_ICC_COLOR_SPACE = null;
+ }
+ }
+
+ public static byte[] createPDF(String content, boolean pdfa) throws DocumentException, IOException {
+ ByteArrayOutputStream baOut = new ByteArrayOutputStream();
+ createPDF(content, pdfa, baOut);
+ return baOut.toByteArray();
+ }
+
+ public static byte[] createPDF(String content) throws DocumentException, IOException {
+ return createPDF(content, false);
+ }
+
+
+ private static void addContent(Document document, boolean pdfa, String content) throws DocumentException {
+ Paragraph p = new Paragraph(content, pdfa ? DEFAULT_TTF_FONT : DEFAULT_FONT);
+ document.add(p);
+ }
+ public static void createPDF(String content, OutputStream outputStream) throws DocumentException, IOException {
+ createPDF(content, false, outputStream);
+ }
+
+ public static void createPDF(String content, boolean pdfa, OutputStream outputStream) throws DocumentException, IOException {
+ final float factor = (float) 72 / (float) 25.4;
+ Document document = new Document(
+ DEFAULT_PAGE_SIZE,
+ (float) DEFAULT_MARGINS_MM[3] * factor, // left
+ (float) DEFAULT_MARGINS_MM[1] * factor, // right
+ (float) DEFAULT_MARGINS_MM[0] * factor, // top
+ (float) DEFAULT_MARGINS_MM[2] * factor // bottom
+ );
+
+ PdfWriter pdfWriter = PdfWriter.getInstance(document, outputStream);
+ if (pdfa) {
+ pdfWriter.setPDFXConformance(PdfWriter.PDFA1B);
+ }
+ document.open();
+ if (pdfa) {
+ PdfDictionary pdfOutputIntent = new PdfDictionary(PdfName.OUTPUTINTENT);
+ pdfOutputIntent.put(PdfName.OUTPUTCONDITIONIDENTIFIER, new PdfString("sRGB IEC61966-2.1"));
+ pdfOutputIntent.put(PdfName.INFO, new PdfString("sRGB IEC61966-2.1"));
+ pdfOutputIntent.put(PdfName.S, PdfName.GTS_PDFA1);
+ if (DEFAULT_ICC_COLOR_SPACE != null) {
+ pdfOutputIntent.put(PdfName.DESTOUTPUTPROFILE, pdfWriter.addToBody(DEFAULT_ICC_COLOR_SPACE).getIndirectReference());
+ }
+ pdfWriter.getExtraCatalog().put(PdfName.OUTPUTINTENTS, new PdfArray(pdfOutputIntent));
+ }
+ addContent(document, pdfa, content);
+ if (pdfa) {
+ pdfWriter.createXmpMetadata();
+ }
+ document.close();
+ }
+
+}
diff --git a/src/main/java/at/gv/egiz/pdfas/test/CreateFreeTextTest.java b/src/main/java/at/gv/egiz/pdfas/test/CreateFreeTextTest.java new file mode 100644 index 0000000..7665f33 --- /dev/null +++ b/src/main/java/at/gv/egiz/pdfas/test/CreateFreeTextTest.java @@ -0,0 +1,26 @@ +package at.gv.egiz.pdfas.test;
+
+import java.io.File;
+
+import at.gv.egiz.pdfas.itext.IText;
+import at.gv.egiz.pdfas.utils.PDFASUtils;
+
+public class CreateFreeTextTest {
+
+ private CreateFreeTextTest() {
+ }
+
+ public static void main(String[] args) {
+
+ try {
+
+ byte[] pdf = IText.createPDF("Hello World", false);
+ PDFASUtils.toFile(pdf, new File("itext.pdf"));
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ }
+
+}
diff --git a/src/main/java/at/gv/egiz/pdfas/utils/ConfigUtils.java b/src/main/java/at/gv/egiz/pdfas/utils/ConfigUtils.java index 88e597b..1862f20 100644 --- a/src/main/java/at/gv/egiz/pdfas/utils/ConfigUtils.java +++ b/src/main/java/at/gv/egiz/pdfas/utils/ConfigUtils.java @@ -1,8 +1,6 @@ package at.gv.egiz.pdfas.utils;
-import java.io.BufferedOutputStream;
import java.io.File;
-import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -81,11 +79,37 @@ public final class ConfigUtils { if (in == null) {
throw new ConfigUtilsException("Unable to find default configuration resource \"" + Constants.DEFAULT_CONFIGURATION_ZIP_RESOURCE + "\".");
}
- ZipInputStream zis = new ZipInputStream(in);
+ return deployFromZIP(in, destination, overwriteExisting);
+ } catch (IOException e) {
+ throw new ConfigUtilsException(e);
+ }
+ }
+
+ /**
+ * Deploys the contents of a ZIP file to a certain location.
+ *
+ * @param inputStream The inputStream of a ZIP container.
+ * @param destination The destination folder.
+ * @param overwriteExisting If set <code>true</code> an already existing configuration is overwritten. If <code>false</code> nothing is being copied if the destination folder already exists.
+ * @return <code>true</code> if the configuration has been deployed, <code>false</code> if not.
+ * @throws ConfigUtilsException Thrown if there was an error during the deployment of the configuration.
+ */
+ public static boolean deployFromZIP(InputStream inputStream, String destination, boolean overwriteExisting) throws ConfigUtilsException {
+ try {
+ if (!overwriteExisting) {
+ if (configurationAlreadyExists(destination)) {
+ logger_.debug("There is at least one file or folder that would be overwritten at destination path \"" + destination + "\". Skipping extraction.");
+ return false;
+ }
+ }
+ if (inputStream == null) {
+ throw new ConfigUtilsException("Unable to deploy ZIP file. InputStream is null.");
+ }
+ ZipInputStream zis = new ZipInputStream(inputStream);
ZipEntry ze;
File destinationFolder = new File(destination);
destinationFolder.mkdirs();
- logger_.debug("Extracting default configuration to folder \"" + destinationFolder.getCanonicalPath() + "\".");
+ logger_.debug("Extracting ZIP contents to folder \"" + destinationFolder.getCanonicalPath() + "\".");
while ((ze = zis.getNextEntry()) != null) {
if (ze.isDirectory()) {
File newFolder = new File(destinationFolder, ze.getName());
@@ -94,7 +118,7 @@ public final class ConfigUtils { } else {
File destFile = new File(destinationFolder, ze.getName());
logger_.trace("Extracting file \"" + destFile.getName() + "\".");
- toFile(zis, destFile);
+ PDFASUtils.toFile(zis, destFile);
}
zis.closeEntry();
}
@@ -104,7 +128,7 @@ public final class ConfigUtils { throw new ConfigUtilsException(e);
}
}
-
+
private static boolean configurationAlreadyExists(String destination) throws ConfigUtilsException, IOException {
logger_.debug("Checking configuration \"" + destination + "\" already exists (resp. if there are any directories or files that would be overwritten).");
File destinationFolder = new File(destination);
@@ -196,25 +220,6 @@ public final class ConfigUtils { return deployDefaultConfiguration(false);
}
- public static boolean toFile(InputStream inputStream, File file) throws IOException {
- boolean result = false;
- BufferedOutputStream bufferedOutputStream = null;
- try {
- bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
- writeInputStreamToOutputStream(inputStream, bufferedOutputStream);
- } finally {
- if (bufferedOutputStream != null) {
- try {
- bufferedOutputStream.close();
- result = true;
- } catch (IOException e) {
- result = false;
- }
- }
- }
- return result;
- }
-
public static void writeInputStreamToOutputStream(InputStream inputStream, OutputStream outputStream) throws IOException {
final int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
diff --git a/src/main/java/at/gv/egiz/pdfas/utils/PDFASUtils.java b/src/main/java/at/gv/egiz/pdfas/utils/PDFASUtils.java index b26cc9b..9841779 100644 --- a/src/main/java/at/gv/egiz/pdfas/utils/PDFASUtils.java +++ b/src/main/java/at/gv/egiz/pdfas/utils/PDFASUtils.java @@ -1,5 +1,12 @@ package at.gv.egiz.pdfas.utils;
+import java.io.BufferedOutputStream;
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
import at.gv.egiz.pdfas.exceptions.ErrorCode;
import at.knowcenter.wag.egov.egiz.exceptions.PDFDocumentException;
@@ -26,5 +33,28 @@ public class PDFASUtils { throw new PDFDocumentException(ErrorCode.DOCUMENT_IS_PROTECTED, "Document is protected.");
}
}
+
+ public static boolean toFile(byte[] data, File file) throws IOException {
+ return PDFASUtils.toFile(new ByteArrayInputStream(data), file);
+ }
+
+ public static boolean toFile(InputStream inputStream, File file) throws IOException {
+ boolean result = false;
+ BufferedOutputStream bufferedOutputStream = null;
+ try {
+ bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
+ ConfigUtils.writeInputStreamToOutputStream(inputStream, bufferedOutputStream);
+ } finally {
+ if (bufferedOutputStream != null) {
+ try {
+ bufferedOutputStream.close();
+ result = true;
+ } catch (IOException e) {
+ result = false;
+ }
+ }
+ }
+ return result;
+ }
}
|