org.cattleframework.cloud.discovery.enhancement.plugin.RequestContext Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cattle-cloud-discovery-common Show documentation
Show all versions of cattle-cloud-discovery-common Show documentation
Cattle framework cloud discovery common component pom
/*
* Copyright (C) 2018 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.cattleframework.cloud.discovery.enhancement.plugin;
import java.net.URI;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
/**
* 请求上下文
*
* @author orange
*
*/
public class RequestContext {
private final URI url;
private final HttpMethod httpMethod;
private final HttpHeaders httpHeaders;
private final Map parameters;
public RequestContext(URI url, HttpMethod httpMethod, HttpHeaders httpHeaders, Map parameters) {
this.url = url;
this.httpMethod = httpMethod;
this.httpHeaders = httpHeaders;
this.parameters = parameters;
}
public HttpMethod getHttpMethod() {
return httpMethod;
}
public HttpHeaders getHttpHeaders() {
return httpHeaders;
}
public URI getUrl() {
return url;
}
public String getParameter(String name) {
return parameters != null ? parameters.get(name) : null;
}
public static ContextRequestBuilder builder() {
return new ContextRequestBuilder();
}
public static final class ContextRequestBuilder {
private HttpMethod httpMethod;
private HttpHeaders httpHeaders;
private URI url;
private final Map parameters;
private ContextRequestBuilder() {
parameters = new HashMap();
}
public ContextRequestBuilder httpMethod(HttpMethod httpMethod) {
this.httpMethod = httpMethod;
return this;
}
public ContextRequestBuilder httpHeaders(HttpHeaders httpHeaders) {
this.httpHeaders = httpHeaders;
return this;
}
public ContextRequestBuilder url(URI url) {
String query = url.getQuery();
if (StringUtils.isBlank(query)) {
this.url = url;
} else {
this.url = URI.create(
String.format("%s://%s:%d%s", url.getScheme(), url.getHost(), url.getPort(), url.getPath()));
String[] parameterArray = StringUtils.split(query, "&");
Arrays.stream(parameterArray).forEach(parameter -> {
int pos = parameter.indexOf("=");
if (pos >= 0) {
parameters.put(parameter.substring(0, pos), parameter.substring(pos + 1));
} else {
parameters.put(parameter, "");
}
});
}
return this;
}
public RequestContext build() {
return new RequestContext(url, httpMethod, httpHeaders, parameters);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy