1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.exoplatform.utils.image;
20
21 import java.io.IOException;
22 import java.net.CookieHandler;
23 import java.net.CookieManager;
24 import java.net.HttpCookie;
25 import java.net.HttpURLConnection;
26 import java.net.URI;
27 import java.net.URISyntaxException;
28
29 import org.apache.http.client.CookieStore;
30 import org.apache.http.cookie.Cookie;
31 import org.exoplatform.singleton.AccountSetting;
32 import org.exoplatform.utils.ExoConnectionUtils;
33
34 import com.squareup.picasso.Picasso;
35 import com.squareup.picasso.UrlConnectionDownloader;
36
37 import android.content.Context;
38 import android.net.Uri;
39 import android.util.Log;
40
41
42
43
44
45
46
47
48
49
50
51
52
53 public class ExoPicassoDownloader extends UrlConnectionDownloader {
54
55 private static final String TAG = "eXo___ExoPicassoDownloader___";
56
57
58
59 public ExoPicassoDownloader(Context context) {
60 super(context);
61 }
62
63
64
65
66
67
68
69
70 private CookieManager initCookieManager() {
71 CookieHandler handler = CookieHandler.getDefault();
72 CookieManager manager;
73 if (handler == null || !(handler instanceof CookieManager)) {
74 manager = new CookieManager();
75 CookieHandler.setDefault(manager);
76
77
78 syncCookies(manager);
79 } else {
80 manager = (CookieManager) handler;
81 }
82 return manager;
83 }
84
85
86
87
88
89
90
91 private void syncCookies(CookieManager manager) {
92 CookieStore store = ExoConnectionUtils.cookiesStore;
93 if (store == null)
94 return;
95
96 for (Cookie cookie : store.getCookies()) {
97 HttpCookie c = new HttpCookie(cookie.getName(), cookie.getValue());
98 c.setDomain(cookie.getDomain());
99 c.setPath(cookie.getPath());
100 c.setVersion(cookie.getVersion());
101 String url = AccountSetting.getInstance().getDomainName() + "/" + cookie.getPath();
102 try {
103 manager.getCookieStore().add(new URI(url), c);
104 } catch (URISyntaxException e) {
105 Log.e(TAG, e.getMessage(), e);
106 }
107 }
108 }
109
110
111
112
113
114
115
116
117 private HttpURLConnection connection(Uri path) throws IOException {
118 HttpURLConnection connection = super.openConnection(path);
119 ExoConnectionUtils.setUserAgent(connection);
120 initCookieManager();
121 return connection;
122 }
123
124 @Override
125 public Response load(Uri uri, int networkPolicy) throws IOException {
126
127
128
129 HttpURLConnection connection = connection(uri);
130 connection.setInstanceFollowRedirects(true);
131 connection.setUseCaches(true);
132
133 int responseCode = connection.getResponseCode();
134
135
136
137 if (responseCode >= 300 && responseCode < 400) {
138 String location = connection.getHeaderField("Location");
139 connection.disconnect();
140 connection = connection(Uri.parse(location));
141 connection.setInstanceFollowRedirects(true);
142 connection.setUseCaches(true);
143 responseCode = connection.getResponseCode();
144 }
145
146 if (responseCode >= 300) {
147 connection.disconnect();
148 throw new ResponseException(responseCode + " " + connection.getResponseMessage(), networkPolicy, responseCode);
149 }
150
151 long contentLength = connection.getHeaderFieldInt("Content-Length", -1);
152
153
154 boolean fromCache = false;
155
156 return new Response(connection.getInputStream(), fromCache, contentLength);
157 }
158
159 }