/*******************************************************************************
* Copyright 2014 by E-Government Innovation Center EGIZ, Graz, Austria
* PDF-AS has been contracted by the E-Government Innovation Center EGIZ, a
* joint initiative of the Federal Chancellery Austria and Graz University of
* Technology.
*
* Licensed under the EUPL, Version 1.1 or - as soon they will be approved by
* the European Commission - subsequent versions of the EUPL (the "Licence");
* You may not use this work except in compliance with the Licence.
* You may obtain a copy of the Licence at:
* http://www.osor.eu/eupl/
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the Licence is distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Licence for the specific language governing permissions and
* limitations under the Licence.
*
* This product combines work with different licenses. See the "NOTICE" text
* file for details on the various modules and licenses.
* The "NOTICE" text file is part of the distribution. Any derivative works
* that you distribute must include a readable copy of the "NOTICE" text file.
******************************************************************************/
package at.gv.egiz.pdfas.web.ws;
import java.util.ArrayList;
import java.util.List;
import javax.jws.WebService;
import javax.xml.ws.WebServiceException;
import javax.xml.ws.soap.MTOM;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import at.gv.egiz.pdfas.api.ws.PDFASBulkSignRequest;
import at.gv.egiz.pdfas.api.ws.PDFASBulkSignResponse;
import at.gv.egiz.pdfas.api.ws.PDFASSignParameters;
import at.gv.egiz.pdfas.api.ws.PDFASSignRequest;
import at.gv.egiz.pdfas.api.ws.PDFASSignResponse;
import at.gv.egiz.pdfas.api.ws.PDFASSigning;
import at.gv.egiz.pdfas.api.ws.PDFASSignParameters.Connector;
import at.gv.egiz.pdfas.web.config.WebConfiguration;
import at.gv.egiz.pdfas.web.helper.PdfAsHelper;
import at.gv.egiz.pdfas.web.store.RequestStore;
@MTOM
@WebService(endpointInterface = "at.gv.egiz.pdfas.api.ws.PDFASSigning")
public class PDFASSigningImpl implements PDFASSigning {
private static final Logger logger = LoggerFactory
.getLogger(PDFASSigningImpl.class);
public byte[] signPDFDokument(byte[] inputDocument,
PDFASSignParameters parameters) {
checkSoapSignEnabled();
try {
return PdfAsHelper.synchornousServerSignature(inputDocument,
parameters);
} catch (Throwable e) {
logger.error("Server Signature failed.", e);
if (WebConfiguration.isShowErrorDetails()) {
throw new WebServiceException("Server Signature failed.", e);
} else {
throw new WebServiceException("Server Signature failed.");
}
}
}
public PDFASSignResponse signPDFDokument(PDFASSignRequest request) {
checkSoapSignEnabled();
if (request == null) {
logger.warn("SOAP Sign Request is null!");
return null;
}
PDFASSignResponse response = new PDFASSignResponse();
try {
if(request.getParameters().getConnector().equals(Connector.MOA) ||
request.getParameters().getConnector().equals(Connector.JKS)) {
// Plain server based signatures!!
response.setSignedPDF(signPDFDokument(request.getInputData(),
request.getParameters()));
} else {
// Signatures with user interaction!!
String id = RequestStore.getInstance().createNewStoreEntry(request);
String userEntryURL = PdfAsHelper.generateUserEntryURL(id);
logger.debug("Generated request store: " + id);
logger.debug("Generated UI URL: " + userEntryURL);
if(userEntryURL == null) {
throw new WebServiceException("Failed to generate User Entry URL");
}
response.setRedirectUrl(userEntryURL);
}
} catch (Throwable e) {
if (e.getCause() != null) {
response.setError(e.getCause().getMessage());
} else {
response.setError(e.getMessage());
}
}
response.setRequestID(request.getRequestID());
return response;
}
public PDFASBulkSignResponse signPDFDokument(PDFASBulkSignRequest request) {
checkSoapSignEnabled();
List responses = new ArrayList();
if (request.getSignRequests() != null) {
for (int i = 0; i < request.getSignRequests().size(); i++) {
PDFASSignResponse response = signPDFDokument(request
.getSignRequests().get(i));
if (response != null) {
responses.add(response);
}
}
PDFASBulkSignResponse response = new PDFASBulkSignResponse();
response.setSignResponses(responses);
return response;
}
logger.error("Server Signature failed. [PDFASBulkSignRequest is NULL]");
if (WebConfiguration.isShowErrorDetails()) {
throw new WebServiceException("PDFASBulkSignRequest is NULL");
} else {
throw new WebServiceException("Server Signature failed.");
}
}
private void checkSoapSignEnabled() {
if(!WebConfiguration.getSoapSignEnabled()) {
throw new WebServiceException("Service disabled!");
}
}
}