/
StringTextStar.java
85 lines (75 loc) · 3.04 KB
/
StringTextStar.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
package org.jboss.resteasy.plugins.providers;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.CompletionStage;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.ext.MessageBodyReader;
import jakarta.ws.rs.ext.Provider;
import org.jboss.resteasy.spi.AsyncMessageBodyWriter;
import org.jboss.resteasy.spi.AsyncOutputStream;
import org.jboss.resteasy.util.MediaTypeHelper;
import org.jboss.resteasy.util.NoContent;
/**
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
* @version $Revision: 1 $
*/
@Provider
@Produces("*/*")
@Consumes("*/*")
public class StringTextStar implements MessageBodyReader<String>, AsyncMessageBodyWriter<String> {
public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return String.class.equals(type);
}
public String readFrom(Class<String> type,
Type genericType,
Annotation[] annotations,
MediaType mediaType,
MultivaluedMap<String, String> httpHeaders,
InputStream entityStream) throws IOException {
if (NoContent.isContentLengthZero(httpHeaders))
return "";
return ProviderHelper.readString(entityStream, mediaType);
}
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return String.class.equals(type) && !MediaTypeHelper.isBlacklisted(mediaType);
}
public long getSize(String o, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return -1;
}
public void writeTo(String o,
Class<?> type,
Type genericType,
Annotation[] annotations,
MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders,
OutputStream entityStream) throws IOException {
String charset = mediaType.getParameters().get("charset");
if (charset == null)
entityStream.write(o.getBytes(StandardCharsets.UTF_8));
else
entityStream.write(o.getBytes(charset));
}
@Override
public CompletionStage<Void> asyncWriteTo(String o, Class<?> type, Type genericType, Annotation[] annotations,
MediaType mediaType, MultivaluedMap<String, Object> httpHeaders,
AsyncOutputStream entityStream) {
String charset = mediaType.getParameters().get("charset");
if (charset == null)
return entityStream.asyncWrite(o.getBytes(StandardCharsets.UTF_8));
else {
try {
return entityStream.asyncWrite(o.getBytes(charset));
} catch (UnsupportedEncodingException e) {
return ProviderHelper.completedException(e);
}
}
}
}