Java tutorial
/* * PDFGalWeb * Copyright (c) 2014, Alejandro Pernas Pan, All rights reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 3.0 of the License, or (at your option) any later version. * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. */ package org.pdfgal.pdfgalweb.services.impl; import java.io.IOException; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang3.StringUtils; import org.apache.pdfbox.exceptions.COSVisitorException; import org.apache.pdfbox.exceptions.CryptographyException; import org.apache.pdfbox.pdmodel.encryption.BadSecurityHandlerException; import org.pdfgal.pdfgal.pdfgal.PDFGal; import org.pdfgal.pdfgalweb.forms.DownloadForm; import org.pdfgal.pdfgalweb.services.UnProtectService; import org.pdfgal.pdfgalweb.utils.FileUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; @Service public class UnProtectServiceImpl implements UnProtectService { @Autowired private FileUtils fileUtils; @Autowired private PDFGal pdfGal; @Override public DownloadForm unProtect(final MultipartFile file, final String password, final HttpServletResponse response) throws COSVisitorException, IOException, BadSecurityHandlerException, CryptographyException { DownloadForm result = new DownloadForm(); if (!file.isEmpty() && StringUtils.isNotBlank(password)) { final String originalName = file.getOriginalFilename(); final String inputUri = this.fileUtils.saveFile(file); final String outputUri = this.fileUtils.getAutogeneratedName(originalName); try { // File is unprotected this.pdfGal.unProtect(inputUri, outputUri, password); } catch (COSVisitorException | IOException | BadSecurityHandlerException | CryptographyException e) { // Temporal files are deleted from system this.fileUtils.delete(inputUri); this.fileUtils.delete(outputUri); throw e; } // Temporal files are deleted from system this.fileUtils.delete(inputUri); result = new DownloadForm(outputUri, originalName); } return result; } }