From 969617f6557c559b173a8cc267c7cd37c6e2e088 Mon Sep 17 00:00:00 2001 From: tkellner Date: Fri, 24 Aug 2012 17:04:54 +0000 Subject: PDF-AS signature working with local BKU git-svn-id: https://svn.iaik.tugraz.at/svn/egiz/prj/current/12PDF-OVER-4.0@12391 3a0b52a2-8410-0410-bc02-ff6273a87459 --- .../pdfover/gui/workflow/states/LocalBKUState.java | 155 +++++++++++++++++++-- 1 file changed, 144 insertions(+), 11 deletions(-) (limited to 'trunk/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/LocalBKUState.java') diff --git a/trunk/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/LocalBKUState.java b/trunk/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/LocalBKUState.java index 795090e7..c9c079a8 100644 --- a/trunk/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/LocalBKUState.java +++ b/trunk/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/LocalBKUState.java @@ -18,15 +18,116 @@ package at.asit.pdfover.gui.workflow.states; // Imports import org.slf4j.Logger; import org.slf4j.LoggerFactory; - +import org.apache.commons.codec.binary.Base64; +import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.HttpException; +import org.apache.commons.httpclient.HttpStatus; +import org.apache.commons.httpclient.methods.PostMethod; import at.asit.pdfover.gui.MainWindowBehavior; import at.asit.pdfover.gui.MainWindow.Buttons; import at.asit.pdfover.gui.workflow.StateMachine; +import at.asit.pdfover.gui.workflow.Status; +import at.asit.pdfover.signator.SLRequest; +import at.asit.pdfover.signator.SLResponse; /** * Logical state for performing the BKU Request to a local BKU */ public class LocalBKUState extends State { + + /** + * HTTP Response server HEADER + */ + public final static String BKU_REPSONE_HEADER_SERVER = "server"; //$NON-NLS-1$ + + /** + * HTTP Response user-agent HEADER + */ + public final static String BKU_REPSONE_HEADER_USERAGENT = "user-agent"; //$NON-NLS-1$ + + /** + * HTTP Response SignatureLayout HEADER + */ + public final static String BKU_REPSONE_HEADER_SIGNATURE_LAYOUT = "SignatureLayout"; //$NON-NLS-1$ + + + /** + * + */ + private final class SignLocalBKUThread implements Runnable { + + private LocalBKUState state; + + /** + * @param localBKUState + */ + public SignLocalBKUThread(LocalBKUState localBKUState) { + this.state = localBKUState; + } + + @Override + public void run() { + try { + SLRequest request = this.state.signingState + .getSignatureRequest(); + + String b64_data = new String(Base64.encodeBase64(request + .getSignatureData().getByteArray())); + + String sl_request = request.getRequest() + .replace( + SLRequest.DATAOBJECT_STRING, + "" + b64_data //$NON-NLS-1$ + + ""); //$NON-NLS-1$ + + HttpClient client = new HttpClient(); + client.getParams().setParameter("http.useragent", //$NON-NLS-1$ + "PDF-Over 4.0"); //$NON-NLS-1$ + + PostMethod method = new PostMethod( + "http://127.0.0.1:3495/http-security-layer-request"); //$NON-NLS-1$ + method.addParameter("XMLRequest", sl_request); //$NON-NLS-1$ + + int returnCode = client.executeMethod(method); + + if(returnCode == HttpStatus.SC_OK) + { + String server = ""; //$NON-NLS-1$ + String userAgent = ""; //$NON-NLS-1$ + String signatureLayout = ""; //$NON-NLS-1$ + + if(method.getResponseHeader(BKU_REPSONE_HEADER_SERVER) != null) + { + server = method.getResponseHeader(BKU_REPSONE_HEADER_SERVER).getValue(); + } + + if(method.getResponseHeader(BKU_REPSONE_HEADER_USERAGENT) != null) + { + userAgent = method.getResponseHeader(BKU_REPSONE_HEADER_USERAGENT).getValue(); + } + + if(method.getResponseHeader(BKU_REPSONE_HEADER_SIGNATURE_LAYOUT) != null) + { + signatureLayout = method.getResponseHeader(BKU_REPSONE_HEADER_SIGNATURE_LAYOUT).getValue(); + } + + String response = method.getResponseBodyAsString(); + log.debug("SL Response: " + response); //$NON-NLS-1$ + SLResponse slResponse = new SLResponse(response, server, userAgent, signatureLayout); + this.state.signingState.setSignatureResponse(slResponse); + } else { + // TODO: Create HTTP exception + this.state.threadException = new HttpException(method.getResponseBodyAsString()); + } + + } catch (Exception e) { + log.error("SignLocalBKUThread: ", e); //$NON-NLS-1$ + } finally { + this.state.stateMachine.invokeUpdate(); + } + } + } + /** * @param stateMachine */ @@ -37,34 +138,66 @@ public class LocalBKUState extends State { /** * SLF4J Logger instance **/ - @SuppressWarnings("unused") - private static final Logger log = LoggerFactory + static final Logger log = LoggerFactory .getLogger(LocalBKUState.class); - /* (non-Javadoc) - * @see at.asit.pdfover.gui.workflow.WorkflowState#update(at.asit.pdfover.gui.workflow.Workflow) + at.asit.pdfover.signator.SigningState signingState; + + Exception threadException = null; + + /* + * (non-Javadoc) + * + * @see + * at.asit.pdfover.gui.workflow.WorkflowState#update(at.asit.pdfover.gui + * .workflow.Workflow) */ @Override public void run() { - // TODO Process SL Request and set SL Response + Status status = this.stateMachine.getStatus(); + + this.signingState = status.getSigningState(); + + if (!this.signingState.hasSignatureResponse() && + this.threadException == null + ) { + Thread t = new Thread(new SignLocalBKUThread(this)); + t.start(); + return; + } + + if(this.threadException != null) { + // TODO: Jump to error state! + } + + if(!this.signingState.hasSignatureResponse()) { + // The thread should set the response or the thread exception!!! + // TODO: Jump to error state! + } + // OK this.setNextState(new SigningState(this.stateMachine)); } - /* (non-Javadoc) + /* + * (non-Javadoc) + * * @see at.asit.pdfover.gui.workflow.states.State#cleanUp() */ @Override public void cleanUp() { // No composite - no cleanup necessary } - - /* (non-Javadoc) + + /* + * (non-Javadoc) + * * @see at.asit.pdfover.gui.workflow.states.State#setMainWindowBehavior() */ @Override public void updateMainWindowBehavior() { - MainWindowBehavior behavior = this.stateMachine.getStatus().getBehavior(); + MainWindowBehavior behavior = this.stateMachine.getStatus() + .getBehavior(); behavior.reset(); behavior.setActive(Buttons.OPEN, true); behavior.setActive(Buttons.POSITION, true); @@ -72,7 +205,7 @@ public class LocalBKUState extends State { } @Override - public String toString() { + public String toString() { return this.getClass().getName(); } } -- cgit v1.2.3