99import org .rest .common .util .QueryConstants ;
1010import org .rest .common .util .SearchCommonUtil ;
1111import org .rest .common .web .WebConstants ;
12+ import org .slf4j .Logger ;
13+ import org .slf4j .LoggerFactory ;
1214import org .springframework .beans .factory .annotation .Autowired ;
1315import org .springframework .beans .factory .annotation .Qualifier ;
1416import org .springframework .http .HttpEntity ;
1719import org .springframework .http .ResponseEntity ;
1820import org .springframework .web .client .HttpClientErrorException ;
1921import org .springframework .web .client .RestTemplate ;
20- import org .springframework .web .util .UriComponentsBuilder ;
2122
2223import com .google .common .base .Preconditions ;
2324
2425@ SuppressWarnings ("rawtypes" )
2526public abstract class AbstractClientRESTTemplate <T extends INameableEntity > implements IClientTemplate <T > {
27+ protected final Logger logger = LoggerFactory .getLogger (getClass ());
2628
2729 protected Class <T > clazz ;
2830
29- @ Autowired
30- protected RestTemplate restTemplate ;
3131 @ Autowired
3232 @ Qualifier ("jacksonMarshaller" )
3333 protected IMarshaller marshaller ;
3434
35+ @ Autowired
36+ protected RestTemplate restTemplate ;
37+
3538 public AbstractClientRESTTemplate (final Class <T > clazzToSet ) {
3639 super ();
3740
3841 clazz = clazzToSet ;
3942 }
4043
41- // API
42-
4344 // find - one
4445
4546 @ Override
@@ -61,13 +62,13 @@ public final T findOneByURI(final String uri) {
6162 // find one - by attributes
6263
6364 @ Override
64- public T findByName (final String name ) {
65+ public final T findByName (final String name ) {
6566 return findOneByAttributes ("name" , name );
6667 }
6768
6869 @ Override
69- public T findOneByAttributes (final String ... attributes ) {
70- final List <T > resourcesByName = findAllByURI (getURI () + QueryConstants .QUERY_PREFIX + constructURI (attributes ));
70+ public final T findOneByAttributes (final String ... attributes ) {
71+ final List <T > resourcesByName = findAllByURI (getURI () + QueryConstants .QUERY_PREFIX + SearchCommonUtil . constructURI (attributes ));
7172 if (resourcesByName .isEmpty ()) {
7273 return null ;
7374 }
@@ -89,6 +90,7 @@ public final List<T> findAllPaginatedAndSorted(final int page, final int size, f
8990 return marshaller .decodeList (allPaginatedAndSortedAsResponse .getBody (), clazz );
9091 }
9192
93+ @ SuppressWarnings ("unchecked" )
9294 @ Override
9395 public final List <T > findAllSorted (final String sortBy , final String sortOrder ) {
9496 final ResponseEntity <List > findAllResponse = restTemplate .exchange (getURI () + QueryConstants .Q_SORT_BY + sortBy , HttpMethod .GET , findRequestEntity (), List .class );
@@ -97,16 +99,24 @@ public final List<T> findAllSorted(final String sortBy, final String sortOrder)
9799
98100 @ Override
99101 public final List <T > findAllPaginated (final int page , final int size ) {
100- throw new UnsupportedOperationException ();
102+ final StringBuilder uri = new StringBuilder (getURI ());
103+ uri .append (QueryConstants .QUESTIONMARK );
104+ uri .append ("page=" );
105+ uri .append (page );
106+ uri .append (SearchCommonUtil .SEPARATOR_AMPER );
107+ uri .append ("size=" );
108+ uri .append (size );
109+ final ResponseEntity <String > findAllResponse = restTemplate .exchange (uri .toString (), HttpMethod .GET , findRequestEntity (), String .class );
110+ return getMarshaller ().decodeList (findAllResponse .getBody (), clazz );
101111 }
102112
103113 @ Override
104114 public final List <T > findAllByAttributes (final String ... attributes ) {
105- final List <T > resourcesByAttributes = findAllByURI (getURI () + QueryConstants .QUERY_PREFIX + constructURI (attributes ));
115+ final List <T > resourcesByAttributes = findAllByURI (getURI () + QueryConstants .QUERY_PREFIX + SearchCommonUtil . constructURI (attributes ));
106116 return resourcesByAttributes ;
107117 }
108118
109- public ResponseEntity <String > findAllPaginatedAndSortedAsResponse (final int page , final int size , final String sortBy , final String sortOrder ) {
119+ final ResponseEntity <String > findAllPaginatedAndSortedAsResponse (final int page , final int size , final String sortBy , final String sortOrder ) {
110120 final StringBuilder uri = new StringBuilder (getURI ());
111121 uri .append (QueryConstants .QUESTIONMARK );
112122 uri .append ("page=" );
@@ -129,10 +139,17 @@ public ResponseEntity<String> findAllPaginatedAndSortedAsResponse(final int page
129139 return restTemplate .exchange (uri .toString (), HttpMethod .GET , findRequestEntity (), String .class );
130140 }
131141
142+ @ SuppressWarnings ("unchecked" )
143+ @ Override
144+ public final List <T > findAllByURI (final String uri ) {
145+ final ResponseEntity <List > response = restTemplate .exchange (uri , HttpMethod .GET , findRequestEntity (), List .class );
146+ return response .getBody ();
147+ }
148+
132149 // create
133150
134151 @ Override
135- public T create (final T resource ) {
152+ public final T create (final T resource ) {
136153 final String locationOfCreatedResource = createAsURI (resource );
137154
138155 return findOneByURI (locationOfCreatedResource );
@@ -141,7 +158,7 @@ public T create(final T resource) {
141158 @ Override
142159 public final String createAsURI (final T resource ) {
143160 givenAuthenticated ();
144- final ResponseEntity responseEntity = restTemplate .exchange (getURI (), HttpMethod .POST , new HttpEntity <T >(resource , createContentTypeHeaders ()), List .class );
161+ final ResponseEntity < List > responseEntity = restTemplate .exchange (getURI (), HttpMethod .POST , new HttpEntity <T >(resource , createContentTypeHeaders ()), List .class );
145162
146163 final String locationOfCreatedResource = responseEntity .getHeaders ().getLocation ().toString ();
147164 Preconditions .checkNotNull (locationOfCreatedResource );
@@ -154,7 +171,7 @@ public final String createAsURI(final T resource) {
154171 @ Override
155172 public final void update (final T resource ) {
156173 givenAuthenticated ();
157- final ResponseEntity responseEntity = restTemplate .exchange (getURI (), HttpMethod .PUT , new HttpEntity <T >(resource , createContentTypeHeaders ()), clazz );
174+ final ResponseEntity < T > responseEntity = restTemplate .exchange (getURI (), HttpMethod .PUT , new HttpEntity <T >(resource , createContentTypeHeaders ()), clazz );
158175 Preconditions .checkState (responseEntity .getStatusCode ().value () == 200 );
159176 }
160177
@@ -175,25 +192,20 @@ public final void deleteAll() {
175192 // search
176193
177194 @ Override
178- public List <T > search (final Triple <String , ClientOperation , String >... constraints ) {
195+ public final List <T > search (final Triple <String , ClientOperation , String >... constraints ) {
179196 throw new UnsupportedOperationException ();
180197 }
181198
182199 // count
183200
184201 @ Override
185- public long count () {
202+ public final long count () {
186203 throw new UnsupportedOperationException ();
187204 }
188205
189206 // util
190207
191- final List <T > findAllByURI (final String uri ) {
192- final ResponseEntity <List > response = restTemplate .exchange (uri , HttpMethod .GET , findRequestEntity (), List .class );
193- return response .getBody ();
194- }
195-
196- protected HttpHeaders createGetHeaders () {
208+ protected final HttpHeaders createGetHeaders () {
197209 final HttpHeaders headers = new HttpHeaders () {
198210 {
199211 set (com .google .common .net .HttpHeaders .ACCEPT , marshaller .getMime ());
@@ -211,22 +223,7 @@ protected final HttpHeaders createContentTypeHeaders() {
211223 return headers ;
212224 }
213225
214- protected static String constructURI (final String ... attributes ) {
215- Preconditions .checkNotNull (attributes );
216- Preconditions .checkArgument (attributes .length > 0 );
217- Preconditions .checkArgument (attributes .length % 2 == 0 );
218-
219- final UriComponentsBuilder queryParam = UriComponentsBuilder .newInstance ();
220- for (int i = 0 ; i <= attributes .length / 2 ; i += 2 ) {
221- queryParam .queryParam (attributes [i ], attributes [i + 1 ]);
222- }
223-
224- String query = queryParam .build ().encode ().getQuery ();
225- // query = query.replaceAll("=", "%3D");
226- return query .replaceAll ("&" , "," );
227- }
228-
229- protected HttpEntity <Void > findRequestEntity () {
226+ protected final HttpEntity <Void > findRequestEntity () {
230227 return new HttpEntity <Void >(createGetHeaders ());
231228 }
232229
@@ -253,4 +250,8 @@ protected final void digestAuth() {
253250 throw new UnsupportedOperationException ();
254251 }
255252
253+ public final IMarshaller getMarshaller () {
254+ return marshaller ;
255+ }
256+
256257}
0 commit comments