SimpleHitWriter.java
4.09 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.RepositoryException
* org.apache.felix.scr.annotations.Component
* org.apache.jackrabbit.util.Text
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceMetadata
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.io.JSONWriter
*/
package com.day.cq.search.writer;
import com.day.cq.search.Query;
import com.day.cq.search.result.Hit;
import com.day.cq.search.writer.ResultHitWriter;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.jcr.RepositoryException;
import org.apache.felix.scr.annotations.Component;
import org.apache.jackrabbit.util.Text;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceMetadata;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
@Component(metatype=0, factory="com.day.cq.search.writer.ResultHitWriter/simple")
public class SimpleHitWriter
implements ResultHitWriter {
public static final long KILO_BYTE = 1024;
public static final long MEGA_BYTE = 0x100000;
public static final long GIGA_BYTE = 0x40000000;
public static final long TERA_BYTE = 0x10000000000L;
public static final long PETA_BYTE = 0x4000000000000L;
@Override
public void write(Hit hit, JSONWriter writer, Query query) throws RepositoryException, JSONException {
SimpleHitWriter.writeSimpleJson(hit, writer);
}
public static void writeSimpleJson(Hit hit, JSONWriter writer) throws RepositoryException, JSONException {
Calendar cal;
long size;
long created;
String mimeType;
Resource resource = hit.getResource();
ResourceMetadata metadata = resource.getResourceMetadata();
ValueMap properties = hit.getProperties();
writer.key("path").value((Object)hit.getPath());
String excerpt = hit.getExcerpt();
if (excerpt != null) {
writer.key("excerpt").value((Object)excerpt);
}
String name = Text.getName((String)hit.getPath());
writer.key("name").value((Object)name);
writer.key("title").value(properties.get("jcr:title", (Object)name));
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long lastModified = metadata.getModificationTime();
if (lastModified < 0) {
Calendar cal2 = (Calendar)properties.get((Object)"cq:lastModified");
if (cal2 == null) {
cal2 = (Calendar)properties.get((Object)"jcr:lastModified");
}
if (cal2 != null) {
lastModified = cal2.getTimeInMillis();
}
}
if (lastModified >= 0) {
writer.key("lastModified").value((Object)format.format(new Date(lastModified)));
}
if ((created = metadata.getCreationTime()) < 0 && (cal = (Calendar)properties.get((Object)"jcr:created")) != null) {
created = cal.getTimeInMillis();
}
if (created >= 0) {
writer.key("created").value((Object)format.format(new Date(created)));
}
if ((size = metadata.getContentLength()) < 0) {
resource.adaptTo(InputStream.class);
size = metadata.getContentLength();
}
if (size >= 0) {
String humanSize = size >= 0x4000000000000L ? "" + size / 0x4000000000000L + " PB" : (size >= 0x10000000000L ? "" + size / 0x10000000000L + " TB" : (size >= 0x40000000 ? "" + size / 0x40000000 + " GB" : (size >= 0x100000 ? "" + size / 0x100000 + " MB" : (size >= 1024 ? "" + size / 1024 + " KB" : "" + size + " Bytes"))));
writer.key("size").value((Object)humanSize);
}
if ((mimeType = metadata.getContentType()) == null) {
mimeType = (String)properties.get((Object)"jcr:mimeType");
}
if (mimeType != null) {
writer.key("mimeType").value((Object)mimeType);
}
}
}