Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.springframework.cloud.client.loadbalancer;

import java.util.Objects;

import org.springframework.cloud.client.ServiceInstance;
import org.springframework.core.style.ToStringCreator;

Expand Down Expand Up @@ -53,4 +55,21 @@ public String toString() {
return to.toString();
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof DefaultResponse)) {
return false;
}
DefaultResponse that = (DefaultResponse) o;
return Objects.equals(serviceInstance, that.serviceInstance);
}

@Override
public int hashCode() {
return Objects.hash(serviceInstance);
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright 2012-2020 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
*
* https://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.cloud.client.loadbalancer;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import org.springframework.core.style.ToStringCreator;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpRequest;
import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.function.client.ClientRequest;

/**
* Represents the data of the request that can be safely read (without passing request reactive stream values).
*
* @author Olga Maciaszek-Sharma
* @since 3.0.0
*/
public class RequestData {

private final HttpMethod httpMethod;

private final URI url;

private final HttpHeaders headers;

private final MultiValueMap<String, String> cookies;

private final Map<String, Object> attributes;

public RequestData(HttpMethod httpMethod, URI url, HttpHeaders headers, MultiValueMap<String, String> cookies,
Map<String, Object> attributes) {
this.httpMethod = httpMethod;
this.url = url;
this.headers = headers;
this.cookies = cookies;
this.attributes = attributes;
}

public RequestData(ClientRequest request) {
this(request.method(), request.url(), request.headers(), request.cookies(), request.attributes());
}

public RequestData(HttpRequest request) {
this(request.getMethod(), request.getURI(), request.getHeaders(), null, new HashMap<>());
}

public HttpMethod getHttpMethod() {
return httpMethod;
}

public URI getUrl() {
return url;
}

public HttpHeaders getHeaders() {
return headers;
}

public MultiValueMap<String, String> getCookies() {
return cookies;
}

public Map<String, Object> getAttributes() {
return attributes;
}

@Override
public String toString() {
ToStringCreator to = new ToStringCreator(this);
to.append("httpMethod", httpMethod);
to.append("url", url);
to.append("headers", headers);
to.append("cookies", cookies);
return to.toString();
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof RequestData)) {
return false;
}
RequestData that = (RequestData) o;
return httpMethod == that.httpMethod && Objects.equals(url, that.url) && Objects.equals(headers, that.headers)
&& Objects.equals(cookies, that.cookies) && Objects.equals(attributes, that.attributes);
}

@Override
public int hashCode() {
return Objects.hash(httpMethod, url, headers, cookies, attributes);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,29 @@
package org.springframework.cloud.client.loadbalancer;

import org.springframework.http.HttpMethod;
import org.springframework.web.reactive.function.client.ClientRequest;

/**
* A {@link RequestData}-based {@link DefaultRequestContext}.
*
* @author Olga Maciaszek-Sharma
* @since 3.0.0
*/
public class ClientRequestContext extends DefaultRequestContext {
public class RequestDataContext extends DefaultRequestContext {

public ClientRequestContext(ClientRequest clientRequest) {
this(clientRequest, "default");
public RequestDataContext(RequestData requestData) {
this(requestData, "default");
}

public ClientRequestContext(ClientRequest clientRequest, String hint) {
super(clientRequest, hint);
public RequestDataContext(RequestData requestData, String hint) {
super(requestData, hint);
}

public ClientRequest getClientRequest() {
return (ClientRequest) super.getClientRequest();
public RequestData getClientRequest() {
return (RequestData) super.getClientRequest();
}

public HttpMethod method() {
return ((ClientRequest) super.getClientRequest()).method();
return ((RequestData) super.getClientRequest()).getHttpMethod();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright 2012-2020 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
*
* https://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.cloud.client.loadbalancer;

import java.util.Objects;

import org.springframework.core.style.ToStringCreator;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseCookie;
import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.function.client.ClientResponse;

/**
* Represents the data of the request that can be safely read (without passing request reactive stream values).
*
* @author Olga Maciaszek-Sharma
* @since 3.0.0
*/
public class ResponseData {

private final HttpStatus httpStatus;

private final HttpHeaders headers;

private final MultiValueMap<String, ResponseCookie> cookies;

private final RequestData requestData;

public ResponseData(HttpStatus httpStatus, HttpHeaders headers, MultiValueMap<String, ResponseCookie> cookies,
RequestData requestData) {
this.httpStatus = httpStatus;
this.headers = headers;
this.cookies = cookies;
this.requestData = requestData;
}

public ResponseData(ClientResponse response, RequestData requestData) {
httpStatus = response.statusCode();
headers = response.headers().asHttpHeaders();
cookies = response.cookies();
this.requestData = requestData;
}

public HttpStatus getHttpStatus() {
return httpStatus;
}

public HttpHeaders getHeaders() {
return headers;
}

public MultiValueMap<String, ResponseCookie> getCookies() {
return cookies;
}

public RequestData getRequestData() {
return requestData;
}

@Override
public String toString() {
ToStringCreator to = new ToStringCreator(this);
to.append("httpStatus", httpStatus);
return to.toString();
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof ResponseData)) {
return false;
}
ResponseData that = (ResponseData) o;
return httpStatus == that.httpStatus && Objects.equals(headers, that.headers)
&& Objects.equals(cookies, that.cookies) && Objects.equals(requestData, that.requestData);
}

@Override
public int hashCode() {
return Objects.hash(httpStatus, headers, cookies, requestData);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public ClientHttpResponse intercept(final HttpRequest request, final byte[] body
Set<LoadBalancerLifecycle> supportedLifecycleProcessors = LoadBalancerLifecycleValidator
.getSupportedLifecycleProcessors(
loadBalancerFactory.getInstances(serviceName, LoadBalancerLifecycle.class),
HttpRequestContext.class, ClientHttpResponse.class, ServiceInstance.class);
RequestDataContext.class, ResponseData.class, ServiceInstance.class);
if (serviceInstance == null) {
if (LOG.isDebugEnabled()) {
LOG.debug("Service instance retrieved from LoadBalancedRetryContext: was null. "
Expand All @@ -103,7 +103,7 @@ public ClientHttpResponse intercept(final HttpRequest request, final byte[] body
}
String hint = getHint(serviceName);
DefaultRequest<RetryableRequestContext> lbRequest = new DefaultRequest<>(
new RetryableRequestContext(previousServiceInstance, request, hint));
new RetryableRequestContext(previousServiceInstance, new RequestData(request), hint));
supportedLifecycleProcessors.forEach(lifecycle -> lifecycle.onStart(lbRequest));
serviceInstance = loadBalancer.choose(serviceName, lbRequest);
if (LOG.isDebugEnabled()) {
Expand Down

This file was deleted.

Loading