com.ning.http.client.ResponseBase Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of async-http-client Show documentation
Show all versions of async-http-client Show documentation
Async Http Client library purpose is to allow Java applications to easily execute HTTP requests and
asynchronously process the HTTP responses.
/*
* Copyright (c) 2014 AsyncHttpClient Project. All rights reserved.
*
* This program is licensed to you under the Apache License Version 2.0,
* and you may not use this file except in compliance with the Apache License Version 2.0.
* You may obtain a copy of the Apache License Version 2.0 at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the Apache License Version 2.0 is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
*/
package com.ning.http.client;
import static com.ning.http.util.MiscUtils.isNonEmpty;
import com.ning.http.client.cookie.Cookie;
import com.ning.http.client.uri.Uri;
import com.ning.http.util.AsyncHttpProviderUtils;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.List;
public abstract class ResponseBase implements Response {
protected final HttpResponseStatus status;
protected final HttpResponseHeaders headers;
protected final List bodyParts;
private List cookies;
protected ResponseBase(HttpResponseStatus status, HttpResponseHeaders headers, List bodyParts) {
this.bodyParts = bodyParts;
this.headers = headers;
this.status = status;
}
protected abstract List buildCookies();
protected Charset calculateCharset(String charset) {
if (charset == null) {
String contentType = getContentType();
if (contentType != null)
charset = AsyncHttpProviderUtils.parseCharset(contentType); // parseCharset can return null
}
return charset != null ? Charset.forName(charset) : AsyncHttpProviderUtils.DEFAULT_CHARSET;
}
@Override
public final int getStatusCode() {
return status.getStatusCode();
}
@Override
public final String getStatusText() {
return status.getStatusText();
}
@Override
public final Uri getUri() {
return status.getUri();
}
@Override
public final String getContentType() {
return headers != null ? getHeader("Content-Type") : null;
}
@Override
public final String getHeader(String name) {
return headers != null ? getHeaders().getFirstValue(name) : null;
}
@Override
public final List getHeaders(String name) {
return headers != null ? getHeaders().get(name) : Collections. emptyList();
}
@Override
public final FluentCaseInsensitiveStringsMap getHeaders() {
return headers != null ? headers.getHeaders() : new FluentCaseInsensitiveStringsMap();
}
@Override
public final boolean isRedirected() {
switch (status.getStatusCode()) {
case 301:
case 302:
case 303:
case 307:
case 308:
return true;
default:
return false;
}
}
@Override
public List getCookies() {
if (cookies == null)
cookies = headers != null ? buildCookies() : Collections. emptyList();
return cookies;
}
@Override
public boolean hasResponseStatus() {
return status != null;
}
@Override
public boolean hasResponseHeaders() {
return headers != null && isNonEmpty(headers.getHeaders());
}
@Override
public boolean hasResponseBody() {
return isNonEmpty(bodyParts);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy