當前位置: 首頁>>代碼示例>>Java>>正文


Java HttpRequest類代碼示例

本文整理匯總了Java中org.springframework.http.HttpRequest的典型用法代碼示例。如果您正苦於以下問題:Java HttpRequest類的具體用法?Java HttpRequest怎麽用?Java HttpRequest使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


HttpRequest類屬於org.springframework.http包,在下文中一共展示了HttpRequest類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: intercept

import org.springframework.http.HttpRequest; //導入依賴的package包/類
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
    ClientHttpRequestExecution execution) throws IOException {

  if (omegaContext.globalTxId() != null) {
    request.getHeaders().add(GLOBAL_TX_ID_KEY, omegaContext.globalTxId());
    request.getHeaders().add(LOCAL_TX_ID_KEY, omegaContext.localTxId());

    LOG.debug("Added {} {} and {} {} to request header",
        GLOBAL_TX_ID_KEY,
        omegaContext.globalTxId(),
        LOCAL_TX_ID_KEY,
        omegaContext.localTxId());
  }
  return execution.execute(request, body);
}
 
開發者ID:apache,項目名稱:incubator-servicecomb-saga,代碼行數:17,代碼來源:TransactionClientHttpRequestInterceptor.java

示例2: getTags

import org.springframework.http.HttpRequest; //導入依賴的package包/類
@Override
public Iterable<Tag> getTags(String urlTemplate, HttpRequest request, ClientHttpResponse response) 
{
	Tag uriTag = StringUtils.hasText(urlTemplate)? RestTemplateExchangeTags.uri(urlTemplate): RestTemplateExchangeTags.uri(request);
	
	
            
	return Arrays.asList(RestTemplateExchangeTags.method(request), uriTag,
			RestTemplateExchangeTags.status(response),
            RestTemplateExchangeTags.clientName(request),
            modelName(request),
            modelImage(request),
            modelVersion(request),
            projectName(),
            deploymentName(),
            deploymentVersion(),
            predictorName(),
            predictorVersion());
}
 
開發者ID:SeldonIO,項目名稱:seldon-core,代碼行數:20,代碼來源:SeldonRestTemplateExchangeTagsProvider.java

示例3: EmptyBodyCheckingHttpInputMessage

import org.springframework.http.HttpRequest; //導入依賴的package包/類
public EmptyBodyCheckingHttpInputMessage(HttpInputMessage inputMessage) throws IOException {
	this.headers = inputMessage.getHeaders();
	InputStream inputStream = inputMessage.getBody();
	if (inputStream == null) {
		this.body = null;
	}
	else if (inputStream.markSupported()) {
		inputStream.mark(1);
		this.body = (inputStream.read() != -1 ? inputStream : null);
		inputStream.reset();
	}
	else {
		PushbackInputStream pushbackInputStream = new PushbackInputStream(inputStream);
		int b = pushbackInputStream.read();
		if (b == -1) {
			this.body = null;
		}
		else {
			this.body = pushbackInputStream;
			pushbackInputStream.unread(b);
		}
	}
	this.method = ((HttpRequest) inputMessage).getMethod();
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:25,代碼來源:AbstractMessageConverterMethodArgumentResolver.java

示例4: fromHttpRequestResetsPortBeforeSettingIt

import org.springframework.http.HttpRequest; //導入依賴的package包/類
@Test
public void fromHttpRequestResetsPortBeforeSettingIt() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addHeader("X-Forwarded-Proto", "https");
	request.addHeader("X-Forwarded-Host", "84.198.58.199");
	request.addHeader("X-Forwarded-Port", 443);
	request.setScheme("http");
	request.setServerName("example.com");
	request.setServerPort(80);
	request.setRequestURI("/rest/mobile/users/1");

	HttpRequest httpRequest = new ServletServerHttpRequest(request);
	UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();

	assertEquals("https", result.getScheme());
	assertEquals("84.198.58.199", result.getHost());
	assertEquals(-1, result.getPort());
	assertEquals("/rest/mobile/users/1", result.getPath());
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:20,代碼來源:UriComponentsBuilderTests.java

示例5: intercept

import org.springframework.http.HttpRequest; //導入依賴的package包/類
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
	throws IOException
{
	if (log.isDebugEnabled())
	{
		log.debug(String.format("Request: %s %s %s", request.getMethod(), request.getURI(),
			new String(body, getCharset(request))));
	}
	
	ClientHttpResponse response = execution.execute(request, body);
	
	if (log.isDebugEnabled())
	{
		log.debug(String.format("Response: %s %s", response.getStatusCode().value(),
			copyToString(response.getBody(), getCharset(response))));
	}
	
	return response;
}
 
開發者ID:markhobson,項目名稱:spring-rest-template-logger,代碼行數:21,代碼來源:LoggingInterceptor.java

示例6: createSessionTemplate

import org.springframework.http.HttpRequest; //導入依賴的package包/類
private RestTemplate createSessionTemplate(VaultEndpoint endpoint,
                                           ClientHttpRequestFactory requestFactory) {

    RestTemplate restTemplate = UtilKt.createRestTemplate(endpoint, requestFactory);

    restTemplate.getInterceptors().add(new ClientHttpRequestInterceptor() {

        @Override
        public ClientHttpResponse intercept(HttpRequest request, byte[] body,
                                            ClientHttpRequestExecution execution) throws IOException {

            final VaultToken sessionToken = sessionManager.getSessionToken();
            if (sessionToken != null) {
                final String token = sessionToken.getToken();
                if (token != null) {
                    request.getHeaders().add(VaultHttpHeaders.VAULT_TOKEN, token);
                }
            }

            return execution.execute(request, body);
        }
    });

    return restTemplate;
}
 
開發者ID:JetBrains,項目名稱:teamcity-hashicorp-vault-plugin,代碼行數:26,代碼來源:VaultTemplate.java

示例7: clientName

import org.springframework.http.HttpRequest; //導入依賴的package包/類
/**
 * 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);
}
 
開發者ID:micrometer-metrics,項目名稱:micrometer,代碼行數:15,代碼來源:RestTemplateExchangeTags.java

示例8: execute

import org.springframework.http.HttpRequest; //導入依賴的package包/類
@Override
public ClientHttpResponse execute(HttpRequest request, byte[] body) throws IOException {
	if (iterator.hasNext()) {
		ClientHttpRequestInterceptor nextInterceptor = iterator.next();
		return nextInterceptor.intercept(request, body, this);
	}
	else {
		ClientHttpRequest delegate = requestFactory.createRequest(request.getURI(), request.getMethod());

		delegate.getHeaders().putAll(request.getHeaders());

		if (body.length > 0) {
			StreamUtils.copy(body, delegate.getBody());
		}
		return delegate.execute();
	}
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:18,代碼來源:InterceptingClientHttpRequest.java

示例9: setApiKey

import org.springframework.http.HttpRequest; //導入依賴的package包/類
/**
 * Sets the api key.
 *
 * @throws JsonParseException the json parse exception
 * @throws JsonMappingException the json mapping exception
 * @throws IOException Signals that an I/O exception has occurred.
 */
private void setApiKey() throws JsonParseException, JsonMappingException, IOException{
	ArrayList<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
	interceptors.add((HttpRequest request, byte[] body, ClientHttpRequestExecution execution) -> {
		if(body.length > 0) {
			body = addTokenInObject(body, new JsonNodeFormatter());
		}else{
			try {
				request = addTokenInURI(request);
			} catch (URISyntaxException e) {
				e.printStackTrace();
			}
		}
		return execution.execute(request, body);
	});
	this.restTemplate.setInterceptors(interceptors);
}
 
開發者ID:ac-silva,項目名稱:desafio-pagarme,代碼行數:24,代碼來源:Client.java

示例10: shouldTransformHttpCallFromClientSideError

import org.springframework.http.HttpRequest; //導入依賴的package包/類
@Test
public void shouldTransformHttpCallFromClientSideError() throws Exception {
  String url = "https://ajax.googleapis.com/ajax/services/search/web?v=1.0";
  URI uri = create(url);
  String requestBody = "requestBody";
  HttpHeaders httpHeaders = new HttpHeaders();
  httpHeaders.put("Content-Type", Arrays.asList("application/json"));

  HttpRequest httpRequest = mock(HttpRequest.class);
  when(httpRequest.getMethod()).thenReturn(POST);
  when(httpRequest.getURI()).thenReturn(uri);
  when(httpRequest.getHeaders()).thenReturn(httpHeaders);

  IOException ioException = new UnknownHostException("Unable to connect");

  SpringHttpRequestTransformer transformer = new SpringHttpRequestTransformer();

  HttpCall httpCall = transformer.transform(httpRequest, toBytes(requestBody), ioException);

  assertThat(httpCall.getMethod(), is("POST"));
  assertThat(httpCall.getPayload(), is(requestBody));
  assertThat(httpCall.getUrl(), is(url));
  assertThat(httpCall.getRequestHeaders().size(), is(1));
  assertNotNull(httpCall.getRequestHeaders().get(CONTENT_TYPE));
  assertThat(httpCall.getError(), is("java.net.UnknownHostException: Unable to connect"));
}
 
開發者ID:jainsahab,項目名稱:AndroidSnooper,代碼行數:27,代碼來源:SpringHttpRequestTransformerTest.java

示例11: log

import org.springframework.http.HttpRequest; //導入依賴的package包/類
private void log(HttpRequest request, byte[] body, ClientHttpResponse response) throws IOException {
    //do logging
    String from = name;
    String to = request.getURI().
            toString().
            replace("http://", "").
            replace("http:// www.", "").
            replace("www.", "").
            replace("/", "%20").
            toLowerCase();

    System.out.println(from);
    System.out.println(to);

    restTemplate.postForObject("http://trace-callback-service/" + from + "/" + to, null, Object.class);
}
 
開發者ID:Clcanny,項目名稱:MicroServiceDemo,代碼行數:17,代碼來源:LoggingRequestInterceptor.java

示例12: initFromRequest

import org.springframework.http.HttpRequest; //導入依賴的package包/類
/**
 * Initialize a builder with a scheme, host,and port (but not path and query).
 */
private static ServletUriComponentsBuilder initFromRequest(HttpServletRequest request) {
	HttpRequest httpRequest = new ServletServerHttpRequest(request);
	UriComponents uriComponents = UriComponentsBuilder.fromHttpRequest(httpRequest).build();
	String scheme = uriComponents.getScheme();
	String host = uriComponents.getHost();
	int port = uriComponents.getPort();

	ServletUriComponentsBuilder builder = new ServletUriComponentsBuilder();
	builder.scheme(scheme);
	builder.host(host);
	if (("http".equals(scheme) && port != 80) || ("https".equals(scheme) && port != 443)) {
		builder.port(port);
	}
	return builder;
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:19,代碼來源:ServletUriComponentsBuilder.java

示例13: fromRequestWithForwardedHostAndPort

import org.springframework.http.HttpRequest; //導入依賴的package包/類
@Test
public void fromRequestWithForwardedHostAndPort() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setScheme("http");
	request.setServerName("localhost");
	request.setServerPort(80);
	request.setRequestURI("/mvc-showcase");
	request.addHeader("X-Forwarded-Proto", "https");
	request.addHeader("X-Forwarded-Host", "84.198.58.199");
	request.addHeader("X-Forwarded-Port", "443");

	HttpRequest httpRequest = new ServletServerHttpRequest(request);
	UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();

	assertEquals("https://84.198.58.199/mvc-showcase", result.toString());
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:17,代碼來源:ServletUriComponentsBuilderTests.java

示例14: fromHttpRequestWithForwardedHostWithForwardedScheme

import org.springframework.http.HttpRequest; //導入依賴的package包/類
@Test
public void fromHttpRequestWithForwardedHostWithForwardedScheme() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setScheme("http");
	request.setServerName("localhost");
	request.setServerPort(10080);
	request.addHeader("X-Forwarded-Host", "example.org");
	request.addHeader("X-Forwarded-Proto", "https");

	HttpRequest httpRequest = new ServletServerHttpRequest(request);
	UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();

	assertEquals("example.org", result.getHost());
	assertEquals("https", result.getScheme());
	assertEquals(-1, result.getPort());
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:17,代碼來源:UriComponentsBuilderTests.java

示例15: fromHttpRequestWithForwardedProtoAndDefaultPort

import org.springframework.http.HttpRequest; //導入依賴的package包/類
@Test
public void fromHttpRequestWithForwardedProtoAndDefaultPort() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setScheme("http");
	request.setServerName("localhost");
	request.setServerPort(80);
	request.setRequestURI("/mvc-showcase");
	request.addHeader("X-Forwarded-Proto", "https");
	request.addHeader("X-Forwarded-Host", "84.198.58.199");
	request.addHeader("X-Forwarded-Port", "443");

	HttpRequest httpRequest = new ServletServerHttpRequest(request);
	UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();

	assertEquals("https://84.198.58.199/mvc-showcase", result.toString());
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:17,代碼來源:UriComponentsBuilderTests.java


注:本文中的org.springframework.http.HttpRequest類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。