Created
January 29, 2015 21:36
-
-
Save samskiter/ef9ce09fdd64f3c389fa to your computer and use it in GitHub Desktop.
First cut of Google-http-client retrofitting
This file contains hidden or 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
package uk.co.airsource.android.authtest; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.util.ArrayList; | |
import java.util.List; | |
import retrofit.client.Client; | |
import retrofit.client.Header; | |
import retrofit.client.Request; | |
import retrofit.client.Response; | |
import retrofit.mime.TypedInput; | |
import retrofit.mime.TypedOutput; | |
import com.google.api.client.auth.oauth2.ClientParametersAuthentication; | |
import com.google.api.client.http.AbstractHttpContent; | |
import com.google.api.client.http.GenericUrl; | |
import com.google.api.client.http.HttpContent; | |
import com.google.api.client.http.HttpHeaders; | |
import com.google.api.client.http.HttpRequest; | |
import com.google.api.client.http.HttpRequestFactory; | |
import com.google.api.client.http.HttpResponse; | |
import com.google.api.client.http.HttpTransport; | |
import com.google.api.client.http.javanet.NetHttpTransport; | |
/** | |
* Created by sduke on 28/01/2015. | |
*/ | |
public class GoogleHttpClient implements Client | |
{ | |
/** Global instance of the HTTP transport. */ | |
private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport(); | |
//(use a nethttptransport) | |
private final HttpRequestFactory requestFactory; | |
//private final Credential.Builder credentialBuilder; | |
//TODO: this goes into the authing http client. | |
private final ClientParametersAuthentication clientParametersAuthentication; | |
public GoogleHttpClient()//, Credential.Builder credentialBuilder) | |
{ | |
super(); | |
// HTTP_TRANSPORT.createRequestFactory() | |
this.requestFactory = HTTP_TRANSPORT.createRequestFactory( /*TODO: insert a request initializer here that checks for the credential*/); | |
//this.credentialBuilder = credentialBuilder; | |
clientParametersAuthentication = new ClientParametersAuthentication("api key", "api password"); | |
//this.credentialBuilder.setClientAuthentication(clientParametersAuthentication); | |
} | |
@Override | |
public Response execute(final Request request) throws IOException | |
{ | |
HttpRequest httpRequest = prepareRequest(request); | |
httpRequest.setFollowRedirects(true); | |
HttpResponse httpResponse = null; | |
httpResponse = httpRequest.execute(); | |
return readResponse(httpResponse); | |
} | |
HttpRequest prepareRequest(Request request) throws IOException { | |
TypedOutput body = request.getBody(); | |
HttpContent httpContent = null; | |
if (body != null) | |
{ | |
httpContent = new HttpContentStream(body); | |
} | |
HttpRequest httpRequest = requestFactory.buildRequest( | |
request.getMethod(), | |
new GenericUrl(request.getUrl()), | |
httpContent | |
); | |
HttpHeaders httpRequestHeaders = httpRequest.getHeaders(); | |
for (Header header :request.getHeaders()) { | |
httpRequestHeaders.set(header.getName(), header.getValue()); | |
} | |
httpRequest.setHeaders(httpRequestHeaders); | |
return httpRequest; | |
} | |
Response readResponse(HttpResponse httpResponse) throws IOException { | |
int status = httpResponse.getStatusCode(); | |
String reason = httpResponse.getStatusMessage(); | |
if (reason == null) reason = ""; | |
List<Header> headers = new ArrayList<Header>(); | |
HttpHeaders httpResponseHeaders = httpResponse.getHeaders(); | |
for (String name : httpResponseHeaders.keySet()) | |
{ | |
for (String value : httpResponseHeaders.getHeaderStringValues(name)) | |
{ | |
headers.add(new Header(name, value)); | |
} | |
} | |
String mimeType = httpResponse.getContentType(); | |
Long lengthObj = httpResponseHeaders.getContentLength(); | |
long length = lengthObj != null ? lengthObj : -1; | |
InputStream stream = httpResponse.getContent(); | |
TypedInput responseBody = new TypedInputStream(mimeType, length, stream); | |
return new Response(httpResponse.getRequest().getUrl().build(), status, reason, headers, responseBody); | |
} | |
// public void doAuth() | |
// { | |
// //a temp method to hold some code for a bit | |
// PasswordTokenRequest passwordTokenRequest = new PasswordTokenRequest(null, null, null, null, null); | |
// TokenResponse tokenResponse = passwordTokenRequest.setClientAuthentication(clientParametersAuthentication).execute(); | |
// Credential credential = new Credential(BearerToken.authorizationHeaderAccessMethod()).setFromTokenResponse(tokenResponse); | |
// //as per the | |
// } | |
private static class HttpContentStream extends AbstractHttpContent { | |
private final TypedOutput output; | |
private HttpContentStream(TypedOutput output) { | |
super(output.mimeType()); | |
this.output = output; | |
} | |
@Override | |
public void writeTo(OutputStream outputStream) throws IOException | |
{ | |
output.writeTo(outputStream); | |
} | |
} | |
private static class TypedInputStream implements TypedInput { | |
private final String mimeType; | |
private final long length; | |
private final InputStream stream; | |
private TypedInputStream(String mimeType, long length, InputStream stream) { | |
this.mimeType = mimeType; | |
this.length = length; | |
this.stream = stream; | |
} | |
@Override public String mimeType() { | |
return mimeType; | |
} | |
@Override public long length() { | |
return length; | |
} | |
@Override public InputStream in() throws IOException { | |
return stream; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment