Skip to content

Commit 3db691b

Browse files
author
Vladimir Kotal
authored
use Jackson for JSON serialization of Messages (#2639)
helps #2046
1 parent 4521283 commit 3db691b

File tree

19 files changed

+542
-267
lines changed

19 files changed

+542
-267
lines changed

apiary.apib

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ Besides `/suggester` and `/search` endpoints, everything is accessible from `loc
7575

7676
{
7777
"tags": ["main"],
78-
"cssClass": "cssClass",
78+
"cssClass": "class",
7979
"text":"test message",
8080
"duration":"PT10M"
8181
}
@@ -98,18 +98,17 @@ Besides `/suggester` and `/search` endpoints, everything is accessible from `loc
9898

9999
[
100100
{
101-
"acceptedTime": "2018-06-28T17:49:01.793Z",
102-
"message": {
103-
"tags": ["main"],
104-
"cssClass": "cssClass",
105-
"text": "test message",
106-
"duration": "PT10M"
107-
},
108-
"expirationTime": "2018-06-28T17:59:01.793Z",
109-
"expired":false
101+
"expired": false,
102+
"created": "2019-01-23 20:39:31 CET",
103+
"tags": [
104+
"main"
105+
],
106+
"expiration": "2019-01-23 20:49:31 CET",
107+
"cssClass": "class",
108+
"text": "test message"
110109
}
111110
]
112-
111+
113112
## Projects [/projects]
114113

115114
### returns a list of all projects [GET]

opengrok-indexer/pom.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,6 @@ Portions Copyright (c) 2017-2018, Chris Fraire <[email protected]>.
8787
<groupId>javax.servlet</groupId>
8888
<artifactId>javax.servlet-api</artifactId>
8989
</dependency>
90-
<dependency>
91-
<groupId>com.googlecode.json-simple</groupId>
92-
<artifactId>json-simple</artifactId>
93-
</dependency>
9490
<dependency>
9591
<groupId>org.apache.ant</groupId>
9692
<artifactId>ant</artifactId>

opengrok-indexer/src/main/java/org/opengrok/indexer/search/Results.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
import org.apache.lucene.index.IndexableField;
4848
import org.apache.lucene.search.IndexSearcher;
4949
import org.apache.lucene.search.ScoreDoc;
50-
import org.json.simple.JSONArray;
5150
import org.opengrok.indexer.analysis.Definitions;
5251
import org.opengrok.indexer.analysis.FileAnalyzer.Genre;
5352
import org.opengrok.indexer.analysis.Scopes;
@@ -61,6 +60,8 @@
6160
import org.opengrok.indexer.web.SearchHelper;
6261
import org.opengrok.indexer.web.Util;
6362

63+
import org.opengrok.indexer.web.messages.MessagesUtils;
64+
6465
import static org.opengrok.indexer.web.messages.MessagesContainer.MESSAGES_MAIN_PAGE_TAG;
6566

6667
/**
@@ -191,9 +192,10 @@ public static void prettyPrint(Writer out, SearchHelper sh, int start,
191192
out.write(sh.desc.get(parent));
192193
out.write("</i>");
193194
}
194-
JSONArray messages;
195-
if ((p = Project.getProject(parent)) != null
196-
&& (messages = Util.messagesToJson(p, MESSAGES_MAIN_PAGE_TAG)).size() > 0) {
195+
196+
p = Project.getProject(parent);
197+
String messages = MessagesUtils.messagesToJson(p, MESSAGES_MAIN_PAGE_TAG);
198+
if (p != null && !messages.isEmpty()) {
197199
out.write(" <a href=\"" + xrefPrefix + "/" + p.getName() + "\">");
198200
out.write("<span class=\"important-note important-note-rounded\" data-messages='" + messages + "'>!</span>");
199201
out.write("</a>");

opengrok-indexer/src/main/java/org/opengrok/indexer/web/Scripts.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public String toHtml() {
126126
SCRIPTS.put("jquery-tablesorter", new FileScript("js/jquery-tablesorter-2.26.6.min.js", 12));
127127
SCRIPTS.put("tablesorter-parsers", new FileScript("js/tablesorter-parsers-0.0.1.js", 13));
128128
SCRIPTS.put("searchable-option-list", new FileScript("js/searchable-option-list-2.0.6.js", 14));
129-
SCRIPTS.put("utils", new FileScript("js/utils-0.0.26.js", 15));
129+
SCRIPTS.put("utils", new FileScript("js/utils-0.0.27.js", 15));
130130
SCRIPTS.put("repos", new FileScript("js/repos-0.0.1.js", 20));
131131
SCRIPTS.put("diff", new FileScript("js/diff-0.0.3.js", 20));
132132
SCRIPTS.put("jquery-caret", new FileScript("js/jquery.caret-1.5.2.min.js", 25));

opengrok-indexer/src/main/java/org/opengrok/indexer/web/Util.java

Lines changed: 0 additions & 207 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,15 @@
4141
import java.net.URLDecoder;
4242
import java.net.URLEncoder;
4343
import java.nio.charset.StandardCharsets;
44-
import java.sql.Date;
4544
import java.text.DecimalFormat;
4645
import java.text.NumberFormat;
47-
import java.text.SimpleDateFormat;
48-
import java.util.ArrayList;
49-
import java.util.Arrays;
5046
import java.util.Collection;
5147
import java.util.HashMap;
5248
import java.util.LinkedList;
5349
import java.util.List;
5450
import java.util.Locale;
5551
import java.util.Map;
5652
import java.util.Map.Entry;
57-
import java.util.SortedSet;
5853
import java.util.TreeMap;
5954
import java.util.function.Function;
6055
import java.util.logging.Level;
@@ -63,16 +58,11 @@
6358
import java.util.regex.Pattern;
6459
import java.util.zip.GZIPInputStream;
6560
import javax.servlet.http.HttpServletRequest;
66-
import org.json.simple.JSONArray;
67-
import org.json.simple.JSONObject;
68-
import org.opengrok.indexer.configuration.Group;
69-
import org.opengrok.indexer.configuration.Project;
7061
import org.opengrok.indexer.configuration.RuntimeEnvironment;
7162
import org.opengrok.indexer.history.Annotation;
7263
import org.opengrok.indexer.history.HistoryException;
7364
import org.opengrok.indexer.history.HistoryGuru;
7465
import org.opengrok.indexer.logger.LoggerFactory;
75-
import org.opengrok.indexer.web.messages.MessagesContainer.AcceptedMessage;
7666

7767
import static org.opengrok.indexer.index.Indexer.PATH_SEPARATOR;
7868

@@ -1264,203 +1254,6 @@ public static void dumpXref(Writer out, Reader in, String contextPath)
12641254
}
12651255
}
12661256

1267-
/**
1268-
* Print list of messages into output
1269-
*
1270-
* @param out output
1271-
* @param set set of messages
1272-
*/
1273-
public static void printMessages(Writer out, SortedSet<AcceptedMessage> set) {
1274-
printMessages(out, set, false);
1275-
}
1276-
1277-
/**
1278-
* Print set of messages into output
1279-
*
1280-
* @param out output
1281-
* @param set set of messages
1282-
* @param limited if the container should be limited
1283-
*/
1284-
public static void printMessages(Writer out, SortedSet<AcceptedMessage> set, boolean limited) {
1285-
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
1286-
if (!set.isEmpty()) {
1287-
try {
1288-
out.write("<ul class=\"message-group");
1289-
if (limited) {
1290-
out.write(" limited");
1291-
}
1292-
out.write("\">\n");
1293-
for (AcceptedMessage m : set) {
1294-
out.write("<li class=\"message-group-item ");
1295-
out.write(Util.encode(m.getMessage().getCssClass()));
1296-
out.write("\" title=\"Expires on ");
1297-
out.write(Util.encode(df.format(Date.from(m.getExpirationTime()))));
1298-
out.write("\">");
1299-
out.write(Util.encode(df.format(Date.from(m.getAcceptedTime()))));
1300-
out.write(": ");
1301-
out.write(m.getMessage().getText());
1302-
out.write("</li>");
1303-
}
1304-
out.write("</ul>");
1305-
} catch (IOException ex) {
1306-
LOGGER.log(Level.WARNING,
1307-
"An error occurred for a group of messages", ex);
1308-
}
1309-
}
1310-
}
1311-
1312-
/**
1313-
* Print set of messages into json array
1314-
*
1315-
* @param set set of messages
1316-
* @return json array containing the set of messages
1317-
*/
1318-
@SuppressWarnings("unchecked")
1319-
public static JSONArray messagesToJson(SortedSet<AcceptedMessage> set) {
1320-
JSONArray array = new JSONArray();
1321-
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
1322-
for (AcceptedMessage m : set) {
1323-
JSONObject message = new JSONObject();
1324-
message.put("class", Util.encode(m.getMessage().getCssClass()));
1325-
message.put("expiration", Util.encode(df.format(Date.from(m.getExpirationTime()))));
1326-
message.put("created", Util.encode(df.format(Date.from(m.getAcceptedTime()))));
1327-
message.put("text", Util.encode(m.getMessage().getText()));
1328-
JSONArray tags = new JSONArray();
1329-
for (String t : m.getMessage().getTags()) {
1330-
tags.add(Util.encode(t));
1331-
}
1332-
message.put("tags", tags);
1333-
array.add(message);
1334-
}
1335-
return array;
1336-
}
1337-
1338-
/**
1339-
* Print set of messages into json object for given tag.
1340-
*
1341-
* @param tag return messages in json format for the given tag
1342-
* @return json object with 'tag' and 'messages' attribute or null
1343-
*/
1344-
@SuppressWarnings("unchecked")
1345-
public static JSONObject messagesToJsonObject(String tag) {
1346-
SortedSet<AcceptedMessage> messages = RuntimeEnvironment.getInstance().getMessages(tag);
1347-
if (messages.isEmpty()) {
1348-
return null;
1349-
}
1350-
JSONObject toRet = new JSONObject();
1351-
toRet.put("tag", tag);
1352-
toRet.put("messages", messagesToJson(messages));
1353-
return toRet;
1354-
}
1355-
1356-
/**
1357-
* Print messages for given tags into json array
1358-
*
1359-
* @param array the array where the result should be stored
1360-
* @param tags list of tags
1361-
* @return json array of the messages (the same as the parameter)
1362-
* @see #messagesToJsonObject(String)
1363-
*/
1364-
@SuppressWarnings("unchecked")
1365-
public static JSONArray messagesToJson(JSONArray array, String... tags) {
1366-
array = array == null ? new JSONArray() : array;
1367-
for (String tag : tags) {
1368-
JSONObject messages = messagesToJsonObject(tag);
1369-
if (messages == null || messages.isEmpty()) {
1370-
continue;
1371-
}
1372-
array.add(messages);
1373-
}
1374-
return array;
1375-
}
1376-
1377-
/**
1378-
* Print messages for given tags into json array
1379-
*
1380-
* @param tags list of tags
1381-
* @return json array of the messages
1382-
* @see #messagesToJson(JSONArray, String...)
1383-
* @see #messagesToJsonObject(String)
1384-
*/
1385-
public static JSONArray messagesToJson(String... tags) {
1386-
return messagesToJson((JSONArray) null, tags);
1387-
}
1388-
1389-
/**
1390-
* Print messages for given tags into json array
1391-
*
1392-
* @param tags list of tags
1393-
* @return json array of the messages
1394-
* @see #messagesToJson(String...)
1395-
* @see #messagesToJsonObject(String)
1396-
*/
1397-
public static JSONArray messagesToJson(List<String> tags) {
1398-
String[] array = new String[tags.size()];
1399-
return messagesToJson(tags.toArray(array));
1400-
}
1401-
1402-
/**
1403-
* Print messages for given project into json array. These messages are
1404-
* tagged by project description or tagged by any of the project's group
1405-
* name.
1406-
*
1407-
* @param project the project
1408-
* @param additionalTags additional list of tags
1409-
* @return the json array
1410-
* @see #messagesToJson(String...)
1411-
*/
1412-
public static JSONArray messagesToJson(Project project, String... additionalTags) {
1413-
if (project == null) {
1414-
return new JSONArray();
1415-
}
1416-
List<String> tags = new ArrayList<>();
1417-
tags.addAll(Arrays.asList(additionalTags));
1418-
tags.add(project.getName());
1419-
project.getGroups().stream().forEach((Group t) -> {
1420-
tags.add(t.getName());
1421-
});
1422-
return messagesToJson(tags);
1423-
}
1424-
1425-
/**
1426-
* Print messages for given project into json array. These messages are
1427-
* tagged by project description or tagged by any of the project's group
1428-
* name
1429-
*
1430-
* @param project the project
1431-
* @return the json array
1432-
* @see #messagesToJson(Project, String...)
1433-
*/
1434-
public static JSONArray messagesToJson(Project project) {
1435-
return messagesToJson(project, new String[0]);
1436-
}
1437-
1438-
/**
1439-
* Print messages for given group into json array.
1440-
*
1441-
* @param group the group
1442-
* @param additionalTags additional list of tags
1443-
* @return the json array
1444-
* @see #messagesToJson(java.util.List)
1445-
*/
1446-
public static JSONArray messagesToJson(Group group, String... additionalTags) {
1447-
List<String> tags = new ArrayList<>();
1448-
tags.add(group.getName());
1449-
tags.addAll(Arrays.asList(additionalTags));
1450-
return messagesToJson(tags);
1451-
}
1452-
1453-
/**
1454-
* Print messages for given group into json array.
1455-
*
1456-
* @param group the group
1457-
* @return the json array
1458-
* @see #messagesToJson(Group, String...)
1459-
*/
1460-
public static JSONArray messagesToJson(Group group) {
1461-
return messagesToJson(group, new String[0]);
1462-
}
1463-
14641257
/**
14651258
* Print a row in an HTML table.
14661259
*
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License (the "License").
6+
* You may not use this file except in compliance with the License.
7+
*
8+
* See LICENSE.txt included in this distribution for the specific
9+
* language governing permissions and limitations under the License.
10+
*
11+
* When distributing Covered Code, include this CDDL HEADER in each
12+
* file and include the License file at LICENSE.txt.
13+
* If applicable, add the following below this CDDL HEADER, with the
14+
* fields enclosed by brackets "[]" replaced with your own identifying
15+
* information: Portions Copyright [yyyy] [name of copyright owner]
16+
*
17+
* CDDL HEADER END
18+
*/
19+
20+
/*
21+
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
22+
*/
23+
24+
package org.opengrok.indexer.web.messages;
25+
26+
import com.fasterxml.jackson.core.JsonProcessingException;
27+
import com.fasterxml.jackson.databind.ObjectMapper;
28+
import org.opengrok.indexer.logger.LoggerFactory;
29+
30+
import java.util.logging.Level;
31+
32+
public interface JSONable {
33+
34+
String EMPTY = "";
35+
36+
/**
37+
* convert object to JSON
38+
* @return JSON string or empty string on error
39+
*/
40+
default String toJSON() {
41+
ObjectMapper mapper = new ObjectMapper();
42+
try {
43+
return mapper.writeValueAsString(this);
44+
} catch (JsonProcessingException e) {
45+
LoggerFactory.getLogger(JSONable.class).log(Level.WARNING,
46+
String.format("failed to convert object of class %s to JSON: %s",
47+
this.getClass().toString(), this), e);
48+
return EMPTY;
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)