use of cz.msebera.android.httpclient.Header in project android-async-http by loopj.
the class Http401AuthSample method getResponseHandler.
@Override
public ResponseHandlerInterface getResponseHandler() {
return new BaseJsonHttpResponseHandler<SampleJSON>() {
@Override
public void onStart() {
clearOutputs();
}
@Override
public void onSuccess(int statusCode, Header[] headers, String rawJsonResponse, SampleJSON response) {
debugHeaders(LOG_TAG, headers);
debugStatusCode(LOG_TAG, statusCode);
if (response != null) {
debugResponse(LOG_TAG, rawJsonResponse);
}
}
@Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, String rawJsonData, SampleJSON errorResponse) {
debugHeaders(LOG_TAG, headers);
debugStatusCode(LOG_TAG, statusCode);
debugThrowable(LOG_TAG, throwable);
// Ask the user for credentials if required by the server.
if (statusCode == 401) {
String realm = "Protected Page";
String authType = null;
// Cycle through the headers and look for the WWW-Authenticate header.
for (Header header : headers) {
String headerName = header.getName();
if (HEADER_WWW_AUTHENTICATE.equalsIgnoreCase(headerName)) {
String headerValue = header.getValue().trim();
String headerValueLowerCase = headerValue.toLowerCase(Locale.US);
// Get the type of auth requested.
int charPos = headerValueLowerCase.indexOf(' ');
if (0 < charPos) {
authType = headerValueLowerCase.substring(0, charPos);
// The second part should begin with a "realm=" prefix.
if (headerValueLowerCase.substring(1 + charPos).startsWith(HEADER_REALM_PREFIX)) {
// The new realm value, including any possible wrapping quotation.
realm = headerValue.substring(1 + charPos + HEADER_REALM_PREFIX.length());
// If realm starts with a quote, remove surrounding quotes.
if (realm.charAt(0) == '"' || realm.charAt(0) == '\'') {
realm = realm.substring(1, realm.length() - 1);
}
}
}
}
}
// We will support basic auth in this sample.
if (authType != null && HEADER_BASIC.equals(authType)) {
// Show a dialog for the user and request user/pass.
Log.d(LOG_TAG, HEADER_REALM_PREFIX + realm);
// Present the dialog.
postRunnable(new DialogRunnable(realm));
}
}
}
@Override
protected SampleJSON parseResponse(String rawJsonData, boolean isFailure) throws Throwable {
return new ObjectMapper().readValues(new JsonFactory().createParser(rawJsonData), SampleJSON.class).next();
}
};
}
use of cz.msebera.android.httpclient.Header in project android-async-http by loopj.
the class RangeResponseSample method getResponseHandler.
@Override
public ResponseHandlerInterface getResponseHandler() {
return new RangeFileAsyncHttpResponseHandler(file) {
@Override
public void onSuccess(int statusCode, Header[] headers, File file) {
debugHeaders(LOG_TAG, headers);
debugStatusCode(LOG_TAG, statusCode);
if (fileSize < 1) {
boolean supportsRange = false;
// Cycle through the headers and look for the Content-Length header.
for (Header header : headers) {
String headerName = header.getName();
if (CONTENT_LENGTH.equals(headerName)) {
fileSize = Long.parseLong(header.getValue());
} else if (ACCEPT_RANGES.equals(headerName)) {
supportsRange = true;
}
}
// Is the content length known?
if (!supportsRange || fileSize < 1) {
Toast.makeText(RangeResponseSample.this, "Unable to determine remote file's size, or\nremote server doesn't support ranges", Toast.LENGTH_LONG).show();
}
}
// If remote file size is known, request next portion.
if (fileSize > 0) {
debugFileResponse(file);
// Send a new request for the same resource.
sendNextRangeRequest();
}
}
@Override
public void onFailure(int statusCode, Header[] headers, Throwable e, File file) {
debugHeaders(LOG_TAG, headers);
debugStatusCode(LOG_TAG, statusCode);
debugThrowable(LOG_TAG, e);
debugFileResponse(file);
}
@Override
public void updateRequestHeaders(HttpUriRequest uriRequest) {
// Call super so appending could work.
super.updateRequestHeaders(uriRequest);
// Length of the downloaded content thus far.
long length = file.length();
// Request the next portion of the file to be downloaded.
uriRequest.setHeader("Range", "bytes=" + length + "-" + (length + CHUNK_SIZE - 1));
}
void debugFileResponse(File file) {
debugResponse(LOG_TAG, "File size thus far: " + file.length() + " bytes");
}
};
}
use of cz.msebera.android.httpclient.Header in project android-async-http by loopj.
the class SampleParentActivity method debugHeaders.
protected final void debugHeaders(String TAG, Header[] headers) {
if (headers != null) {
Log.d(TAG, "Return Headers:");
StringBuilder builder = new StringBuilder();
for (Header h : headers) {
String _h = String.format(Locale.US, "%s : %s", h.getName(), h.getValue());
Log.d(TAG, _h);
builder.append(_h);
builder.append("\n");
}
addView(getColoredView(YELLOW, builder.toString()));
}
}
use of cz.msebera.android.httpclient.Header in project android-async-http by loopj.
the class IntentUtil method serializeHeaders.
public static String[] serializeHeaders(Header[] headers) {
if (headers == null) {
return new String[0];
}
String[] rtn = new String[headers.length * 2];
int index = -1;
for (Header h : headers) {
rtn[++index] = h.getName();
rtn[++index] = h.getValue();
}
return rtn;
}
use of cz.msebera.android.httpclient.Header in project android-async-http by loopj.
the class MyRedirectHandler method getLocationURI.
@Override
public URI getLocationURI(final HttpResponse response, final HttpContext context) throws ProtocolException {
if (response == null) {
throw new IllegalArgumentException("HTTP response may not be null");
}
//get the location header to find out where to redirect to
Header locationHeader = response.getFirstHeader("location");
if (locationHeader == null) {
// got a redirect response, but no location header
throw new ProtocolException("Received redirect response " + response.getStatusLine() + " but no location header");
}
//HERE IS THE MODIFIED LINE OF CODE
String location = locationHeader.getValue().replaceAll(" ", "%20");
URI uri;
try {
uri = new URI(location);
} catch (URISyntaxException ex) {
throw new ProtocolException("Invalid redirect URI: " + location, ex);
}
HttpParams params = response.getParams();
// Location = "Location" ":" absoluteURI
if (!uri.isAbsolute()) {
if (params.isParameterTrue(ClientPNames.REJECT_RELATIVE_REDIRECT)) {
throw new ProtocolException("Relative redirect location '" + uri + "' not allowed");
}
// Adjust location URI
HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
if (target == null) {
throw new IllegalStateException("Target host not available " + "in the HTTP context");
}
HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
try {
URI requestURI = new URI(request.getRequestLine().getUri());
URI absoluteRequestURI = URIUtils.rewriteURI(requestURI, target, true);
uri = URIUtils.resolve(absoluteRequestURI, uri);
} catch (URISyntaxException ex) {
throw new ProtocolException(ex.getMessage(), ex);
}
}
if (params.isParameterFalse(ClientPNames.ALLOW_CIRCULAR_REDIRECTS)) {
RedirectLocations redirectLocations = (RedirectLocations) context.getAttribute(REDIRECT_LOCATIONS);
if (redirectLocations == null) {
redirectLocations = new RedirectLocations();
context.setAttribute(REDIRECT_LOCATIONS, redirectLocations);
}
URI redirectURI;
if (uri.getFragment() != null) {
try {
HttpHost target = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
redirectURI = URIUtils.rewriteURI(uri, target, true);
} catch (URISyntaxException ex) {
throw new ProtocolException(ex.getMessage(), ex);
}
} else {
redirectURI = uri;
}
if (redirectLocations.contains(redirectURI)) {
throw new CircularRedirectException("Circular redirect to '" + redirectURI + "'");
} else {
redirectLocations.add(redirectURI);
}
}
return uri;
}
Aggregations