in connectors/elasticsearch/connector/src/main/java/org/apache/manifoldcf/agents/output/elasticsearch/ElasticSearchIndex.java [151:293]
public void writeTo(OutputStream out)
throws IOException {
PrintWriter pw = new PrintWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8));
try
{
pw.print("{");
Iterator<String> i = document.getFields();
boolean needComma = false;
while (i.hasNext()){
String fieldName = i.next();
Date[] dateFieldValues = document.getFieldAsDates(fieldName);
if (dateFieldValues != null)
{
needComma = writeField(pw, needComma, fieldName, dateFieldValues);
}
else
{
String[] fieldValues = document.getFieldAsStrings(fieldName);
needComma = writeField(pw, needComma, fieldName, fieldValues);
}
}
// Standard document fields
if (fullDocumentURI != null && fullUriAttributeName != null && fullUriAttributeName.length() > 0) {
needComma = writeField(pw, needComma, fullUriAttributeName, new String[]{fullDocumentURI});
}
final Date createdDate = document.getCreatedDate();
if (createdDate != null && createdDateAttributeName != null && createdDateAttributeName.length() > 0)
{
needComma = writeField(pw, needComma, createdDateAttributeName, new Date[]{createdDate});
}
final Date modifiedDate = document.getModifiedDate();
if (modifiedDate != null && modifiedDateAttributeName != null && modifiedDateAttributeName.length() > 0)
{
needComma = writeField(pw, needComma, modifiedDateAttributeName, new Date[]{modifiedDate});
}
final Date indexingDate = document.getIndexingDate();
if (indexingDate != null && indexingDateAttributeName != null && indexingDateAttributeName.length() > 0)
{
needComma = writeField(pw, needComma, indexingDateAttributeName, new Date[]{indexingDate});
}
final String mimeType = document.getMimeType();
if (mimeType != null && mimeTypeAttributeName != null && mimeTypeAttributeName.length() > 0)
{
needComma = writeField(pw, needComma, mimeTypeAttributeName, new String[]{mimeType});
}
needComma = writeACLs(pw, needComma, "document", acls, denyAcls);
needComma = writeACLs(pw, needComma, "share", shareAcls, shareDenyAcls);
needComma = writeACLs(pw, needComma, "parent", parentAcls, parentDenyAcls);
if (useIngesterAttachment && inputStream != null) {
if (contentAttributeName != null)
{
if (needComma) {
pw.print(",");
}
String contentType = document.getMimeType();
if (contentType != null)
pw.print("\"_content_type\" : "+jsonStringEscape(contentType)+",");
String fileName = document.getFileName();
if (fileName != null)
pw.print("\"_name\" : "+jsonStringEscape(fileName)+",");
pw.append(jsonStringEscape(contentAttributeName)).append(" : \"");
Base64 base64 = new Base64();
base64.encodeStream(inputStream, pw);
pw.print("\"");
needComma = true;
}
}
if (useMapperAttachments && inputStream != null) {
if(needComma){
pw.print(",");
}
// I'm told this is not necessary: see CONNECTORS-690
//pw.print("\"type\" : \"attachment\",");
pw.print("\"file\" : {");
String contentType = document.getMimeType();
if (contentType != null)
pw.print("\"_content_type\" : "+jsonStringEscape(contentType)+",");
String fileName = document.getFileName();
if (fileName != null)
pw.print("\"_name\" : "+jsonStringEscape(fileName)+",");
// Since ES 1.0
pw.print(" \"_content\" : \"");
Base64 base64 = new Base64();
base64.encodeStream(inputStream, pw);
pw.print("\"}");
needComma = true;
}
if (!useIngesterAttachment && !useMapperAttachments && inputStream != null) {
if (contentAttributeName != null)
{
Reader r = new InputStreamReader(inputStream, Consts.UTF_8);
if (needComma) {
pw.print(",");
}
pw.append(jsonStringEscape(contentAttributeName)).append(" : \"");
char[] buffer = new char[65536];
while (true)
{
int amt = r.read(buffer,0,buffer.length);
if (amt == -1)
break;
for (int j = 0; j < amt; j++) {
final char x = buffer[j];
if (x == '\n')
pw.append('\\').append('n');
else if (x == '\r')
pw.append('\\').append('r');
else if (x == '\t')
pw.append('\\').append('t');
else if (x == '\b')
pw.append('\\').append('b');
else if (x == '\f')
pw.append('\\').append('f');
else if (x < 32)
{
pw.append("\\u").append(String.format(Locale.ROOT, "%04x", (int)x));
}
else
{
if (x == '\"' || x == '\\' || x == '/')
pw.append('\\');
pw.append(x);
}
}
}
pw.append("\"");
needComma = true;
}
}
pw.print("}");
} catch (ManifoldCFException e)
{
throw new IOException(e.getMessage());
} finally
{
pw.flush();
IOUtils.closeQuietly(pw);
}
}