/
DataSourceProvider.java
251 lines (225 loc) · 6.91 KB
/
DataSourceProvider.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/**
*
*/
package org.jboss.resteasy.plugins.providers;
import org.jboss.resteasy.util.NoContent;
import javax.xml.transform.stream.StreamSource;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.SequenceInputStream;
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:$
*/
@Provider
@Consumes("*/*")
@Produces("*/*")
public class DataSourceProvider extends AbstractEntityProvider<DataSource>
{
protected static class SequencedDataSource implements DataSource
{
private final byte[] byteBuffer;
private final int byteBufferOffset;
private final int byteBufferLength;
private final File tempFile;
private final String type;
protected SequencedDataSource(byte[] byteBuffer, int byteBufferOffset,
int byteBufferLength, File tempFile, String type)
{
super();
this.byteBuffer = byteBuffer;
this.byteBufferOffset = byteBufferOffset;
this.byteBufferLength = byteBufferLength;
this.tempFile = tempFile;
this.type = type;
}
@Override
public String getContentType()
{
return type;
}
@Override
public InputStream getInputStream() throws IOException
{
InputStream bis = new ByteArrayInputStream(byteBuffer, byteBufferOffset, byteBufferLength);
if (tempFile == null)
return bis;
InputStream fis = new FileInputStream(tempFile);
return new SequenceInputStream(bis, fis) {
@Override
public int read(byte[] b, int off, int len) throws IOException
{
int r = super.read(b, off, len);
if(r == -1)
{
deleteTempFile();
}
return r;
}
@Override
public int read() throws IOException
{
int r = super.read();
if(r == -1)
{
deleteTempFile();
}
return r;
}
};
}
private void deleteTempFile()
{
if(tempFile.exists())
{
tempFile.delete();
}
}
@Override
public String getName()
{
return "";
}
@Override
public OutputStream getOutputStream() throws IOException
{
throw new IOException("No output stream allowed");
}
}
/**
* @param in
* @param mediaType
* @return
* @throws IOException
*/
public static DataSource readDataSource(final InputStream in, final MediaType mediaType) throws IOException
{
byte[] memoryBuffer = new byte[4096];
int readCount = in.read(memoryBuffer, 0, memoryBuffer.length);
File tempFile = null;
if (readCount > 0)
{
byte[] buffer = new byte[4096];
int count = in.read(buffer, 0, buffer.length);
if (count > -1) {
tempFile = File.createTempFile("resteasy-provider-datasource", null);
tempFile.deleteOnExit();
FileOutputStream fos = new FileOutputStream(tempFile);
fos.write(buffer, 0, count);
try
{
ProviderHelper.writeTo(in, fos);
}
finally
{
fos.close();
}
}
}
if (readCount == -1)
readCount = 0;
return new SequencedDataSource(memoryBuffer, 0, readCount, tempFile, mediaType.toString());
}
/**
* FIXME Comment this
*
* @param type
* @param genericType
* @param annotations
* @return
* @see javax.ws.rs.ext.MessageBodyReader
*/
@Override
public boolean isReadable(Class<?> type,
Type genericType,
Annotation[] annotations, MediaType mediaType)
{
return DataSource.class.isAssignableFrom(type);
}
/**
* FIXME Comment this
*
* @param type
* @param genericType
* @param annotations
* @param mediaType
* @param httpHeaders
* @param entityStream
* @return
* @throws IOException
* @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,
MediaType mediaType,
MultivaluedMap<String, String> httpHeaders,
InputStream entityStream) throws IOException
{
if (NoContent.isContentLengthZero(httpHeaders)) return readDataSource(new ByteArrayInputStream(new byte[0]), mediaType);
return readDataSource(entityStream, mediaType);
}
/**
* FIXME Comment this
*
* @param type
* @param genericType
* @param annotations
* @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);
}
/**
* FIXME Comment this
*
* @param dataSource
* @param type
* @param genericType
* @param annotations
* @param mediaType
* @param httpHeaders
* @param entityStream
* @throws IOException
* @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,
Annotation[] annotations,
MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders,
OutputStream entityStream) throws IOException
{
InputStream in = dataSource.getInputStream();
try
{
ProviderHelper.writeTo(in, entityStream);
}
finally
{
in.close();
}
}
}