1 13 14 package org.ejbca.core.model.hardtoken.profiles; 15 16 17 import java.awt.image.BufferedImage ; 18 import java.awt.image.RenderedImage ; 19 import java.awt.print.Printable ; 20 import java.awt.print.PrinterException ; 21 import java.io.ByteArrayOutputStream ; 22 import java.io.FileNotFoundException ; 23 import java.io.IOException ; 24 import java.io.Reader ; 25 import java.text.DateFormat ; 26 import java.util.ArrayList ; 27 import java.util.Collection ; 28 import java.util.Date ; 29 import java.util.Iterator ; 30 import java.util.regex.Pattern ; 31 32 import org.apache.batik.dom.GenericText; 33 import org.apache.batik.dom.svg.SAXSVGDocumentFactory; 34 import org.apache.batik.dom.svg.SVGDOMImplementation; 35 import org.apache.batik.dom.svg.SVGOMDocument; 36 import org.apache.batik.dom.svg.SVGOMImageElement; 37 import org.apache.batik.dom.svg.SVGOMTSpanElement; 38 import org.apache.batik.svggen.ImageHandlerBase64Encoder; 39 import org.apache.batik.svggen.SVGGeneratorContext; 40 import org.apache.batik.svggen.SimpleImageHandler; 41 import org.apache.batik.transcoder.TranscoderInput; 42 import org.apache.batik.transcoder.TranscoderOutput; 43 import org.apache.batik.transcoder.print.PrintTranscoder; 44 import org.apache.batik.util.XMLResourceDescriptor; 45 import org.apache.commons.lang.StringUtils; 46 import org.apache.log4j.Logger; 47 import org.ejbca.core.model.ra.UserDataVO; 48 import org.ejbca.util.dn.DNFieldExtractor; 49 import org.w3c.dom.Document ; 50 import org.w3c.dom.Node ; 51 import org.w3c.dom.NodeList ; 52 import org.w3c.dom.svg.SVGDocument; 53 import org.w3c.dom.svg.SVGRectElement; 54 import org.w3c.dom.svg.SVGTextElement; 55 56 57 58 59 60 67 public class SVGImageManipulator { 68 71 private static final Logger log = Logger.getLogger(SVGImageManipulator.class); 72 73 77 private static final Pattern USERNAME = Pattern.compile("\\$USERNAME", Pattern.CASE_INSENSITIVE); 78 private static final Pattern UID = Pattern.compile("\\$UID", Pattern.CASE_INSENSITIVE); 79 private static final Pattern CN = Pattern.compile("\\$CN", Pattern.CASE_INSENSITIVE); 80 private static final Pattern SN = Pattern.compile("\\$SN", Pattern.CASE_INSENSITIVE); 81 private static final Pattern GIVENNAME= Pattern.compile("\\$GIVENNAME", Pattern.CASE_INSENSITIVE); 82 private static final Pattern INITIALS = Pattern.compile("\\$INITIALS", Pattern.CASE_INSENSITIVE); 83 private static final Pattern SURNAME = Pattern.compile("\\$SURNAME", Pattern.CASE_INSENSITIVE); 84 private static final Pattern O = Pattern.compile("\\$O", Pattern.CASE_INSENSITIVE); 85 private static final Pattern OU = Pattern.compile("\\$OU", Pattern.CASE_INSENSITIVE); 86 private static final Pattern C = Pattern.compile("\\$C", Pattern.CASE_INSENSITIVE); 87 private static final Pattern LOCATION = Pattern.compile("\\$LOCATION", Pattern.CASE_INSENSITIVE); 88 private static final Pattern TITLE = Pattern.compile("\\$TITLE", Pattern.CASE_INSENSITIVE); 89 90 93 private static final Pattern STARTDATE = Pattern.compile("\\$STARTDATE", Pattern.CASE_INSENSITIVE); 94 97 private static final Pattern ENDDATE = Pattern.compile("\\$ENDDATE", Pattern.CASE_INSENSITIVE); 98 99 private static final Pattern HARDTOKENSN = Pattern.compile("\\$HARDTOKENSN", Pattern.CASE_INSENSITIVE); 100 101 private static final Pattern HARDTOKENSNWITHOUTPREFIX = Pattern.compile("\\$HARDTOKENSNWITHOUTPREFIX", Pattern.CASE_INSENSITIVE); 102 103 106 private static final Pattern [] PINS; 107 private static final Pattern [] PUKS; 108 109 112 120 static { 121 PINS = new Pattern [0x50]; 122 PUKS = new Pattern [PINS.length]; 123 for (int i=0; i<PINS.length; i++) { 124 final int pinNr = i+1; 125 PINS[i] = Pattern.compile("\\$PIN"+pinNr, Pattern.CASE_INSENSITIVE); 126 PUKS[i] = Pattern.compile("\\$PUK"+pinNr, Pattern.CASE_INSENSITIVE); 127 } 128 } 129 130 143 144 public SVGImageManipulator(Reader svgdata, 145 int validity, 146 String hardtokensnprefix) throws IOException { 147 this.validityms = ( ((long)validity) * 1000 * 3600 * 24); this.hardtokensnprefix = hardtokensnprefix; 149 150 String parser = XMLResourceDescriptor.getXMLParserClassName(); 151 SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser); 152 String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI; 153 Document doc = f.createDocument(svgNS, svgdata); 154 155 svgdoc = ((SVGOMDocument) doc); 156 } 157 158 165 public Printable print(UserDataVO userdata, 166 String [] pincodes, String [] pukcodes, 167 String hardtokensn, String copyoftokensn) throws IOException , PrinterException { 168 DNFieldExtractor dnfields = new DNFieldExtractor(userdata.getDN(), DNFieldExtractor.TYPE_SUBJECTDN); 170 Date currenttime = new Date (); 172 String startdate = DateFormat.getDateInstance(DateFormat.SHORT).format(currenttime); 173 174 String enddate = DateFormat.getDateInstance(DateFormat.SHORT).format(new Date (currenttime.getTime() + (this.validityms))); 175 String hardtokensnwithoutprefix = hardtokensn.substring(this.hardtokensnprefix.length()); 176 String copyoftokensnwithoutprefix = copyoftokensn.substring(this.hardtokensnprefix.length()); 177 178 final SVGOMDocument clone = (SVGOMDocument)svgdoc.cloneNode(true); 179 process( "text", userdata, dnfields, pincodes, pukcodes, 181 hardtokensn, hardtokensnwithoutprefix, 182 copyoftokensn, copyoftokensnwithoutprefix, 183 startdate, enddate, clone); 184 process( "svg:text", userdata, dnfields, pincodes, pukcodes, 185 hardtokensn, hardtokensnwithoutprefix, 186 copyoftokensn, copyoftokensnwithoutprefix, 187 startdate, enddate, clone); 188 189 195 insertImage(userdata, clone); 197 PrintTranscoder t = new PrintTranscoder(); 198 TranscoderInput input = new TranscoderInput(clone); 199 TranscoderOutput output = new TranscoderOutput(new ByteArrayOutputStream ()); 200 t.transcode(input, output); 201 { 202 final String aDoNot = clone.getRootElement().getAttribute("doNotScaleToPage"); 203 t.addTranscodingHint( PrintTranscoder.KEY_SCALE_TO_PAGE, 204 new Boolean (aDoNot==null||aDoNot.trim().length()<=0) ); 205 } 206 return t; 207 } 208 209 private void process(String tagName, UserDataVO userdata, 210 DNFieldExtractor dnfields, String [] pincodes, String [] pukcodes, 211 String hardtokensn, String hardtokensnwithoutprefix, 212 String copyoftokensn, String copyoftokensnwithoutprefix, 213 String startdate, String enddate, SVGDocument clone){ 214 Collection texts = new ArrayList (); 215 NodeList list = clone.getDocumentElement().getElementsByTagName(tagName); 216 int numberofelements = list.getLength(); 217 for(int i=0; i<numberofelements; i++){ 218 Node node = list.item(i); 219 if(node instanceof SVGTextElement){ 220 NodeList list2 = ((SVGTextElement) node).getChildNodes(); 221 int numberofelements2 = list2.getLength(); 222 for(int j=0;j<numberofelements2;j++){ 223 Node node2 = list2.item(j); 224 if(node2 instanceof GenericText) 225 texts.add(node2); 226 if(node2 instanceof SVGOMTSpanElement){ 227 SVGOMTSpanElement tspan = (SVGOMTSpanElement) node2; 228 NodeList list3 = tspan.getChildNodes(); 229 int numberofelements3 = list3.getLength(); 230 for(int k=0;k<numberofelements3;k++){ 231 Node node3 = list3.item(k); 232 if(node3 instanceof GenericText) 233 texts.add(node3); 234 } 235 } 236 } 237 } 238 } 239 Iterator iter = texts.iterator(); 240 String data = ""; 241 while(iter.hasNext()){ 242 GenericText text = (GenericText) iter.next(); 243 data = text.getData(); 244 data = processString(data, userdata, dnfields, pincodes, pukcodes, 245 hardtokensn, hardtokensnwithoutprefix, 246 copyoftokensn, copyoftokensnwithoutprefix, 247 startdate, enddate); 248 text.setData(data); 249 } 250 } 251 252 253 private String processString(String text, UserDataVO userdata, DNFieldExtractor dnfields, 254 String [] pincodes, String [] pukcodes, 255 String hardtokensn, String hardtokensnwithoutprefix, 256 String copyoftokensn, String copyoftokensnwithoutprefix, 257 String startdate, String enddate){ 258 259 260 text = USERNAME.matcher(text).replaceAll(userdata.getUsername()); 261 text = UID.matcher(text).replaceAll(dnfields.getField(DNFieldExtractor.UID, 0)); 262 text = CN.matcher(text).replaceAll(dnfields.getField(DNFieldExtractor.CN, 0)); 263 text = OU.matcher(text).replaceAll(dnfields.getField(DNFieldExtractor.OU, 0)); 264 text = O.matcher(text).replaceAll(dnfields.getField(DNFieldExtractor.O, 0)); 265 text = C.matcher(text).replaceAll(dnfields.getField(DNFieldExtractor.C, 0)); 266 text = LOCATION.matcher(text).replaceAll(dnfields.getField(DNFieldExtractor.L, 0)); 267 text = TITLE.matcher(text).replaceAll(dnfields.getField(DNFieldExtractor.T, 0)); 268 text = INITIALS.matcher(text).replaceAll(dnfields.getField(DNFieldExtractor.INITIALS, 0)); 269 text = SN.matcher(text).replaceAll(dnfields.getField(DNFieldExtractor.SN, 0)); 270 text = SURNAME.matcher(text).replaceAll(dnfields.getField(DNFieldExtractor.SURNAME, 0)); 271 text = GIVENNAME.matcher(text).replaceAll(dnfields.getField(DNFieldExtractor.GIVENNAME, 0)); 272 273 text = STARTDATE.matcher(text).replaceAll(startdate); 274 text = ENDDATE.matcher(text).replaceAll(enddate); 275 text = HARDTOKENSN.matcher(text).replaceAll(hardtokensn); 276 text = HARDTOKENSNWITHOUTPREFIX.matcher(text).replaceAll(hardtokensnwithoutprefix); 277 278 for(int i=Math.min(PINS.length, pincodes.length)-1; i>=0; i--){ 279 text = PINS[i].matcher(text).replaceAll(pincodes[i]); 280 } 281 282 for(int i=Math.min(PUKS.length, pukcodes.length)-1; i>=0; i--){ 283 text = PUKS[i].matcher(text).replaceAll(pukcodes[i]); 284 } 285 286 if (StringUtils.isNotEmpty(copyoftokensn) || StringUtils.isNotEmpty(copyoftokensnwithoutprefix)) { 292 log.debug("copyoftokensn: "+copyoftokensn+" and/or copyoftokensnwithoutprefix: "+copyoftokensnwithoutprefix+" is not used."); 293 } 294 297 298 return text; 299 } 300 301 302 private void insertImage(UserDataVO userdata, SVGOMDocument clone) throws FileNotFoundException , IOException { 304 log.debug(">insertImage("+userdata != null ? userdata.getUsername() : "null"+")"); 305 int imgx = 0; 306 int imgy = 0; 307 int imgwidth = 0; 308 int imgheight = 0; 309 310 String transform = null; 311 NodeList list = clone.getDocumentElement().getElementsByTagName("rect"); 313 int numberofelements = list.getLength(); 314 for(int i=0; i<numberofelements; i++){ 315 Node node = list.item(i); 316 if(node instanceof SVGRectElement){ 317 SVGRectElement rectnode = (SVGRectElement) node; 318 if(rectnode.getId().equalsIgnoreCase("USERPICTURE")){ 319 transform = rectnode.getAttribute("transform"); 320 imgx = (int) rectnode.getX().getBaseVal().getValue(); 321 imgy = (int) rectnode.getY().getBaseVal().getValue(); 322 imgwidth = (int) rectnode.getWidth().getBaseVal().getValue(); 323 imgheight = (int) rectnode.getHeight().getBaseVal().getValue(); 324 325 } 326 } 327 } 328 329 if(imgwidth != 0 && imgheight != 0){ 330 BufferedImage image = null; 334 335 SVGOMImageElement imageelement = new SVGOMImageElement("", clone); 336 SimpleImageHandler imagehandler = new SimpleImageHandler(new ImageHandlerBase64Encoder()); 337 338 SVGGeneratorContext svgcxt = SVGGeneratorContext.createDefault(clone); 339 340 imagehandler.handleImage((RenderedImage ) image, imageelement, 341 imgx, imgy, 342 imgwidth, imgheight, 343 svgcxt); 344 345 if(transform != null && !transform.equals("")) 346 imageelement.setAttribute("transform", transform); 347 348 clone.getRootElement().appendChild(imageelement); 349 } 350 351 } 352 353 final private SVGOMDocument svgdoc; 355 final private long validityms; 356 final private String hardtokensnprefix; 357 358 359 } 360 | Popular Tags |