From 5419dff5eb71d21146eb01dda381f68e9e4d582b Mon Sep 17 00:00:00 2001 From: Scott Frederick Date: Mon, 5 Jun 2017 16:17:02 -0500 Subject: [PATCH] Set Accept and Content-type headers. Simplify type usage with RestTemplate. --- .../configuration/CredHubConfiguration.java | 12 -- .../CloudFoundryAppInstanceProperties.java | 79 ------------- .../credhub/core/CredHubClient.java | 39 +++++++ .../credhub/core/CredHubTemplate.java | 14 ++- .../credhub/core/TypeUtils.java | 104 ------------------ .../CredHubTemplateDetailUnitTestsBase.java | 38 +++---- .../core/CredHubTemplateUnitTestsBase.java | 2 +- 7 files changed, 62 insertions(+), 226 deletions(-) delete mode 100644 spring-credhub-core/src/main/java/org/springframework/credhub/core/CloudFoundryAppInstanceProperties.java delete mode 100644 spring-credhub-core/src/main/java/org/springframework/credhub/core/TypeUtils.java diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/configuration/CredHubConfiguration.java b/spring-credhub-core/src/main/java/org/springframework/credhub/configuration/CredHubConfiguration.java index 4e5edd5..57835a2 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/configuration/CredHubConfiguration.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/configuration/CredHubConfiguration.java @@ -20,7 +20,6 @@ import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.credhub.core.CloudFoundryAppInstanceProperties; import org.springframework.credhub.core.CredHubProperties; import org.springframework.credhub.core.CredHubTemplate; import org.springframework.credhub.support.ClientOptions; @@ -56,17 +55,6 @@ public class CredHubConfiguration { return new CredHubProperties(); } - /** - * Create the {@link CloudFoundryAppInstanceProperties} that contains information - * about the application instance running on Cloud Foundry. - * - * @return the {@link CloudFoundryAppInstanceProperties} bean - */ - @Bean - public CloudFoundryAppInstanceProperties cloudFoundryAppInstanceProperties() { - return new CloudFoundryAppInstanceProperties(); - } - /** * Create the {@link CredHubTemplate} that the application will use to interact * with CredHub. diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/core/CloudFoundryAppInstanceProperties.java b/spring-credhub-core/src/main/java/org/springframework/credhub/core/CloudFoundryAppInstanceProperties.java deleted file mode 100644 index d2ebbc2..0000000 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/core/CloudFoundryAppInstanceProperties.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright 2016-2017 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.springframework.credhub.core; - -import org.springframework.beans.factory.annotation.Value; - -/** - * Properties containing information about an application instance - * running on Cloud Foundry. - * - * @author Scott Frederick - */ -public class CloudFoundryAppInstanceProperties { - @Value("${CF_INSTANCE_CERT}") - private String instanceCertLocation; - - @Value("${CF_INSTANCE_KEY}") - private String instanceKeyLocation; - - /** - * Create a new instance without initializing properties. - */ - public CloudFoundryAppInstanceProperties() { - if (instanceCertLocation == null) { - instanceCertLocation = System.getenv("CF_INSTANCE_CERT"); - } - - if (instanceKeyLocation == null) { - instanceKeyLocation = System.getenv("CF_INSTANCE_KEY"); - } - } - - /** - * Create a new instance from the provided property values. Intended to be used - * internally for testing. - * - * @param instanceCertLocation the absolute path of the certificate file in the app - * instance container - * @param instanceKeyLocation the absolute path of the private key file in the app - * instance container - */ - CloudFoundryAppInstanceProperties(String instanceCertLocation, - String instanceKeyLocation) { - this.instanceCertLocation = instanceCertLocation; - this.instanceKeyLocation = instanceKeyLocation; - } - - /** - * Get the absolute path of the certificate file in the app instance container. - * - * @return the certificate file path - */ - public String getInstanceCertLocation() { - return instanceCertLocation; - } - - /** - * Get the absolute path of the private key file in the app instance container. - * - * @return the key file path - */ - public String getInstanceKeyLocation() { - return instanceKeyLocation; - } -} diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubClient.java b/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubClient.java index 9ece95f..a623f76 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubClient.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubClient.java @@ -18,11 +18,19 @@ package org.springframework.credhub.core; +import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.springframework.credhub.support.JsonUtils; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpRequest; +import org.springframework.http.MediaType; +import org.springframework.http.client.ClientHttpRequestExecution; import org.springframework.http.client.ClientHttpRequestFactory; +import org.springframework.http.client.ClientHttpRequestInterceptor; +import org.springframework.http.client.ClientHttpResponse; +import org.springframework.http.client.support.HttpRequestWrapper; import org.springframework.http.converter.ByteArrayHttpMessageConverter; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.StringHttpMessageConverter; @@ -31,6 +39,8 @@ import org.springframework.web.client.RestTemplate; import org.springframework.web.util.DefaultUriTemplateHandler; import org.springframework.web.util.UriTemplateHandler; +import static java.util.Collections.singletonList; + /** * Factory for creating a {@link RestTemplate} configured for communication with * a CredHub server. @@ -52,6 +62,7 @@ public class CredHubClient { restTemplate.setRequestFactory(clientHttpRequestFactory); restTemplate.setUriTemplateHandler(createUriTemplateHandler(baseUri)); restTemplate.setMessageConverters(createMessageConverters()); + restTemplate.setInterceptors(createInterceptors()); return restTemplate; } @@ -82,4 +93,32 @@ public class CredHubClient { return messageConverters; } + + /** + * Create the {@link ClientHttpRequestInterceptor} necessary to configure requests and responses. + * + * @return the list of {@link ClientHttpRequestInterceptor}s + */ + private static List createInterceptors() { + List interceptors = new ArrayList(1); + interceptors.add(new CredHubRequestInterceptor()); + return interceptors; + } + + /** + * A request interceptor that sets headers common to all CredHub requests. + */ + private static class CredHubRequestInterceptor implements ClientHttpRequestInterceptor { + @Override + public ClientHttpResponse intercept(HttpRequest request, byte[] body, + ClientHttpRequestExecution execution) throws IOException { + HttpRequestWrapper requestWrapper = new HttpRequestWrapper(request); + + HttpHeaders headers = requestWrapper.getHeaders(); + headers.setAccept(singletonList(MediaType.APPLICATION_JSON)); + headers.setContentType(MediaType.APPLICATION_JSON); + + return execution.execute(requestWrapper, body); + } + } } diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubTemplate.java b/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubTemplate.java index d490abf..1cbfd28 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubTemplate.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubTemplate.java @@ -38,8 +38,6 @@ import org.springframework.web.client.HttpStatusCodeException; import org.springframework.web.client.RestOperations; import org.springframework.web.client.RestTemplate; -import static org.springframework.credhub.core.TypeUtils.getDetailsDataReference; -import static org.springframework.credhub.core.TypeUtils.getDetailsReference; import static org.springframework.http.HttpMethod.GET; import static org.springframework.http.HttpMethod.POST; import static org.springframework.http.HttpMethod.PUT; @@ -97,7 +95,8 @@ public class CredHubTemplate implements CredHubOperations { Assert.notNull(credentialRequest, "credentialRequest must not be null"); Class credentialType = (Class) credentialRequest.getValue().getClass(); - final ParameterizedTypeReference> ref = getDetailsReference(credentialType); + final ParameterizedTypeReference> ref = + new ParameterizedTypeReference>() {}; return doWithRest(new RestOperationsCallback>() { @Override @@ -119,7 +118,8 @@ public class CredHubTemplate implements CredHubOperations { Assert.notNull(parametersRequest, "generateRequest must not be null"); Class credentialType = (Class) parametersRequest.getParameters().getClass(); - final ParameterizedTypeReference> ref = getDetailsReference(credentialType); + final ParameterizedTypeReference> ref = + new ParameterizedTypeReference>() {}; return doWithRest(new RestOperationsCallback>() { @Override @@ -140,7 +140,8 @@ public class CredHubTemplate implements CredHubOperations { Assert.notNull(id, "credential id must not be null"); Assert.notNull(credentialType, "credential type must not be null"); - final ParameterizedTypeReference> ref = getDetailsReference(credentialType); + final ParameterizedTypeReference> ref = + new ParameterizedTypeReference>() {}; return doWithRest(new RestOperationsCallback>() { @Override @@ -160,7 +161,8 @@ public class CredHubTemplate implements CredHubOperations { Assert.notNull(name, "credential name must not be null"); Assert.notNull(credentialType, "credential type must not be null"); - final ParameterizedTypeReference> ref = getDetailsDataReference(credentialType); + final ParameterizedTypeReference> ref = + new ParameterizedTypeReference>() {}; return doWithRest(new RestOperationsCallback>>() { @Override diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/core/TypeUtils.java b/spring-credhub-core/src/main/java/org/springframework/credhub/core/TypeUtils.java deleted file mode 100644 index 3cbaecf..0000000 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/core/TypeUtils.java +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright 2016-2017 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.springframework.credhub.core; - -import org.springframework.core.ParameterizedTypeReference; -import org.springframework.credhub.support.CredentialDetails; -import org.springframework.credhub.support.CredentialDetailsData; -import org.springframework.util.Assert; - -import java.lang.reflect.ParameterizedType; -import java.lang.reflect.Type; - -/** - * Utility methods for creating type references. - * - * @author Scott Frederick - */ -public class TypeUtils { - /** - * Create a {@link ParameterizedTypeReference} for {@code credentialType}. - * - * @param credentialType must not be {@literal null} - * @return the {@link ParameterizedTypeReference} for {@code credentialType} - */ - static ParameterizedTypeReference> getDetailsDataReference( - final Class credentialType) { - - Assert.notNull(credentialType, "Response type must not be null"); - - final Type supportType = new ParameterizedType() { - @Override - public Type[] getActualTypeArguments() { - return new Type[] { credentialType }; - } - - @Override - public Type getRawType() { - return CredentialDetailsData.class; - } - - @Override - public Type getOwnerType() { - return CredentialDetailsData.class; - } - }; - - return new ParameterizedTypeReference>() { - @Override - public Type getType() { - return supportType; - } - }; - } - - /** - * Create a {@link ParameterizedTypeReference} for {@code credentialType}. - * - * @param credentialType must not be {@literal null} - * @return the {@link ParameterizedTypeReference} for {@code credentialType} - */ - static ParameterizedTypeReference> getDetailsReference( - final Class credentialType) { - - Assert.notNull(credentialType, "Response type must not be null"); - - final Type supportType = new ParameterizedType() { - @Override - public Type[] getActualTypeArguments() { - return new Type[] { credentialType }; - } - - @Override - public Type getRawType() { - return CredentialDetails.class; - } - - @Override - public Type getOwnerType() { - return CredentialDetails.class; - } - }; - - return new ParameterizedTypeReference>() { - @Override - public Type getType() { - return supportType; - } - }; - } -} diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailUnitTestsBase.java b/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailUnitTestsBase.java index 3d1a6ed..81f6de9 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailUnitTestsBase.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailUnitTestsBase.java @@ -34,17 +34,20 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.notNullValue; import static org.junit.Assert.assertThat; import static org.junit.Assert.fail; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.isA; +import static org.mockito.ArgumentMatchers.isNull; import static org.mockito.Mockito.when; import static org.springframework.credhub.core.CredHubTemplate.BASE_URL_PATH; import static org.springframework.credhub.core.CredHubTemplate.ID_URL_PATH; import static org.springframework.credhub.core.CredHubTemplate.NAME_URL_QUERY; -import static org.springframework.credhub.core.TypeUtils.getDetailsReference; import static org.springframework.http.HttpMethod.GET; import static org.springframework.http.HttpMethod.POST; import static org.springframework.http.HttpMethod.PUT; import static org.springframework.http.HttpStatus.OK; import static org.springframework.http.HttpStatus.UNAUTHORIZED; +@SuppressWarnings("unchecked") public abstract class CredHubTemplateDetailUnitTestsBase extends CredHubTemplateUnitTestsBase { private static final String CREDENTIAL_ID = "1111-1111-1111-1111"; @@ -79,11 +82,8 @@ public abstract class CredHubTemplateDetailUnitTestsBase extends CredHubTe void verifyWrite(ResponseEntity> expectedResponse) { CredentialRequest request = getWriteRequest(); - final ParameterizedTypeReference> ref = - getDetailsReference(getType()); - - when(restTemplate.exchange(BASE_URL_PATH, PUT, - new HttpEntity>(request), ref)) + when(restTemplate.exchange(eq(BASE_URL_PATH), eq(PUT), + eq(new HttpEntity>(request)), isA(ParameterizedTypeReference.class))) .thenReturn(expectedResponse); if (!expectedResponse.getStatusCode().equals(HttpStatus.OK)) { @@ -105,11 +105,8 @@ public abstract class CredHubTemplateDetailUnitTestsBase extends CredHubTe void verifyGenerate(ResponseEntity> expectedResponse) { ParametersRequest

request = getGenerateRequest(); - final ParameterizedTypeReference> ref = - getDetailsReference(getType()); - - when(restTemplate.exchange(BASE_URL_PATH, POST, - new HttpEntity>(request), ref)) + when(restTemplate.exchange(eq(BASE_URL_PATH), eq(POST), + eq(new HttpEntity>(request)), isA(ParameterizedTypeReference.class))) .thenReturn(expectedResponse); if (!expectedResponse.getStatusCode().equals(HttpStatus.OK)) { @@ -129,10 +126,8 @@ public abstract class CredHubTemplateDetailUnitTestsBase extends CredHubTe } void verifyGetById(ResponseEntity> expectedResponse) { - final ParameterizedTypeReference> ref = - getDetailsReference(getType()); - - when(restTemplate.exchange(ID_URL_PATH, GET, null, ref, CREDENTIAL_ID)) + when(restTemplate.exchange(eq(ID_URL_PATH), eq(GET), isNull(HttpEntity.class), + isA(ParameterizedTypeReference.class), eq(CREDENTIAL_ID))) .thenReturn(expectedResponse); if (!expectedResponse.getStatusCode().equals(HttpStatus.OK)) { @@ -153,10 +148,8 @@ public abstract class CredHubTemplateDetailUnitTestsBase extends CredHubTe } void verifyGetByNameWithString(ResponseEntity> expectedResponse) { - ParameterizedTypeReference> ref = TypeUtils - .getDetailsDataReference(getType()); - - when(restTemplate.exchange(NAME_URL_QUERY, GET, null, ref, NAME.getName())) + when(restTemplate.exchange(eq(NAME_URL_QUERY), eq(GET), isNull(HttpEntity.class), + isA(ParameterizedTypeReference.class), eq(NAME.getName()))) .thenReturn(expectedResponse); if (!expectedResponse.getStatusCode().equals(OK)) { @@ -177,12 +170,9 @@ public abstract class CredHubTemplateDetailUnitTestsBase extends CredHubTe } } - void verifyGetByNameWithCredentialName(ResponseEntity> expectedResponse) { - ParameterizedTypeReference> ref = TypeUtils - .getDetailsDataReference(getType()); - - when(restTemplate.exchange(NAME_URL_QUERY, GET, null, ref, NAME.getName())) + when(restTemplate.exchange(eq(NAME_URL_QUERY), eq(GET), isNull(HttpEntity.class), + isA(ParameterizedTypeReference.class), eq(NAME.getName()))) .thenReturn(expectedResponse); if (!expectedResponse.getStatusCode().equals(OK)) { diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateUnitTestsBase.java b/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateUnitTestsBase.java index 7911361..adfccef 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateUnitTestsBase.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateUnitTestsBase.java @@ -30,7 +30,7 @@ public abstract class CredHubTemplateUnitTestsBase { protected static final SimpleCredentialName NAME = new SimpleCredentialName("example", "credential"); @Rule - public MockitoRule rule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS); + public MockitoRule mockitoRule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS); @Mock protected RestTemplate restTemplate;