From 9e50f1a103a37d9dcfbeb5ea45bcf6c6c0c0f6dd Mon Sep 17 00:00:00 2001 From: Tobias Kellner Date: Thu, 27 Nov 2014 20:57:33 +0100 Subject: Implement PDF-AS 4 changes --- .../at/asit/pdfover/gui/bku/LocalBKUConnector.java | 158 +++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 pdf-over-gui/src/main/java/at/asit/pdfover/gui/bku/LocalBKUConnector.java (limited to 'pdf-over-gui/src/main/java/at/asit/pdfover/gui/bku/LocalBKUConnector.java') diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/bku/LocalBKUConnector.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/bku/LocalBKUConnector.java new file mode 100644 index 00000000..f862455c --- /dev/null +++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/bku/LocalBKUConnector.java @@ -0,0 +1,158 @@ +/* + * Copyright 2012 by A-SIT, Secure Information Technology Center Austria + * + * 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://joinup.ec.europa.eu/software/page/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. + */ +package at.asit.pdfover.gui.bku; + +// Imports +import java.io.IOException; + +import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.HttpException; +import org.apache.commons.httpclient.HttpMethod; +import org.apache.commons.httpclient.HttpStatus; +import org.apache.commons.httpclient.methods.PostMethod; +import org.apache.commons.httpclient.methods.multipart.FilePart; +import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; +import org.apache.commons.httpclient.methods.multipart.Part; +import org.apache.commons.httpclient.methods.multipart.StringPart; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.asit.pdfover.gui.Constants; +import at.asit.pdfover.gui.utils.FileUploadSource; +import at.asit.pdfover.signator.BkuSlConnector; +import at.asit.pdfover.signator.SLRequest; +import at.asit.pdfover.signator.SLResponse; +import at.asit.pdfover.signator.SignatureException; + +/** + * + */ +public class LocalBKUConnector implements BkuSlConnector { + /** + * SLF4J Logger instance + **/ + private static final Logger log = LoggerFactory + .getLogger(LocalBKUConnector.class); + + /** Whether to use Base64 or FileUpload Request */ + private boolean useBase64Request = false; + + /** + * HTTP Response server HEADER + */ + public final static String BKU_RESPONSE_HEADER_SERVER = "server"; //$NON-NLS-1$ + + /** + * HTTP Response user-agent HEADER + */ + public final static String BKU_RESPONSE_HEADER_USERAGENT = "user-agent"; //$NON-NLS-1$ + + /** + * HTTP Response SignatureLayout HEADER + */ + public final static String BKU_RESPONSE_HEADER_SIGNATURE_LAYOUT = "SignatureLayout"; //$NON-NLS-1$ + + /** + * Null-Operation SL-Request + */ + private final static String NULL_OPERATION_REQUEST = "" + //$NON-NLS-1$ + ""; //$NON-NLS-1$ + + /* (non-Javadoc) + * @see at.asit.pdfover.signator.BkuSlConnector#handleSLRequest(java.lang.String) + */ + @Override + public SLResponse handleSLRequest(SLRequest request) throws SignatureException { + try { + HttpClient client = BKUHelper.getHttpClient(); + + PostMethod method = new PostMethod(Constants.LOCAL_BKU_URL); + + String sl_request = NULL_OPERATION_REQUEST; + method.addParameter("XMLRequest", sl_request); //$NON-NLS-1$ + int returnCode = client.executeMethod(method); + + String userAgent = getResponseHeader(method, BKU_RESPONSE_HEADER_USERAGENT); + String server = getResponseHeader(method, BKU_RESPONSE_HEADER_SERVER); + if (server != null && server.contains("trustDeskbasic")) //$NON-NLS-1$ + this.useBase64Request = true; // TDB doesn't support MultiPart requests + + method = new PostMethod(Constants.LOCAL_BKU_URL); + + if (request.getSignatureData() != null) { + if (this.useBase64Request) + { + sl_request = request.getBase64Request(); + method.addParameter("XMLRequest", sl_request); //$NON-NLS-1$ + } else { + sl_request = request.getFileUploadRequest(); + StringPart xmlpart = new StringPart( + "XMLRequest", sl_request, "UTF-8"); //$NON-NLS-1$ //$NON-NLS-2$ + + FilePart filepart = new FilePart("fileupload", //$NON-NLS-1$ + new FileUploadSource(request.getSignatureData())); + + Part[] parts = { xmlpart, filepart }; + + method.setRequestEntity(new MultipartRequestEntity(parts, method + .getParams())); + } + } else { + method.addParameter("XMLRequest", request.getRequest()); //$NON-NLS-1$ + } + log.debug("SL REQUEST: " + sl_request); //$NON-NLS-1$ + + returnCode = client.executeMethod(method); + + if (returnCode != HttpStatus.SC_OK) { + throw new HttpException( + method.getResponseBodyAsString()); + } + + server = getResponseHeader(method, BKU_RESPONSE_HEADER_SERVER); + if (server == null) + server = ""; //$NON-NLS-1$ + userAgent = getResponseHeader(method, BKU_RESPONSE_HEADER_USERAGENT); + if (userAgent == null) + userAgent = ""; //$NON-NLS-1$ + String signatureLayout = getResponseHeader(method, BKU_RESPONSE_HEADER_SIGNATURE_LAYOUT); + + String response = method.getResponseBodyAsString(); + log.debug("SL Response: " + response); //$NON-NLS-1$ + SLResponse slResponse = new SLResponse(response, server, + userAgent, signatureLayout); + return slResponse; + } catch (HttpException e) { + log.error("LocalBKUConnector: ", e); //$NON-NLS-1$ + throw new SignatureException(e); + } catch (IOException e) { + log.error("LocalBKUConnector: ", e); //$NON-NLS-1$ + throw new SignatureException(e); + } + } + + /** + * Returns the value corresponding to the given header name + * @param method the HTTP method + * @param headerName the header name + * @return the header value (or null if not found) + */ + private static String getResponseHeader(HttpMethod method, String headerName) { + if (method.getResponseHeader(headerName) == null) + return null; + return method.getResponseHeader(headerName).getValue(); + } +} -- cgit v1.2.3