All Implemented Interfaces:
Serializable, Servlet, ServletConfig

public class QRServletextends HttpServlet
This servlet generates QR code images.
See Also:
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      The serialization version of the servlet.
      See Also:
    • INIT_PARAM_VERSION

      private static final String INIT_PARAM_VERSION
      Init parameter naming the image version.
      See Also:
    • INIT_PARAM_CACHE_PROVIDER

      private static final String INIT_PARAM_CACHE_PROVIDER
      Init parameter naming the cache provider class.
      See Also:
    • PARAM_IDENTICON_SIZE_SHORT

      private static final String PARAM_IDENTICON_SIZE_SHORT
      Request parameter for the identicon size.
      See Also:
    • PARAM_IDENTICON_CODE_SHORT

      private static final String PARAM_IDENTICON_CODE_SHORT
      Request parameter for the identicon code.
      See Also:
    • PARAM_IDENTICON_TEXT_SHORT

      private static final String PARAM_IDENTICON_TEXT_SHORT
      Request parameter for the identicon text.
      See Also:
    • PARAM_FORMAT

      private static final String PARAM_FORMAT
      Request parameter for the image format.
      See Also:
    • IDENTICON_IMAGE_FORMAT

      private static final String IDENTICON_IMAGE_FORMAT
      Image format name the writer is asked for.
      See Also:
    • IDENTICON_IMAGE_MIMETYPE

      private static final String IDENTICON_IMAGE_MIMETYPE
      Mime type of the identicon raster image.
      See Also:
    • IDENTICON_SVG_MIMETYPE

      private static final String IDENTICON_SVG_MIMETYPE
      Mime type of the identicon SVG image.
      See Also:
    • DEFAULT_IDENTICON_EXPIRES_IN_MILLIS

      private static final long DEFAULT_IDENTICON_EXPIRES_IN_MILLIS
      Default time before an identicon expires.
      See Also:
    • DEFAULT_FONT_NAME

      private static final String DEFAULT_FONT_NAME
      Font used when the request names none.
    • version

      private int version
      Bumped when the rendered output format changes.
    • cache

      private IdenticonCache cache
      Cache of rendered identicons, keyed by seed.
    • identiconExpiresInMillis

      private long identiconExpiresInMillis
      How long a rendered identicon stays in the cache.
  • Constructor Details

    • QRServlet

      public QRServlet()
      A servlet with no init parameters applied yet; the container calls init() before the first request, so construction alone configures nothing.
  • Method Details

    • acceptsSvg

      private static boolean acceptsSvg(HttpServletRequest req)
      Check if client accepts SVG.
      Parameters:
      req - the request whose Accept header is inspected
      Returns:
      true if the Accept header mentions image/svg+xml
    • bitMatrixToSvg

      private static String bitMatrixToSvg(BitMatrix matrix, String text, String textName)
      Convert QR BitMatrix to SVG markup.
      Parameters:
      matrix - the QR code modules to render
      text - the label to draw under the QR code, may be null
      textName - the label used when text is null or empty
      Returns:
      the complete SVG document markup, with the label text only if a non-empty label was given
    • appendQrPath

      private static int appendQrPath(StringBuilder sb, BitMatrix matrix, int width, int height, int yOffset)
      Append SVG path data for QR modules.
      Parameters:
      sb - the buffer the path data is appended to
      matrix - the QR code modules to scan
      width - the number of columns to scan
      height - the number of rows to scan
      yOffset - rows added to each module's y coordinate
      Returns:
      the largest y coordinate written, for placing the label text
    • escapeXml

      private static String escapeXml(String s)
      Escape XML special characters.
      Parameters:
      s - the text to escape
      Returns:
      s with &, <, >, a double quote and an apostrophe replaced by their XML entities
    • init

      public void init(ServletConfig cfg) throws ServletException
      Load configuration from servlet initialization parameters.
      Specified by:
      init in interface Servlet
      Overrides:
      init in class GenericServlet
      Throws:
      ServletException
    • doGet

      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
      Handle GET request: generate QR code image or SVG.
      Overrides:
      doGet in class HttpServlet
      Throws:
      ServletException
      IOException