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

The following examples show how to use org.springframework.http.HttpMethod#name() . These examples are extracted from open source projects. 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 Project: wingtips   File: WingtipsSpringUtilTest.java    License: 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 Project: spring-analysis-note   File: AsyncRestTemplate.java    License: 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
/**
 * 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
/**
 * @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 5
Source Project: java-technology-stack   File: RestTemplate.java    License: 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 6
@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 7
@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 8
Source Project: wingtips   File: WingtipsSpringUtil.java    License: 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 9
/**
 * 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 10
Source Project: spring-openapi   File: OperationsTransformer.java    License: 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 11
Source Project: spring-openapi   File: OperationsTransformer.java    License: 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 12
public MethodNotAllowedException(HttpMethod method, Collection<HttpMethod> supportedMethods) {
	this(method.name(), supportedMethods);
}
 
Example 13
@Override
public ServerHttpRequest.Builder method(HttpMethod httpMethod) {
	this.httpMethodValue = httpMethod.name();
	return this;
}
 
Example 14
@Override
public ServerHttpRequest.Builder method(HttpMethod httpMethod) {
	this.httpMethodValue = httpMethod.name();
	return this;
}
 
Example 15
private String getWebscriptName(String webscript, HttpMethod httpMethod) {
	String beanName = "webscript." + webscript + "." + httpMethod.name();
	return beanName.toLowerCase();
}
 
Example 16
Source Project: heimdall   File: HttpImpl.java    License: 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
/**
 * 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 18
/**
 * 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 19
/**
 * 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
/**
 * 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);
}