Skip to content

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,9 @@
<artifactId>javassist</artifactId>
<version>3.21.0-GA</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
</dependencies>
</project>
38 changes: 21 additions & 17 deletions client/src/main/java/org/asynchttpclient/RequestBuilderBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Copy link
Contributor

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.


/**
* Builder for {@link Request}
Expand Down Expand Up @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing Javadoc

this.headers.clear();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a clearHeaders method?

headers.forEach((name, value) -> this.headers.add(name, value));
Copy link
Contributor

Choose a reason for hiding this comment

The 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);
Expand Down
54 changes: 53 additions & 1 deletion client/src/main/java/org/asynchttpclient/cookie/Cookie.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The 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;
Expand Down Expand Up @@ -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;
}
Expand Down
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure this dep is really optional? I feel like the Cookie class would fail to load and crash.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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>
Expand Down Expand Up @@ -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>
Expand Down