diff options
author | Andreas Fitzek <andreas.fitzek@iaik.tugraz.at> | 2014-01-09 12:52:06 +0100 |
---|---|---|
committer | Andreas Fitzek <andreas.fitzek@iaik.tugraz.at> | 2014-01-09 12:52:06 +0100 |
commit | 11c4bfac46f66ab3ba24b6354a6a372da40c0b1f (patch) | |
tree | c1e8f6f9b11eb8d939fefb08ef35ece09eab51a5 /pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper | |
parent | cec0065b747d30c6a0a17d18f2c7c8962a9102ed (diff) | |
download | pdf-as-4-11c4bfac46f66ab3ba24b6354a6a372da40c0b1f.tar.gz pdf-as-4-11c4bfac46f66ab3ba24b6354a6a372da40c0b1f.tar.bz2 pdf-as-4-11c4bfac46f66ab3ba24b6354a6a372da40c0b1f.zip |
PDF-AS web Developments
Diffstat (limited to 'pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper')
4 files changed, 633 insertions, 62 deletions
diff --git a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/HTMLFormater.java b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/HTMLFormater.java new file mode 100644 index 00000000..fe2c9271 --- /dev/null +++ b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/HTMLFormater.java @@ -0,0 +1,13 @@ +package at.gv.egiz.pdfas.web.helper; + +public class HTMLFormater { + + public static String formatStackTrace(StackTraceElement[] elements) { + StringBuilder sb = new StringBuilder(); + for(int i = 0; i < elements.length; i++) { + sb.append(elements[i].toString()); + sb.append("</br>"); + } + return sb.toString(); + } +} diff --git a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/PdfAsHelper.java b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/PdfAsHelper.java index 154cfd01..daf18108 100644 --- a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/PdfAsHelper.java +++ b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/PdfAsHelper.java @@ -5,6 +5,9 @@ import java.io.IOException; import java.util.Iterator; import java.util.List; +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletContext; +import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; @@ -12,14 +15,29 @@ import javax.xml.bind.JAXBElement; import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringEscapeUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import com.lowagie.text.html.WebColors; + +import at.gv.egiz.pdfas.lib.api.ByteArrayDataSink; import at.gv.egiz.pdfas.lib.api.ByteArrayDataSource; import at.gv.egiz.pdfas.lib.api.Configuration; +import at.gv.egiz.pdfas.lib.api.DataSink; import at.gv.egiz.pdfas.lib.api.PdfAs; import at.gv.egiz.pdfas.lib.api.PdfAsFactory; +import at.gv.egiz.pdfas.lib.api.SignaturePosition; import at.gv.egiz.pdfas.lib.api.StatusRequest; +import at.gv.egiz.pdfas.lib.api.sign.IPlainSigner; import at.gv.egiz.pdfas.lib.api.sign.SignParameter; +import at.gv.egiz.pdfas.lib.api.sign.SignResult; +import at.gv.egiz.pdfas.lib.impl.PdfAsImpl; +import at.gv.egiz.pdfas.lib.impl.SignaturePositionImpl; import at.gv.egiz.pdfas.sigs.pades.PAdESSigner; +import at.gv.egiz.pdfas.sigs.pades.PAdESSignerKeystore; +import at.gv.egiz.pdfas.sigs.pkcs7detached.PKCS7DetachedSigner; +import at.gv.egiz.pdfas.web.config.WebConfiguration; +import at.gv.egiz.pdfas.web.exception.PdfAsWebException; import at.gv.egiz.sl.CreateCMSSignatureRequestType; import at.gv.egiz.sl.CreateCMSSignatureResponseType; import at.gv.egiz.sl.InfoboxAssocArrayPairType; @@ -27,6 +45,7 @@ import at.gv.egiz.sl.InfoboxReadRequestType; import at.gv.egiz.sl.InfoboxReadResponseType; import at.gv.egiz.sl.ObjectFactory; import at.gv.egiz.sl.util.BKUSLConnector; +import at.gv.egiz.sl.util.MOAConnector; import at.gv.egiz.sl.util.SLMarschaller; public class PdfAsHelper { @@ -34,32 +53,240 @@ public class PdfAsHelper { private static final String PDF_CONFIG = "PDF_CONFIG"; private static final String PDF_STATUS = "PDF_STATUS"; private static final String PDF_SL_CONNECTOR = "PDF_SL_CONNECTOR"; + private static final String PDF_SIGNER = "PDF_SIGNER"; + private static final String PDF_SL_INTERACTIVE = "PDF_SL_INTERACTIVE"; + private static final String PDF_SIGNED_DATA = "PDF_SIGNED_DATA"; + private static final String PDF_ERR_MESSAGE = "PDF_ERR_MESSAGE"; + private static final String PDF_ERR_THROWABLE = "PDF_ERR_THROWABLE"; + private static final String PDF_ERROR_PAGE = "/ErrorPage"; + private static final String PDF_PROVIDE_PAGE = "/ProvidePDF"; + private static final String PDF_PDFDATA_PAGE = "/PDFData"; + private static final String PDF_DATAURL_PAGE = "/DataURL"; + private static final String PDF_ERR_URL = "PDF_ERR_URL"; + private static final String PDF_INVOKE_URL = "PDF_INVOKE_URL"; + private static final String REQUEST_FROM_DU = "REQ_DATA_URL"; + + // For development only: + public static final String keyStoreFile = "/home/afitzek/devel/pdfas_neu/test.p12"; + public static final String keyStoreType = "PKCS12"; + public static final String keyStorePass = "123456"; + // public static final String keyAlias = "pdf"; + public static final String keyAlias = "ecc_test"; + public static final String keyPass = "123456"; + + private static final Logger logger = LoggerFactory + .getLogger(PdfAsHelper.class); private static PdfAs pdfAs; private static ObjectFactory of = new ObjectFactory(); static { + // TODO: read from config file + logger.debug("Creating PDF-AS"); pdfAs = PdfAsFactory.createPdfAs(new File("/home/afitzek/.pdfas")); + logger.debug("Creating PDF-AS done"); } - public static void startSignature(HttpServletRequest request, + private static void validatePdfSize(HttpServletRequest request, + HttpServletResponse response, byte[] pdfData) + throws PdfAsWebException { + // Try to check num-bytes + String pdfSizeString = PdfAsParameterExtractor.getNumBytes(request); + if (pdfSizeString != null) { + long pdfSize = -1; + try { + pdfSize = Long.parseLong(pdfSizeString); + } catch (NumberFormatException e) { + throw new PdfAsWebException( + PdfAsParameterExtractor.PARAM_NUM_BYTES + + " parameter has to be a positiv number!", e); + } + if (pdfSize <= 0) { + throw new PdfAsWebException( + "Invalid PDF Size! Has to bigger than zero!"); + } + + if (pdfData.length != pdfSize) { + throw new PdfAsWebException("Signature Data Size and " + + PdfAsParameterExtractor.PARAM_NUM_BYTES + + " missmatch!"); + } + } + } + + private static String buildPosString(HttpServletRequest request, + HttpServletResponse response) throws PdfAsWebException { + String posP = PdfAsParameterExtractor.getSigPosP(request); + String posX = PdfAsParameterExtractor.getSigPosX(request); + String posY = PdfAsParameterExtractor.getSigPosY(request); + String posW = PdfAsParameterExtractor.getSigPosW(request); + + if (posP == null && posW == null && posX == null && posY == null) { + return null; + } + + StringBuilder sb = new StringBuilder(); + + if (posX != null) { + try { + Float.parseFloat(posX); + } catch (NumberFormatException e) { + throw new PdfAsWebException( + PdfAsParameterExtractor.PARAM_SIG_POS_X + + " has invalid value!", e); + } + sb.append("x:" + posX.trim() + ";"); + } else { + sb.append("x:auto;"); + } + + if (posY != null) { + try { + Float.parseFloat(posY); + } catch (NumberFormatException e) { + throw new PdfAsWebException( + PdfAsParameterExtractor.PARAM_SIG_POS_Y + + " has invalid value!", e); + } + sb.append("y:" + posY.trim() + ";"); + } else { + sb.append("y:auto;"); + } + + if (posW != null) { + try { + Float.parseFloat(posW); + } catch (NumberFormatException e) { + throw new PdfAsWebException( + PdfAsParameterExtractor.PARAM_SIG_POS_W + + " has invalid value!", e); + } + sb.append("w:" + posW.trim() + ";"); + } else { + sb.append("w:auto;"); + } + + if (posP != null) { + if (!(posP.equals("auto") || posP.equals("new"))) { + throw new PdfAsWebException( + PdfAsParameterExtractor.PARAM_SIG_POS_P + + " has invalid value! (auto | new )"); + } + sb.append("p:" + posP.trim() + ";"); + } else { + sb.append("P:auto;"); + } + + return sb.toString(); + } + + public static void doSignature(HttpServletRequest request, + HttpServletResponse response, byte[] pdfData) throws Exception { + + } + + /** + * Create synchronous PDF Signature + * + * @param request + * The Web request + * @param response + * The Web response + * @param pdfData + * The pdf data + * @return The signed pdf data + * @throws Exception + */ + public static byte[] synchornousSignature(HttpServletRequest request, HttpServletResponse response, byte[] pdfData) throws Exception { + validatePdfSize(request, response, pdfData); + + Configuration config = pdfAs.getConfiguration(); + + // Generate Sign Parameter + SignParameter signParameter = PdfAsFactory.createSignParameter(config, + new ByteArrayDataSource(pdfData)); + + // Get Connector + String connector = PdfAsParameterExtractor.getConnector(request); + + if (!connector.equals("moa") && !connector.equals("jks")) { + throw new PdfAsWebException("Invalid connector (moa | jks)"); + } + + IPlainSigner signer; + if (connector.equals("moa")) { + signer = new PAdESSigner(new MOAConnector(config)); + } else { + // TODO: + // signer = new PAdESSignerKeystore(file, alias, kspassword, + // keypassword, type) + signer = new PKCS7DetachedSigner(keyStoreFile, keyAlias, + keyStorePass, keyPass, keyStoreType); + } + + signParameter.setPlainSigner(signer); + + // set Signature Profile (null use default ...) + signParameter.setSignatureProfileId(PdfAsParameterExtractor + .getSigType(request)); + + ByteArrayDataSink output = new ByteArrayDataSink(); + signParameter.setOutput(output); + + // set Signature Position + signParameter.setSignaturePosition(buildPosString(request, response)); + + pdfAs.sign(signParameter); + + return output.getData(); + } + + public static void startSignature(HttpServletRequest request, + HttpServletResponse response, ServletContext context, byte[] pdfData) + throws Exception { + + validatePdfSize(request, response, pdfData); HttpSession session = request.getSession(); Configuration config = pdfAs.getConfiguration(); session.setAttribute(PDF_CONFIG, config); - BKUSLConnector bkuSLConnector = new BKUSLConnector(config); + + // Generate Sign Parameter SignParameter signParameter = PdfAsFactory.createSignParameter(config, new ByteArrayDataSource(pdfData)); - signParameter.setPlainSigner(new PAdESSigner(bkuSLConnector)); - - session.setAttribute(PDF_SL_CONNECTOR, bkuSLConnector); + + // Get Connector + String connector = PdfAsParameterExtractor.getConnector(request); + + IPlainSigner signer; + if (connector.equals("bku") || connector.equals("onlinebku") || connector.equals("mobilebku")) { + BKUSLConnector conn = new BKUSLConnector(config); + signer = new PAdESSigner(conn); + session.setAttribute(PDF_SL_CONNECTOR, conn); + } else { + throw new PdfAsWebException("Invalid connector (bku | onlinebku | mobilebku | moa | jks)"); + } + + signParameter.setPlainSigner(signer); + session.setAttribute(PDF_SIGNER, signer); + session.setAttribute(PDF_SL_INTERACTIVE, connector); + + // set Signature Profile (null use default ...) + signParameter.setSignatureProfileId(PdfAsParameterExtractor + .getSigType(request)); + + ByteArrayDataSink dataSink = new ByteArrayDataSink(); + signParameter.setOutput(dataSink); + + // set Signature Position + signParameter.setSignaturePosition(buildPosString(request, response)); StatusRequest statusRequest = pdfAs.startSign(signParameter); session.setAttribute(PDF_STATUS, statusRequest); - PdfAsHelper.process(request, response); + PdfAsHelper.process(request, response, context); } private static byte[] getCertificate( @@ -69,36 +296,40 @@ public class PdfAsHelper { List<InfoboxAssocArrayPairType> pairs = infoboxReadResponseType .getAssocArrayData().getPair(); Iterator<InfoboxAssocArrayPairType> pairIterator = pairs.iterator(); - while(pairIterator.hasNext()) { + while (pairIterator.hasNext()) { InfoboxAssocArrayPairType pair = pairIterator.next(); - if(pair.getKey().equals("SecureSignatureKeypair")) { + if (pair.getKey().equals("SecureSignatureKeypair")) { return pair.getBase64Content(); } } } // SecureSignatureKeypair - + return data; } public static void injectCertificate(HttpServletRequest request, - HttpServletResponse response, - InfoboxReadResponseType infoboxReadResponseType) throws Exception { - + HttpServletResponse response, + InfoboxReadResponseType infoboxReadResponseType, + ServletContext context) throws Exception { + HttpSession session = request.getSession(); - StatusRequest statusRequest = (StatusRequest)session.getAttribute(PDF_STATUS); - + StatusRequest statusRequest = (StatusRequest) session + .getAttribute(PDF_STATUS); + statusRequest.setCertificate(getCertificate(infoboxReadResponseType)); statusRequest = pdfAs.process(statusRequest); session.setAttribute(PDF_STATUS, statusRequest); - - PdfAsHelper.process(request, response); + + PdfAsHelper.process(request, response, context); } public static void injectSignature(HttpServletRequest request, HttpServletResponse response, - CreateCMSSignatureResponseType createCMSSignatureResponseType) - throws Exception { + CreateCMSSignatureResponseType createCMSSignatureResponseType, + ServletContext context) throws Exception { + + logger.info("Got CMS Signature Response"); HttpSession session = request.getSession(); StatusRequest statusRequest = (StatusRequest) session @@ -109,58 +340,88 @@ public class PdfAsHelper { statusRequest = pdfAs.process(statusRequest); session.setAttribute(PDF_STATUS, statusRequest); - PdfAsHelper.process(request, response); + PdfAsHelper.process(request, response, context); } public static void process(HttpServletRequest request, - HttpServletResponse response) throws Exception { + HttpServletResponse response, ServletContext context) + throws Exception { HttpSession session = request.getSession(); StatusRequest statusRequest = (StatusRequest) session .getAttribute(PDF_STATUS); - BKUSLConnector bkuSLConnector = (BKUSLConnector) session - .getAttribute(PDF_SL_CONNECTOR); - Configuration config = (Configuration) session.getAttribute(PDF_CONFIG); - - if (statusRequest.needCertificate()) { - // build SL Request to read certificate - InfoboxReadRequestType readCertificateRequest = bkuSLConnector - .createInfoboxReadRequest(); - - JAXBElement<InfoboxReadRequestType> readRequest = of - .createInfoboxReadRequest(readCertificateRequest); - - String url = request.getContextPath() + "/DataURL;jsessionid=" - + session.getId(); - String fullurl = request.getScheme() + "://" - + request.getServerName() + ":" + request.getServerPort() - + url; - String slRequest = SLMarschaller.marshalToString(readRequest); - String template = getTemplateSL(); - template = template.replace("##BKU##", - "http://127.0.0.1:3495/http-security-layer-request"); - template = template.replace("##XMLRequest##", - StringEscapeUtils.escapeHtml4(slRequest)); - template = template.replace("##DataURL##", fullurl); - response.getWriter().write(template); - response.getWriter().close(); - } else if (statusRequest.needSignature()) { - // build SL Request for cms signature - CreateCMSSignatureRequestType createCMSSignatureRequestType = - bkuSLConnector.createCMSRequest(statusRequest.getSignatureData(), - statusRequest.getSignatureDataByteRange()); - - String slRequest = SLMarschaller.marshalToString(of - .createCreateCMSSignatureRequest(createCMSSignatureRequestType)); - - response.setContentType("text/xml"); - response.getWriter().write(slRequest); - response.getWriter().close(); - - } else if (statusRequest.isReady()) { - // TODO: store pdf document redirect to Finish URL + IPlainSigner plainSigner = (IPlainSigner) session + .getAttribute(PDF_SIGNER); + + String connector = (String) session.getAttribute(PDF_SL_INTERACTIVE); + + if (connector.equals("bku") || connector.equals("onlinebku") || connector.equals("mobilebku")) { + BKUSLConnector bkuSLConnector = (BKUSLConnector) session + .getAttribute(PDF_SL_CONNECTOR); + + // TODO Handle logic for BKU interaction + + Configuration config = (Configuration) session + .getAttribute(PDF_CONFIG); + + if (statusRequest.needCertificate()) { + logger.info("Needing Certificate from BKU"); + // build SL Request to read certificate + InfoboxReadRequestType readCertificateRequest = bkuSLConnector + .createInfoboxReadRequest(); + + JAXBElement<InfoboxReadRequestType> readRequest = of + .createInfoboxReadRequest(readCertificateRequest); + + String url = request.getContextPath() + "/DataURL;jsessionid=" + + session.getId(); + String fullurl = request.getScheme() + "://" + + request.getServerName() + ":" + + request.getServerPort() + url; + String slRequest = SLMarschaller.marshalToString(readRequest); + String template = getTemplateSL(); + template = template.replace("##BKU##", + generateBKUURL(connector)); + template = template.replace("##XMLRequest##", + StringEscapeUtils.escapeHtml4(slRequest)); + template = template.replace("##DataURL##", fullurl); + response.getWriter().write(template); + response.getWriter().close(); + } else if (statusRequest.needSignature()) { + logger.info("Needing Signature from BKU"); + // build SL Request for cms signature + CreateCMSSignatureRequestType createCMSSignatureRequestType = bkuSLConnector + .createCMSRequest(statusRequest.getSignatureData(), + statusRequest.getSignatureDataByteRange()); + + String slRequest = SLMarschaller + .marshalToString(of + .createCreateCMSSignatureRequest(createCMSSignatureRequestType)); + + response.setContentType("text/xml"); + response.getWriter().write(slRequest); + response.getWriter().close(); + + } else if (statusRequest.isReady()) { + // TODO: store pdf document redirect to Finish URL + logger.info("Document ready!"); + + SignResult result = pdfAs.finishSign(statusRequest); + DataSink output = result.getOutputDocument(); + if (output instanceof ByteArrayDataSink) { + ByteArrayDataSink byteDataSink = (ByteArrayDataSink) output; + PdfAsHelper.setSignedPdf(request, response, + byteDataSink.getData()); + PdfAsHelper.gotoProvidePdf(context, request, response); + } else { + // TODO: no signature data available! + } + + } else { + // TODO: invalid state + } } else { - // TODO: invalid state + // TODO Handle logic for } } @@ -169,5 +430,187 @@ public class PdfAsHelper { .toFile(PdfAsHelper.class.getResource("/template_sl.html"))); return xml; } + + public static String getErrorRedirectTemplateSL() throws IOException { + String xml = FileUtils.readFileToString(FileUtils + .toFile(PdfAsHelper.class.getResource("/template_error_redirect.html"))); + return xml; + } + + public static String getInvokeRedirectTemplateSL() throws IOException { + String xml = FileUtils.readFileToString(FileUtils + .toFile(PdfAsHelper.class.getResource("/template_invoke_redirect.html"))); + return xml; + } + + public static byte[] getSignedPdf(HttpServletRequest request, + HttpServletResponse response) { + HttpSession session = request.getSession(); + Object signedData = session.getAttribute(PDF_SIGNED_DATA); + if (signedData == null) { + return null; + } + + if (signedData instanceof byte[]) { + return (byte[]) signedData; + } + logger.warn("PDF_SIGNED_DATA in session is not a byte[] type!"); + return null; + } + + public static void setSignedPdf(HttpServletRequest request, + HttpServletResponse response, byte[] signedData) { + HttpSession session = request.getSession(); + session.setAttribute(PDF_SIGNED_DATA, signedData); + } + + public static void setSessionException(HttpServletRequest request, + HttpServletResponse response, String message, Throwable e) { + HttpSession session = request.getSession(); + session.setAttribute(PDF_ERR_MESSAGE, message); + session.setAttribute(PDF_ERR_THROWABLE, e); + } + + public static String getSessionErrMessage(HttpServletRequest request, + HttpServletResponse response) { + HttpSession session = request.getSession(); + Object obj = session.getAttribute(PDF_ERR_MESSAGE); + return obj == null ? null : obj.toString(); + } + + public static Throwable getSessionException(HttpServletRequest request, + HttpServletResponse response) { + HttpSession session = request.getSession(); + Object obj = session.getAttribute(PDF_ERR_THROWABLE); + if (obj == null) { + return null; + } + + if (obj instanceof Throwable) { + return (Throwable) obj; + } + logger.warn("PDF_ERR_THROWABLE in session is not a throwable type!"); + return null; + } + + public static void gotoError(ServletContext context, + HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + if (PdfAsHelper.getFromDataUrl(request)) { + response.sendRedirect(generateErrorURL(request, response)); + } else { + RequestDispatcher dispatcher = context + .getRequestDispatcher(PDF_ERROR_PAGE); + dispatcher.forward(request, response); + } + } + + public static void gotoProvidePdf(ServletContext context, + HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + if (PdfAsHelper.getFromDataUrl(request)) { + response.sendRedirect(generateProvideURL(request, response)); + } else { + RequestDispatcher dispatcher = context + .getRequestDispatcher(PDF_PROVIDE_PAGE); + dispatcher.forward(request, response); + } + } + + public static void setErrorURL(HttpServletRequest request, + HttpServletResponse response, String url) { + HttpSession session = request.getSession(); + session.setAttribute(PDF_ERR_URL, url); + } + + public static String getErrorURL(HttpServletRequest request, + HttpServletResponse response) { + HttpSession session = request.getSession(); + Object obj = session.getAttribute(PDF_ERR_URL); + return obj == null ? null : obj.toString(); + } + + public static void setInvokeURL(HttpServletRequest request, + HttpServletResponse response, String url) { + HttpSession session = request.getSession(); + session.setAttribute(PDF_INVOKE_URL, url); + logger.info("External Invoke URL: " + url); + } + + public static String getInvokeURL(HttpServletRequest request, + HttpServletResponse response) { + HttpSession session = request.getSession(); + Object obj = session.getAttribute(PDF_INVOKE_URL); + return obj == null ? null : obj.toString(); + } + private static String generateURL(HttpServletRequest request, + HttpServletResponse response, String Servlet) { + HttpSession session = request.getSession(); + String publicURL = WebConfiguration.getPublicURL(); + String dataURL = null; + if (publicURL != null) { + dataURL = publicURL + Servlet + ";jsessionid=" + session.getId(); + } else { + if ((request.getScheme().equals("http") && request.getServerPort() == 80) + || (request.getScheme().equals("https") && request + .getServerPort() == 443)) { + dataURL = request.getScheme() + "://" + request.getServerName() + + request.getContextPath() + Servlet + ";jsessionid=" + + session.getId(); + } else { + dataURL = request.getScheme() + "://" + request.getServerName() + + ":" + request.getServerPort() + + request.getContextPath() + Servlet + ";jsessionid=" + + session.getId(); + } + } + logger.info("Generated URL: " + dataURL); + return dataURL; + } + + public static String generateDataURL(HttpServletRequest request, + HttpServletResponse response) { + return generateURL(request, response, PDF_DATAURL_PAGE); + } + + public static String generateProvideURL(HttpServletRequest request, + HttpServletResponse response) { + return generateURL(request, response, PDF_PROVIDE_PAGE); + } + + public static String generateErrorURL(HttpServletRequest request, + HttpServletResponse response) { + return generateURL(request, response, PDF_ERROR_PAGE); + } + + public static String generatePdfURL(HttpServletRequest request, + HttpServletResponse response) { + return generateURL(request, response, PDF_PDFDATA_PAGE); + } + + public static String generateBKUURL(String connector) { + if(connector.equals("bku")) { + return WebConfiguration.getLocalBKUURL(); + } else if(connector.equals("onlinebku")) { + return WebConfiguration.getOnlineBKUURL(); + } else if(connector.equals("mobilebku")) { + return WebConfiguration.getHandyBKUURL(); + } + return WebConfiguration.getLocalBKUURL(); + } + + public static void setFromDataUrl(HttpServletRequest request) { + request.setAttribute(REQUEST_FROM_DU, (Boolean) true); + } + + public static boolean getFromDataUrl(HttpServletRequest request) { + Object obj = request.getAttribute(REQUEST_FROM_DU); + if (obj != null) { + if (obj instanceof Boolean) { + return ((Boolean) obj).booleanValue(); + } + } + return false; + } } diff --git a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/PdfAsParameterExtractor.java b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/PdfAsParameterExtractor.java new file mode 100644 index 00000000..5a79bb4a --- /dev/null +++ b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/PdfAsParameterExtractor.java @@ -0,0 +1,85 @@ +package at.gv.egiz.pdfas.web.helper; + +import javax.servlet.http.HttpServletRequest; + +public class PdfAsParameterExtractor { + + public static final String PARAM_CONNECTOR = "connector"; + public static final String PARAM_CONNECTOR_DEFAULT = "bku"; + + + public static final String PARAM_INVOKE_URL = "invoke-app-url"; + public static final String PARAM_INVOKE_URL_ERROR = "invoke-app-error-url"; + public static final String PARAM_LOCALE = "locale"; + public static final String PARAM_NUM_BYTES = "num-bytes"; + public static final String PARAM_PDF_URL = "pdf-url"; + public static final String PARAM_SIG_TYPE = "sig_type"; + public static final String PARAM_SIG_POS_P = "sig-pos-p"; + public static final String PARAM_SIG_POS_Y = "sig-pos-y"; + public static final String PARAM_SIG_POS_X = "sig-pos-x"; + public static final String PARAM_SIG_POS_W = "sig-pos-w"; + + public static String getConnector(HttpServletRequest request) { + String connector = (String)request.getAttribute(PARAM_CONNECTOR); + if(connector != null) { + return connector; + } + return PARAM_CONNECTOR_DEFAULT; + } + + public static String getInvokeURL(HttpServletRequest request) { + return (String)request.getAttribute(PARAM_INVOKE_URL); + } + + public static String getInvokeErrorURL(HttpServletRequest request) { + String url = (String)request.getAttribute(PARAM_INVOKE_URL_ERROR); + if(url != null) { + //TODO validation! + } + return url; + } + + public static String getLocale(HttpServletRequest request) { + return (String)request.getAttribute(PARAM_LOCALE); + } + + public static String getNumBytes(HttpServletRequest request) { + return (String)request.getAttribute(PARAM_NUM_BYTES); + } + + public static String getPdfUrl(HttpServletRequest request) { + return (String)request.getAttribute(PARAM_PDF_URL); + } + + public static String getSigType(HttpServletRequest request) { + return (String)request.getAttribute(PARAM_SIG_TYPE); + } + + public static String getSigPosP(HttpServletRequest request) { + return (String)request.getAttribute(PARAM_SIG_POS_P); + } + + public static String getSigPosY(HttpServletRequest request) { + return (String)request.getAttribute(PARAM_SIG_POS_Y); + } + + public static String getSigPosX(HttpServletRequest request) { + return (String)request.getAttribute(PARAM_SIG_POS_X); + } + + public static String getSigPosW(HttpServletRequest request) { + return (String)request.getAttribute(PARAM_SIG_POS_W); + } + + // legacy Parameter + public static final String PARAM_PDF_ID = "pdf-id"; + public static final String PARAM_SESSION_ID = "session-id"; + + public static String getPdfId(HttpServletRequest request) { + return (String)request.getAttribute(PARAM_PDF_ID); + } + + public static String getSessionId(HttpServletRequest request) { + return (String)request.getAttribute(PARAM_SESSION_ID); + } +} diff --git a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/RemotePDFFetcher.java b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/RemotePDFFetcher.java new file mode 100644 index 00000000..7c90dbc8 --- /dev/null +++ b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/RemotePDFFetcher.java @@ -0,0 +1,30 @@ +package at.gv.egiz.pdfas.web.helper; + +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; + +import at.gv.egiz.pdfas.common.utils.StreamUtils; +import at.gv.egiz.pdfas.web.exception.PdfAsWebException; + +public class RemotePDFFetcher { + + public static byte[] fetchPdfFile(String pdfURL) throws PdfAsWebException { + URL url; + try { + url = new URL(pdfURL); + } catch (MalformedURLException e) { + throw new PdfAsWebException("Not a valid URL!", e); + } + if(url.getProtocol().equals("http") || url.getProtocol().equals("https")) { + try { + InputStream is = url.openStream(); + return StreamUtils.inputStreamToByteArray(is); + } catch (Exception e) { + throw new PdfAsWebException("Failed to fetch pdf document!", e); + } + } else { + throw new PdfAsWebException("Failed to fetch pdf document protocol " + url.getProtocol() + " is not supported"); + } + } +} |