1 58 package org.krysalis.barcode.servlet; 59 60 import java.awt.image.BufferedImage ; 61 import java.io.ByteArrayOutputStream ; 62 import java.io.IOException ; 63 import javax.servlet.ServletException ; 64 import javax.servlet.http.HttpServlet ; 65 import javax.servlet.http.HttpServletRequest ; 66 import javax.servlet.http.HttpServletResponse ; 67 import javax.xml.transform.Result ; 68 import javax.xml.transform.Source ; 69 import javax.xml.transform.Transformer ; 70 import javax.xml.transform.TransformerFactory ; 71 72 import org.apache.avalon.framework.configuration.Configuration; 73 import org.apache.avalon.framework.configuration.DefaultConfiguration; 74 import org.apache.avalon.framework.logger.ConsoleLogger; 75 import org.apache.avalon.framework.logger.Logger; 76 import org.krysalis.barcode.BarcodeException; 77 import org.krysalis.barcode.BarcodeGenerator; 78 import org.krysalis.barcode.BarcodeUtil; 79 import org.krysalis.barcode.output.bitmap.BitmapCanvasProvider; 80 import org.krysalis.barcode.output.eps.EPSCanvasProvider; 81 import org.krysalis.barcode.output.svg.SVGCanvasProvider; 82 import org.krysalis.barcode.tools.MimeTypes; 83 84 89 public class BarcodeServlet extends HttpServlet { 90 91 92 public static final String BARCODE_MSG = "msg"; 93 94 public static final String BARCODE_TYPE = "type"; 95 96 public static final String BARCODE_HEIGHT = "height"; 97 98 public static final String BARCODE_MODULE_WIDTH = "mw"; 99 100 public static final String BARCODE_WIDE_FACTOR = "wf"; 101 102 public static final String BARCODE_QUIET_ZONE = "qz"; 103 104 public static final String BARCODE_HUMAN_READABLE_POS = "hrp"; 105 106 public static final String BARCODE_FORMAT = "fmt"; 107 108 public static final String BARCODE_IMAGE_RESOLUTION = "res"; 109 110 public static final String BARCODE_IMAGE_GRAYSCALE = "gray"; 111 112 private Logger log = new ConsoleLogger(ConsoleLogger.LEVEL_INFO); 113 114 117 protected void doGet(HttpServletRequest request, HttpServletResponse response) 118 throws ServletException , IOException { 119 120 try { 121 String format = determineFormat(request); 122 123 Configuration cfg = buildCfg(request); 124 125 String msg = request.getParameter(BARCODE_MSG); 126 if (msg == null) msg = "0123456789"; 127 128 BarcodeUtil util = BarcodeUtil.getInstance(); 129 BarcodeGenerator gen = util.createBarcodeGenerator(cfg, log); 130 131 ByteArrayOutputStream bout = new ByteArrayOutputStream (4096); 132 try { 133 if (format.equals(MimeTypes.MIME_SVG)) { 134 SVGCanvasProvider svg = new SVGCanvasProvider(false); 136 try { 137 gen.generateBarcode(svg, msg); 138 } catch (Exception e) { 139 throw new BarcodeException("Error while generating barcode", e); 140 } 141 org.w3c.dom.DocumentFragment frag = svg.getDOMFragment(); 142 143 TransformerFactory factory = TransformerFactory.newInstance(); 145 Transformer trans = factory.newTransformer(); 146 Source src = new javax.xml.transform.dom.DOMSource (frag); 147 Result res = new javax.xml.transform.stream.StreamResult (bout); 148 trans.transform(src, res); 149 } else if (format.equals(MimeTypes.MIME_EPS)) { 150 EPSCanvasProvider eps = new EPSCanvasProvider(bout); 151 gen.generateBarcode(eps, msg); 152 eps.finish(); 153 } else { 154 String resText = request.getParameter(BARCODE_IMAGE_RESOLUTION); 155 int resolution = 300; if (resText != null) { 157 resolution = Integer.parseInt(resText); 158 } 159 if (resolution > 2400) { 160 throw new IllegalArgumentException ( 161 "Resolutions above 2400dpi are not allowed"); 162 } 163 if (resolution < 10) { 164 throw new IllegalArgumentException ( 165 "Minimum resolution must be 10dpi"); 166 } 167 String gray = request.getParameter(BARCODE_IMAGE_GRAYSCALE); 168 BitmapCanvasProvider bitmap = ("true".equalsIgnoreCase(gray) 169 ? new BitmapCanvasProvider( 170 bout, format, resolution, 171 BufferedImage.TYPE_BYTE_GRAY, true) 172 : new BitmapCanvasProvider( 173 bout, format, resolution, 174 BufferedImage.TYPE_BYTE_BINARY, false)); 175 gen.generateBarcode(bitmap, msg); 176 bitmap.finish(); 177 } 178 } finally { 179 bout.close(); 180 } 181 response.setContentType(format); 182 response.setContentLength(bout.size()); 183 response.getOutputStream().write(bout.toByteArray()); 184 response.getOutputStream().flush(); 185 } catch (Exception e) { 186 log.error("Error while generating barcode", e); 187 throw new ServletException (e); 188 } catch (Throwable t) { 189 log.error("Error while generating barcode", t); 190 throw new ServletException (t); 191 } 192 } 193 194 199 protected String determineFormat(HttpServletRequest request) { 200 String format = request.getParameter(BARCODE_FORMAT); 201 format = MimeTypes.expandFormat(format); 202 if (format == null) format = MimeTypes.MIME_SVG; 203 return format; 204 } 205 206 211 protected Configuration buildCfg(HttpServletRequest request) { 212 DefaultConfiguration cfg = new DefaultConfiguration("barcode"); 213 String type = request.getParameter(BARCODE_TYPE); 215 if (type == null) type = "code128"; 216 DefaultConfiguration child = new DefaultConfiguration(type); 217 cfg.addChild(child); 218 DefaultConfiguration attr; 220 String height = request.getParameter(BARCODE_HEIGHT); 221 if (height != null) { 222 attr = new DefaultConfiguration("height"); 223 attr.setValue(height); 224 child.addChild(attr); 225 } 226 String moduleWidth = request.getParameter(BARCODE_MODULE_WIDTH); 227 if (moduleWidth != null) { 228 attr = new DefaultConfiguration("module-width"); 229 attr.setValue(moduleWidth); 230 child.addChild(attr); 231 } 232 String wideFactor = request.getParameter(BARCODE_WIDE_FACTOR); 233 if (wideFactor != null) { 234 attr = new DefaultConfiguration("wide-factor"); 235 attr.setValue(wideFactor); 236 child.addChild(attr); 237 } 238 String quietZone = request.getParameter(BARCODE_QUIET_ZONE); 239 if (quietZone != null) { 240 attr = new DefaultConfiguration("quiet-zone"); 241 if (quietZone.startsWith("disable")) { 242 attr.setAttribute("enabled", "false"); 243 } else { 244 attr.setValue(quietZone); 245 } 246 child.addChild(attr); 247 } 248 String humanReadable = request.getParameter(BARCODE_HUMAN_READABLE_POS); 249 if (humanReadable != null) { 250 attr = new DefaultConfiguration("human-readable"); 251 attr.setValue(humanReadable); 252 child.addChild(attr); 253 } 254 return cfg; 255 } 256 257 } 258 | Popular Tags |