77import org .rest .common .persistence .model .INameableEntity ;
88import org .rest .common .search .ClientOperation ;
99import org .rest .common .util .QueryConstants ;
10+ import org .rest .common .util .SearchCommonUtil ;
1011import org .rest .common .web .WebConstants ;
1112import org .springframework .beans .factory .annotation .Autowired ;
1213import org .springframework .beans .factory .annotation .Qualifier ;
2021
2122import com .google .common .base .Preconditions ;
2223
23- @ SuppressWarnings ({ "unchecked" , " rawtypes" } )
24+ @ SuppressWarnings (" rawtypes" )
2425public abstract class AbstractClientRESTTemplate <T extends INameableEntity > implements IClientTemplate <T > {
2526
2627 protected Class <T > clazz ;
@@ -78,13 +79,14 @@ public T findOneByAttributes(final String... attributes) {
7879
7980 @ Override
8081 public final List <T > findAll () {
81- final ResponseEntity <List > findAllResponse = restTemplate .exchange (getURI (), HttpMethod .GET , findRequestEntity (), List .class );
82- return findAllResponse .getBody ();
82+ final ResponseEntity <String > findAllResponse = restTemplate .exchange (getURI (), HttpMethod .GET , findRequestEntity (), String .class );
83+ return marshaller . decodeList ( findAllResponse .getBody (), clazz );
8384 }
8485
8586 @ Override
8687 public final List <T > findAllPaginatedAndSorted (final int page , final int size , final String sortBy , final String sortOrder ) {
87- throw new UnsupportedOperationException ();
88+ final ResponseEntity <String > allPaginatedAndSortedAsResponse = findAllPaginatedAndSortedAsResponse (page , size , sortBy , sortOrder );
89+ return marshaller .decodeList (allPaginatedAndSortedAsResponse .getBody (), clazz );
8890 }
8991
9092 @ Override
@@ -104,14 +106,34 @@ public final List<T> findAllByAttributes(final String... attributes) {
104106 return resourcesByAttributes ;
105107 }
106108
109+ public ResponseEntity <String > findAllPaginatedAndSortedAsResponse (final int page , final int size , final String sortBy , final String sortOrder ) {
110+ final StringBuilder uri = new StringBuilder (getURI ());
111+ uri .append (QueryConstants .QUESTIONMARK );
112+ uri .append ("page=" );
113+ uri .append (page );
114+ uri .append (SearchCommonUtil .SEPARATOR_AMPER );
115+ uri .append ("size=" );
116+ uri .append (size );
117+ Preconditions .checkState (!(sortBy == null && sortOrder != null ));
118+ if (sortBy != null ) {
119+ uri .append (SearchCommonUtil .SEPARATOR_AMPER );
120+ uri .append (QueryConstants .SORT_BY + "=" );
121+ uri .append (sortBy );
122+ }
123+ if (sortOrder != null ) {
124+ uri .append (SearchCommonUtil .SEPARATOR_AMPER );
125+ uri .append (QueryConstants .SORT_ORDER + "=" );
126+ uri .append (sortOrder );
127+ }
128+
129+ return restTemplate .exchange (uri .toString (), HttpMethod .GET , findRequestEntity (), String .class );
130+ }
131+
107132 // create
108133
109134 @ Override
110135 public T create (final T resource ) {
111- final ResponseEntity responseEntity = restTemplate .exchange (getURI (), HttpMethod .POST , new HttpEntity <T >(resource , createContentTypeHeaders ()), Void .class );
112-
113- final String locationOfCreatedResource = responseEntity .getHeaders ().getLocation ().toString ();
114- Preconditions .checkNotNull (locationOfCreatedResource );
136+ final String locationOfCreatedResource = createAsURI (resource );
115137
116138 return findOneByURI (locationOfCreatedResource );
117139 }
@@ -131,6 +153,7 @@ public final String createAsURI(final T resource) {
131153
132154 @ Override
133155 public final void update (final T resource ) {
156+ givenAuthenticated ();
134157 final ResponseEntity responseEntity = restTemplate .exchange (getURI (), HttpMethod .PUT , new HttpEntity <T >(resource , createContentTypeHeaders ()), clazz );
135158 Preconditions .checkState (responseEntity .getStatusCode ().value () == 200 );
136159 }
@@ -209,8 +232,6 @@ protected HttpEntity<Void> findRequestEntity() {
209232
210233 // template method
211234
212- public abstract String getURI ();
213-
214235 @ Override
215236 public final IClientTemplate <T > givenAuthenticated () {
216237 if (isBasicAuth ()) {
0 commit comments