Set Accept and Content-type headers. Simplify type usage with RestTemplate.
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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<ClientHttpRequestInterceptor> createInterceptors() {
|
||||
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>(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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<T> credentialType = (Class<T>) credentialRequest.getValue().getClass();
|
||||
final ParameterizedTypeReference<CredentialDetails<T>> ref = getDetailsReference(credentialType);
|
||||
final ParameterizedTypeReference<CredentialDetails<T>> ref =
|
||||
new ParameterizedTypeReference<CredentialDetails<T>>() {};
|
||||
|
||||
return doWithRest(new RestOperationsCallback<CredentialDetails<T>>() {
|
||||
@Override
|
||||
@@ -119,7 +118,8 @@ public class CredHubTemplate implements CredHubOperations {
|
||||
Assert.notNull(parametersRequest, "generateRequest must not be null");
|
||||
|
||||
Class<T> credentialType = (Class<T>) parametersRequest.getParameters().getClass();
|
||||
final ParameterizedTypeReference<CredentialDetails<T>> ref = getDetailsReference(credentialType);
|
||||
final ParameterizedTypeReference<CredentialDetails<T>> ref =
|
||||
new ParameterizedTypeReference<CredentialDetails<T>>() {};
|
||||
|
||||
return doWithRest(new RestOperationsCallback<CredentialDetails<T>>() {
|
||||
@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<CredentialDetails<T>> ref = getDetailsReference(credentialType);
|
||||
final ParameterizedTypeReference<CredentialDetails<T>> ref =
|
||||
new ParameterizedTypeReference<CredentialDetails<T>>() {};
|
||||
|
||||
return doWithRest(new RestOperationsCallback<CredentialDetails<T>>() {
|
||||
@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<CredentialDetailsData<T>> ref = getDetailsDataReference(credentialType);
|
||||
final ParameterizedTypeReference<CredentialDetailsData<T>> ref =
|
||||
new ParameterizedTypeReference<CredentialDetailsData<T>>() {};
|
||||
|
||||
return doWithRest(new RestOperationsCallback<List<CredentialDetails<T>>>() {
|
||||
@Override
|
||||
|
||||
@@ -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 <T> ParameterizedTypeReference<CredentialDetailsData<T>> getDetailsDataReference(
|
||||
final Class<T> 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<CredentialDetailsData<T>>() {
|
||||
@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 <T> ParameterizedTypeReference<CredentialDetails<T>> getDetailsReference(
|
||||
final Class<T> 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<CredentialDetails<T>>() {
|
||||
@Override
|
||||
public Type getType() {
|
||||
return supportType;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -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<T, P> extends CredHubTemplateUnitTestsBase {
|
||||
private static final String CREDENTIAL_ID = "1111-1111-1111-1111";
|
||||
|
||||
@@ -79,11 +82,8 @@ public abstract class CredHubTemplateDetailUnitTestsBase<T, P> extends CredHubTe
|
||||
void verifyWrite(ResponseEntity<CredentialDetails<T>> expectedResponse) {
|
||||
CredentialRequest<T> request = getWriteRequest();
|
||||
|
||||
final ParameterizedTypeReference<CredentialDetails<T>> ref =
|
||||
getDetailsReference(getType());
|
||||
|
||||
when(restTemplate.exchange(BASE_URL_PATH, PUT,
|
||||
new HttpEntity<CredentialRequest<T>>(request), ref))
|
||||
when(restTemplate.exchange(eq(BASE_URL_PATH), eq(PUT),
|
||||
eq(new HttpEntity<CredentialRequest<T>>(request)), isA(ParameterizedTypeReference.class)))
|
||||
.thenReturn(expectedResponse);
|
||||
|
||||
if (!expectedResponse.getStatusCode().equals(HttpStatus.OK)) {
|
||||
@@ -105,11 +105,8 @@ public abstract class CredHubTemplateDetailUnitTestsBase<T, P> extends CredHubTe
|
||||
void verifyGenerate(ResponseEntity<CredentialDetails<T>> expectedResponse) {
|
||||
ParametersRequest<P> request = getGenerateRequest();
|
||||
|
||||
final ParameterizedTypeReference<CredentialDetails<T>> ref =
|
||||
getDetailsReference(getType());
|
||||
|
||||
when(restTemplate.exchange(BASE_URL_PATH, POST,
|
||||
new HttpEntity<ParametersRequest<P>>(request), ref))
|
||||
when(restTemplate.exchange(eq(BASE_URL_PATH), eq(POST),
|
||||
eq(new HttpEntity<ParametersRequest<P>>(request)), isA(ParameterizedTypeReference.class)))
|
||||
.thenReturn(expectedResponse);
|
||||
|
||||
if (!expectedResponse.getStatusCode().equals(HttpStatus.OK)) {
|
||||
@@ -129,10 +126,8 @@ public abstract class CredHubTemplateDetailUnitTestsBase<T, P> extends CredHubTe
|
||||
}
|
||||
|
||||
void verifyGetById(ResponseEntity<CredentialDetails<T>> expectedResponse) {
|
||||
final ParameterizedTypeReference<CredentialDetails<T>> 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<T, P> extends CredHubTe
|
||||
}
|
||||
|
||||
void verifyGetByNameWithString(ResponseEntity<CredentialDetailsData<T>> expectedResponse) {
|
||||
ParameterizedTypeReference<CredentialDetailsData<T>> 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<T, P> extends CredHubTe
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void verifyGetByNameWithCredentialName(ResponseEntity<CredentialDetailsData<T>> expectedResponse) {
|
||||
ParameterizedTypeReference<CredentialDetailsData<T>> 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)) {
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user