Last active
December 14, 2015 06:38
-
-
Save imminent/5043565 to your computer and use it in GitHub Desktop.
Collection of code to use the Client interface of Retrofit to use HttpUrlConnection with SharedPreferences persistent cookies.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.os.PatternMatcher; | |
import com.github.kevinsawicki.http.HttpRequest; | |
import com.keepandshare.android.utils.CircularByteBuffer; | |
import com.keepandshare.android.utils.Ln; | |
import org.apache.http.protocol.HttpContext; | |
import retrofit.http.Header; | |
import retrofit.http.client.Client; | |
import retrofit.http.client.Request; | |
import retrofit.http.client.Response; | |
import retrofit.http.mime.TypedInput; | |
import retrofit.http.mime.TypedOutput; | |
import java.io.BufferedInputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
import static com.google.common.collect.Maps.newHashMap; | |
/** | |
* Custom implementation of {@link Client} that can utilize an {@link HttpContext}. | |
* @author Dandré Allison | |
*/ | |
public class RecommendedAndroidClient implements Client { | |
@Override | |
public Response execute(Request request) throws IOException { | |
Ln.v("Request: %s '%s'", request.getMethod(), _sanitize_passwords.reset(request.getUrl()).replaceAll("p=***")); | |
// Creates the request | |
final HttpRequest http_response = new HttpRequest(request.getUrl(), request.getMethod()) | |
// Requests compressed responses | |
.acceptGzipEncoding() | |
// Automatically uncompresses response | |
.uncompress(true); | |
prepareRequest(http_response); | |
final List<Header> headers = request.getHeaders(); | |
if (headers != null) | |
http_response.headers(prepareHeaders(request.getHeaders())); | |
final TypedOutput body = request.getBody(); | |
if (body != null) { | |
final CircularByteBuffer byte_buffer = new CircularByteBuffer(CircularByteBuffer.INFINITE_SIZE); | |
request.getBody().writeTo(byte_buffer.getOutputStream()); | |
http_response.send(byte_buffer.getInputStream()); | |
} | |
// Creates the response | |
return parseResponse(http_response); | |
} | |
Response parseResponse(HttpRequest response) throws IOException { | |
final InputStream stream = response.buffer(); | |
final int status = response.code(); | |
final String reason = response.message(); | |
final List<Header> headers = new ArrayList<Header>(); | |
String content_type = "application/octet-stream"; | |
for (Map.Entry<String, List<String>> header : response.headers().entrySet()) { | |
final String name = header.getKey(); | |
final String value = header.getValue().get(0); | |
if (_content_type_header.match(name)) | |
content_type = value; | |
headers.add(new Header(name, value)); | |
} | |
prepareResponse(response, stream); | |
final TypedInputStream body = stream == null? null : new TypedInputStream(content_type, stream); | |
return new Response(status, reason, headers, body); | |
} | |
/** Callback for additional preparation of the request before execution. */ | |
protected void prepareRequest(HttpRequest request) { } | |
/** Callback for additional preparation of the response before parsing. */ | |
protected void prepareResponse(HttpRequest response, InputStream body) { } | |
static class TypedInputStream implements TypedInput { | |
public TypedInputStream(String mime_type, InputStream in) { | |
_mime_type = mime_type; | |
_in = new BufferedInputStream(in); | |
} | |
@Override | |
public String mimeType() { | |
return _mime_type; | |
} | |
@Override | |
public long length() { | |
return -1; | |
} | |
@Override | |
public InputStream in() throws IOException { | |
return _in; | |
} | |
private final String _mime_type; | |
public final InputStream _in; | |
} | |
private Map<String, String> prepareHeaders(List<Header> headers) { | |
if (headers == null) return null; | |
final Map<String, String> header_map = newHashMap(); | |
for (Header header : headers) | |
header_map.put(header.getName(), header.getValue()); | |
return header_map; | |
} | |
private static final PatternMatcher _content_type_header = new PatternMatcher(HttpRequest.HEADER_CONTENT_TYPE, PatternMatcher.PATTERN_LITERAL); | |
private static final Matcher _sanitize_passwords = Pattern.compile("(p=)[^&]*").matcher(""); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Sets the AndroidCookieStore as the CookieStore to use for managing magic-cookies | |
final CookieManager cookie_manager = new CookieManager(cookie_jar, CookiePolicy.ACCEPT_ORIGINAL_SERVER); | |
CookieHandler.setDefault(new CookieManager()); | |
// Configures the RestAdapter to use RecommendedAndroidClient instead of AndroidHttpClient | |
RestAdapter rest_adapter = new RestAdapter.Builder().setServer("http://www.example.com) | |
.setClient(http_client).build(); |
setClient(http_client) ? what is http_client, is not defined anywhere!.
Hi
I have found one good example here
Android Session Management Using SharedPreferences
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Where can I get the code for com.keepandshare.android.utils.CryptographyUtil?
Thanks.