1 /*
2 * Copyright (c) 2011, Fraunhofer-Gesellschaft
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * (1) Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the disclaimer at the end.
11 * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * (2) Neither the name of Fraunhofer nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * DISCLAIMER
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 * @author owaeld
60 *
61 */
62 @Provider
63 public class URIListProvider
64 implements MessageBodyWriter<List<URI>>, MessageBodyReader<List<URI>>
65 {
66
67 /*
68 * (non-Javadoc)
69 *
70 * @see javax.ws.rs.ext.MessageBodyReader#isReadable(java.lang.Class, java.lang.reflect.Type,
71 * java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)
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 // make sure that the content type is an uri list
84 //
85 if ( mediaType.isCompatible( new MediaType( "text", "uri-list" ) ) )
86 {
87 return true;
88 }
89 }
90 }
91 return false;
92 }
93
94 /*
95 * (non-Javadoc)
96 *
97 * @see javax.ws.rs.ext.MessageBodyReader#readFrom(java.lang.Class, java.lang.reflect.Type,
98 * java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType, javax.ws.rs.core.MultivaluedMap,
99 * java.io.InputStream)
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 * (non-Javadoc)
126 *
127 * @see javax.ws.rs.ext.MessageBodyWriter#isWriteable(java.lang.Class, java.lang.reflect.Type,
128 * java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)
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 // make sure that the content type is an uri list
141 //
142 if ( mediaType.isCompatible( new MediaType( "text", "uri-list" ) ) )
143 {
144 return true;
145 }
146 }
147 }
148 return false;
149 }
150
151 /*
152 * (non-Javadoc)
153 *
154 * @see javax.ws.rs.ext.MessageBodyWriter#getSize(java.lang.Object, java.lang.Class,
155 * java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)
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 * (non-Javadoc)
165 *
166 * @see javax.ws.rs.ext.MessageBodyWriter#writeTo(java.lang.Object, java.lang.Class,
167 * java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType,
168 * javax.ws.rs.core.MultivaluedMap, java.io.OutputStream)
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 // if ( iterator.hasNext() )
188 // {
189 // result.append( "\n" );
190 // }
191 }
192 return result.toString();
193 }
194 }