If you think the Android project hubblog listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package com.github.mobile.Accounts;
/*www.java2s.com*/import android.text.TextUtils;
import org.eclipse.egit.github.core.client.GitHubRequest;
import org.eclipse.egit.github.core.client.GitHubResponse;
import java.io.IOException;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
/**
* Created with IntelliJ IDEA.
* User: donski
* Date: 10/12/13
* Time: 20:44
*/publicclass TwoFactorAuthClient extends DefaultClient {
/**
* Two-factor authentication code header
*/protectedstaticfinal String HEADER_OTP = "X-GitHub-OTP";
/**
* Two-factor authentication type by application
*/publicstaticfinalint TWO_FACTOR_AUTH_TYPE_APP = 1001;
/**
* Two-factor authentication type by sms
*/publicstaticfinalint TWO_FACTOR_AUTH_TYPE_SMS = 1002;
private String otpCode;
public TwoFactorAuthClient() {
super();
}
/**
* Set OTP code which will be added to POST requests
*
* @param otpCode
*/publicvoid setOtpCode(String otpCode) {
this.otpCode = otpCode;
}
/**
* Get response from URI and bind to specified type
*
* @param request
* @return response
* @throws java.io.IOException
*/
@Override
public GitHubResponse get(GitHubRequest request) throws IOException {
HttpURLConnection httpRequest = createGet(request.generateUri());
try {
String accept = request.getResponseContentType();
if (accept != null)
httpRequest.setRequestProperty(HEADER_ACCEPT, accept);
finalint code = httpRequest.getResponseCode();
updateRateLimits(httpRequest);
if (isOk(code))
returnnew GitHubResponse(httpRequest, getBody(request,
getStream(httpRequest)));
if (isEmpty(code))
returnnew GitHubResponse(httpRequest, null);
throw createException(getStream(httpRequest), code,
httpRequest.getResponseMessage());
} catch (IOException e) {
throw checkTwoFactorAuthError(httpRequest, e);
}
}
/**
* Post data to URI
*
* @param <V>
* @param uri
* @param params
* @param type
* @return response
* @throws IOException
*/
@Override
public <V> V post(final String uri, final Object params, final Type type)
throws IOException {
HttpURLConnection request = createPost(uri);
if (!TextUtils.isEmpty(otpCode))
request.setRequestProperty(HEADER_OTP, otpCode);
try {
return sendJson(request, params, type);
} catch (IOException e) {
throw checkTwoFactorAuthError(request, e);
}
}
private IOException checkTwoFactorAuthError(HttpURLConnection request, IOException e) throws IOException {
String otpHeader = request.getHeaderField(HEADER_OTP);
if (!TextUtils.isEmpty(otpHeader) && otpHeader.contains("required"))
return createTwoFactorAuthException(e, otpHeader);
elsereturn e;
}
private TwoFactorAuthException createTwoFactorAuthException(
IOException cause, String otpHeader) {
int twoFactorAuthType = -1;
if (otpHeader.contains("app"))
twoFactorAuthType = TWO_FACTOR_AUTH_TYPE_APP;
elseif (otpHeader.contains("sms"))
twoFactorAuthType = TWO_FACTOR_AUTH_TYPE_SMS;
returnnew TwoFactorAuthException(cause, twoFactorAuthType);
}
private <V> V sendJson(final HttpURLConnection request,
final Object params, final Type type) throws IOException {
sendParams(request, params);
finalint code = request.getResponseCode();
updateRateLimits(request);
if (isOk(code))
if (type != null)
return parseJson(getStream(request), type);
elsereturn null;
if (isEmpty(code))
return null;
throw createException(getStream(request), code,
request.getResponseMessage());
}
}