BrightSide Workbench Full Report + Source Code
SignedReport.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.zkoss.print;
19 
20 import com.lowagie.text.DocumentException;
21 import com.lowagie.text.Rectangle;
22 import com.lowagie.text.pdf.PdfReader;
23 import com.lowagie.text.pdf.PdfSignatureAppearance;
24 import com.lowagie.text.pdf.PdfStamper;
25 import java.io.ByteArrayOutputStream;
26 import java.io.IOException;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
29 import org.turro.elephant.context.Application;
30 import org.turro.elephant.context.ElephantContext;
31 import org.turro.elephant.impl.context.ContextFactory;
32 import org.turro.elephant.impl.security.CertificateStore;
33 import org.zkoss.util.media.AMedia;
34 import org.zkoss.util.media.Media;
35 import org.zkoss.zkex.zul.Jasperreport;
36 
41 public class SignedReport extends Jasperreport {
42 
43  private String certificateStore;
44 
45  public String getCertificateStore() {
46  return certificateStore;
47  }
48 
49  public void setCertificateStore(String certificateStore) {
50  this.certificateStore = certificateStore;
51  }
52 
53  @Override
54  public Object getExtraCtrl() {
55  return new SignCtrl();
56  }
57 
58  protected class SignCtrl extends Jasperreport.ExtraCtrl {
59  @Override
60  public Media getMedia(String pathInfo) {
61  try {
62  CertificateStore cs = "pdf".equals(getType()) ?
64  Application.getApplication().getConstructor(), "ICertificateStore_" + certificateStore) : null;
65  if(cs != null) {
66  Media media = super.getMedia(pathInfo);
67  PdfReader in = new PdfReader(media.getStreamData());
68  ByteArrayOutputStream out = new ByteArrayOutputStream();
69  PdfStamper stp = PdfStamper.createSignature(in, out, '\0');
70  PdfSignatureAppearance sap = stp.getSignatureAppearance();
71  sap.setCrypto(cs.getKey(), cs.getChain(), null, PdfSignatureAppearance.WINCER_SIGNED);
72  sap.setReason((String) cs.getAttributes().get("reason"));
73  sap.setLocation((String) cs.getAttributes().get("location"));
74  if("true".equals((String) cs.getAttributes().get("visible"))) {
75  sap.setVisibleSignature(new Rectangle(
76  cs.getLongAttribute("ltx"),
77  cs.getLongAttribute("lty"),
78  cs.getLongAttribute("rbx"),
79  cs.getLongAttribute("rby")
80  ), 1, null);
81  }
82  stp.close();
83  return new AMedia(media.getName(), media.getFormat(), media.getContentType(), out.toByteArray());
84  //Events.postEvent(new URIEvent("", SignedReport.this, null));
85  //return new AMedia(media.getName(), media.getFormat(), "application/octet-stream", out.toByteArray());
86  } else {
87  return super.getMedia(pathInfo);
88  //Events.postEvent(new Event(Events.ON_URI_CHANGE, SignedReport.this));
89  //Media media = super.getMedia(pathInfo);
90  //return new AMedia(media.getName(), media.getFormat(), "application/octet-stream", media.getStreamData());
91  }
92  } catch (DocumentException | IOException ex) {
93  Logger.getLogger(SignedReport.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
94  }
95  return null;
96  }
97  }
98 
99 }
static Object getImplementation(IElement iel, String name)
void setCertificateStore(String certificateStore)