forked from spring-by-example/spring-by-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Working REST client abstract classes.
- Loading branch information
David Winterfeldt
committed
Aug 27, 2012
1 parent
9e73d32
commit 7221e0c
Showing
8 changed files
with
250 additions
and
189 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
...rest-services/src/main/java/org/springbyexample/web/client/AbstractPersistenceClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright 2007-2012 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springbyexample.web.client; | ||
|
||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
import org.springbyexample.schema.beans.entity.PkEntityBase; | ||
import org.springbyexample.schema.beans.response.ResponseResult; | ||
import org.springbyexample.web.service.PersistenceMarshallingService; | ||
import org.springframework.http.HttpMethod; | ||
|
||
|
||
/** | ||
* Abstract persistence client. | ||
* | ||
* @author David Winterfeldt | ||
* | ||
* @param <R> Generic response. | ||
* @param <FR> Find response. | ||
* @param <S> Save request. | ||
*/ | ||
public abstract class AbstractPersistenceClient<R extends ResponseResult, FR extends ResponseResult, S extends PkEntityBase> | ||
extends AbstractPersistenceFindClient<R, FR> | ||
implements PersistenceMarshallingService<R, FR, S> { | ||
|
||
private final String saveRequest; | ||
private final String deleteRequest; | ||
|
||
public AbstractPersistenceClient(RestClient client, | ||
String findByIdRequest, String findPaginatedRequest, String findRequest, | ||
String saveRequest, String deleteRequest, | ||
Class<R> responseClazz, Class<FR> findResponseClazz) { | ||
super(client, | ||
findByIdRequest, findPaginatedRequest, findRequest, | ||
responseClazz, findResponseClazz); | ||
|
||
this.saveRequest = saveRequest; | ||
this.deleteRequest = deleteRequest; | ||
} | ||
|
||
@Override | ||
public R save(S request) { | ||
R response = null; | ||
|
||
String url = client.createUrl(saveRequest); | ||
|
||
logger.debug("REST client save. id={} url='{}'", request.getId(), url); | ||
|
||
response = client.getRestTemplate().postForObject(url, request, responseClazz); | ||
|
||
return response; | ||
} | ||
|
||
@Override | ||
public ResponseResult delete(long id) { | ||
ResponseResult response = null; | ||
|
||
String url = client.createUrl(deleteRequest); | ||
|
||
logger.debug("REST client delete. id={} url='{}'", id, url); | ||
|
||
Map<String, Long> vars = Collections.singletonMap(ID_VAR, id); | ||
|
||
response = client.getRestTemplate().exchange(url, HttpMethod.DELETE, null, ResponseResult.class, vars).getBody(); | ||
|
||
return response; | ||
} | ||
|
||
} |
116 changes: 116 additions & 0 deletions
116
...-services/src/main/java/org/springbyexample/web/client/AbstractPersistenceFindClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* Copyright 2007-2012 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springbyexample.web.client; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springbyexample.schema.beans.response.ResponseResult; | ||
import org.springbyexample.web.client.RestClient; | ||
import org.springbyexample.web.service.PersistenceFindMarshallingService; | ||
|
||
|
||
/** | ||
* Abstract persistence find client. | ||
* | ||
* @author David Winterfeldt | ||
* | ||
* @param <R> Generic response. | ||
* @param <FR> Find response. | ||
*/ | ||
public abstract class AbstractPersistenceFindClient<R extends ResponseResult, FR extends ResponseResult> | ||
implements PersistenceFindMarshallingService<R, FR> { | ||
|
||
final Logger logger = LoggerFactory.getLogger(getClass()); | ||
|
||
protected final RestClient client; | ||
|
||
private final String findByIdRequest; | ||
private final String findPaginatedRequest; | ||
private final String findRequest; | ||
protected final Class<R> responseClazz; | ||
protected final Class<FR> findResponseClazz; | ||
|
||
|
||
public AbstractPersistenceFindClient(RestClient client, | ||
String findByIdRequest, String findPaginatedRequest, String findRequest, | ||
Class<R> responseClazz, Class<FR> findResponseClazz) { | ||
this.client = client; | ||
this.findByIdRequest = findByIdRequest; | ||
this.findPaginatedRequest = findPaginatedRequest; | ||
this.findRequest = findRequest; | ||
this.responseClazz = responseClazz; | ||
this.findResponseClazz = findResponseClazz; | ||
} | ||
|
||
@Override | ||
public R findById(long id) { | ||
R response = null; | ||
|
||
String url = client.createUrl(findByIdRequest); | ||
|
||
logger.debug("REST client findById. id={} url='{}'", id, url); | ||
|
||
Map<String, Long> vars = Collections.singletonMap(ID_VAR, id); | ||
|
||
response = client.getRestTemplate().getForObject(url, responseClazz, vars); | ||
|
||
return response; | ||
} | ||
|
||
@Override | ||
public FR find(int page, int pageSize) { | ||
FR response = null; | ||
|
||
String url = client.createUrl(findPaginatedRequest); | ||
|
||
logger.debug("REST client paginated find. page={} pageSize={} url='{}'", | ||
new Object[] { page, pageSize, url}); | ||
|
||
response = client.getRestTemplate().getForObject(url, findResponseClazz, createPageVars(page, pageSize)); | ||
|
||
return response; | ||
} | ||
|
||
@Override | ||
public FR find() { | ||
FR response = null; | ||
|
||
String url = client.createUrl(findRequest); | ||
|
||
logger.debug("REST client find. url='{}'", url); | ||
|
||
response = client.getRestTemplate().getForObject(url, findResponseClazz); | ||
|
||
return response; | ||
} | ||
|
||
/** | ||
* Create page vars for a paginated request. | ||
*/ | ||
public Map<String, Integer> createPageVars(int page, int pageSize) { | ||
Map<String, Integer> result = new HashMap<String, Integer>(); | ||
|
||
result.put(PAGE_VAR, page); | ||
result.put(PAGE_SIZE_VAR, pageSize); | ||
|
||
return result; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.