| 14792 |
manas |
1 |
/**
|
|
|
2 |
* Copyright 2010-present Facebook.
|
|
|
3 |
*
|
|
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
5 |
* you may not use this file except in compliance with the License.
|
|
|
6 |
* You may obtain a copy of the License at
|
|
|
7 |
*
|
|
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
9 |
*
|
|
|
10 |
* Unless required by applicable law or agreed to in writing, software
|
|
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
13 |
* See the License for the specific language governing permissions and
|
|
|
14 |
* limitations under the License.
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
package com.facebook.internal;
|
|
|
18 |
|
|
|
19 |
import android.content.Context;
|
|
|
20 |
import android.util.Log;
|
|
|
21 |
import com.facebook.LoggingBehavior;
|
|
|
22 |
|
|
|
23 |
import java.io.IOException;
|
|
|
24 |
import java.io.InputStream;
|
|
|
25 |
import java.io.InputStreamReader;
|
|
|
26 |
import java.io.OutputStream;
|
|
|
27 |
import java.net.URI;
|
|
|
28 |
import java.net.URISyntaxException;
|
|
|
29 |
|
|
|
30 |
class UrlRedirectCache {
|
|
|
31 |
static final String TAG = UrlRedirectCache.class.getSimpleName();
|
|
|
32 |
private static final String REDIRECT_CONTENT_TAG = TAG + "_Redirect";
|
|
|
33 |
|
|
|
34 |
private volatile static FileLruCache urlRedirectCache;
|
|
|
35 |
|
|
|
36 |
synchronized static FileLruCache getCache(Context context) throws IOException{
|
|
|
37 |
if (urlRedirectCache == null) {
|
|
|
38 |
urlRedirectCache = new FileLruCache(context.getApplicationContext(), TAG, new FileLruCache.Limits());
|
|
|
39 |
}
|
|
|
40 |
return urlRedirectCache;
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
static URI getRedirectedUri(Context context, URI uri) {
|
|
|
44 |
if (uri == null) {
|
|
|
45 |
return null;
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
String uriString = uri.toString();
|
|
|
49 |
InputStreamReader reader = null;
|
|
|
50 |
try {
|
|
|
51 |
InputStream stream;
|
|
|
52 |
FileLruCache cache = getCache(context);
|
|
|
53 |
boolean redirectExists = false;
|
|
|
54 |
while ((stream = cache.get(uriString, REDIRECT_CONTENT_TAG)) != null) {
|
|
|
55 |
redirectExists = true;
|
|
|
56 |
|
|
|
57 |
// Get the redirected url
|
|
|
58 |
reader = new InputStreamReader(stream);
|
|
|
59 |
char[] buffer = new char[128];
|
|
|
60 |
int bufferLength;
|
|
|
61 |
StringBuilder urlBuilder = new StringBuilder();
|
|
|
62 |
while ((bufferLength = reader.read(buffer, 0, buffer.length)) > 0) {
|
|
|
63 |
urlBuilder.append(buffer, 0, bufferLength);
|
|
|
64 |
}
|
|
|
65 |
Utility.closeQuietly(reader);
|
|
|
66 |
|
|
|
67 |
// Iterate to the next url in the redirection
|
|
|
68 |
uriString = urlBuilder.toString();
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
if (redirectExists) {
|
|
|
72 |
return new URI(uriString);
|
|
|
73 |
}
|
|
|
74 |
} catch (URISyntaxException e) {
|
|
|
75 |
// caching is best effort, so ignore the exception
|
|
|
76 |
} catch (IOException ioe) {
|
|
|
77 |
} finally {
|
|
|
78 |
Utility.closeQuietly(reader);
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
return null;
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
static void cacheUriRedirect(Context context, URI fromUri, URI toUri) {
|
|
|
85 |
if (fromUri == null || toUri == null) {
|
|
|
86 |
return;
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
OutputStream redirectStream = null;
|
|
|
90 |
try {
|
|
|
91 |
FileLruCache cache = getCache(context);
|
|
|
92 |
redirectStream = cache.openPutStream(fromUri.toString(), REDIRECT_CONTENT_TAG);
|
|
|
93 |
redirectStream.write(toUri.toString().getBytes());
|
|
|
94 |
} catch (IOException e) {
|
|
|
95 |
// Caching is best effort
|
|
|
96 |
} finally {
|
|
|
97 |
Utility.closeQuietly(redirectStream);
|
|
|
98 |
}
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
static void clearCache(Context context) {
|
|
|
102 |
try {
|
|
|
103 |
getCache(context).clearCache();
|
|
|
104 |
} catch (IOException e) {
|
|
|
105 |
Logger.log(LoggingBehavior.CACHE, Log.WARN, TAG, "clearCache failed " + e.getMessage());
|
|
|
106 |
}
|
|
|
107 |
}
|
|
|
108 |
}
|