Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / Helpers.java @ bc393f5e

History | View | Annotate | Download (3.2 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

    
86
                final String body = response.getText();
87
                if(body != null && body.trim().length() > 0) {
88
                    final String s = body.trim().substring(0, 120);
89
                    Pithos.LOG(body, body.length() <= 120 ? "" : " ...");
90
                }
91

    
92
                final Header[] headers = response.getHeaders();
93
                for(Header header : headers) {
94
                    final String name = header.getName();
95
                    final String value = header.getValue();
96
                    Pithos.LOG("  <== ", name, ": ", value);
97
                }
98
            }
99
            catch(Exception e) {
100
                Pithos.LOG("ERROR trying to log response", e);
101
            }
102
        }
103
    }
104
}