| 1 |
// |
| 2 |
// ======================================================================== |
| 3 |
// Copyright (c) 1995-2022 Mort Bay Consulting Pty Ltd and others. |
| 4 |
// ------------------------------------------------------------------------ |
| 5 |
// All rights reserved. This program and the accompanying materials |
| 6 |
// are made available under the terms of the Eclipse Public License v1.0 |
| 7 |
// and Apache License v2.0 which accompanies this distribution. |
| 8 |
// |
| 9 |
// The Eclipse Public License is available at |
| 10 |
// http://www.eclipse.org/legal/epl-v10.html |
| 11 |
// |
| 12 |
// The Apache License v2.0 is available at |
| 13 |
// http://www.opensource.org/licenses/apache2.0.php |
| 14 |
// |
| 15 |
// You may elect to redistribute this code under either of these licenses. |
| 16 |
// ======================================================================== |
| 17 |
// |
| 18 |
|
| 19 |
package org.eclipse.jetty.nosql.mongodb; |
| 20 |
|
| 21 |
import java.io.ByteArrayInputStream; |
| 22 |
import java.io.ByteArrayOutputStream; |
| 23 |
import java.io.IOException; |
| 24 |
import java.io.ObjectOutputStream; |
| 25 |
import java.util.Date; |
| 26 |
import java.util.HashMap; |
| 27 |
import java.util.Map; |
| 28 |
|
| 29 |
import com.mongodb.BasicDBObject; |
| 30 |
import com.mongodb.DBObject; |
| 31 |
import org.eclipse.jetty.util.ClassLoadingObjectInputStream; |
| 32 |
import org.eclipse.jetty.util.URIUtil; |
| 33 |
|
| 34 |
/** |
| 35 |
* MongoUtils |
| 36 |
* |
| 37 |
* Some utility methods for manipulating mongo data. This class facilitates testing. |
| 38 |
*/ |
| 39 |
public class MongoUtils |
| 40 |
{ |
| 41 |
|
| 42 |
public static Object decodeValue(final Object valueToDecode) throws IOException, ClassNotFoundException |
| 43 |
{ |
| 44 |
if (valueToDecode == null || valueToDecode instanceof Number || valueToDecode instanceof String || valueToDecode instanceof Boolean || valueToDecode instanceof Date) |
| 45 |
{ |
| 46 |
return valueToDecode; |
| 47 |
} |
| 48 |
else if (valueToDecode instanceof byte[]) |
| 49 |
{ |
| 50 |
final byte[] decodeObject = (byte[])valueToDecode; |
| 51 |
final ByteArrayInputStream bais = new ByteArrayInputStream(decodeObject); |
| 52 |
final ClassLoadingObjectInputStream objectInputStream = new ClassLoadingObjectInputStream(bais); |
| 53 |
return objectInputStream.readUnshared(); |
| 54 |
} |
| 55 |
else if (valueToDecode instanceof DBObject) |
| 56 |
{ |
| 57 |
Map<String, Object> map = new HashMap<String, Object>(); |
| 58 |
for (String name : ((DBObject)valueToDecode).keySet()) |
| 59 |
{ |
| 60 |
String attr = decodeName(name); |
| 61 |
map.put(attr, decodeValue(((DBObject)valueToDecode).get(name))); |
| 62 |
} |
| 63 |
return map; |
| 64 |
} |
| 65 |
else |
| 66 |
{ |
| 67 |
throw new IllegalStateException(valueToDecode.getClass().toString()); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
public static String decodeName(String name) |
| 72 |
{ |
| 73 |
return URIUtil.decodeSpecific(name, ".%"); |
| 74 |
} |
| 75 |
|
| 76 |
public static String encodeName(String name) |
| 77 |
{ |
| 78 |
return URIUtil.encodeSpecific(name, ".%"); |
| 79 |
} |
| 80 |
|
| 81 |
public static Object encodeName(Object value) throws IOException |
| 82 |
{ |
| 83 |
if (value instanceof Number || value instanceof String || value instanceof Boolean || value instanceof Date) |
| 84 |
{ |
| 85 |
return value; |
| 86 |
} |
| 87 |
else if (value.getClass().equals(HashMap.class)) |
| 88 |
{ |
| 89 |
BasicDBObject o = new BasicDBObject(); |
| 90 |
for (Map.Entry<?, ?> entry : ((Map<?, ?>)value).entrySet()) |
| 91 |
{ |
| 92 |
if (!(entry.getKey() instanceof String)) |
| 93 |
{ |
| 94 |
o = null; |
| 95 |
break; |
| 96 |
} |
| 97 |
o.append(encodeName(entry.getKey().toString()), encodeName(entry.getValue())); |
| 98 |
} |
| 99 |
|
| 100 |
if (o != null) |
| 101 |
return o; |
| 102 |
} |
| 103 |
|
| 104 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
| 105 |
ObjectOutputStream out = new ObjectOutputStream(bout); |
| 106 |
out.reset(); |
| 107 |
out.writeUnshared(value); |
| 108 |
out.flush(); |
| 109 |
return bout.toByteArray(); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Dig through a given dbObject for the nested value |
| 114 |
* |
| 115 |
* @param dbObject the mongo object to search |
| 116 |
* @param nestedKey the field key to find |
| 117 |
* @return the value of the field key |
| 118 |
*/ |
| 119 |
public static Object getNestedValue(DBObject dbObject, String nestedKey) |
| 120 |
{ |
| 121 |
String[] keyChain = nestedKey.split("\\."); |
| 122 |
|
| 123 |
DBObject temp = dbObject; |
| 124 |
|
| 125 |
for (int i = 0; i < keyChain.length - 1; ++i) |
| 126 |
{ |
| 127 |
temp = (DBObject)temp.get(keyChain[i]); |
| 128 |
|
| 129 |
if (temp == null) |
| 130 |
{ |
| 131 |
return null; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
return temp.get(keyChain[keyChain.length - 1]); |
| 136 |
} |
| 137 |
} |