aboutsummaryrefslogtreecommitdiff
path: root/simpleSigning/src/main/java/at/gv/egiz/simpleSigning/helper
diff options
context:
space:
mode:
Diffstat (limited to 'simpleSigning/src/main/java/at/gv/egiz/simpleSigning/helper')
-rw-r--r--simpleSigning/src/main/java/at/gv/egiz/simpleSigning/helper/PDFHelper.java141
-rw-r--r--simpleSigning/src/main/java/at/gv/egiz/simpleSigning/helper/SessionHelper.java126
2 files changed, 267 insertions, 0 deletions
diff --git a/simpleSigning/src/main/java/at/gv/egiz/simpleSigning/helper/PDFHelper.java b/simpleSigning/src/main/java/at/gv/egiz/simpleSigning/helper/PDFHelper.java
new file mode 100644
index 0000000..9c94dac
--- /dev/null
+++ b/simpleSigning/src/main/java/at/gv/egiz/simpleSigning/helper/PDFHelper.java
@@ -0,0 +1,141 @@
+package at.gv.egiz.simpleSigning.helper;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.lowagie.text.Document;
+import com.lowagie.text.PageSize;
+import com.lowagie.text.Paragraph;
+import com.lowagie.text.pdf.PdfWriter;
+
+import at.gv.egiz.simpleSigning.ErrorSignature;
+
+public class PDFHelper {
+
+ private static final Logger logger = LoggerFactory
+ .getLogger(PDFHelper.class);
+
+ private static byte[] createPDFiText(String text) {
+ try {
+ Document document = new Document(PageSize.A4, 36, 72, 108, 180);
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ PdfWriter.getInstance(document,baos);
+ document.open();
+ document.add(new Paragraph(text));
+ System.out.println("Text is inserted into pdf file");
+ document.close();
+ baos.close();
+ return baos.toByteArray();
+ } catch(Throwable e) {
+ logger.error("Failed to create PDF", e);
+ }
+ return null;
+ }
+
+ /*
+ private static byte[] createPDFPdfBox(String text) {
+
+ PDDocument document = null;
+
+ try {
+ // Create a document and add a page to it
+ document = new PDDocument();
+ PDPage page = new PDPage();
+ document.addPage(page);
+
+ PDPageContentStream contentStream = new PDPageContentStream(document, page);
+
+ PDFont pdfFont = PDType1Font.HELVETICA;
+ float fontSize = 25;
+ float leading = 1.5f * fontSize;
+
+ PDRectangle mediabox = page.findMediaBox();
+ float margin = 72;
+ float width = mediabox.getWidth() - 2*margin;
+ float startX = mediabox.getLowerLeftX() + margin;
+ float startY = mediabox.getUpperRightY() - margin;
+
+ List<String> lines = new ArrayList<String>();
+ int lastSpace = -1;
+ while (text.length() > 0)
+ {
+ int spaceIndex = text.indexOf(' ', lastSpace + 1);
+ if (spaceIndex < 0)
+ {
+ lines.add(text);
+ text = "";
+ }
+ else
+ {
+ String subString = text.substring(0, spaceIndex);
+ float size = fontSize * pdfFont.getStringWidth(subString) / 1000;
+ if (size > width)
+ {
+ if (lastSpace < 0) // So we have a word longer than the line... draw it anyways
+ lastSpace = spaceIndex;
+ subString = text.substring(0, lastSpace);
+ lines.add(subString);
+ text = text.substring(lastSpace).trim();
+ lastSpace = -1;
+ }
+ else
+ {
+ lastSpace = spaceIndex;
+ }
+ }
+ }
+
+ contentStream.beginText();
+ contentStream.setFont(pdfFont, fontSize);
+ contentStream.moveTextPositionByAmount(startX, startY);
+ for (String line: lines)
+ {
+ contentStream.drawString(line);
+ contentStream.moveTextPositionByAmount(0, -leading);
+ }
+ contentStream.endText();
+
+
+
+ // Make sure that the content stream is closed:
+ contentStream.close();
+
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ // Save the results and ensure that the document is properly closed:
+ document.save(baos);
+
+ return baos.toByteArray();
+ } catch (Throwable e) {
+ logger.error("Failed to create PDF", e);
+ } finally {
+ if(document != null) {
+ try {
+ document.close();
+ } catch (Throwable e) {
+ logger.error("Failed to close PDF", e);
+ }
+ }
+ }
+ return null;
+ }
+ */
+ public static byte[] createPDFDocument(String text) {
+ return createPDFiText(text);
+ }
+
+ public static void toError(HttpServletRequest req,
+ HttpServletResponse resp, String error, String cause)
+ throws ServletException, IOException {
+ req.setAttribute(ErrorSignature.PARAM_ERROR, error);
+ req.setAttribute(ErrorSignature.PARAM_CAUSE, cause);
+ req.getRequestDispatcher("Error").forward(req, resp);
+ }
+
+}
diff --git a/simpleSigning/src/main/java/at/gv/egiz/simpleSigning/helper/SessionHelper.java b/simpleSigning/src/main/java/at/gv/egiz/simpleSigning/helper/SessionHelper.java
new file mode 100644
index 0000000..0f2c6b5
--- /dev/null
+++ b/simpleSigning/src/main/java/at/gv/egiz/simpleSigning/helper/SessionHelper.java
@@ -0,0 +1,126 @@
+package at.gv.egiz.simpleSigning.helper;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpSession;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class SessionHelper {
+
+ private static final String SESSION_CONTENT = "SESSION_CONTENT";
+ private static final String SESSION_TYPE = "SESSION_TYPE";
+ private static final String SESSION_TARGETURL = "SESSION_TARGETURL";
+ private static final String SESSION_CONNECTOR = "SESSION_CONNECTOR";
+ private static final String SESSION_EVENTID = "SESSION_EVENTID";
+ private static final String SESSION_DOCUMENT = "SESSION_DOCUMENT";
+
+ private static final String SESSION_TYPE_TEXT = "TEXT";
+ private static final String SESSION_TYPE_PDF = "PDF";
+
+ private static final Logger logger = LoggerFactory
+ .getLogger(SessionHelper.class);
+
+ public enum Type {
+ TEXT, PDF
+ }
+
+ public static void setType(HttpServletRequest request, String value) {
+ if (value != null) {
+ if (value.equals(SESSION_TYPE_PDF)) {
+ request.getSession().setAttribute(SESSION_TYPE, Type.PDF);
+ } else {
+ request.getSession().setAttribute(SESSION_TYPE, Type.TEXT);
+ }
+ } else {
+ request.getSession().setAttribute(SESSION_TYPE, Type.TEXT);
+ }
+ }
+
+ public static Type getType(HttpServletRequest request) {
+ Object obj = request.getSession().getAttribute(SESSION_TYPE);
+ if (obj != null && obj instanceof Type) {
+ return (Type) obj;
+ } else {
+ return Type.TEXT;
+ }
+ }
+
+ public static void setContent(HttpServletRequest request, String value) {
+ request.getSession().setAttribute(SESSION_CONTENT, value);
+ }
+
+ public static String getContent(HttpServletRequest request) {
+ Object s = request.getSession().getAttribute(SESSION_CONTENT);
+ if (s != null) {
+ return s.toString();
+ } else {
+ return null;
+ }
+ }
+
+ public static void setConnector(HttpServletRequest request, String value) {
+ request.getSession().setAttribute(SESSION_CONNECTOR, value);
+ }
+
+ public static String getConnector(HttpServletRequest request) {
+ Object s = request.getSession().getAttribute(SESSION_CONNECTOR);
+ if (s != null) {
+ return s.toString();
+ } else {
+ return null;
+ }
+ }
+
+ public static void setEventID(HttpServletRequest request, String value) {
+ request.getSession().setAttribute(SESSION_EVENTID, value);
+ }
+
+ public static String getEventID(HttpServletRequest request) {
+ Object s = request.getSession().getAttribute(SESSION_EVENTID);
+ if (s != null) {
+ return s.toString();
+ } else {
+ return null;
+ }
+ }
+
+ public static void setTargetURL(HttpServletRequest request, String target) {
+ request.getSession().setAttribute(SESSION_TARGETURL, target);
+ }
+
+ public static String getTargetURL(HttpServletRequest request) {
+ Object s = request.getSession().getAttribute(SESSION_TARGETURL);
+ if (s != null) {
+ return s.toString();
+ } else {
+ return null;
+ }
+ }
+
+ public static void setDocument(HttpServletRequest request, byte[] doc) {
+ if(doc == null) {
+ logger.info("[" + request.getSession().getId() + "]: setting Document to NULL!!!");
+ } else {
+ logger.info("[" + request.getSession().getId() + "]: setting Document");
+ }
+ request.getSession().setAttribute(SESSION_DOCUMENT, doc);
+ }
+
+ public static byte[] getDocument(HttpServletRequest request) {
+ logger.info("[" + request.getSession().getId() + "]: getting Document");
+ HttpSession session = request.getSession(false);
+ if (session != null) {
+ Object obj = session.getAttribute(SESSION_DOCUMENT);
+ if (obj != null && obj instanceof byte[]) {
+ return (byte[]) obj;
+ }
+ }
+ return null;
+ }
+
+ public static void killSession(HttpServletRequest request) {
+ logger.info("Killing Session: " + request.getSession().getId());
+ request.getSession().invalidate();
+ }
+}