-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
110 additions
and
12 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
src/main/java/io/github/dbstarll/utils/http/client/response/AbstractResponseHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package io.github.dbstarll.utils.http.client.response; | ||
|
||
import org.apache.hc.client5.http.HttpResponseException; | ||
import org.apache.hc.client5.http.impl.classic.AbstractHttpClientResponseHandler; | ||
import org.apache.hc.core5.http.ClassicHttpResponse; | ||
import org.apache.hc.core5.http.HttpEntity; | ||
import org.apache.hc.core5.http.HttpStatus; | ||
import org.apache.hc.core5.http.io.entity.EntityUtils; | ||
|
||
import java.io.IOException; | ||
|
||
public abstract class AbstractResponseHandler<T> extends AbstractHttpClientResponseHandler<T> { | ||
private final boolean alwaysProcessEntity; | ||
|
||
protected AbstractResponseHandler(final boolean alwaysProcessEntity) { | ||
this.alwaysProcessEntity = alwaysProcessEntity; | ||
} | ||
|
||
@Override | ||
public final T handleResponse(final ClassicHttpResponse response) throws IOException { | ||
final HttpEntity entity = response.getEntity(); | ||
if (response.getCode() >= HttpStatus.SC_REDIRECTION && (entity == null || !alwaysProcessEntity)) { | ||
EntityUtils.consume(entity); | ||
throw new HttpResponseException(response.getCode(), response.getReasonPhrase()); | ||
} | ||
return entity == null ? null : handleEntity(entity); | ||
} | ||
} |
8 changes: 4 additions & 4 deletions
8
src/main/java/io/github/dbstarll/utils/http/client/response/BasicResponseHandlerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
package io.github.dbstarll.utils.http.client.response; | ||
|
||
import org.apache.hc.client5.http.impl.classic.BasicHttpClientResponseHandler; | ||
|
||
public class BasicResponseHandlerFactory extends AbstractResponseHandlerFactory { | ||
/** | ||
* 构造BasicResponseHandlerFactory. | ||
* | ||
* @param alwaysProcessEntity 在返回错误的状态码时,是否还要继续解析entity | ||
*/ | ||
public BasicResponseHandlerFactory() { | ||
addResponseHandler(String.class, new BasicHttpClientResponseHandler()); | ||
public BasicResponseHandlerFactory(final boolean alwaysProcessEntity) { | ||
addResponseHandler(String.class, new StringResponseHandler(alwaysProcessEntity)); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/io/github/dbstarll/utils/http/client/response/StringResponseHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.github.dbstarll.utils.http.client.response; | ||
|
||
import org.apache.hc.client5.http.impl.classic.BasicHttpClientResponseHandler; | ||
import org.apache.hc.core5.http.HttpEntity; | ||
|
||
import java.io.IOException; | ||
|
||
public final class StringResponseHandler extends AbstractResponseHandler<String> { | ||
private static final BasicHttpClientResponseHandler DEFAULT_HANDLER = new BasicHttpClientResponseHandler(); | ||
|
||
/** | ||
* @param alwaysProcessEntity 在返回错误的状态码时,是否还要继续解析entity | ||
*/ | ||
public StringResponseHandler(final boolean alwaysProcessEntity) { | ||
super(alwaysProcessEntity); | ||
} | ||
|
||
@Override | ||
public String handleEntity(final HttpEntity entity) throws IOException { | ||
return DEFAULT_HANDLER.handleEntity(entity); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/test/java/io/github/dbstarll/utils/http/client/response/StringResponseHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package io.github.dbstarll.utils.http.client.response; | ||
|
||
import org.apache.hc.client5.http.HttpResponseException; | ||
import org.apache.hc.core5.http.ClassicHttpResponse; | ||
import org.apache.hc.core5.http.io.support.ClassicResponseBuilder; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
import static org.junit.jupiter.api.Assertions.assertThrowsExactly; | ||
|
||
class StringResponseHandlerTest { | ||
@Test | ||
void handleResponse() throws IOException { | ||
final ClassicHttpResponse response = ClassicResponseBuilder.create(200).setEntity("abc").build(); | ||
assertEquals("abc", new StringResponseHandler(false).handleResponse(response)); | ||
} | ||
|
||
@Test | ||
void handleNull() throws IOException { | ||
final ClassicHttpResponse response = ClassicResponseBuilder.create(200).build(); | ||
assertNull(new StringResponseHandler(false).handleResponse(response)); | ||
} | ||
|
||
@Test | ||
void handle404() { | ||
final ClassicHttpResponse response = ClassicResponseBuilder.create(404).setEntity("abc").build(); | ||
final HttpResponseException e = assertThrowsExactly(HttpResponseException.class, | ||
() -> new StringResponseHandler(false).handleResponse(response)); | ||
assertEquals(404, e.getStatusCode()); | ||
assertEquals("Not Found", e.getReasonPhrase()); | ||
} | ||
|
||
@Test | ||
void alwaysProcessEntity() throws IOException { | ||
final ClassicHttpResponse response = ClassicResponseBuilder.create(404).setEntity("abc").build(); | ||
assertEquals("abc", new StringResponseHandler(true).handleResponse(response)); | ||
} | ||
|
||
@Test | ||
void alwaysProcessEntityNull() { | ||
final ClassicHttpResponse response = ClassicResponseBuilder.create(404).build(); | ||
final HttpResponseException e = assertThrowsExactly(HttpResponseException.class, | ||
() -> new StringResponseHandler(true).handleResponse(response)); | ||
assertEquals(404, e.getStatusCode()); | ||
assertEquals("Not Found", e.getReasonPhrase()); | ||
} | ||
} |