-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add some useful API methods #1295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,13 +15,22 @@ | |
*/ | ||
package org.asynchttpclient; | ||
|
||
import static org.asynchttpclient.util.HttpUtils.*; | ||
import static org.asynchttpclient.util.MiscUtils.isNonEmpty; | ||
import io.netty.handler.codec.http.DefaultHttpHeaders; | ||
import io.netty.handler.codec.http.HttpHeaders; | ||
import io.netty.resolver.DefaultNameResolver; | ||
import io.netty.resolver.NameResolver; | ||
import io.netty.util.concurrent.ImmediateEventExecutor; | ||
import org.asynchttpclient.channel.ChannelPoolPartitioning; | ||
import org.asynchttpclient.cookie.Cookie; | ||
import org.asynchttpclient.proxy.ProxyServer; | ||
import org.asynchttpclient.request.body.generator.BodyGenerator; | ||
import org.asynchttpclient.request.body.generator.ReactiveStreamsBodyGenerator; | ||
import org.asynchttpclient.request.body.multipart.Part; | ||
import org.asynchttpclient.uri.Uri; | ||
import org.asynchttpclient.util.UriEncoder; | ||
import org.reactivestreams.Publisher; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.File; | ||
import java.io.InputStream; | ||
|
@@ -34,17 +43,9 @@ | |
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.asynchttpclient.channel.ChannelPoolPartitioning; | ||
import org.asynchttpclient.cookie.Cookie; | ||
import org.asynchttpclient.proxy.ProxyServer; | ||
import org.asynchttpclient.request.body.generator.BodyGenerator; | ||
import org.asynchttpclient.request.body.generator.ReactiveStreamsBodyGenerator; | ||
import org.asynchttpclient.request.body.multipart.Part; | ||
import org.asynchttpclient.uri.Uri; | ||
import org.asynchttpclient.util.UriEncoder; | ||
import org.reactivestreams.Publisher; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import static org.asynchttpclient.util.HttpUtils.parseCharset; | ||
import static org.asynchttpclient.util.HttpUtils.validateSupportedScheme; | ||
import static org.asynchttpclient.util.MiscUtils.isNonEmpty; | ||
|
||
/** | ||
* Builder for {@link Request} | ||
|
@@ -194,14 +195,17 @@ public T setHeaders(HttpHeaders headers) { | |
public T setHeaders(Map<String, Collection<String>> headers) { | ||
this.headers.clear(); | ||
if (headers != null) { | ||
for (Map.Entry<String, Collection<String>> entry : headers.entrySet()) { | ||
String headerName = entry.getKey(); | ||
this.headers.add(headerName, entry.getValue()); | ||
} | ||
headers.forEach((name, values) -> this.headers.add(name, values)); | ||
} | ||
return asDerivedType(); | ||
} | ||
|
||
public T setSingleHeaders(Map<String, String> headers) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing Javadoc |
||
this.headers.clear(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add a |
||
headers.forEach((name, value) -> this.headers.add(name, value)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing null check, like in setHeaders |
||
return asDerivedType(); | ||
} | ||
|
||
private void lazyInitCookies() { | ||
if (this.cookies == null) | ||
this.cookies = new ArrayList<>(3); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,16 +12,65 @@ | |
*/ | ||
package org.asynchttpclient.cookie; | ||
|
||
import static org.asynchttpclient.cookie.CookieUtil.*; | ||
import static org.asynchttpclient.cookie.CookieUtil.validateCookieAttribute; | ||
import static org.asynchttpclient.cookie.CookieUtil.validateCookieName; | ||
import static org.asynchttpclient.cookie.CookieUtil.validateCookieValue; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please revert |
||
|
||
public class Cookie { | ||
|
||
/** | ||
* Method to get a {@link org.asynchttpclient.cookie.Cookie} from {@code javax.servlet} package's {@link javax.servlet.http.Cookie} | ||
* | ||
* @param cookie base entity to convert | ||
* @param wrap true if the value of this {@link Cookie} is to be wrapped with double quotes. | ||
* @return converted {@link Cookie} | ||
*/ | ||
public static Cookie newCookie(javax.servlet.http.Cookie cookie, boolean wrap) { | ||
return new Cookie(cookie.getName(), cookie.getValue(), wrap, cookie.getDomain(), cookie.getPath(), cookie.getMaxAge(), cookie.getSecure(), cookie.isHttpOnly()); | ||
} | ||
|
||
/** | ||
* Method to get a valid {@link org.asynchttpclient.cookie.Cookie} from {@code javax.servlet} package's {@link javax.servlet.http.Cookie} | ||
* | ||
* @param cookie base entity to convert | ||
* @param wrap true if the value of this {@link Cookie} is to be wrapped with double quotes. | ||
* @return converted {@link Cookie} | ||
* @throws IllegalArgumentException if any part of {@code cookie} is invalid | ||
*/ | ||
public static Cookie newValidCookie(javax.servlet.http.Cookie cookie, boolean wrap) { | ||
return new Cookie(validateCookieName(cookie.getName()), validateCookieValue(cookie.getValue()), wrap, validateCookieAttribute("domain", cookie.getDomain()), validateCookieAttribute("path", cookie.getPath()), cookie.getMaxAge(), cookie.getSecure(), cookie.isHttpOnly()); | ||
} | ||
|
||
/** | ||
* Method to get a {@link org.asynchttpclient.cookie.Cookie} from Netty's {@link io.netty.handler.codec.http.cookie.Cookie} | ||
* | ||
* @param cookie base entity to convert | ||
* @return converted {@link Cookie} | ||
*/ | ||
public static Cookie newCookie(io.netty.handler.codec.http.cookie.Cookie cookie) { | ||
return new Cookie(cookie.name(), cookie.value(), cookie.wrap(), cookie.domain(), cookie.path(), cookie.maxAge(), cookie.isSecure(), cookie.isHttpOnly()); | ||
} | ||
|
||
/** | ||
* Method to get a valid {@link org.asynchttpclient.cookie.Cookie} from Netty's {@link io.netty.handler.codec.http.cookie.Cookie} | ||
* | ||
* @param cookie base entity to convert | ||
* @return converted {@link Cookie} | ||
* @throws IllegalArgumentException if any part of {@code cookie} is invalid | ||
*/ | ||
public static Cookie newValidCookie(io.netty.handler.codec.http.cookie.Cookie cookie) { | ||
return new Cookie(validateCookieName(cookie.name()), validateCookieValue(cookie.value()), cookie.wrap(), validateCookieAttribute("domain", cookie.domain()), validateCookieAttribute("path", cookie.path()), cookie.maxAge(), cookie.isSecure(), cookie.isHttpOnly()); | ||
} | ||
|
||
public static Cookie newValidCookie(String name, String value, boolean wrap, String domain, String path, long maxAge, boolean secure, boolean httpOnly) { | ||
return new Cookie(validateCookieName(name), validateCookieValue(value), wrap, validateCookieAttribute("domain", domain), validateCookieAttribute("path", path), maxAge, secure, httpOnly); | ||
} | ||
|
||
private final String name; | ||
private final String value; | ||
/** | ||
* If value should be wrapped with double quotes during serialization | ||
*/ | ||
private final boolean wrap; | ||
private final String domain; | ||
private final String path; | ||
|
@@ -52,6 +101,9 @@ public String getValue() { | |
return value; | ||
} | ||
|
||
/** | ||
* @return true if the value of this {@link Cookie} is to be wrapped with double quotes. | ||
*/ | ||
public boolean isWrap() { | ||
return wrap; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -251,6 +251,12 @@ | |
<version>${netty.version}</version> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax.servlet</groupId> | ||
<artifactId>javax.servlet-api</artifactId> | ||
<version>${javax.servlet-api.version}</version> | ||
<optional>true</optional> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you sure this dep is really optional? I feel like the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, just import doesn't mean anything. Cookie class would be loaded (and fail) if somebody actually would use these new methods. |
||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
<dependencies> | ||
|
@@ -375,6 +381,7 @@ | |
<source.property>1.8</source.property> | ||
<target.property>1.8</target.property> | ||
<netty.version>4.0.42.Final</netty.version> | ||
<javax.servlet-api.version>3.1.0</javax.servlet-api.version> | ||
<slf4j.version>1.7.21</slf4j.version> | ||
<logback.version>1.1.7</logback.version> | ||
<testng.version>6.9.10</testng.version> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please revert imports ordering: unrelated.