use of org.primefaces.application.resource.DynamicContentType in project primefaces by primefaces.
the class BarcodeRenderer method encodeEnd.
@Override
public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
ResponseWriter writer = context.getResponseWriter();
Barcode barcode = (Barcode) component;
String clientId = barcode.getClientId(context);
String styleClass = barcode.getStyleClass();
String src = null;
Object value = barcode.getValue();
String type = barcode.getType();
DynamicContentType dynamicContentType = "qr".equals(type) ? DynamicContentType.QR_CODE : DynamicContentType.BARCODE;
if (value == null) {
return;
}
try {
Resource resource = context.getApplication().getResourceHandler().createResource("dynamiccontent.properties", "primefaces", "image/png");
String resourcePath = resource.getRequestPath();
String sessionKey = UUID.randomUUID().toString();
Map<String, Object> session = context.getExternalContext().getSessionMap();
Map<String, String> barcodeMapping = (Map) session.get(Constants.BARCODE_MAPPING);
if (barcodeMapping == null) {
barcodeMapping = new HashMap<>();
session.put(Constants.BARCODE_MAPPING, barcodeMapping);
}
barcodeMapping.put(sessionKey, (String) value);
StringBuilder builder = SharedStringBuilder.get(context, SB_BUILD);
src = builder.append(resourcePath).append("&").append(Constants.DYNAMIC_CONTENT_PARAM).append("=").append(URLEncoder.encode(sessionKey, "UTF-8")).append("&").append(Constants.DYNAMIC_CONTENT_TYPE_PARAM).append("=").append(dynamicContentType.toString()).append("&gen=").append(type).append("&fmt=").append(barcode.getFormat()).append("&qrec=").append(barcode.getQrErrorCorrection()).append("&hrp=").append(barcode.getHrp()).append("&").append(Constants.DYNAMIC_CONTENT_CACHE_PARAM).append("=").append(barcode.isCache()).append("&ori=").append(barcode.getOrientation()).toString();
} catch (UnsupportedEncodingException ex) {
throw new IOException(ex);
}
writer.startElement("img", barcode);
if (shouldWriteId(component)) {
writer.writeAttribute("id", clientId, "id");
}
if (styleClass != null) {
writer.writeAttribute("class", styleClass, "styleClass");
}
writer.writeAttribute("src", context.getExternalContext().encodeResourceURL(src), null);
renderPassThruAttributes(context, barcode, HTML.IMG_ATTRS);
writer.endElement("img");
}
Aggregations