forked from rnewson/couchdb-lucene
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Utils.java
41 lines (30 loc) · 1.3 KB
/
Utils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package org.apache.couchdb.lucene;
import net.sf.json.JSONObject;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Store;
class Utils {
public static void log(final String fmt, final Object... args) {
final String msg = String.format(fmt, args);
System.out.printf("{\"log\":\"%s\"}\n", msg);
}
public static String throwableToJSON(final Throwable t) {
return error(t.getMessage() == null ? "Unknown error" : String.format("%s: %s", t.getClass(), t.getMessage()));
}
public static String error(final String txt) {
return error(500, txt);
}
public static String digest(final String data) {
return DigestUtils.md5Hex(data);
}
public static String error(final int code, final String txt) {
return new JSONObject().element("code", code).element("body", StringEscapeUtils.escapeHtml(txt)).toString();
}
public static Field text(final String name, final String value, final boolean store) {
return new Field(name, value, store ? Store.YES : Store.NO, Field.Index.ANALYZED);
}
public static Field token(final String name, final String value, final boolean store) {
return new Field(name, value, store ? Store.YES : Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS);
}
}