|
| 1 | +package com.baeldung.client.template; |
| 2 | + |
| 3 | +import org.apache.commons.lang3.tuple.Pair; |
| 4 | +import org.springframework.beans.factory.annotation.Autowired; |
| 5 | +import org.springframework.http.HttpHeaders; |
| 6 | + |
| 7 | +import com.baeldung.client.marshall.IMarshaller; |
| 8 | +import com.baeldung.client.util.HeaderUtil; |
| 9 | +import com.google.common.base.Preconditions; |
| 10 | + |
| 11 | +public abstract class AbstractBaseClientRestTemplate { |
| 12 | + |
| 13 | + @Autowired |
| 14 | + protected IMarshaller marshaller; |
| 15 | + |
| 16 | + public AbstractBaseClientRestTemplate() { |
| 17 | + super(); |
| 18 | + } |
| 19 | + |
| 20 | + // find - one |
| 21 | + |
| 22 | + // util |
| 23 | + |
| 24 | + // template method |
| 25 | + |
| 26 | + protected abstract Pair<String, String> getDefaultCredentials(); |
| 27 | + |
| 28 | + protected Pair<String, String> getReadCredentials() { |
| 29 | + return getDefaultCredentials(); |
| 30 | + } |
| 31 | + |
| 32 | + protected Pair<String, String> getReadExtendedCredentials() { |
| 33 | + return getReadCredentials(); |
| 34 | + } |
| 35 | + |
| 36 | + protected Pair<String, String> getWriteCredentials() { |
| 37 | + return getDefaultCredentials(); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * - 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 |
| 42 | + */ |
| 43 | + protected void beforeReadOperation() { |
| 44 | + // |
| 45 | + } |
| 46 | + |
| 47 | + // read |
| 48 | + |
| 49 | + /** |
| 50 | + * - note: hook to be able to customize the find headers if needed |
| 51 | + */ |
| 52 | + protected HttpHeaders readHeaders() { |
| 53 | + return HeaderUtil.createAcceptHeaders(marshaller); |
| 54 | + } |
| 55 | + |
| 56 | + protected HttpHeaders readHeadersWithAuth() { |
| 57 | + final Pair<String, String> defaultCredentials = getReadCredentials(); |
| 58 | + return readHeadersWithAuth(defaultCredentials.getLeft(), defaultCredentials.getRight()); |
| 59 | + } |
| 60 | + |
| 61 | + protected HttpHeaders readHeadersWithAuth(final Pair<String, String> credentials) { |
| 62 | + if (credentials == null) { |
| 63 | + final Pair<String, String> readCredentials = getReadCredentials(); |
| 64 | + return readHeadersWithAuth(readCredentials.getLeft(), readCredentials.getRight()); |
| 65 | + } |
| 66 | + return readHeadersWithAuth(credentials.getLeft(), credentials.getRight()); |
| 67 | + } |
| 68 | + |
| 69 | + private final HttpHeaders readHeadersWithAuth(final String username, final String password) { |
| 70 | + Preconditions.checkNotNull(username); |
| 71 | + Preconditions.checkNotNull(password); |
| 72 | + return HeaderUtil.createAcceptAndBasicAuthHeaders(marshaller, username, password); |
| 73 | + } |
| 74 | + |
| 75 | + // write |
| 76 | + |
| 77 | + protected HttpHeaders writeHeadersWithAuth() { |
| 78 | + final Pair<String, String> defaultCredentials = getWriteCredentials(); |
| 79 | + return writeHeadersWithAuth(defaultCredentials.getLeft(), defaultCredentials.getRight()); |
| 80 | + } |
| 81 | + |
| 82 | + protected HttpHeaders writeHeadersWithAuth(final Pair<String, String> credentials) { |
| 83 | + if (credentials == null) { |
| 84 | + final Pair<String, String> writeCredentials = getWriteCredentials(); |
| 85 | + return writeHeadersWithAuth(writeCredentials.getLeft(), writeCredentials.getRight()); |
| 86 | + } |
| 87 | + return writeHeadersWithAuth(credentials.getLeft(), credentials.getRight()); |
| 88 | + } |
| 89 | + |
| 90 | + private final HttpHeaders writeHeadersWithAuth(final String username, final String password) { |
| 91 | + Preconditions.checkNotNull(username); |
| 92 | + Preconditions.checkNotNull(password); |
| 93 | + return HeaderUtil.createContentTypeAndBasicAuthHeaders(marshaller, username, password); |
| 94 | + } |
| 95 | + |
| 96 | +} |
0 commit comments