Skip to content
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

Support Apache HttpEntity as an entity type when using Apache Connector #4581

Merged
merged 2 commits into from
Sep 25, 2020
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,10 @@ private HttpEntity getHttpEntity(final ClientRequest clientRequest, final boolea
return null;
}

if (HttpEntity.class.isInstance(entity)) {
return wrapHttpEntity(clientRequest, (HttpEntity) entity);
}

final AbstractHttpEntity httpEntity = new AbstractHttpEntity() {
@Override
public boolean isRepeatable() {
Expand Down Expand Up @@ -639,6 +643,70 @@ public boolean isStreaming() {
}
};

return bufferEntity(httpEntity, bufferingEnabled);
}

private HttpEntity wrapHttpEntity(final ClientRequest clientRequest, final HttpEntity originalEntity) {
final boolean bufferingEnabled = BufferedHttpEntity.class.isInstance(originalEntity);

try {
clientRequest.setEntity(originalEntity.getContent());
} catch (IOException e) {
throw new ProcessingException(LocalizationMessages.ERROR_READING_HTTPENTITY_STREAM(e.getMessage()), e);
}

final AbstractHttpEntity httpEntity = new AbstractHttpEntity() {
@Override
public boolean isRepeatable() {
return originalEntity.isRepeatable();
}

@Override
public long getContentLength() {
return originalEntity.getContentLength();
}

@Override
public Header getContentType() {
return originalEntity.getContentType();
}

@Override
public Header getContentEncoding() {
return originalEntity.getContentEncoding();
}

@Override
public InputStream getContent() throws IOException, IllegalStateException {
return originalEntity.getContent();
}

@Override
public void writeTo(final OutputStream outputStream) throws IOException {
clientRequest.setStreamProvider(new OutboundMessageContext.StreamProvider() {
@Override
public OutputStream getOutputStream(final int contentLength) throws IOException {
return outputStream;
}
});
clientRequest.writeEntity();
}

@Override
public boolean isStreaming() {
return originalEntity.isStreaming();
}

@Override
public boolean isChunked() {
return originalEntity.isChunked();
}
};

return bufferEntity(httpEntity, bufferingEnabled);
}

private static HttpEntity bufferEntity(HttpEntity httpEntity, boolean bufferingEnabled) {
if (bufferingEnabled) {
try {
return new BufferedHttpEntity(httpEntity);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2013, 2018 Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 2013, 2020 Oracle and/or its affiliates. All rights reserved.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -15,6 +15,7 @@
#

error.buffering.entity=Error buffering the entity.
error.reading.httpentity.stream=Error reading InputStream from HttpEntity: "{0}"
failed.to.stop.client=Failed to stop the client.
# {0} - property name, e.g. jersey.config.client.httpclient.connectionManager; {1}, {2} - full class name
ignoring.value.of.property=Ignoring value of property "{0}" ("{1}") - not instance of "{2}".
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.jersey.apache.connector;

import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.InputStreamEntity;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.logging.LoggingFeature;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Assert;
import org.junit.Test;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.ByteArrayInputStream;
import java.util.logging.Logger;

public class HttpEntityTest extends JerseyTest {

private static final Logger LOGGER = Logger.getLogger(HttpEntityTest.class.getName());
private static final String ECHO_MESSAGE = "ECHO MESSAGE";

@Path("/")
public static class Resource {
@POST
public String echo(String message) {
return message;
}
}

@Override
protected Application configure() {
return new ResourceConfig(Resource.class)
.register(new LoggingFeature(LOGGER, LoggingFeature.Verbosity.PAYLOAD_ANY));
}

@Override
protected void configureClient(ClientConfig config) {
config.register(new LoggingFeature(LOGGER, LoggingFeature.Verbosity.PAYLOAD_ANY));
config.connectorProvider(new ApacheConnectorProvider());
}

@Test
public void testInputStreamEntity() {
ByteArrayInputStream bais = new ByteArrayInputStream(ECHO_MESSAGE.getBytes());
InputStreamEntity entity = new InputStreamEntity(bais);

try (Response response = target().request().post(Entity.entity(entity, MediaType.APPLICATION_OCTET_STREAM))) {
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals(ECHO_MESSAGE, response.readEntity(String.class));
}
}

@Test
public void testByteArrayEntity() {
ByteArrayEntity entity = new ByteArrayEntity(ECHO_MESSAGE.getBytes());

try (Response response = target().request().post(Entity.entity(entity, MediaType.APPLICATION_OCTET_STREAM))) {
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals(ECHO_MESSAGE, response.readEntity(String.class));
}
}
}