1
|
package eu.dnetlib.data.search.web.api;
|
2
|
|
3
|
import eu.dnetlib.data.search.web.utils.RequestResponseHandler;
|
4
|
import eu.dnetlib.domain.data.SearchResult;
|
5
|
import org.apache.log4j.Logger;
|
6
|
|
7
|
import javax.servlet.http.HttpServletRequest;
|
8
|
import javax.ws.rs.core.MediaType;
|
9
|
import java.util.Iterator;
|
10
|
import java.util.List;
|
11
|
|
12
|
/**
|
13
|
* Created by kiatrop on 21/11/2016.
|
14
|
*/
|
15
|
public class APIResponseFormatter {
|
16
|
|
17
|
private static final Logger logger = Logger.getLogger(APIResponseFormatter.class);
|
18
|
|
19
|
public static String compose404Message(String type, String message) {
|
20
|
if(type.equals(MediaType.APPLICATION_JSON)) {
|
21
|
return String.format(JSONResponseFormat.error404Message, message);
|
22
|
}
|
23
|
return String.format(XMLResponseFormat.error404Message, message);
|
24
|
}
|
25
|
|
26
|
public static String compose400Message(String type, String message) {
|
27
|
if(type.equals(MediaType.APPLICATION_JSON)) {
|
28
|
return String.format(JSONResponseFormat.error400Message, message);
|
29
|
}
|
30
|
return String.format(XMLResponseFormat.error400Message, message);
|
31
|
}
|
32
|
|
33
|
public static String compose500Message(String type, String message, String description) {
|
34
|
if(type.equals(MediaType.APPLICATION_JSON)) {
|
35
|
return String.format(JSONResponseFormat.error500Message, message);
|
36
|
}
|
37
|
return String.format(XMLResponseFormat.error500Message, message, description);
|
38
|
}
|
39
|
|
40
|
public static String createEntitiesResponse(HttpServletRequest request, RequestResponseHandler.Entity entityType, String query, List<String> filters, SearchResult searchResult, boolean refine, String type) {
|
41
|
if (refine) {
|
42
|
if (type.equals(MediaType.APPLICATION_JSON)) {
|
43
|
return String.format(JSONResponseFormat.response, createMeta(request, type, query, filters, searchResult.getTotal(), searchResult.getPage(), searchResult.getSize()), formatSearchResults(type,searchResult.getSearchResults()), formatRefineResults(type, searchResult.getBrowseResults()));
|
44
|
}
|
45
|
return String.format(XMLResponseFormat.response, createMeta(request, type, query, filters, searchResult.getTotal(), searchResult.getPage(), searchResult.getSize()), formatSearchResults(type,searchResult.getSearchResults()), formatRefineResults(type, searchResult.getBrowseResults()));
|
46
|
}
|
47
|
|
48
|
if (type.equals(MediaType.APPLICATION_JSON)) {
|
49
|
if (searchResult.getSearchResults()==null || searchResult.getSearchResults().isEmpty()) {
|
50
|
return String.format(JSONResponseFormat.response, createMeta(request, type, query, filters, searchResult.getTotal(), searchResult.getPage(), searchResult.getSize()), formatSearchResults(type, searchResult.getSearchResults()), "");
|
51
|
}
|
52
|
}
|
53
|
|
54
|
return String.format(XMLResponseFormat.response, createMeta(request, type, query, filters, searchResult.getTotal(), searchResult.getPage(), searchResult.getSize()), formatSearchResults(type, searchResult.getSearchResults()), "");
|
55
|
|
56
|
}
|
57
|
|
58
|
private static String formatRefineResults(String type, List<String> refineResults) {
|
59
|
if (type.equals(MediaType.APPLICATION_JSON)) {
|
60
|
if (refineResults == null || refineResults.isEmpty()) {
|
61
|
return ",\"refineResults\":{}";
|
62
|
}
|
63
|
|
64
|
return ",\"refineResults\": {" + appendJSONStrings(refineResults) + "}";
|
65
|
}
|
66
|
|
67
|
if (refineResults == null || refineResults.isEmpty()) {
|
68
|
return "<refineResults></refineResults>";
|
69
|
}
|
70
|
|
71
|
return "<refineResults>" + appendXMLStrings(refineResults) + "</refineResults>";
|
72
|
}
|
73
|
|
74
|
private static String formatSearchResults(String type, List<String> searchResults) {
|
75
|
if (searchResults == null || searchResults.isEmpty()) {
|
76
|
return "";
|
77
|
}
|
78
|
|
79
|
if (type.equals(MediaType.APPLICATION_JSON)) {
|
80
|
return appendJSONStrings(searchResults);
|
81
|
}
|
82
|
|
83
|
return appendXMLStrings(searchResults);
|
84
|
|
85
|
}
|
86
|
|
87
|
private static String appendXMLStrings(List<String> searchResults) {
|
88
|
StringBuilder stringBuilder = new StringBuilder();
|
89
|
for (String xml:searchResults) {
|
90
|
stringBuilder.append(xml);
|
91
|
}
|
92
|
return stringBuilder.toString();
|
93
|
}
|
94
|
|
95
|
private static String appendJSONStrings(List<String> searchResults) {
|
96
|
StringBuilder stringBuilder = new StringBuilder();
|
97
|
for (Iterator<String> iterator = searchResults.iterator(); iterator.hasNext();) {
|
98
|
stringBuilder.append(iterator.next());
|
99
|
if (iterator.hasNext()) {
|
100
|
stringBuilder.append(",");
|
101
|
}
|
102
|
}
|
103
|
return stringBuilder.toString();
|
104
|
}
|
105
|
|
106
|
/*
|
107
|
public static String createEntitiesResponse(HttpServletRequest request, RequestResponseHandler.Entity entityType,
|
108
|
String query, String entities, List<String> filters, int total, int currentOffset, int limit, boolean refine, List<String> refineResults, String type) {
|
109
|
if (refine) {
|
110
|
if (type.equals(MediaType.APPLICATION_JSON)) {
|
111
|
return String.format(JSONResponseFormat.response, createMeta(request, type, query, filters, total, currentOffset, limit), entities, ", refine", new Gson().toJson(refineResults));
|
112
|
}
|
113
|
return String.format(XMLResponseFormat.response, createMeta(request, type, query, filters, total, currentOffset, limit), entities, "", "");
|
114
|
}
|
115
|
|
116
|
if (type.equals(MediaType.APPLICATION_JSON)) {
|
117
|
return String.format(JSONResponseFormat.response, createMeta(request, type, query, filters, total, currentOffset, limit), entities, "", "");
|
118
|
}
|
119
|
|
120
|
return String.format(XMLResponseFormat.response, createMeta(request, type, query, filters, total, currentOffset, limit));
|
121
|
|
122
|
}*/
|
123
|
|
124
|
public static String createMeta(HttpServletRequest request, String type, String query, List<String> filters, int total, int currentOffset, int limit) {
|
125
|
if (type.equals(MediaType.APPLICATION_JSON)) {
|
126
|
return String.format(JSONResponseFormat.metaSearch, query, createFilters(type, filters), total, currentOffset, limit, createPaging(request, type, total, currentOffset, limit));
|
127
|
}
|
128
|
return String.format(XMLResponseFormat.metaSearch, query, createFilters(type, filters), total, currentOffset, limit, createPaging(request, type, total, currentOffset, limit));
|
129
|
}
|
130
|
|
131
|
public static String createFilters(String type, List<String> fieldQueries) {
|
132
|
StringBuilder builder = new StringBuilder();
|
133
|
if (fieldQueries != null) {
|
134
|
if (type.equals(MediaType.APPLICATION_JSON)) {
|
135
|
for (Iterator<String> iterator = fieldQueries.iterator(); iterator.hasNext(); ) {
|
136
|
builder.append("\"").append(iterator.next()).append("\"");
|
137
|
if (iterator.hasNext()) {
|
138
|
builder.append(",");
|
139
|
}
|
140
|
}
|
141
|
} else {
|
142
|
for (Iterator<String> iterator = fieldQueries.iterator(); iterator.hasNext(); ) {
|
143
|
builder.append("<filter>").append(iterator.next()).append("</filter>");
|
144
|
}
|
145
|
}
|
146
|
}
|
147
|
|
148
|
return builder.toString();
|
149
|
}
|
150
|
|
151
|
private static String createPaging(HttpServletRequest request, String type, int total, int currentOffset, int limit) {
|
152
|
String url = request.getRequestURL().toString();
|
153
|
|
154
|
if (limit > 0) {
|
155
|
String selfPageUrl = pageUrlCreation(url, currentOffset, limit);
|
156
|
String firstPageUrl = pageUrlCreation(url, 0, limit);
|
157
|
String lastPageUrl = pageUrlCreation(url, calculateLastPage(total, limit), limit);
|
158
|
String previousPageUrl = pageUrlCreation(url, calculatePreviousPage(total, currentOffset, limit), limit);
|
159
|
String nextPageUrl = pageUrlCreation(url, calculateNextPage(total, currentOffset, limit), limit);
|
160
|
|
161
|
return formatPaging(type, firstPageUrl, lastPageUrl, previousPageUrl, nextPageUrl, selfPageUrl);
|
162
|
|
163
|
}
|
164
|
|
165
|
String selfPageUrl = pageUrlCreation(url, currentOffset, limit);
|
166
|
return formatSelfPaging(type, selfPageUrl);
|
167
|
}
|
168
|
|
169
|
private static String formatPaging(String type, String first, String last, String previous, String next, String self) {
|
170
|
if (type.equals(MediaType.APPLICATION_JSON)) {
|
171
|
return String.format(JSONResponseFormat._links, first, last, previous, next, self);
|
172
|
}
|
173
|
|
174
|
return String.format(XMLResponseFormat._links, first, last, previous, next, self);
|
175
|
}
|
176
|
|
177
|
private static String formatSelfPaging(String type, String self) {
|
178
|
if (type.equals(MediaType.APPLICATION_JSON)) {
|
179
|
return String.format(JSONResponseFormat._selflink, self);
|
180
|
}
|
181
|
|
182
|
return String.format(XMLResponseFormat._selflink, self);
|
183
|
}
|
184
|
|
185
|
private static int calculateLastPage(int total, int limit) {
|
186
|
if (total <= limit) {
|
187
|
return 0;
|
188
|
|
189
|
} else {
|
190
|
if (total%limit == 0) {
|
191
|
return total / limit - 1;
|
192
|
}
|
193
|
return total/limit ;
|
194
|
}
|
195
|
}
|
196
|
|
197
|
private static int calculatePreviousPage(int total, int currentOffset, int limit) {
|
198
|
if (currentOffset-1 <= 0) {
|
199
|
return 0;
|
200
|
}
|
201
|
|
202
|
return currentOffset-1;
|
203
|
}
|
204
|
|
205
|
private static int calculateNextPage(int total, int currentOffset, int limit) {
|
206
|
int lastPage = calculateLastPage(total, limit);
|
207
|
|
208
|
if (currentOffset + 1 >= lastPage) {
|
209
|
return lastPage;
|
210
|
}
|
211
|
return currentOffset + 1;
|
212
|
}
|
213
|
|
214
|
private static String pageUrlCreation(String urlPrefix, int offset, int limit) {
|
215
|
return urlPrefix + "?page=" + offset + "&size=" + limit;
|
216
|
}
|
217
|
|
218
|
}
|