|
1 | 1 | package org.rest.common.client.template; |
2 | 2 |
|
3 | | -import static org.rest.common.search.ClientOperation.EQ; |
4 | | - |
5 | | -import org.apache.commons.lang3.tuple.ImmutableTriple; |
6 | 3 | import org.apache.commons.lang3.tuple.Pair; |
7 | | -import org.rest.common.persistence.model.INameableEntity; |
8 | | -import org.rest.common.search.ClientOperation; |
9 | | -import org.rest.common.util.SearchField; |
| 4 | +import org.rest.common.client.web.HeaderUtil; |
| 5 | +import org.rest.common.persistence.model.IEntity; |
| 6 | +import org.rest.common.web.WebConstants; |
| 7 | +import org.springframework.http.HttpEntity; |
| 8 | +import org.springframework.http.HttpHeaders; |
| 9 | +import org.springframework.http.HttpMethod; |
| 10 | +import org.springframework.http.ResponseEntity; |
| 11 | + |
| 12 | +import com.google.common.base.Preconditions; |
10 | 13 |
|
11 | | -@SuppressWarnings({ "unchecked" }) |
12 | | -public abstract class AbstractClientRestTemplate<T extends INameableEntity> extends AbstractRawClientRestTemplate<T> implements IClientTemplate<T> { |
| 14 | +public abstract class AbstractClientRestTemplate<T extends IEntity> extends AbstractReadOnlyClientRestTemplate<T> { |
13 | 15 |
|
14 | 16 | public AbstractClientRestTemplate(final Class<T> clazzToSet) { |
15 | 17 | super(clazzToSet); |
16 | 18 | } |
17 | 19 |
|
18 | | - // find one - by attributes |
| 20 | + // create |
19 | 21 |
|
20 | 22 | @Override |
21 | | - public T findByName(final String name) { |
22 | | - beforeReadOperation(); |
23 | | - return searchOne(new ImmutableTriple<String, ClientOperation, String>(SearchField.name.name(), EQ, name)); |
| 23 | + public final T create(final T resource, final Pair<String, String> credentials) { |
| 24 | + final String locationOfCreatedResource = createAsUri(resource, credentials); |
| 25 | + |
| 26 | + return findOneByUri(locationOfCreatedResource, credentials); |
24 | 27 | } |
25 | 28 |
|
26 | | - // util |
| 29 | + @Override |
| 30 | + public final T create(final T resource) { |
| 31 | + final String locationOfCreatedResource = createAsUri(resource, null); |
| 32 | + |
| 33 | + return findOneByUri(locationOfCreatedResource, null); |
| 34 | + } |
27 | 35 |
|
28 | | - protected void auth(final Pair<String, String> credentials) { |
| 36 | + @Override |
| 37 | + public final String createAsUri(final T resource, final Pair<String, String> credentials) { |
29 | 38 | if (credentials != null) { |
30 | 39 | auth.givenAuthenticated(restTemplate, credentials.getLeft(), credentials.getRight()); |
31 | 40 | } else { |
32 | 41 | givenAuthenticated(); |
33 | 42 | } |
| 43 | + final ResponseEntity<Void> responseEntity = restTemplate.exchange(getUri(), HttpMethod.POST, new HttpEntity<T>(resource, writeHeaders()), Void.class); |
| 44 | + |
| 45 | + final String locationOfCreatedResource = responseEntity.getHeaders().getLocation().toString(); |
| 46 | + Preconditions.checkNotNull(locationOfCreatedResource); |
| 47 | + |
| 48 | + return locationOfCreatedResource; |
| 49 | + } |
| 50 | + |
| 51 | + // update |
| 52 | + |
| 53 | + @Override |
| 54 | + public final void update(final T resource) { |
| 55 | + givenAuthenticated(); |
| 56 | + final ResponseEntity<T> responseEntity = restTemplate.exchange(getUri() + "/" + resource.getId(), HttpMethod.PUT, new HttpEntity<T>(resource, writeHeaders()), clazz); |
| 57 | + Preconditions.checkState(responseEntity.getStatusCode().value() == 200); |
| 58 | + } |
| 59 | + |
| 60 | + // delete |
| 61 | + |
| 62 | + @Override |
| 63 | + public final void delete(final long id) { |
| 64 | + // final ResponseEntity<Object> deleteResourceResponse = restTemplate.exchange(getUri() + WebConstants.PATH_SEP + id, HttpMethod.DELETE, new HttpEntity<T>(writeHeaders()), null); |
| 65 | + final ResponseEntity<Void> deleteResourceResponse = restTemplate.exchange(getUri() + WebConstants.PATH_SEP + id, HttpMethod.DELETE, new HttpEntity<Void>(writeHeaders()), Void.class); |
| 66 | + |
| 67 | + Preconditions.checkState(deleteResourceResponse.getStatusCode().value() == 204); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public final void deleteAll() { |
| 72 | + throw new UnsupportedOperationException(); |
| 73 | + } |
| 74 | + |
| 75 | + // template method |
| 76 | + |
| 77 | + @Override |
| 78 | + public abstract Pair<String, String> getDefaultCredentials(); |
| 79 | + |
| 80 | + /** |
| 81 | + * - this is a hook that executes before read operations, in order to allow custom security work to happen for read operations; similar to: AbstractRestTemplate.findRequest |
| 82 | + */ |
| 83 | + @Override |
| 84 | + protected void beforeReadOperation() { |
| 85 | + // |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * - note: hook to be able to customize the find headers if needed |
| 90 | + */ |
| 91 | + @Override |
| 92 | + protected HttpHeaders findHeaders() { |
| 93 | + return HeaderUtil.createAcceptHeaders(marshaller); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * - note: hook to be able to customize the write headers if needed |
| 98 | + */ |
| 99 | + protected HttpHeaders writeHeaders() { |
| 100 | + return HeaderUtil.createContentTypeHeaders(marshaller); |
34 | 101 | } |
35 | 102 |
|
36 | 103 | } |
0 commit comments