Skip to content
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
58 changes: 36 additions & 22 deletions ...resteasy-jaxrs/src/main/java/org/jboss/resteasy/plugins/providers/DataSourceProvider.java
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@

import org.jboss.resteasy.util.NoContent;

import javax.activation.DataSource;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.Provider;
import javax.xml.transform.stream.StreamSource;
import java.io.ByteArrayInputStream;
import java.io.File;
Expand All @@ -24,6 +17,14 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import javax.activation.DataSource;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.Provider;

/**
* @author <a href="mailto:ryan@damnhandy.com">Ryan J. McDonough</a>
* @version $Revision:$
Expand All @@ -36,11 +37,11 @@ public class DataSourceProvider extends AbstractEntityProvider<DataSource>

protected static class SequencedDataSource implements DataSource
{
private byte[] byteBuffer;
private int byteBufferOffset;
private int byteBufferLength;
private File tempFile;
private String type;
private final byte[] byteBuffer;
private final int byteBufferOffset;
private final int byteBufferLength;
private final File tempFile;
private final String type;

public SequencedDataSource(byte[] byteBuffer, int byteBufferOffset,
int byteBufferLength, File tempFile, String type)
Expand All @@ -53,11 +54,13 @@ public SequencedDataSource(byte[] byteBuffer, int byteBufferOffset,
this.type = type;
}

@Override
public String getContentType()
{
return type;
}

@Override
public InputStream getInputStream() throws IOException
{
InputStream bis = new ByteArrayInputStream(byteBuffer, byteBufferOffset, byteBufferLength);
Expand All @@ -67,11 +70,13 @@ public InputStream getInputStream() throws IOException
return new SequenceInputStream(bis, fis);
}

@Override
public String getName()
{
return "";
}

@Override
public OutputStream getOutputStream() throws IOException
{
throw new IOException("No output stream allowed");
Expand All @@ -92,17 +97,22 @@ public static DataSource readDataSource(final InputStream in, final MediaType me
int readCount = in.read(memoryBuffer, 0, memoryBuffer.length);

File tempFile = null;
if (in.available() > 0)
if (readCount > 0)
{
tempFile = File.createTempFile("resteasy-provider-datasource", null);
FileOutputStream fos = new FileOutputStream(tempFile);
try
{
ProviderHelper.writeTo(in, fos);
}
finally
{
fos.close();
byte[] buffer = new byte[4096];
int count = in.read(buffer, 0, buffer.length);
if (count > -1) {
tempFile = File.createTempFile("resteasy-provider-datasource", null);
FileOutputStream fos = new FileOutputStream(tempFile);
fos.write(buffer, 0, count);
try
{
ProviderHelper.writeTo(in, fos);
}
finally
{
fos.close();
}
}
}

Expand All @@ -121,6 +131,7 @@ public static DataSource readDataSource(final InputStream in, final MediaType me
* @return
* @see javax.ws.rs.ext.MessageBodyReader
*/
@Override
public boolean isReadable(Class<?> type,
Type genericType,
Annotation[] annotations, MediaType mediaType)
Expand All @@ -143,6 +154,7 @@ public boolean isReadable(Class<?> type,
* @throws WebApplicationException
* @see @see javax.ws.rs.ext.MessageBodyReader#readFrom(java.lang.Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType, javax.ws.rs.core.MultivaluedMap, java.io.InputStream)
*/
@Override
public DataSource readFrom(Class<DataSource> type,
Type genericType,
Annotation[] annotations,
Expand All @@ -164,6 +176,7 @@ public DataSource readFrom(Class<DataSource> type,
* @return
* @see @see javax.ws.rs.ext.MessageBodyWriter#isWriteable(java.lang.Class, java.lang.reflect.Type, java.lang.annotation.Annotation[])
*/
@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType)
{
return DataSource.class.isAssignableFrom(type);
Expand All @@ -183,6 +196,7 @@ public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotat
* @throws WebApplicationException
* @see @see javax.ws.rs.ext.MessageBodyWriter#writeTo(java.lang.Object, java.lang.Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType, javax.ws.rs.core.MultivaluedMap, java.io.OutputStream)
*/
@Override
public void writeTo(DataSource dataSource,
Class<?> type,
Type genericType,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package org.jboss.resteasy.plugins.providers;

import static org.junit.Assert.*;

import java.io.IOException;
import java.io.InputStream;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.HttpParams;
import org.jboss.resteasy.test.BaseResourceTest;
import org.jboss.resteasy.test.TestPortProvider;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

/**
* see https://issues.jboss.org/browse/RESTEASY-779
*/
public class RESTEASY779Test extends BaseResourceTest {

@Path("/")
public static class BigDataResource {

public static final int KBs = 5;
public static final int SIZE = KBs * 1024;

@GET
@Produces("text/plain")
public String get()
{
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < SIZE; i++) {
buffer.append("x");
}
return buffer.toString();
}

}

@Before
public void setUp() throws Exception
{
addPerRequestResource(BigDataResource.class);
}

@Test
public void testDataSourceProvider() throws Exception
{
HttpParams params = new BasicHttpParams();
// important - see https://issues.jboss.org/browse/RESTEASY-779
params.setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, (BigDataResource.KBs - 1) * 1024);
HttpClient httpClient = new DefaultHttpClient(params);
HttpGet httpGet = new HttpGet(TestPortProvider.generateURL("/"));
HttpResponse response = httpClient.execute(httpGet);
InputStream inputStream = null;
try {
inputStream = response.getEntity().getContent();
DataSourceProvider.readDataSource(inputStream, MediaType.TEXT_PLAIN_TYPE);

assertEquals(0, findSizeOfRemainingDataInStream(inputStream));

} finally {
IOUtils.closeQuietly(inputStream);
}
}

private int findSizeOfRemainingDataInStream(InputStream inputStream) throws IOException
{
byte[] buf = new byte[4 * 1024];
int bytesRead, totalBytesRead = 0;
while ((bytesRead = inputStream.read(buf, 0, buf.length)) != -1)
{
totalBytesRead += bytesRead;
}
return totalBytesRead;
}

}