|
| 1 | +/* |
| 2 | + * Licensed to Elastic Search and Shay Banon under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. Elastic Search licenses this |
| 6 | + * file to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.river.mongodb.util; |
| 21 | + |
| 22 | +import java.io.ByteArrayOutputStream; |
| 23 | +import java.io.IOException; |
| 24 | +import java.io.InputStream; |
| 25 | +import java.util.Arrays; |
| 26 | +import java.util.HashSet; |
| 27 | +import java.util.Set; |
| 28 | + |
| 29 | +import org.elasticsearch.common.Base64; |
| 30 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 31 | +import org.elasticsearch.common.xcontent.XContentFactory; |
| 32 | + |
| 33 | +import com.mongodb.DBObject; |
| 34 | +import com.mongodb.gridfs.GridFSDBFile; |
| 35 | + |
| 36 | +/* |
| 37 | + * GridFS Helper class |
| 38 | + */ |
| 39 | +public abstract class MongoDBHelper { |
| 40 | + |
| 41 | + public static XContentBuilder serialize(GridFSDBFile file) |
| 42 | + throws IOException { |
| 43 | + |
| 44 | + XContentBuilder builder = XContentFactory.jsonBuilder(); |
| 45 | + |
| 46 | + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); |
| 47 | + |
| 48 | + int nRead; |
| 49 | + byte[] data = new byte[1024]; |
| 50 | + |
| 51 | + InputStream stream = file.getInputStream(); |
| 52 | + while ((nRead = stream.read(data, 0, data.length)) != -1) { |
| 53 | + buffer.write(data, 0, nRead); |
| 54 | + } |
| 55 | + |
| 56 | + buffer.flush(); |
| 57 | + stream.close(); |
| 58 | + |
| 59 | + String encodedContent = Base64.encodeBytes(buffer.toByteArray()); |
| 60 | + |
| 61 | + // Probably not necessary... |
| 62 | + buffer.close(); |
| 63 | + |
| 64 | + builder.startObject(); |
| 65 | + builder.startObject("content"); |
| 66 | + builder.field("content_type", file.getContentType()); |
| 67 | + builder.field("title", file.getFilename()); |
| 68 | + builder.field("content", encodedContent); |
| 69 | + builder.endObject(); |
| 70 | + builder.field("filename", file.getFilename()); |
| 71 | + builder.field("contentType", file.getContentType()); |
| 72 | + builder.field("md5", file.getMD5()); |
| 73 | + builder.field("length", file.getLength()); |
| 74 | + builder.field("chunkSize", file.getChunkSize()); |
| 75 | + builder.field("uploadDate", file.getUploadDate()); |
| 76 | + builder.startObject("metadata"); |
| 77 | + DBObject metadata = file.getMetaData(); |
| 78 | + if (metadata != null) { |
| 79 | + for (String key : metadata.keySet()) { |
| 80 | + builder.field(key, metadata.get(key)); |
| 81 | + } |
| 82 | + } |
| 83 | + builder.endObject(); |
| 84 | + builder.endObject(); |
| 85 | + |
| 86 | + return builder; |
| 87 | + } |
| 88 | + |
| 89 | + public static DBObject applyExcludeFields(DBObject bsonObject, |
| 90 | + Set<String> excludeFields) { |
| 91 | + if (excludeFields == null) { |
| 92 | + return bsonObject; |
| 93 | + } |
| 94 | + |
| 95 | + DBObject filteredObject = bsonObject; |
| 96 | + for (String field : excludeFields) { |
| 97 | + if (field.contains(".")) { |
| 98 | + String rootObject = field.substring(0, field.indexOf(".")); |
| 99 | + String childObject = field.substring(field.indexOf(".") + 1); |
| 100 | + if (filteredObject.containsField(rootObject)) { |
| 101 | + Object object = filteredObject.get(rootObject); |
| 102 | + if (object instanceof DBObject) { |
| 103 | + DBObject object2 = (DBObject) object; |
| 104 | + object2 = applyExcludeFields(object2, |
| 105 | + new HashSet<String>(Arrays.asList(childObject))); |
| 106 | + } |
| 107 | + } |
| 108 | + } else { |
| 109 | + if (filteredObject.containsField(field)) { |
| 110 | + filteredObject.removeField(field); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + return filteredObject; |
| 115 | + } |
| 116 | + |
| 117 | +} |
0 commit comments