Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Minor clean up

  • Loading branch information...
commit b5145426947331130fccfe03c5bf102f823e0c63 1 parent cdb9c5a
Stephane Landelle slandelle authored
2  .gitignore
View
@@ -15,6 +15,6 @@ nbproject
.DS_Store
target
test-output
-/META-INF/MANIFEST.MF
+MANIFEST.MF
work
atlassian-ide-plugin.xml
55 api/src/main/java/com/ning/http/client/providers/ResponseBase.java
View
@@ -3,8 +3,10 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
+import java.util.Collections;
import java.util.List;
+import com.ning.http.client.Cookie;
import com.ning.http.client.FluentCaseInsensitiveStringsMap;
import com.ning.http.client.HttpResponseBodyPart;
import com.ning.http.client.HttpResponseHeaders;
@@ -19,6 +21,7 @@
protected final List<HttpResponseBodyPart> bodyParts;
protected final HttpResponseHeaders headers;
protected final HttpResponseStatus status;
+ private List<Cookie> cookies;
protected ResponseBase(HttpResponseStatus status,
HttpResponseHeaders headers,
@@ -81,34 +84,52 @@ public String getResponseBody() throws IOException {
}
public String getResponseBody(String charset) throws IOException {
- String contentType = getContentType();
- if (contentType != null && charset == null) {
- charset = AsyncHttpProviderUtils.parseCharset(contentType);
- }
-
- if (charset == null) {
- charset = DEFAULT_CHARSET;
- }
-
- return AsyncHttpProviderUtils.contentToString(bodyParts, charset);
+ return AsyncHttpProviderUtils.contentToString(bodyParts, calculateCharset(charset));
}
/* @Override */
public InputStream getResponseBodyAsStream() throws IOException {
return AsyncHttpProviderUtils.contentAsStream(bodyParts);
}
+
+ protected abstract List<Cookie> buildCookies();
+
+ public List<Cookie> getCookies() {
- protected String calculateCharset() {
- String charset = null;
- String contentType = getContentType();
- if (contentType != null) {
- charset = AsyncHttpProviderUtils.parseCharset(contentType);
+ if (headers == null) {
+ return Collections.emptyList();
}
- if (charset == null) {
- charset = DEFAULT_CHARSET;
+ if (cookies == null) {
+ cookies = buildCookies();
}
+ return cookies;
+
+ }
+
+ protected String calculateCharset(String charset) {
+
+ if (charset == null) {
+ String contentType = getContentType();
+ if (contentType != null) {
+ charset = AsyncHttpProviderUtils.parseCharset(contentType);
+ } else {
+ charset = DEFAULT_CHARSET;
+ }
+ }
+
return charset;
}
+ public boolean hasResponseStatus() {
+ return status != null;
+ }
+
+ public boolean hasResponseHeaders() {
+ return headers != null && !headers.getHeaders().isEmpty();
+ }
+
+ public boolean hasResponseBody() {
+ return bodyParts != null && !bodyParts.isEmpty();
+ }
}
47 api/src/main/java/com/ning/http/client/providers/jdk/JDKResponse.java
View
@@ -26,7 +26,6 @@
import java.util.Map;
public class JDKResponse extends ResponseBase {
- private final List<Cookie> cookies = new ArrayList<Cookie>();
public JDKResponse(HttpResponseStatus status,
HttpResponseHeaders headers,
@@ -47,46 +46,18 @@ public String getResponseBodyExcerpt(int maxLength, String charset) throws IOExc
}
/* @Override */
- public List<Cookie> getCookies() {
- if (headers == null) {
- return Collections.emptyList();
- }
- if (cookies.isEmpty()) {
- for (Map.Entry<String, List<String>> header : headers.getHeaders().entrySet()) {
- if (header.getKey().equalsIgnoreCase("Set-Cookie")) {
- // TODO: ask for parsed header
- List<String> v = header.getValue();
- for (String value : v) {
- Cookie cookie = AsyncHttpProviderUtils.parseCookie(value);
- cookies.add(cookie);
- }
+ public List<Cookie> buildCookies() {
+ List<Cookie> cookies = new ArrayList<Cookie>();
+ for (Map.Entry<String, List<String>> header : headers.getHeaders().entrySet()) {
+ if (header.getKey().equalsIgnoreCase("Set-Cookie")) {
+ // TODO: ask for parsed header
+ List<String> v = header.getValue();
+ for (String value : v) {
+ Cookie cookie = AsyncHttpProviderUtils.parseCookie(value);
+ cookies.add(cookie);
}
}
}
return Collections.unmodifiableList(cookies);
}
-
- /**
- * {@inheritDoc}
- */
- /* @Override */
- public boolean hasResponseStatus() {
- return (bodyParts != null ? true : false);
- }
-
- /**
- * {@inheritDoc}
- */
- /* @Override */
- public boolean hasResponseHeaders() {
- return (headers != null ? true : false);
- }
-
- /**
- * {@inheritDoc}
- */
- /* @Override */
- public boolean hasResponseBody() {
- return (bodyParts != null && bodyParts.size() > 0 ? true : false);
- }
}
60 providers/apache/src/main/java/com/ning/http/client/providers/apache/ApacheResponse.java
View
@@ -27,9 +27,7 @@
public class ApacheResponse extends ResponseBase {
- private final List<Cookie> cookies = new ArrayList<Cookie>();
-
- public ApacheResponse(HttpResponseStatus status,
+ public ApacheResponse(HttpResponseStatus status,
HttpResponseHeaders headers,
List<HttpResponseBodyPart> bodyParts) {
super(status, headers, bodyParts);
@@ -44,60 +42,24 @@ public String getResponseBodyExcerpt(int maxLength) throws IOException {
/* @Override */
public String getResponseBodyExcerpt(int maxLength, String charset) throws IOException {
- String contentType = getContentType();
- if (contentType != null && charset == null) {
- charset = AsyncHttpProviderUtils.parseCharset(contentType);
- }
-
- if (charset == null) {
- charset = DEFAULT_CHARSET;
- }
-
+ charset = calculateCharset(charset);
String response = AsyncHttpProviderUtils.contentToString(bodyParts, charset);
return response.length() <= maxLength ? response : response.substring(0, maxLength);
}
/* @Override */
- public List<Cookie> getCookies() {
- if (headers == null) {
- return Collections.emptyList();
- }
- if (cookies.isEmpty()) {
- for (Map.Entry<String, List<String>> header : headers.getHeaders().entrySet()) {
- if (header.getKey().equalsIgnoreCase("Set-Cookie")) {
- // TODO: ask for parsed header
- List<String> v = header.getValue();
- for (String value : v) {
- Cookie cookie = AsyncHttpProviderUtils.parseCookie(value);
- cookies.add(cookie);
- }
+ public List<Cookie> buildCookies() {
+ List<Cookie> cookies = new ArrayList<Cookie>();
+ for (Map.Entry<String, List<String>> header : headers.getHeaders().entrySet()) {
+ if (header.getKey().equalsIgnoreCase("Set-Cookie")) {
+ // TODO: ask for parsed header
+ List<String> v = header.getValue();
+ for (String value : v) {
+ Cookie cookie = AsyncHttpProviderUtils.parseCookie(value);
+ cookies.add(cookie);
}
}
}
return Collections.unmodifiableList(cookies);
}
-
- /**
- * {@inheritDoc}
- */
- /* @Override */
- public boolean hasResponseStatus() {
- return (bodyParts != null ? true : false);
- }
-
- /**
- * {@inheritDoc}
- */
- /* @Override */
- public boolean hasResponseHeaders() {
- return (headers != null ? true : false);
- }
-
- /**
- * {@inheritDoc}
- */
- /* @Override */
- public boolean hasResponseBody() {
- return (bodyParts != null && bodyParts.size() > 0 ? true : false);
- }
}
61 providers/grizzly/src/main/java/com/ning/http/client/providers/grizzly/GrizzlyResponse.java
View
@@ -45,9 +45,6 @@
public class GrizzlyResponse extends ResponseBase {
private final Buffer responseBody;
- private List<Cookie> cookies;
-
-
// ------------------------------------------------------------ Constructors
@@ -93,9 +90,7 @@ public InputStream getResponseBodyAsStream() throws IOException {
* {@inheritDoc}
*/
public String getResponseBodyExcerpt(int maxLength, String charset) throws IOException {
- if (charset == null) {
- charset = calculateCharset();
- }
+ charset = calculateCharset(charset);
final int len = Math.min(responseBody.remaining(), maxLength);
final int pos = responseBody.position();
return responseBody.toStringContent(getCharset(charset), pos, len + pos);
@@ -148,55 +143,21 @@ public String getResponseBody() throws IOException {
/**
* {@inheritDoc}
*/
- public List<Cookie> getCookies() {
-
- if (headers == null) {
- return Collections.emptyList();
- }
-
- if (cookies == null) {
- List<String> values = headers.getHeaders().get("set-cookie");
- if (values != null && !values.isEmpty()) {
- CookiesBuilder.ServerCookiesBuilder builder =
- new CookiesBuilder.ServerCookiesBuilder(false);
- for (String header : values) {
- builder.parse(header);
- }
- cookies = convertCookies(builder.build());
+ public List<Cookie> buildCookies() {
- } else {
- cookies = Collections.unmodifiableList(Collections.<Cookie>emptyList());
+ List<String> values = headers.getHeaders().get("set-cookie");
+ if (values != null && !values.isEmpty()) {
+ CookiesBuilder.ServerCookiesBuilder builder = new CookiesBuilder.ServerCookiesBuilder(false);
+ for (String header : values) {
+ builder.parse(header);
}
- }
- return cookies;
-
- }
-
-
- /**
- * {@inheritDoc}
- */
- public boolean hasResponseStatus() {
- return (status != null);
- }
-
-
- /**
- * {@inheritDoc}
- */
- public boolean hasResponseHeaders() {
- return (headers != null && !headers.getHeaders().isEmpty());
- }
+ return convertCookies(builder.build());
-
- /**
- * {@inheritDoc}
- */
- public boolean hasResponseBody() {
- return (bodyParts != null && !bodyParts.isEmpty());
+ } else {
+ return Collections.unmodifiableList(Collections.<Cookie>emptyList());
+ }
}
-
// --------------------------------------------------------- Private Methods
56 providers/netty/src/main/java/com/ning/http/client/providers/netty/NettyResponse.java
View
@@ -32,7 +32,6 @@
* Wrapper around the {@link com.ning.http.client.Response} API.
*/
public class NettyResponse extends ResponseBase {
- private final List<Cookie> cookies = new ArrayList<Cookie>();
public NettyResponse(HttpResponseStatus status,
HttpResponseHeaders headers,
@@ -47,56 +46,23 @@ public String getResponseBodyExcerpt(int maxLength) throws IOException {
public String getResponseBodyExcerpt(int maxLength, String charset) throws IOException {
// should be fine; except that it may split multi-byte chars (last char may become '?')
- if (charset == null) {
- charset = calculateCharset();
- }
+ charset = calculateCharset(charset);
byte[] b = AsyncHttpProviderUtils.contentToBytes(bodyParts, maxLength);
return new String(b, charset);
}
-
- /* @Override */
-
- public List<Cookie> getCookies() {
- if (headers == null) {
- return Collections.emptyList();
- }
- if (cookies.isEmpty()) {
- for (Map.Entry<String, List<String>> header : headers.getHeaders().entrySet()) {
- if (header.getKey().equalsIgnoreCase("Set-Cookie")) {
- // TODO: ask for parsed header
- List<String> v = header.getValue();
- for (String value : v) {
- Cookie cookie = AsyncHttpProviderUtils.parseCookie(value);
- cookies.add(cookie);
- }
+
+ protected List<Cookie> buildCookies() {
+ List<Cookie> cookies = new ArrayList<Cookie>();
+ for (Map.Entry<String, List<String>> header : headers.getHeaders().entrySet()) {
+ if (header.getKey().equalsIgnoreCase("Set-Cookie")) {
+ // TODO: ask for parsed header
+ List<String> v = header.getValue();
+ for (String value : v) {
+ Cookie cookie = AsyncHttpProviderUtils.parseCookie(value);
+ cookies.add(cookie);
}
}
}
return Collections.unmodifiableList(cookies);
}
-
- /**
- * {@inheritDoc}
- */
- /* @Override */
- public boolean hasResponseStatus() {
- return (status != null ? true : false);
- }
-
- /**
- * {@inheritDoc}
- */
- /* @Override */
- public boolean hasResponseHeaders() {
- return (headers != null ? true : false);
- }
-
- /**
- * {@inheritDoc}
- */
- /* @Override */
- public boolean hasResponseBody() {
- return (bodyParts != null && bodyParts.size() > 0 ? true : false);
- }
-
}
Please sign in to comment.
Something went wrong with that request. Please try again.