Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (3.7 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
                if(!Pithos.HTTPHeadersToIgnoreInLOG.contains(headerName)) {
75
                    Pithos.LOG("  ==> ", headerName, ": ", headerValue);
76
                }
77
            }
78
        }
79
    }
80

    
81
    public static void LOGResponse(Response response) {
82
        if(Pithos.IsDetailedHTTPLOGEnabled) {
83
            try {
84
                final int statusCode = response.getStatusCode();
85
                final String statusText = response.getStatusText();
86
                Pithos.LOG("  <== [STATUS] ", statusCode, " ", statusText);
87

    
88
                final String body = response.getText();
89
                if(body != null) {
90
                    final String trimmedBody = body.trim();
91

    
92
                    if(trimmedBody.length() > 0) {
93
                        if(Pithos.IsFullResponseBodyLOGEnabled) {
94
                            Pithos.LOG("  <== [BODY] ", body);
95
                        }
96
                        else {
97
                            final int LEN = 120;
98
                            Pithos.LOG("  <== [BODY] ", trimmedBody.substring(0, LEN), body.length() <= LEN ? "" : " ...");
99
                        }
100
                    }
101
                }
102

    
103
                final Header[] headers = response.getHeaders();
104
                for(Header header : headers) {
105
                    final String headerName = header.getName();
106
                    final String headerValue = header.getValue();
107
                    if(!Pithos.HTTPHeadersToIgnoreInLOG.contains(headerName)) {
108
                        Pithos.LOG("  <== ", headerName, ": ", headerValue);
109
                    }
110
                }
111
            }
112
            catch(Exception e) {
113
                Pithos.LOG("ERROR trying to log response", e);
114
            }
115
        }
116
    }
117
}