root / src / gr / grnet / pithos / web / client / Helpers.java @ 45c76262
History | View | Annotate | Download (2.9 kB)
1 |
package gr.grnet.pithos.web.client; |
---|---|
2 |
|
3 |
import com.google.gwt.http.client.Header; |
4 |
import com.google.gwt.http.client.RequestBuilder; |
5 |
import com.google.gwt.http.client.Response; |
6 |
import com.google.gwt.json.client.JSONArray; |
7 |
import com.google.gwt.json.client.JSONString; |
8 |
|
9 |
import java.util.*; |
10 |
|
11 |
/**
|
12 |
* Helper methods.
|
13 |
*/
|
14 |
public final class Helpers { |
15 |
private Helpers() {}
|
16 |
|
17 |
public static boolean isEmptySafe(CharSequence s) { |
18 |
return s == null || s.length() == 0; |
19 |
} |
20 |
|
21 |
public static <K, V> HashMap<K, V> copyHashMap(HashMap<K, V> map) { |
22 |
assert map != null; |
23 |
return new HashMap<K, V>(map); |
24 |
} |
25 |
|
26 |
public static <T> List<T> safeList(List<T> list) { |
27 |
if(list == null) { |
28 |
return new ArrayList<T>(); |
29 |
} |
30 |
return list;
|
31 |
} |
32 |
|
33 |
public static <T> List<T> toList(T ...items) { |
34 |
final List<T> list = new ArrayList<T>(); |
35 |
Collections.addAll(list, items);
|
36 |
return list;
|
37 |
} |
38 |
|
39 |
public static JSONArray listToJSONArray(List<String> list) { |
40 |
final JSONArray jsonArray = new JSONArray(); |
41 |
if(list == null) { |
42 |
return jsonArray;
|
43 |
} |
44 |
|
45 |
for(int i = 0; i < list.size(); i++) { |
46 |
final JSONString jsonString = new JSONString(list.get(i)); |
47 |
jsonArray.set(i, jsonString); |
48 |
} |
49 |
|
50 |
return jsonArray;
|
51 |
} |
52 |
|
53 |
public static String stripTrailing(String s, String trailing) { |
54 |
while(s.endsWith(trailing)) {
|
55 |
s = s.substring(0, s.length() - trailing.length());
|
56 |
} |
57 |
return s;
|
58 |
} |
59 |
|
60 |
public static String upToIncludingLastPart(String s, String part) { |
61 |
int index = s.lastIndexOf(part);
|
62 |
if(index == -1) { |
63 |
return s;
|
64 |
} |
65 |
return s.substring(0, index + part.length()); |
66 |
} |
67 |
|
68 |
public static void setHeaders(RequestBuilder builder, Map<String, String> headers) { |
69 |
for (String headerName : headers.keySet()) { |
70 |
final String headerValue = headers.get(headerName); |
71 |
builder.setHeader(headerName, headerValue); |
72 |
|
73 |
if(Pithos.IsDetailedHTTPLOGEnabled) {
|
74 |
Pithos.LOG(" ==> ", headerName, ": ", headerValue); |
75 |
} |
76 |
} |
77 |
} |
78 |
|
79 |
public static void LOGResponse(Response response) { |
80 |
if(Pithos.IsDetailedHTTPLOGEnabled) {
|
81 |
try {
|
82 |
final int statusCode = response.getStatusCode(); |
83 |
final String statusText = response.getStatusText(); |
84 |
Pithos.LOG(" ", statusCode, " ", statusText); |
85 |
final Header[] headers = response.getHeaders(); |
86 |
for(Header header : headers) {
|
87 |
final String name = header.getName(); |
88 |
final String value = header.getValue(); |
89 |
Pithos.LOG(" <== ", name, ": ", value); |
90 |
} |
91 |
} |
92 |
catch(Exception e) { |
93 |
Pithos.LOG("ERROR trying to log response", e);
|
94 |
} |
95 |
} |
96 |
} |
97 |
} |