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 package org.ogf.graap.wsag.api.rest.providers;
36
37 import java.io.BufferedReader;
38 import java.io.IOException;
39 import java.io.InputStream;
40 import java.io.InputStreamReader;
41 import java.io.OutputStream;
42 import java.lang.annotation.Annotation;
43 import java.lang.reflect.ParameterizedType;
44 import java.lang.reflect.Type;
45 import java.net.URI;
46 import java.net.URISyntaxException;
47 import java.util.Iterator;
48 import java.util.List;
49 import java.util.Vector;
50
51 import javax.ws.rs.WebApplicationException;
52 import javax.ws.rs.core.MediaType;
53 import javax.ws.rs.core.MultivaluedMap;
54 import javax.ws.rs.ext.MessageBodyReader;
55 import javax.ws.rs.ext.MessageBodyWriter;
56 import javax.ws.rs.ext.Provider;
57
58
59
60
61
62 @Provider
63 public class URIListProvider
64 implements MessageBodyWriter<List<URI>>, MessageBodyReader<List<URI>>
65 {
66
67
68
69
70
71
72
73 public boolean
74 isReadable( Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType )
75 {
76 if ( genericType instanceof ParameterizedType )
77 {
78 ParameterizedType parameterized = (ParameterizedType) genericType;
79 Type[] typeArgs = parameterized.getActualTypeArguments();
80 if ( parameterized.getRawType() == List.class && typeArgs.length == 1 && typeArgs[0] == URI.class )
81 {
82
83
84
85 if ( mediaType.isCompatible( new MediaType( "text", "uri-list" ) ) )
86 {
87 return true;
88 }
89 }
90 }
91 return false;
92 }
93
94
95
96
97
98
99
100
101 public List<URI> readFrom( Class<List<URI>> type, Type genericType, Annotation[] annotations,
102 MediaType mediaType, MultivaluedMap<String, String> httpHeaders,
103 InputStream entityStream ) throws IOException
104 {
105 BufferedReader reader = new BufferedReader( new InputStreamReader( entityStream ) );
106 Vector<URI> input = new Vector<URI>();
107
108 String url;
109 while ( ( url = reader.readLine() ) != null )
110 {
111 try
112 {
113 input.add( new URI( url ) );
114 }
115 catch ( URISyntaxException e )
116 {
117 new WebApplicationException( e );
118 }
119 }
120
121 return input;
122 }
123
124
125
126
127
128
129
130 public boolean
131 isWriteable( Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType )
132 {
133 if ( genericType instanceof ParameterizedType )
134 {
135 ParameterizedType parameterized = (ParameterizedType) genericType;
136 Type[] typeArgs = parameterized.getActualTypeArguments();
137 if ( parameterized.getRawType() == List.class && typeArgs.length == 1 && typeArgs[0] == URI.class )
138 {
139
140
141
142 if ( mediaType.isCompatible( new MediaType( "text", "uri-list" ) ) )
143 {
144 return true;
145 }
146 }
147 }
148 return false;
149 }
150
151
152
153
154
155
156
157 public long getSize( List<URI> t, Class<?> type, Type genericType, Annotation[] annotations,
158 MediaType mediaType )
159 {
160 return toString( t ).getBytes().length;
161 }
162
163
164
165
166
167
168
169
170 public void writeTo( List<URI> t, Class<?> type, Type genericType, Annotation[] annotations,
171 MediaType mediaType, MultivaluedMap<String, Object> httpHeaders,
172 OutputStream entityStream ) throws IOException
173 {
174 entityStream.write( toString( t ).getBytes() );
175 }
176
177 private String toString( List<URI> t )
178 {
179 StringBuffer result = new StringBuffer();
180
181 Iterator<URI> iterator = t.iterator();
182 while ( iterator.hasNext() )
183 {
184 result.append( iterator.next().toASCIIString() );
185 result.append( "\n" );
186
187
188
189
190
191 }
192 return result.toString();
193 }
194 }