diff --git a/enterprise/spring-rest-services/src/main/java/org/springbyexample/web/client/AbstractPersistenceClient.java b/enterprise/spring-rest-services/src/main/java/org/springbyexample/web/client/AbstractPersistenceClient.java new file mode 100644 index 00000000..7ec290d9 --- /dev/null +++ b/enterprise/spring-rest-services/src/main/java/org/springbyexample/web/client/AbstractPersistenceClient.java @@ -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 Generic response. + * @param Find response. + * @param Save request. +*/ +public abstract class AbstractPersistenceClient + extends AbstractPersistenceFindClient + implements PersistenceMarshallingService { + + private final String saveRequest; + private final String deleteRequest; + + public AbstractPersistenceClient(RestClient client, + String findByIdRequest, String findPaginatedRequest, String findRequest, + String saveRequest, String deleteRequest, + Class responseClazz, Class 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 vars = Collections.singletonMap(ID_VAR, id); + + response = client.getRestTemplate().exchange(url, HttpMethod.DELETE, null, ResponseResult.class, vars).getBody(); + + return response; + } + +} diff --git a/enterprise/spring-rest-services/src/main/java/org/springbyexample/web/client/AbstractPersistenceFindClient.java b/enterprise/spring-rest-services/src/main/java/org/springbyexample/web/client/AbstractPersistenceFindClient.java new file mode 100644 index 00000000..bc126840 --- /dev/null +++ b/enterprise/spring-rest-services/src/main/java/org/springbyexample/web/client/AbstractPersistenceFindClient.java @@ -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 Generic response. + * @param Find response. + */ +public abstract class AbstractPersistenceFindClient + implements PersistenceFindMarshallingService { + + 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 responseClazz; + protected final Class findResponseClazz; + + + public AbstractPersistenceFindClient(RestClient client, + String findByIdRequest, String findPaginatedRequest, String findRequest, + Class responseClazz, Class 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 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 createPageVars(int page, int pageSize) { + Map result = new HashMap(); + + result.put(PAGE_VAR, page); + result.put(PAGE_SIZE_VAR, pageSize); + + return result; + } + +} diff --git a/enterprise/spring-rest-services/src/main/java/org/springbyexample/web/client/RestClient.java b/enterprise/spring-rest-services/src/main/java/org/springbyexample/web/client/RestClient.java index 774c18a0..e9f16b11 100644 --- a/enterprise/spring-rest-services/src/main/java/org/springbyexample/web/client/RestClient.java +++ b/enterprise/spring-rest-services/src/main/java/org/springbyexample/web/client/RestClient.java @@ -49,9 +49,9 @@ public RestTemplate getRestTemplate() { } /** - * Gets URL based on the URI passed in. + * Creates URL based on the URI passed in. */ - public String getUrl(String uri) { + public String createUrl(String uri) { StringBuilder sb = new StringBuilder(); sb.append(clientProperties.getUrl()); diff --git a/enterprise/spring-rest-services/src/main/java/org/springbyexample/web/client/person/PersonClient.java b/enterprise/spring-rest-services/src/main/java/org/springbyexample/web/client/person/PersonClient.java index 1c92445f..cebea75c 100644 --- a/enterprise/spring-rest-services/src/main/java/org/springbyexample/web/client/person/PersonClient.java +++ b/enterprise/spring-rest-services/src/main/java/org/springbyexample/web/client/person/PersonClient.java @@ -15,20 +15,13 @@ */ package org.springbyexample.web.client.person; -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.person.Person; import org.springbyexample.schema.beans.person.PersonFindResponse; import org.springbyexample.schema.beans.person.PersonResponse; -import org.springbyexample.schema.beans.response.ResponseResult; +import org.springbyexample.web.client.AbstractPersistenceClient; import org.springbyexample.web.client.RestClient; import org.springbyexample.web.service.person.PersonMarshallingService; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpMethod; import org.springframework.stereotype.Component; @@ -38,90 +31,15 @@ * @author David Winterfeldt */ @Component -public class PersonClient implements PersonMarshallingService { - - final Logger logger = LoggerFactory.getLogger(getClass()); - - private final RestClient client; +public class PersonClient extends AbstractPersistenceClient + implements PersonMarshallingService { @Autowired public PersonClient(RestClient client) { - this.client = client; - } - - @Override - public PersonResponse findById(long id) { - PersonResponse response = null; - - String url = client.getUrl(FIND_BY_ID_REQUEST); - - logger.debug("REST client findById. id={} url='{}'", id, url); - - Map vars = Collections.singletonMap(ID_VAR, id); - - response = client.getRestTemplate().getForObject(url, PersonResponse.class, vars); - - return response; - } - - @Override - public PersonFindResponse find(int page, int pageSize) { - PersonFindResponse response = null; - - String url = client.getUrl(FIND_PAGINATED_REQUEST); - - logger.debug("REST client paginated find. page={} pageSize={} url='{}'", - new Object[] { page, pageSize, url}); - - Map vars = new HashMap(); - - vars.put(PAGE_VAR, page); - vars.put(PAGE_SIZE_VAR, pageSize); - - response = client.getRestTemplate().getForObject(url, PersonFindResponse.class, vars); - - return response; - } - - @Override - public PersonFindResponse find() { - PersonFindResponse response = null; - - String url = client.getUrl(FIND_REQUEST); - - logger.debug("REST client find. url='{}'", url); - - response = client.getRestTemplate().getForObject(url, PersonFindResponse.class); - - return response; - } - - @Override - public PersonResponse save(Person request) { - PersonResponse response = null; - - String url = client.getUrl(SAVE_REQUEST); - - logger.debug("REST client save. id={} url='{}'", request.getId(), url); - - response = client.getRestTemplate().postForObject(url, request, PersonResponse.class); - - return response; - } - - @Override - public ResponseResult delete(long id) { - ResponseResult response = null; - - String url = client.getUrl(DELETE_REQUEST); - - logger.debug("REST client delete. id={} url='{}'", id, url); - - Map vars = Collections.singletonMap(ID_VAR, id); - - response = client.getRestTemplate().exchange(url, HttpMethod.DELETE, null, ResponseResult.class, vars).getBody(); - - return response; + super(client, + FIND_BY_ID_REQUEST, FIND_PAGINATED_REQUEST, FIND_REQUEST, + SAVE_REQUEST, DELETE_REQUEST, + PersonResponse.class, PersonFindResponse.class); } } diff --git a/enterprise/spring-rest-services/src/main/java/org/springbyexample/web/client/person/ProfessionalClient.java b/enterprise/spring-rest-services/src/main/java/org/springbyexample/web/client/person/ProfessionalClient.java index f6e35c40..2a9dbe44 100644 --- a/enterprise/spring-rest-services/src/main/java/org/springbyexample/web/client/person/ProfessionalClient.java +++ b/enterprise/spring-rest-services/src/main/java/org/springbyexample/web/client/person/ProfessionalClient.java @@ -15,113 +15,31 @@ */ package org.springbyexample.web.client.person; -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.person.Professional; import org.springbyexample.schema.beans.person.ProfessionalFindResponse; import org.springbyexample.schema.beans.person.ProfessionalResponse; -import org.springbyexample.schema.beans.response.ResponseResult; +import org.springbyexample.web.client.AbstractPersistenceClient; import org.springbyexample.web.client.RestClient; import org.springbyexample.web.service.person.ProfessionalMarshallingService; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpMethod; import org.springframework.stereotype.Component; /** - * Person client. + * Professional client. * * @author David Winterfeldt */ @Component -public class ProfessionalClient implements ProfessionalMarshallingService { - - final Logger logger = LoggerFactory.getLogger(getClass()); - - private final RestClient client; +public class ProfessionalClient extends AbstractPersistenceClient + implements ProfessionalMarshallingService { @Autowired public ProfessionalClient(RestClient client) { - this.client = client; - } - - @Override - public ProfessionalResponse findById(long id) { - ProfessionalResponse response = null; - - String url = client.getUrl(FIND_BY_ID_REQUEST); - - logger.debug("REST client findById. id={} url='{}'", id, url); - - Map vars = Collections.singletonMap(ID_VAR, id); - - response = client.getRestTemplate().getForObject(url, ProfessionalResponse.class, vars); - - return response; - } - - @Override - public ProfessionalFindResponse find(int page, int pageSize) { - ProfessionalFindResponse response = null; - - String url = client.getUrl(FIND_PAGINATED_REQUEST); - - logger.debug("REST client paginated find. page={} pageSize={} url='{}'", - new Object[] { page, pageSize, url}); - - Map vars = new HashMap(); - - vars.put(PAGE_VAR, page); - vars.put(PAGE_SIZE_VAR, pageSize); - - response = client.getRestTemplate().getForObject(url, ProfessionalFindResponse.class, vars); - - return response; - } - - @Override - public ProfessionalFindResponse find() { - ProfessionalFindResponse response = null; - - String url = client.getUrl(FIND_REQUEST); - - logger.debug("REST client find. url='{}'", url); - - response = client.getRestTemplate().getForObject(url, ProfessionalFindResponse.class); - - return response; - } - - @Override - public ProfessionalResponse save(Professional request) { - ProfessionalResponse response = null; - - String url = client.getUrl(SAVE_REQUEST); - - logger.debug("REST client save. id={} url='{}'", request.getId(), url); - - response = client.getRestTemplate().postForObject(url, request, ProfessionalResponse.class); - - return response; - } - - @Override - public ResponseResult delete(long id) { - ResponseResult response = null; - - String url = client.getUrl(DELETE_REQUEST); - - logger.debug("REST client delete. id={} url='{}'", id, url); - - Map vars = Collections.singletonMap(ID_VAR, id); - - response = client.getRestTemplate().exchange(url, HttpMethod.DELETE, null, ResponseResult.class, vars).getBody(); - - return response; + super(client, + FIND_BY_ID_REQUEST, FIND_PAGINATED_REQUEST, FIND_REQUEST, + SAVE_REQUEST, DELETE_REQUEST, + ProfessionalResponse.class, ProfessionalFindResponse.class); } } diff --git a/enterprise/spring-rest-services/src/main/java/org/springbyexample/web/service/PersistenceMarshallingService.java b/enterprise/spring-rest-services/src/main/java/org/springbyexample/web/service/PersistenceMarshallingService.java index 542b2600..0831320d 100644 --- a/enterprise/spring-rest-services/src/main/java/org/springbyexample/web/service/PersistenceMarshallingService.java +++ b/enterprise/spring-rest-services/src/main/java/org/springbyexample/web/service/PersistenceMarshallingService.java @@ -25,15 +25,15 @@ * * @param Generic response. * @param Find response. - * @param Entity. + * @param Save request. */ -public interface PersistenceMarshallingService +public interface PersistenceMarshallingService extends PersistenceFindMarshallingService { /** * Save record. */ - public R save(T request); + public R save(S request); /** * Delete record. diff --git a/enterprise/spring-rest-services/src/main/resources/entity-base.xsd b/enterprise/spring-rest-services/src/main/resources/entity-base.xsd new file mode 100644 index 00000000..20d05512 --- /dev/null +++ b/enterprise/spring-rest-services/src/main/resources/entity-base.xsd @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + diff --git a/enterprise/spring-rest-services/src/main/resources/person.xsd b/enterprise/spring-rest-services/src/main/resources/person.xsd index 1d1e857a..faea5e8d 100644 --- a/enterprise/spring-rest-services/src/main/resources/person.xsd +++ b/enterprise/spring-rest-services/src/main/resources/person.xsd @@ -4,8 +4,10 @@ xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:annox="http://annox.dev.java.net" jaxb:extensionBindingPrefixes="annox" + xmlns:entity="http://www.springbyexample.org/schema/beans/entity" xmlns:response="http://www.springbyexample.org/schema/beans/response"> + @@ -64,11 +66,14 @@ - - - - - + + + + + + + +