1 package gr.grnet.pithos.web.client;
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;
14 public final class Helpers {
17 public static boolean isEmptySafe(CharSequence s) {
18 return s == null || s.length() == 0;
21 public static <K, V> HashMap<K, V> copyHashMap(HashMap<K, V> map) {
23 return new HashMap<K, V>(map);
26 public static <T> List<T> safeList(List<T> list) {
28 return new ArrayList<T>();
33 public static <T> List<T> toList(T ...items) {
34 final List<T> list = new ArrayList<T>();
35 Collections.addAll(list, items);
39 public static JSONArray listToJSONArray(List<String> list) {
40 final JSONArray jsonArray = new JSONArray();
45 for(int i = 0; i < list.size(); i++) {
46 final JSONString jsonString = new JSONString(list.get(i));
47 jsonArray.set(i, jsonString);
53 public static String stripTrailing(String s, String trailing) {
54 while(s.endsWith(trailing)) {
55 s = s.substring(0, s.length() - trailing.length());
60 public static String upToIncludingLastPart(String s, String part) {
61 int index = s.lastIndexOf(part);
65 return s.substring(0, index + part.length());
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);
73 if(Pithos.IsDetailedHTTPLOGEnabled) {
74 if(!Pithos.HTTPHeadersToIgnoreInLOG.contains(headerName)) {
75 Pithos.LOG(" ==> ", headerName, ": ", headerValue);
81 public static void LOGResponse(Response response) {
82 if(Pithos.IsDetailedHTTPLOGEnabled) {
84 final int statusCode = response.getStatusCode();
85 final String statusText = response.getStatusText();
86 Pithos.LOG(" ", statusCode, " ", statusText);
88 final String body = response.getText();
89 if(body != null && body.trim().length() > 0) {
90 final String s = body.trim().substring(0, 120);
91 Pithos.LOG(body, body.length() <= 120 ? "" : " ...");
94 final Header[] headers = response.getHeaders();
95 for(Header header : headers) {
96 final String headerName = header.getName();
97 final String headerValue = header.getValue();
98 if(!Pithos.HTTPHeadersToIgnoreInLOG.contains(headerName)) {
99 Pithos.LOG(" <== ", headerName, ": ", headerValue);
104 Pithos.LOG("ERROR trying to log response", e);
109 public static native void copyToClipboardFrom(com.google.gwt.user.client.Element element) /*-{
110 $wnd.window.clipboardData.setData('text', element);