public void execute(WebScriptRequest req, WebScriptResponse res) throws IOException {

    final String value = req.getParameter(P_VALUE);
    NodeRef nodeRef = null;
    if (null != value && NodeRef.isNodeRef(value)) {
      nodeRef = new NodeRef(value);
    }

    final String engine = req.getParameter(P_ENGINE);

    String mimetype = req.getParameter(P_MIMETYPE);
    mimetype = null == mimetype ? D_MIMETYPE : mimetype;

    final ByteArrayOutputStream output = new ByteArrayOutputStream();

    if (req.getServerPath().contains("/new")) {

      final String reference = referenceProviderService.getNewReference(engine, null);
      generateBarcode(reference, output, mimetype);

    } else {

      String barcodeValue = value;

      if (null != nodeRef) {
        barcodeValue = referenceProviderService.getExistingReference(nodeRef);
      }

      if (null != value && !value.isEmpty()) {
        generateBarcode(barcodeValue, output, mimetype);
      } else {
        logger.debug(String.format("No barcode generated for value '%s'", value));
      }
    }

    res.setContentType(mimetype);
    // res.setContentEncoding(reader.getEncoding());
    res.setHeader("Content-Length", Long.toString(output.size()));

    // get the content and stream directly to the response output stream
    // assuming the repository is capable of streaming in chunks, this should allow large files
    // to be streamed directly to the browser response stream.
    try {
      res.getOutputStream().write(output.toByteArray());
    } catch (SocketException e1) {
      // the client cut the connection - our mission was accomplished apart from a little error
      // message
      if (logger.isInfoEnabled()) logger.info("Client aborted stream read.");
    } catch (ContentIOException e2) {
      if (logger.isInfoEnabled()) logger.info("Client aborted stream read.");
    }
  }