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

The following examples show how to use org.springframework.http.HttpMethod#equals() . 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: cucumber-rest-steps   File: RestSteps.java    License: MIT License 6 votes vote down vote up
private void callWithFile(String httpMethodString, String path, String file, Resource resource) {
  HttpMethod httpMethod = HttpMethod.valueOf(httpMethodString.toUpperCase());

  if (httpMethod.equals(HttpMethod.GET)) {
    throw new IllegalArgumentException("You can't submit a file in a GET call");
  }

  MultipartBodyBuilder builder = new MultipartBodyBuilder();
  builder.part(file, resource);

  responseSpec = webClient.method(httpMethod)
      .uri(path)
      .syncBody(builder.build())
      .exchange();
  expectBody = null;
}
 
Example 2
Source Project: Poseidon   File: APIApplication.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void handleRequest(PoseidonRequest request, PoseidonResponse response) throws ElementNotFoundException, BadRequestException, ProcessingException, InternalErrorException {
    HttpMethod method = request.getAttribute(RequestConstants.METHOD);
    boolean handled = false;
    if (method != null && method.equals(HttpMethod.OPTIONS)) {
        handled = handleOptionsRequest(request, response);
    }

    if (!handled) {
        // Ideally, we have to get the buildable for this request, get
        // API name from the buildable and use it to start a meter here.
        // As getting a buildable could be time consuming (say we use trie
        // instead of a map as in APIBuildable), we start a meter in
        // APILegoSet.getBuildable() and stop it here
        try {
            lego.buildResponse(request, response);
        } finally {
            Object timerContext = request.getAttribute(TIMER_CONTEXT);
            if (timerContext != null && timerContext instanceof Timer.Context) {
                ((Timer.Context) timerContext).stop();
            }
        }
    }
}
 
Example 3
Source Project: cucumber-rest-steps   File: RestSteps.java    License: MIT License 5 votes vote down vote up
private void call(String httpMethodString, URI uri, Object requestObj) throws JSONException {
  HttpMethod httpMethod = HttpMethod.valueOf(httpMethodString.toUpperCase());

  if (httpMethod.equals(HttpMethod.GET) && requestObj != null) {
    throw new IllegalArgumentException("You can't pass data in a GET call");
  }

  final String path = uri.toString();
  if (path.contains("$.")) {
    initExpectBody();
    Pattern pattern = Pattern.compile("(((\\$.*)\\/.*)|(\\$.*))");
    Matcher matcher = pattern.matcher(path);
    assertTrue(matcher.find());
    final String jsonPath = matcher.group(0);
    final byte[] responseBody = expectBody.jsonPath(jsonPath).exists().returnResult().getResponseBody();
    final String value = ((JSONObject) JSONParser.parseJSON(new String(responseBody))).getString(jsonPath.replace("$.", ""));
    uri = URI.create(path.replace(jsonPath, value));
  }

  final RequestBodySpec requestBodySpec = webClient.method(httpMethod).uri(uri);
  if (requestObj != null) {
    requestBodySpec.syncBody(requestObj);
    requestBodySpec.contentType(MediaType.APPLICATION_JSON);
  }

  if (headers != null) {
    headers.forEach((key, value) -> requestBodySpec.header(key, value.toArray(new String[0])));
  }

  responseSpec = requestBodySpec.exchange();
  expectBody = null;
  headers = null;
}
 
Example 4
@Override
public boolean match(HttpRequest request) {
	boolean matched = false;
	HttpMethod httpMethod = request.getMethod();
	if (httpMethod != null) {
		for (HttpMethod method : getMethods()) {
			if (httpMethod.equals(method)) {
				matched = true;
				break;
			}
		}
	}
	return matched;
}
 
Example 5
/**
 * Determines if the resources supports the resource action specified by resourceInterfaceWithOneMethod 
 * @param resourceInterfaceWithOneMethod The resource action
 * @param httpMethod http method the action supports.
 * @param helper Holder of simple meta data
 */
private static void findOperation(Class<? extends ResourceAction> resourceInterfaceWithOneMethod, HttpMethod httpMethod, MetaHelperCallback helper)
{
    if (resourceInterfaceWithOneMethod.isAssignableFrom(helper.resource))
    {
        Method aMethod = findMethod(resourceInterfaceWithOneMethod, helper.resource);
        ResourceOperation operation = inspectOperation(helper.resource, aMethod, httpMethod);

        if (isDeleted(aMethod))
        {
            helper.whenOperationDeleted(resourceInterfaceWithOneMethod, aMethod);    
        } 
        else 
        {
            helper.whenNewOperation(operation, aMethod);
        }

        if (isNoAuth(aMethod))
        {
            if (! (httpMethod.equals(HttpMethod.GET) || httpMethod.equals(HttpMethod.POST)))
            {
                throw new IllegalArgumentException("@WebApiNoAuth should only be on GET or POST methods: " + operation.getTitle());
            }
            helper.whenOperationNoAuth(resourceInterfaceWithOneMethod, aMethod);
        }
    }
}
 
Example 6
@Test
public void wrapPutAndPatchOnly() throws Exception {
	request.setContent("".getBytes("ISO-8859-1"));
	for (HttpMethod method : HttpMethod.values()) {
		request.setMethod(method.name());
		filterChain = new MockFilterChain();
		filter.doFilter(request, response, filterChain);
		if (method.equals(HttpMethod.PUT) || method.equals(HttpMethod.PATCH)) {
			assertNotSame("Should wrap HTTP method " + method, request, filterChain.getRequest());
		}
		else {
			assertSame("Should not wrap for HTTP method " + method, request, filterChain.getRequest());
		}
	}
}
 
Example 7
public static Object findFunction(HttpMethod method, FunctionCatalog functionCatalog,
										Map<String, Object> attributes, String path) {
	if (method.equals(HttpMethod.GET) || method.equals(HttpMethod.POST)) {
		return doFindFunction(method, functionCatalog, attributes, path);
	}
	else {
		throw new IllegalStateException("HTTP method '" + method + "' is not supported;");
	}
}
 
Example 8
private static Object doFindFunction(HttpMethod method, FunctionCatalog functionCatalog,
										Map<String, Object> attributes, String path) {
	path = path.startsWith("/") ? path.substring(1) : path;
	if (method.equals(HttpMethod.GET)) {
		Supplier<Publisher<?>> supplier = functionCatalog.lookup(Supplier.class, path);
		if (supplier != null) {
			attributes.put(WebRequestConstants.SUPPLIER, supplier);
			return supplier;
		}
	}

	StringBuilder builder = new StringBuilder();
	String name = path;
	String value = null;
	for (String element : path.split("/")) {
		if (builder.length() > 0) {
			builder.append("/");
		}
		builder.append(element);
		name = builder.toString();
		value = path.length() > name.length() ? path.substring(name.length() + 1)
				: null;
		Function<Object, Object> function = functionCatalog.lookup(Function.class,
				name);
		if (function != null) {
			attributes.put(WebRequestConstants.FUNCTION, function);
			if (value != null) {
				attributes.put(WebRequestConstants.ARGUMENT, value);
			}
			return function;
		}
	}
	return null;
}