Skip to content

Commit

Permalink
Working REST client abstract classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
David Winterfeldt committed Aug 27, 2012
1 parent 9e73d32 commit 7221e0c
Show file tree
Hide file tree
Showing 8 changed files with 250 additions and 189 deletions.
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;
}

}
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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand All @@ -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<PersonResponse, PersonFindResponse, Person>
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<String, Long> 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<String, Integer> vars = new HashMap<String, Integer>();

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<String, Long> 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);
}

}
Loading

0 comments on commit 7221e0c

Please sign in to comment.