protected Blob convert(Blob blob) throws IOException { String mimetype = blob.getMimeType(); if (mimetype == null || !mimetype.startsWith("text/")) { return blob; } String encoding = blob.getEncoding(); if (encoding != null && "UTF-8".equals(encoding)) { return blob; } InputStream in = new BufferedInputStream(blob.getStream()); String filename = blob.getFilename(); if (encoding == null || encoding.length() == 0) { encoding = detectEncoding(in); } Blob newBlob; if ("UTF-8".equals(encoding)) { newBlob = new InputStreamBlob(in); } else { String content = IOUtils.toString(in, encoding); newBlob = new StringBlob(content); } newBlob.setMimeType(mimetype); newBlob.setEncoding("UTF-8"); newBlob.setFilename(filename); return newBlob; }
protected static String guessEncoding(Blob blob) throws IOException { // encoding already known? if (blob.getEncoding() != null) { return null; } // bad mime type? String mimeType = blob.getMimeType(); if (mimeType == null) { return null; } if (!mimeType.startsWith("text/") && !mimeType.startsWith("application/xhtml")) { // not a text file, we shouldn't be in the Note importer return null; } byte[] bytes = blob.getByteArray(); List<String> charsets = Arrays.asList("utf-8", "iso-8859-1"); // charset specified in MIME type? String CSEQ = "charset="; int i = mimeType.indexOf(CSEQ); if (i > 0) { String onlyMimeType = mimeType.substring(0, i).replace(";", "").trim(); blob.setMimeType(onlyMimeType); String charset = mimeType.substring(i + CSEQ.length()); i = charset.indexOf(";"); if (i > 0) { charset = charset.substring(0, i); } charset = charset.trim().replace("\"", ""); charsets = new ArrayList<String>(charsets); charsets.add(0, charset); } // resort to auto-detection for (String charset : charsets) { try { Charset cs = Charset.forName(charset); CharsetDecoder d = cs.newDecoder() .onMalformedInput(CodingErrorAction.REPORT) .onUnmappableCharacter(CodingErrorAction.REPORT); CharBuffer cb = d.decode(ByteBuffer.wrap(bytes)); return cb.toString(); } catch (IllegalArgumentException e) { // illegal charset } catch (CharacterCodingException e) { // could not decode } } // nothing worked, use platform return null; }
protected String getContentTypeHeader(Blob blob) { String contentType = blob.getMimeType(); String encoding = blob.getEncoding(); if (contentType != null && !StringUtils.isBlank(encoding)) { int i = contentType.indexOf(';'); if (i >= 0) { contentType = contentType.substring(0, i); } contentType += "; charset=" + encoding; } return contentType; }
protected void setValueBlob(T state, Blob blob) throws PropertyException { BlobInfo blobInfo = new BlobInfo(); if (blob != null) { BlobManager blobManager = Framework.getService(BlobManager.class); try { blobInfo.key = blobManager.writeBlob(blob, this); } catch (IOException e) { throw new PropertyException("Cannot get blob info for: " + blob, e); } blobInfo.filename = blob.getFilename(); blobInfo.mimeType = blob.getMimeType(); blobInfo.encoding = blob.getEncoding(); blobInfo.digest = blob.getDigest(); blobInfo.length = blob.getLength() == -1 ? null : Long.valueOf(blob.getLength()); } setBlobInfo(state, blobInfo); }
/** * Checks if the {@code inputBlob} string contains a {@code charset} meta tag. If not, add it. * * @param inputBlob the input blob * @throws IOException Signals that an I/O exception has occurred. */ protected Blob checkCharsetMeta(Blob inputBlob) throws IOException { String charset = inputBlob.getEncoding(); if (!StringUtils.isEmpty(charset)) { Pattern charsetMetaPattern = Pattern.compile(String.format("content=\"text/html;\\s*charset=%s\"", charset)); Matcher charsetMetaMatcher = charsetMetaPattern.matcher(inputBlob.getString()); if (!charsetMetaMatcher.find()) { String charsetMetaTag = String.format( "<META http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">", charset); StringBuilder sb = new StringBuilder(charsetMetaTag); sb.append(new String(inputBlob.getByteArray(), charset)); Blob blobWithCharsetMetaTag = Blobs.createBlob(sb.toString(), "text/html", charset, inputBlob.getFilename()); return blobWithCharsetMetaTag; } } return inputBlob; }
protected void writeBlobProperty(JsonGenerator jg, Property prop) throws IOException { Blob blob = (Blob) prop.getValue(); if (blob == null) { jg.writeNull(); return; } jg.writeStartObject(); String v = blob.getFilename(); if (v == null) { jg.writeNullField("name"); } else { jg.writeStringField("name", v); } v = blob.getMimeType(); if (v == null) { jg.writeNullField("mime-type"); } else { jg.writeStringField("mime-type", v); } v = blob.getEncoding(); if (v == null) { jg.writeNullField("encoding"); } else { jg.writeStringField("encoding", v); } v = blob.getDigest(); if (v == null) { jg.writeNullField("digest"); } else { jg.writeStringField("digest", v); } jg.writeStringField("length", Long.toString(blob.getLength())); String blobUrl = getBlobUrl(prop); if (blobUrl == null) { blobUrl = ""; } jg.writeStringField("data", blobUrl); jg.writeEndObject(); }
@Override public TemplateModel get(String name) throws TemplateModelException { try { if (blob != null) { if (keys[0].equals(name)) { return new SimpleScalar(blob.getFilename()); } else if (keys[1].equals(name)) { return new SimpleScalar(blob.getString()); } else if (keys[2].equals(name)) { return new SimpleNumber(blob.getLength()); } else if (keys[3].equals(name)) { return new SimpleScalar(blob.getMimeType()); } else if (keys[4].equals(name)) { return new SimpleScalar(blob.getEncoding()); } else if (keys[5].equals(name)) { return new SimpleScalar(blob.getDigest()); } } return NOTHING; } catch (IOException e) { throw new TemplateModelException(e); } }
@Override public TemplateCollectionModel values() throws TemplateModelException { try { List<Object> list = new ArrayList<Object>(keys.length); if (blob != null) { list.add(blob.getFilename()); list.add(blob.getString()); list.add(blob.getLength()); list.add(blob.getMimeType()); list.add(blob.getEncoding()); list.add(blob.getDigest()); } else { list.add(""); list.add(""); list.add(""); list.add(""); list.add(""); list.add(""); } return (TemplateCollectionModel) wrapper.wrap(list); } catch (IOException e) { throw new TemplateModelException("Failed to adapt complex property values", e); } }