Revision 44602
Added by Katerina Iatropoulou over 7 years ago
ResponseFormatter.java | ||
---|---|---|
13 | 13 |
private static final Logger logger = Logger.getLogger(ResponseFormatter.class); |
14 | 14 |
|
15 | 15 |
private static final String error404Message = "{ \"status\" : \"error\", \"code\" : \"404\", \"message\" : \"%s\"}"; |
16 |
private static final String error400Message = "{ \"status\" : \"error\", \"code\" : \"400\", \"message\" : \"%s\"}"; |
|
16 | 17 |
private static final String error500Message = "{ \"status\" : \"fail\", \"code\" : \"500\", \"message\" : \"%s\", \"description\" : \"%s\" }"; |
17 | 18 |
|
18 | 19 |
private static final String metaBasic = "{\"status\": \"success\", \"code\":\"200\" }"; //TODO add self |
... | ... | |
25 | 26 |
" \"next\": {\"href\":\"%s\"}, " + |
26 | 27 |
" \"self\": {\"href\":\"%s\"}}"; |
27 | 28 |
|
28 |
public static String compose404Message(String message) { |
|
29 |
return String.format(error404Message, message); |
|
30 |
} |
|
29 |
private static final String _self = "{\"self\": {\"href\":\"%s\"}}"; |
|
31 | 30 |
|
32 |
public static String compose500Message(String message, String description) { |
|
33 |
return String.format(error500Message, message, description); |
|
34 |
} |
|
35 |
|
|
36 | 31 |
private static final String result = "{%s}"; |
37 | 32 |
|
38 | 33 |
private static final String resource = "\"%s\": {%s}"; |
... | ... | |
42 | 37 |
private static final String response = "{\"meta\": %s, \"results\" : [%s] %s %s}"; |
43 | 38 |
|
44 | 39 |
private static final String singleResponse = "{\"meta\": %s, %s }"; |
45 |
|
|
46 | 40 |
|
41 |
|
|
42 |
public static String compose404Message(String message) { |
|
43 |
return String.format(error404Message, message); |
|
44 |
} |
|
45 |
|
|
46 |
public static String compose400Message(String message) { |
|
47 |
return String.format(error400Message, message); |
|
48 |
} |
|
49 |
|
|
50 |
public static String compose500Message(String message, String description) { return String.format(error500Message, message, description); } |
|
51 |
|
|
47 | 52 |
public static String createCountMeta(String query, int total) { |
48 | 53 |
return String.format(countSearch, query, total); |
49 | 54 |
} |
... | ... | |
61 | 66 |
} |
62 | 67 |
|
63 | 68 |
private static String formatMetaSearch(String query, String total, String currentOffset, String limit, String links) { |
64 |
return String.format(metaSearch, query, total, currentOffset, limit ,links);
|
|
69 |
return String.format(metaSearch, query, total, limit, currentOffset,links);
|
|
65 | 70 |
} |
66 | 71 |
|
67 | 72 |
public static String createEntityResponse(RequestResponseHandler.Entity entityType, String entity) { |
... | ... | |
73 | 78 |
return String.format(singleResponse, createMetaBasic(), entity); |
74 | 79 |
} |
75 | 80 |
|
76 |
public static String createEntitiesResponse(HttpServletRequest request, RequestResponseHandler.Entity entityType, String query, String entities, String total, String currentOffset, String limit, boolean refine, StringBuilder refineResults) {
|
|
81 |
public static String createEntitiesResponse(HttpServletRequest request, RequestResponseHandler.Entity entityType, String query, String entities, String total, String currentOffset, String limit, boolean refine, String refineResults) { |
|
77 | 82 |
if (!refine) |
78 | 83 |
return String.format(response, createMetaSearch(request, query, Integer.valueOf(total), Integer.valueOf(limit), Integer.valueOf(currentOffset)), entities, "", ""); |
79 | 84 |
|
... | ... | |
82 | 87 |
|
83 | 88 |
private static String createPaging(HttpServletRequest request, int total, int currentOffset, int limit) { |
84 | 89 |
String url = request.getRequestURL().toString(); |
90 |
logger.debug("limit " + limit); |
|
85 | 91 |
|
86 |
String selfPageUrl = pageUrlCreation(url, currentOffset, limit); |
|
87 |
String firstPageUrl = pageUrlCreation(url, 0, 10); |
|
88 |
String lastPageUrl = pageUrlCreation(url, calculateLastPage(total,limit), 10); |
|
89 |
String previousPageUrl = pageUrlCreation(url, calculatePreviousPage(total, currentOffset, limit), 10); |
|
90 |
String nextPageUrl = pageUrlCreation(url, calculateNextPage(total, currentOffset, limit), 10); |
|
92 |
if (limit > 0) { |
|
93 |
String selfPageUrl = pageUrlCreation(url, currentOffset, limit); |
|
94 |
String firstPageUrl = pageUrlCreation(url, 0, 10); |
|
95 |
String lastPageUrl = pageUrlCreation(url, calculateLastPage(total, limit), 10); |
|
96 |
String previousPageUrl = pageUrlCreation(url, calculatePreviousPage(total, currentOffset, limit), 10); |
|
97 |
String nextPageUrl = pageUrlCreation(url, calculateNextPage(total, currentOffset, limit), 10); |
|
91 | 98 |
|
92 |
return formatPaging(firstPageUrl, lastPageUrl, previousPageUrl, nextPageUrl, selfPageUrl); |
|
93 |
|
|
99 |
return formatPaging(firstPageUrl, lastPageUrl, previousPageUrl, nextPageUrl, selfPageUrl); |
|
100 |
} |
|
101 |
return String.format(url); |
|
94 | 102 |
} |
95 | 103 |
|
96 | 104 |
private static String formatPaging(String first, String last, String previous, String next, String self) { |
Also available in: Unified diff
Added methods for single solr query for search and browse. Size does not trigger extra solr query. Refine uses field queries. Pending APIs for different entities - only the genarla resources api is currently enabled. Pending handling format for both XML and JSON. All these related to #1432 and #2479