Java Code Examples for org.springframework.http.HttpMethod#name()

The following examples show how to use org.springframework.http.HttpMethod#name() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WingtipsSpringUtilTest.java    From wingtips with Apache License 2.0 6 votes vote down vote up
@DataProvider(value = {
    "GET",
    "HEAD",
    "POST",
    "PUT",
    "PATCH",
    "DELETE",
    "OPTIONS",
    "TRACE",
    "null"
})
@Test
public void getRequestMethodAsString_works_as_expected(
    HttpMethod method
) {
    // given
    String expectedResult = (method == null) ? "UNKNOWN_HTTP_METHOD" : method.name();

    // when
    String result = WingtipsSpringUtil.getRequestMethodAsString(method);

    // then
    assertThat(result).isEqualTo(expectedResult);
}
 
Example 2
Source File: AsyncRestTemplate.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Execute the given method on the provided URI. The
 * {@link org.springframework.http.client.ClientHttpRequest}
 * is processed using the {@link RequestCallback}; the response with
 * the {@link ResponseExtractor}.
 * @param url the fully-expanded URL to connect to
 * @param method the HTTP method to execute (GET, POST, etc.)
 * @param requestCallback object that prepares the request (can be {@code null})
 * @param responseExtractor object that extracts the return value from the response (can
 * be {@code null})
 * @return an arbitrary object, as returned by the {@link ResponseExtractor}
 */
protected <T> ListenableFuture<T> doExecute(URI url, HttpMethod method,
		@Nullable AsyncRequestCallback requestCallback,
		@Nullable ResponseExtractor<T> responseExtractor) throws RestClientException {

	Assert.notNull(url, "'url' must not be null");
	Assert.notNull(method, "'method' must not be null");
	try {
		org.springframework.http.client.AsyncClientHttpRequest request = createAsyncRequest(url, method);
		if (requestCallback != null) {
			requestCallback.doWithRequest(request);
		}
		ListenableFuture<ClientHttpResponse> responseFuture = request.executeAsync();
		return new ResponseExtractorFuture<>(method, url, responseFuture, responseExtractor);
	}
	catch (IOException ex) {
		throw new ResourceAccessException("I/O error on " + method.name() +
				" request for \"" + url + "\":" + ex.getMessage(), ex);
	}
}
 
Example 3
Source File: AsyncRestTemplate.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Execute the given method on the provided URI. The
 * {@link org.springframework.http.client.ClientHttpRequest}
 * is processed using the {@link RequestCallback}; the response with
 * the {@link ResponseExtractor}.
 * @param url the fully-expanded URL to connect to
 * @param method the HTTP method to execute (GET, POST, etc.)
 * @param requestCallback object that prepares the request (can be {@code null})
 * @param responseExtractor object that extracts the return value from the response (can
 * be {@code null})
 * @return an arbitrary object, as returned by the {@link ResponseExtractor}
 */
protected <T> ListenableFuture<T> doExecute(URI url, HttpMethod method,
		@Nullable AsyncRequestCallback requestCallback,
		@Nullable ResponseExtractor<T> responseExtractor) throws RestClientException {

	Assert.notNull(url, "'url' must not be null");
	Assert.notNull(method, "'method' must not be null");
	try {
		org.springframework.http.client.AsyncClientHttpRequest request = createAsyncRequest(url, method);
		if (requestCallback != null) {
			requestCallback.doWithRequest(request);
		}
		ListenableFuture<ClientHttpResponse> responseFuture = request.executeAsync();
		return new ResponseExtractorFuture<>(method, url, responseFuture, responseExtractor);
	}
	catch (IOException ex) {
		throw new ResourceAccessException("I/O error on " + method.name() +
				" request for \"" + url + "\":" + ex.getMessage(), ex);
	}
}
 
Example 4
Source File: RestTemplate.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Execute the given method on the provided URI.
 * <p>The {@link ClientHttpRequest} is processed using the {@link RequestCallback};
 * the response with the {@link ResponseExtractor}.
 * @param url the fully-expanded URL to connect to
 * @param method the HTTP method to execute (GET, POST, etc.)
 * @param requestCallback object that prepares the request (can be {@code null})
 * @param responseExtractor object that extracts the return value from the response (can be {@code null})
 * @return an arbitrary object, as returned by the {@link ResponseExtractor}
 */
protected <T> T doExecute(URI url, HttpMethod method, RequestCallback requestCallback,
		ResponseExtractor<T> responseExtractor) throws RestClientException {

	Assert.notNull(url, "'url' must not be null");
	Assert.notNull(method, "'method' must not be null");
	ClientHttpResponse response = null;
	try {
		ClientHttpRequest request = createRequest(url, method);
		if (requestCallback != null) {
			requestCallback.doWithRequest(request);
		}
		response = request.execute();
		handleResponse(url, method, response);
		if (responseExtractor != null) {
			return responseExtractor.extractData(response);
		}
		else {
			return null;
		}
	}
	catch (IOException ex) {
		throw new ResourceAccessException("I/O error on " + method.name() +
				" request for \"" + url + "\": " + ex.getMessage(), ex);
	}
	finally {
		if (response != null) {
			response.close();
		}
	}
}
 
Example 5
Source File: WingtipsSpringUtil.java    From wingtips with Apache License 2.0 5 votes vote down vote up
/**
 * @param method The HTTP method.
 * @return "UNKNOWN_HTTP_METHOD" if the method is null, otherwise {@link HttpMethod#name()}.
 */
public static String getRequestMethodAsString(HttpMethod method) {
    if (method == null) {
        return "UNKNOWN_HTTP_METHOD";
    }

    return method.name();
}
 
Example 6
Source File: SpringHttpClientTagAdapter.java    From wingtips with Apache License 2.0 5 votes vote down vote up
@Override
public @Nullable String getRequestHttpMethod(@Nullable HttpRequest request) {
    if (request == null) {
        return null;
    }

    HttpMethod method = request.getMethod();

    return (method == null) ? "UNKNOWN_HTTP_METHOD" : method.name();
}
 
Example 7
Source File: RestTemplateConfig.java    From spring-boot-cookbook with Apache License 2.0 5 votes vote down vote up
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
    long begin = System.currentTimeMillis();
    String httpMethodName = UNKNOWN_HTTP_METHOD;
    HttpMethod method = request.getMethod();
    if (method != null) {
        httpMethodName = method.name();
    }
    String requestBody = IOUtils.toString(body, "utf-8");
    try {
        ClientHttpResponse response = execution.execute(request, body);
        long cost = System.currentTimeMillis() - begin;

        MediaType contentType = response.getHeaders().getContentType();
        String responseContent;
        if (contentType != null) {
            String subtype = contentType.getSubtype();
            if (subtype.contains("json") || contentType.getType().contains("text") || subtype.contains("xml")) {
                responseContent = IOUtils.toString(response.getBody(), "utf-8");
            } else {
                responseContent = "neither text,nor xml,nor json";
            }
        } else {
            responseContent = HAS_NO_CONTENT_TYPE;
        }

        if (cost > 3000) {
            log.info("【特殊标识】慢接口 【 TODOLIST 】 大于3秒 cost:[{}]ms,{} {}. request:{},response:{},", cost, httpMethodName, request.getURI().toString(), requestBody, responseContent);
        } else if (cost > 2000) {
            log.info("【特殊标识】慢接口 【 TODOLIST 】 大于2秒 cost:[{}]ms,{} {}. request:{},response:{},", cost, httpMethodName, request.getURI().toString(), requestBody, responseContent);
        } else {
            log.info("cost:[{}]ms,on {} {}. request:{},response:{},", cost, httpMethodName, request.getURI().toString(), requestBody, responseContent);
        }
        return response;
    } catch (IOException e) {
        log.error("【特殊标识】【 TODOLIST 】 接口 cost:[{}]ms,I/O error on {} {}. request:{},response:{},", (System.currentTimeMillis() - begin), httpMethodName, request.getURI().toString(), requestBody, e.getMessage());
        throw e;
    }

}
 
Example 8
Source File: RestTemplate.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Execute the given method on the provided URI.
 * <p>The {@link ClientHttpRequest} is processed using the {@link RequestCallback};
 * the response with the {@link ResponseExtractor}.
 * @param url the fully-expanded URL to connect to
 * @param method the HTTP method to execute (GET, POST, etc.)
 * @param requestCallback object that prepares the request (can be {@code null})
 * @param responseExtractor object that extracts the return value from the response (can be {@code null})
 * @return an arbitrary object, as returned by the {@link ResponseExtractor}
 */
@Nullable
protected <T> T doExecute(URI url, @Nullable HttpMethod method, @Nullable RequestCallback requestCallback,
		@Nullable ResponseExtractor<T> responseExtractor) throws RestClientException {

	Assert.notNull(url, "URI is required");
	Assert.notNull(method, "HttpMethod is required");
	ClientHttpResponse response = null;
	try {
		ClientHttpRequest request = createRequest(url, method);
		if (requestCallback != null) {
			requestCallback.doWithRequest(request);
		}
		response = request.execute();
		handleResponse(url, method, response);
		return (responseExtractor != null ? responseExtractor.extractData(response) : null);
	}
	catch (IOException ex) {
		String resource = url.toString();
		String query = url.getRawQuery();
		resource = (query != null ? resource.substring(0, resource.indexOf('?')) : resource);
		throw new ResourceAccessException("I/O error on " + method.name() +
				" request for \"" + resource + "\": " + ex.getMessage(), ex);
	}
	finally {
		if (response != null) {
			response.close();
		}
	}
}
 
Example 9
Source File: WingtipsSpringWebfluxExchangeFilterFunction.java    From wingtips with Apache License 2.0 5 votes vote down vote up
/**
 * @return The value of {@link HttpMethod#name()}, or "UNKNOWN_HTTP_METHOD" if the given method is null.
 */
protected @NotNull String getRequestMethodAsString(@Nullable HttpMethod method) {
    if (method == null) {
        return "UNKNOWN_HTTP_METHOD";
    }

    return method.name();
}
 
Example 10
Source File: DefaultServerHttpRequestBuilder.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public ServerHttpRequest.Builder method(HttpMethod httpMethod) {
	this.httpMethodValue = httpMethod.name();
	return this;
}
 
Example 11
Source File: DefaultServerHttpRequestBuilder.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public ServerHttpRequest.Builder method(HttpMethod httpMethod) {
	this.httpMethodValue = httpMethod.name();
	return this;
}
 
Example 12
Source File: MethodNotAllowedException.java    From spring-analysis-note with MIT License 4 votes vote down vote up
public MethodNotAllowedException(HttpMethod method, Collection<HttpMethod> supportedMethods) {
	this(method.name(), supportedMethods);
}
 
Example 13
Source File: AlfrescoRestRegistrar.java    From alfresco-mvc with Apache License 2.0 4 votes vote down vote up
private String getWebscriptName(String webscript, HttpMethod httpMethod) {
	String beanName = "webscript." + webscript + "." + httpMethod.name();
	return beanName.toLowerCase();
}
 
Example 14
Source File: OperationsTransformer.java    From spring-openapi with MIT License 4 votes vote down vote up
private String getOperationId(String nameFromAnnotation, Method method, HttpMethod httpMethod) {
	return StringUtils.isBlank(nameFromAnnotation) ? method.getName() + "Using" + httpMethod.name() : nameFromAnnotation;
}
 
Example 15
Source File: OperationsTransformer.java    From spring-openapi with MIT License 4 votes vote down vote up
private String getOperationId(String nameFromAnnotation, Method method, HttpMethod httpMethod) {
	return StringUtils.isBlank(nameFromAnnotation) ? method.getName() + "Using" + httpMethod.name() : nameFromAnnotation;
}
 
Example 16
Source File: HttpImpl.java    From heimdall with Apache License 2.0 3 votes vote down vote up
private <T> ResponseEntity<T> sendRequest(URI uri, HttpMethod method, HttpEntity httpEntity, Class<T> responseType) {

        if (isFailSafeEnabled) {

            String url = method.name() + ":" + uri.toString();

            return circuitBreakerManager.failsafe(
                    () -> this.restTemplate.exchange(uri, method, httpEntity, responseType),
                    url
            );
        }

        return this.restTemplate.exchange(uri, method, httpEntity, responseType);

    }
 
Example 17
Source File: MockHttpServletRequestBuilder.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * Alternative to {@link #MockHttpServletRequestBuilder(HttpMethod, String, Object...)}
 * with a pre-built URI.
 * @param httpMethod the HTTP method (GET, POST, etc)
 * @param url the URL
 * @since 4.0.3
 */
MockHttpServletRequestBuilder(HttpMethod httpMethod, URI url) {
	this(httpMethod.name(), url);
}
 
Example 18
Source File: MockHttpServletRequestBuilder.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * Package private constructor. To get an instance, use static factory
 * methods in {@link MockMvcRequestBuilders}.
 * <p>Although this class cannot be extended, additional ways to initialize
 * the {@code MockHttpServletRequest} can be plugged in via
 * {@link #with(RequestPostProcessor)}.
 * @param httpMethod the HTTP method (GET, POST, etc)
 * @param url a URL template; the resulting URL will be encoded
 * @param vars zero or more URI variables
 */
MockHttpServletRequestBuilder(HttpMethod httpMethod, String url, Object... vars) {
	this(httpMethod.name(), UriComponentsBuilder.fromUriString(url).buildAndExpand(vars).encode().toUri());
}
 
Example 19
Source File: MockHttpServletRequestBuilder.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * Package private constructor. To get an instance, use static factory
 * methods in {@link MockMvcRequestBuilders}.
 * <p>Although this class cannot be extended, additional ways to initialize
 * the {@code MockHttpServletRequest} can be plugged in via
 * {@link #with(RequestPostProcessor)}.
 * @param httpMethod the HTTP method (GET, POST, etc)
 * @param url a URL template; the resulting URL will be encoded
 * @param vars zero or more URI variables
 */
MockHttpServletRequestBuilder(HttpMethod httpMethod, String url, Object... vars) {
	this(httpMethod.name(), UriComponentsBuilder.fromUriString(url).buildAndExpand(vars).encode().toUri());
}
 
Example 20
Source File: MockHttpServletRequestBuilder.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * Alternative to {@link #MockHttpServletRequestBuilder(HttpMethod, String, Object...)}
 * with a pre-built URI.
 * @param httpMethod the HTTP method (GET, POST, etc)
 * @param url the URL
 * @since 4.0.3
 */
MockHttpServletRequestBuilder(HttpMethod httpMethod, URI url) {
	this(httpMethod.name(), url);
}