Search in sources :

Example 1 with DynamicContentType

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");
}
Also used : SharedStringBuilder(org.primefaces.util.SharedStringBuilder) DynamicContentType(org.primefaces.application.resource.DynamicContentType) Resource(javax.faces.application.Resource) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) ResponseWriter(javax.faces.context.ResponseWriter) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Resource (javax.faces.application.Resource)1 ResponseWriter (javax.faces.context.ResponseWriter)1 DynamicContentType (org.primefaces.application.resource.DynamicContentType)1 SharedStringBuilder (org.primefaces.util.SharedStringBuilder)1