Provide RightClick/CopyToClipboard functionality for "Public/Private Link" in File...
[pithos-web-client] / src / gr / grnet / pithos / web / client / Helpers.java
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("  ", statusCode, " ", statusText);
87
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 ? "" : " ...");
92                 }
93
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);
100                     }
101                 }
102             }
103             catch(Exception e) {
104                 Pithos.LOG("ERROR trying to log response", e);
105             }
106         }
107     }
108
109     public static native void copyToClipboardFrom(com.google.gwt.user.client.Element element) /*-{
110       $wnd.window.clipboardData.setData('text', element);
111     }-*/;
112 }