本文整理了Java中org.springframework.http.HttpRequest
类的一些代码示例,展示了HttpRequest
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpRequest
类的具体详情如下:
包路径:org.springframework.http.HttpRequest
类名称:HttpRequest
[英]Represents an HTTP request message, consisting of #getMethod() and #getURI().
[中]
代码示例来源:origin: spring-projects/spring-framework
HttpHeaders headers = request.getHeaders();
String origin = headers.getOrigin();
if (origin == null) {
return true;
URI uri = request.getURI();
scheme = uri.getScheme();
host = uri.getHost();
port = uri.getPort();
代码示例来源:origin: javamelody/javamelody
protected String getRequestName(HttpRequest httpRequest) {
String uri = httpRequest.getURI().toString();
final int index = uri.indexOf('?');
if (index != -1) {
uri = uri.substring(0, index);
}
return uri + ' ' + httpRequest.getMethod();
}
}
代码示例来源:origin: eugenp/tutorials
private void logRequest(HttpRequest request, byte[] body) throws UnsupportedEncodingException {
log.info("URI: " + request.getURI());
log.info("HTTP Method: " + request.getMethod());
log.info("HTTP Headers: " + headersToString(request.getHeaders()));
log.info("Request Body: " + new String(body, StandardCharsets.UTF_8));
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Create a new {@code UriComponents} object from the URI associated with
* the given HttpRequest while also overlaying with values from the headers
* "Forwarded" (<a href="http://tools.ietf.org/html/rfc7239">RFC 7239</a>),
* or "X-Forwarded-Host", "X-Forwarded-Port", and "X-Forwarded-Proto" if
* "Forwarded" is not found.
* @param request the source request
* @return the URI components of the URI
* @since 4.1.5
*/
public static UriComponentsBuilder fromHttpRequest(HttpRequest request) {
return fromUri(request.getURI()).adaptFromForwardedHeaders(request.getHeaders());
}
代码示例来源:origin: openzipkin/brave
@Override public String url(HttpRequest request) {
return request.getURI().toString();
}
代码示例来源:origin: spring-cloud-incubator/spring-cloud-alibaba
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
ClientHttpRequestExecution execution) throws IOException {
URI uri = request.getURI();
String hostResource = uri.getScheme() + "://" + uri.getHost()
+ (uri.getPort() == -1 ? "" : ":" + uri.getPort());
String hostWithPathResource = hostResource + uri.getPath();
boolean entryWithPath = true;
response = execution.execute(request, body);
代码示例来源:origin: Nike-Inc/wingtips
@Test
public void getRequestUrl_returns_request_URI_toString() {
// given
URI expectedFullUri = URI.create("/foo/bar/" + UUID.randomUUID().toString() + "?stuff=things");
doReturn(expectedFullUri).when(requestMock).getURI();
// when
String result = implSpy.getRequestUrl(requestMock);
// then
assertThat(result).isEqualTo(expectedFullUri.toString());
}
代码示例来源:origin: aillamsun/devX
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
DevxTraceLogWrap.request("RestTemplate", request.getMethod().name(), request.getURI().toURL().toString());
ClientHttpResponse response = execution.execute(request, body);
DevxTraceLogWrap.response("RestTemplate", response.getRawStatusCode(), request.getMethod().name(), request.getURI().toURL().toString());
return response;
}
代码示例来源:origin: spring-projects/spring-framework
@Test // SPR-8809
public void interceptor() throws Exception {
final String headerName = "MyHeader";
final String headerValue = "MyValue";
ClientHttpRequestInterceptor interceptor = (request, body, execution) -> {
request.getHeaders().add(headerName, headerValue);
return execution.execute(request, body);
};
InterceptingClientHttpRequestFactory factory = new InterceptingClientHttpRequestFactory(
createRequestFactory(), Collections.singletonList(interceptor));
ClientHttpResponse response = null;
try {
ClientHttpRequest request = factory.createRequest(new URI(baseUrl + "/echo"), HttpMethod.GET);
response = request.execute();
assertEquals("Invalid response status", HttpStatus.OK, response.getStatusCode());
HttpHeaders responseHeaders = response.getHeaders();
assertEquals("Custom header invalid", headerValue, responseHeaders.getFirst(headerName));
}
finally {
if (response != null) {
response.close();
}
}
}
代码示例来源:origin: opentracing-contrib/java-spring-web
@Override
public void onRequest(HttpRequest request, Span span) {
Tags.COMPONENT.set(span, COMPONENT_NAME);
// this can be sometimes only path e.g. "/foo"
Tags.HTTP_URL.set(span, request.getURI().toString());
Tags.HTTP_METHOD.set(span, request.getMethod().toString());
if (request.getURI().getPort() != -1) {
Tags.PEER_PORT.set(span, request.getURI().getPort());
}
}
代码示例来源:origin: stagemonitor/stagemonitor
@Test
public void testB3HeaderContextPropagation() throws Exception {
HttpRequest httpRequest = new MockClientHttpRequest(HttpMethod.GET, new URI("http://example.com/foo?bar=baz"));
new SpringRestTemplateContextPropagatingInterceptor(tracingPlugin)
.intercept(httpRequest, null, mock(ClientHttpRequestExecution.class));
assertThat(httpRequest.getHeaders()).containsKey(B3HeaderFormat.SPAN_ID_NAME);
assertThat(httpRequest.getHeaders()).containsKey(B3HeaderFormat.TRACE_ID_NAME);
assertThat(mockTracer.finishedSpans()).hasSize(1);
assertThat(mockTracer.finishedSpans().get(0).operationName()).isEqualTo("GET http://example.com/foo");
}
代码示例来源:origin: spring-cloud/spring-cloud-commons
@Test(expected = IllegalStateException.class)
public void interceptInvalidHost() throws Throwable {
HttpRequest request = mock(HttpRequest.class);
when(request.getURI()).thenReturn(new URI("http://foo_underscore"));
lbProperties.setEnabled(true);
RetryLoadBalancerInterceptor interceptor = new RetryLoadBalancerInterceptor(client, lbProperties, lbRequestFactory,
loadBalancedRetryFactory);
byte[] body = new byte[]{};
ClientHttpRequestExecution execution = mock(ClientHttpRequestExecution.class);
interceptor.intercept(request, body, execution);
}
代码示例来源:origin: stagemonitor/stagemonitor
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
final Scope scope = new ExternalHttpRequest(tracingPlugin.getTracer(), request.getMethod().toString(), removeQuery(request.getURI()), request.getURI().getHost(), request.getURI().getPort()).createScope();
try {
Profiler.start(request.getMethod().toString() + " " + request.getURI() + " ");
tracingPlugin.getTracer().inject(scope.span().context(), Format.Builtin.HTTP_HEADERS, new SpringHttpRequestInjectAdapter(request));
return execution.execute(request, body);
} finally {
Profiler.stop();
scope.close();
}
}
代码示例来源:origin: org.springframework.boot/spring-boot-actuator
/**
* Create a {@code clientName} {@code Tag} derived from the {@link URI#getHost host}
* of the {@link HttpRequest#getURI() URI} of the given {@code request}.
* @param request the request
* @return the clientName tag
*/
public static Tag clientName(HttpRequest request) {
String host = request.getURI().getHost();
if (host == null) {
host = "none";
}
return Tag.of("clientName", host);
}
代码示例来源:origin: liuyangming/ByteTCC
String path = httpRequest.getURI().getPath();
int position = path.startsWith("/") ? path.indexOf("/", 1) : -1;
String pathWithoutContextPath = position > 0 ? path.substring(position) : null;
if (StringUtils.startsWith(path, PREFIX_TRANSACTION_KEY) //
|| StringUtils.startsWith(pathWithoutContextPath, PREFIX_TRANSACTION_KEY)) {
return execution.execute(httpRequest, body);
} else if (compensable == null) {
return execution.execute(httpRequest, body);
} else if (compensable.getTransactionContext().isCompensable() == false) {
return execution.execute(httpRequest, body);
代码示例来源:origin: liuyangming/ByteTCC
String reqTransactionStr = Base64.getEncoder().encodeToString(reqByteArray);
HttpHeaders reqHeaders = httpRequest.getHeaders();
reqHeaders.add(HEADER_TRANCACTION_KEY, reqTransactionStr);
reqHeaders.add(HEADER_PROPAGATION_KEY, this.identifier);
String targetHost = httpRequest.getURI().getHost();
int targetPort = httpRequest.getURI().getPort();
代码示例来源:origin: Nike-Inc/wingtips
@Override
public @Nullable String getRequestPath(@Nullable HttpRequest request) {
if (request == null) {
return null;
}
return request.getURI().getPath();
}
代码示例来源:origin: spring-projects/spring-framework
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),
ex.getStatusText());
assertEquals(MediaType.TEXT_PLAIN, ex.getHeaders().getContentType());
assertEquals(errorMessage, ex.getResponseBodyAsString());
assertEquals(HttpMethod.GET, request.getMethod());
assertEquals(URI.create(this.server.url(path).toString()), request.getURI());
assertNotNull(request.getHeaders());
})
.verify(Duration.ofSeconds(3));
代码示例来源:origin: Nike-Inc/wingtips
@Before
public void beforeMethod() {
uri = URI.create("http://localhost:4242/" + UUID.randomUUID().toString());
method = HttpMethod.PATCH;
requestMock = mock(HttpRequest.class);
doReturn(uri).when(requestMock).getURI();
doReturn(method).when(requestMock).getMethod();
}
代码示例来源:origin: spring-cloud/spring-cloud-zookeeper
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
String clientName = request.getURI().getHost();
ZookeeperDependency dependencyForAlias = DependencyRestTemplateAutoConfiguration.this.zookeeperDependencies.getDependencyForAlias(clientName);
HttpHeaders headers = getUpdatedHeadersIfPossible(request, dependencyForAlias);
request.getHeaders().putAll(headers);
return execution.execute(request, body);
}
内容来源于网络,如有侵权,请联系作者删除!