Debian Codegraph @

jetty-integrations/jetty-nosql/src/main/java/org/eclipse/jetty/nosql/mongodb/MongoUtils.java

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