Java Code Examples for org.springframework.web.util.UriComponents#getPathSegments()

The following examples show how to use org.springframework.web.util.UriComponents#getPathSegments() . 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: HtmlUnitRequestBuilder.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private void contextPath(MockHttpServletRequest request, UriComponents uriComponents) {
	if (this.contextPath == null) {
		List<String> pathSegments = uriComponents.getPathSegments();
		if (pathSegments.isEmpty()) {
			request.setContextPath("");
		}
		else {
			request.setContextPath("/" + pathSegments.get(0));
		}
	}
	else {
		String path = uriComponents.getPath();
		Assert.isTrue(path != null && path.startsWith(this.contextPath),
				() -> "\"" + uriComponents.getPath() +
						"\" should start with context path \"" + this.contextPath + "\"");
		request.setContextPath(this.contextPath);
	}
}
 
Example 2
Source File: HtmlUnitRequestBuilder.java    From java-technology-stack with MIT License 6 votes vote down vote up
private void contextPath(MockHttpServletRequest request, UriComponents uriComponents) {
	if (this.contextPath == null) {
		List<String> pathSegments = uriComponents.getPathSegments();
		if (pathSegments.isEmpty()) {
			request.setContextPath("");
		}
		else {
			request.setContextPath("/" + pathSegments.get(0));
		}
	}
	else {
		String path = uriComponents.getPath();
		Assert.isTrue(path != null && path.startsWith(this.contextPath),
				() -> "\"" + uriComponents.getPath() +
						"\" should start with context path \"" + this.contextPath + "\"");
		request.setContextPath(this.contextPath);
	}
}
 
Example 3
Source File: HtmlUnitRequestBuilder.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
private void contextPath(MockHttpServletRequest request, UriComponents uriComponents) {
	if (this.contextPath == null) {
		List<String> pathSegments = uriComponents.getPathSegments();
		if (pathSegments.isEmpty()) {
			request.setContextPath("");
		}
		else {
			request.setContextPath("/" + pathSegments.get(0));
		}
	}
	else {
		if (!uriComponents.getPath().startsWith(this.contextPath)) {
			throw new IllegalArgumentException(uriComponents.getPath() + " should start with contextPath "
					+ this.contextPath);
		}
		request.setContextPath(this.contextPath);
	}
}
 
Example 4
Source File: RestAPIController.java    From gemini with Apache License 2.0 4 votes vote down vote up
private Object requestHandler(String entityString, Object body, HttpServletRequest request, HttpServletResponse response) throws GeminiException {
    String method = request.getMethod();
    Entity entity = checkEntity(entityString.toUpperCase(), method);
    UriComponents uriComponents = UriComponentsBuilder.fromUriString(request.getRequestURI()).build();
    List<String> paths = uriComponents.getPathSegments();
    ensurePathsAreConsistent(paths, entity);
    Map<String, String[]> parameters = request.getParameterMap(); // query string params
    List<String> geminiHeader = getGeminiHeader(request);
    EntityOperationContext entityOperationContext = createEntityOperationContext(request, entityString, body);
    if (paths.size() == 2) {
        // this is a root entity requet - METHOD ALLOWED POST AND GET (for list)
        switch (method) {
            case "POST":
                if (body == null) {
                    throw InvalidRequesException.BODY_REQUIRED();
                }
                return handleInsertRecord(entity, geminiHeader, body, entityOperationContext);
            case "GET": {
                if (entity.isOneRecord())
                    return handleGetEntityOneRecord(entity, parameters, entityOperationContext);
                return handleGetEntityList(entity, parameters, entityOperationContext);
            }
            case "PUT": {
                if (entity.isOneRecord()) {
                    return handleUpdateEntityOneRecord(entity, body, entityOperationContext);
                }
            }
            default:
                throw InvalidRequesException.INVALID_METHOD_FOR_REQUEST(method);
        }
    }

    if (paths.size() > 2) {
        int requestLkLenght = paths.size() - 2;
        Entity.LogicalKey logicalKey = entity.getLogicalKey();
        List<EntityField> logicalKeyList = logicalKey.getLogicalKeyList();
        if (requestLkLenght != 1 && logicalKeyList.size() < requestLkLenght) {
            // it is not a UUID and it is a multi lk entity request
            throw InvalidRequesException.CANNOT_HANDLE_REQUEST();
        }
        if (requestLkLenght == 1 || logicalKeyList.size() == requestLkLenght) {
            // we can query a entity record so we can GET/UPDATE/DELETE
            String[] lkStringsArray = decodeLogicalKeyStrings(paths);
            switch (method) {
                case "GET":
                    return handleGetRecord(entity, entityOperationContext, lkStringsArray);
                case "PUT":
                    return handleUpdateRecord(entity, body, entityOperationContext, lkStringsArray);
                case "DELETE":
                    return handleDeleteRecord(entity, entityOperationContext, lkStringsArray);
                default:
                    throw InvalidRequesException.INVALID_METHOD_FOR_REQUEST(method);
            }
        }
        if (logicalKeyList.size() > requestLkLenght) {
            // we are quering a subfield or a subfield list
            throw InvalidRequesException.CANNOT_HANDLE_REQUEST();
        }
    }
    throw InvalidRequesException.CANNOT_HANDLE_REQUEST();
}