Closed Github-Polaris closed 4 months ago
嗨,@BreakMeEveryday!看来我们又在这里相遇了,希望这次我能帮到你。我正在查看你的问题,很快会给你一个完整的答复。
我跑get的方法是对的,所以client这边应该没有问题,是因为我没有配置请求头吗,还是说这里不要用RestClient去请求,而是用普通的http工具?比如hutool下的一些Http工具类去发送请求?
@dosu-bot , 请问动态创建图的 API 在 java-client 或者 HTTP 请求中应该如何构造, 另外请给出动态创建图的 API 代码引用以及相关的测试代码例子
There is the result after invoking Apifox.
我跑get的方法是对的,所以client这边应该没有问题,是因为我没有配置请求头吗,还是说这里不要用RestClient去请求,而是用普通的http工具?比如hutool下的一些Http工具类去发送请求?
@dosu-bot , 请问动态创建图的 API 在 java-client 或者 HTTP 请求中应该如何构造, 另外请给出动态创建图的 API 代码引用以及相关的测试代码例子
@dosu-bot, please respond promptly to the user's questions based on the previous context
To address the issue with the client.post()
method and guide you on how to dynamically create a graph using the HugeGraph API, here's a focused approach:
Resolve java.net.UnknownHostException
: This error indicates a problem with network connectivity or DNS resolution. Ensure the HugeGraph server URL is correct and accessible from your development environment. Verify network settings, firewall configurations, and DNS resolution to ensure your application can reach the HugeGraph server.
Use HugeClient.builder(url, graph)
for Client Configuration: Properly configure your HugeClient
instance with the server URL, graph name, and any necessary authentication details. Adjust connection parameters as needed, including timeout settings and SSL configurations if your server uses SSL.
Check Server and Client Dependencies: Ensure the HugeGraph server is correctly configured, especially if using RocksDB, and that all required client dependencies are included in your project.
Dynamic Graph Creation API Usage:
GraphsAPI.java
:
public Map<String, String> create(String name, String cloneGraphName, String configText) {
RestHeaders headers = new RestHeaders().add(RestHeaders.CONTENT_TYPE, "text/plain");
Map<String, Object> params = null;
if (StringUtils.isNotEmpty(cloneGraphName)) {
params = ImmutableMap.of("clone_graph_name", cloneGraphName);
}
RestResult result = this.client.post(joinPath(this.path(), name), configText, headers, params);
return result.readObject(Map.class);
}
name
: Name of the new graph.cloneGraphName
: (Optional) Name of an existing graph to clone.configText
: Configuration for the new graph.This method checks the API version for compatibility and uses the client.post()
method with appropriate headers and parameters to create the graph. Ensure your request includes the correct content type and any necessary parameters, such as the name of an existing graph to clone, if applicable.
By following these steps and utilizing the provided API method, you should be able to dynamically create graphs in HugeGraph without encountering the client.post()
method failure.
To continue the conversation, mention @dosu.
@dosu Sorry, your answer is not working and there is no 4 parameters overload method with client.post(). This is my solution and it works. 对不起,您的答案不起作用,并且没有带有 client.post() 的 4 个参数重载方法。这是我的解决方案,它有效。
/**
* 该Test方法成功,url可以替换成你自己的路径,我只是用了常量定义,HttpUtil使用的是hutool 5.8.27包,这个也可以自定义替换
* */
@Test
public void createGraphUsingHttpUtil(){
String url = ConstTransferPath.BASE_URL.getPath()
+ConstTransferPath.GRAPHS.getPath()
+"hugegraph2";
String configInfo = "gremlin.graph=org.apache.hugegraph.auth.HugeFactoryAuthProxy\n" +
"backend=rocksdb\n" +
"serializer=binary\n" +
"store=hugegraph2\n" +
"rocksdb.data_path=./rks-data-2\n" +
"rocksdb.wal_path=./rks-data-2";
try{
String response = HttpUtil.createPost(url)
.header("Content-Type", "text/plain") //Attention Please! The content-type is text/plain.
.body(configInfo)
.execute()
.body();
System.out.println(JSONUtil.toJsonPrettyStr(response));
}catch (Exception e){
log.error(e.getMessage());
}
}
Thank you for sharing your solution. It's great to see you've found a way to dynamically create a graph using HttpUtil from the hutool library. Your approach, which involves setting the Content-Type
header to text/plain
and directly posting the configuration information to the server, is a practical example of interacting with the API. This method is indeed effective for users who prefer or require an alternative to the provided Java client.
For others who might be following this discussion, the key takeaway from @BreakMeEveryday's solution is the importance of correctly setting the Content-Type
header and ensuring that the body of the request contains the necessary configuration for creating a new graph. This example also highlights the flexibility of the HugeGraph API, allowing for various HTTP client tools to be used for interaction, not limited to the Java client.
If there are any further questions or if anyone needs additional clarification on working with the HugeGraph API, feel free to ask.
To continue the conversation, mention @dosu.
@BreakMeEveryday indeed there are actually 2 problems:
HTTP-API
direct request to create the graph failed
java-client
side does not provide an related tests for it (in toolchain -> client)
/graphs/new-graph.properties
& restart server to make it work and avoid blocking here (we have 3 ways to make it work)BTW, u could try to use docker version to avoid some environmental impacts and run tests quickly
@dosu u need tell users to move/check the toolchain repo for client related issues (they may miss the context if u don't remind them)
@imbajin @dosu
Yeah, I did found the org.apache.hugegraph.api.graphs.GraphsAPI.create(String name, String cloneGraphName, String configText)
in hugegraph-client project, it may works and I will give it a try.
A bad news, I'm failed and here is my result. I had run the test method test/java/org.apache.hugegraph.api.GraphsApiTest.testCreateAndDropGraph()
in hugegraph-toolchain/hugegraph-client module.
@Test
public void testCreateAndDropGraph() {
int initialGraphNumber = graphsAPI.list().size();
// Create new graph dynamically
String config;
try {
config = FileUtils.readFileToString(new File(CONFIG2_PATH),
StandardCharsets.UTF_8);
} catch (IOException e) {
throw new ClientException("Failed to read config file: %s",
CONFIG2_PATH);
}
Map<String, String> result = graphsAPI.create(GRAPH2, null, config);
...
}
But a same exception as before occured.
And here is the stack of exceptions of server
java.lang.IllegalStateException: Missing authentication context when verifying resource permission
at com.google.common.base.Preconditions.checkState(Preconditions.java:532) ~[guava-30.0-jre.jar:?]
at org.apache.hugegraph.util.E.checkState(E.java:64) ~[hugegraph-common-1.3.0.jar:1.3.0]
at org.apache.hugegraph.auth.HugeGraphAuthProxy.verifyResPermission(HugeGraphAuthProxy.java:964) ~[classes/:?]
at org.apache.hugegraph.auth.HugeGraphAuthProxy.verifyResPermission(HugeGraphAuthProxy.java:955) ~[classes/:?]
at org.apache.hugegraph.auth.HugeGraphAuthProxy.verifyPermission(HugeGraphAuthProxy.java:828) ~[classes/:?]
at org.apache.hugegraph.auth.HugeGraphAuthProxy.create(HugeGraphAuthProxy.java:781) ~[classes/:?]
at org.apache.hugegraph.core.GraphManager.createGraph(GraphManager.java:591) ~[classes/:?]
at org.apache.hugegraph.core.GraphManager.createGraph(GraphManager.java:188) ~[classes/:?]
at org.apache.hugegraph.api.profile.GraphsAPI.create(GraphsAPI.java:136) ~[classes/:?]
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:?]
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
at java.lang.reflect.Method.invoke(Method.java:566) ~[?:?]
at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.lambda$static$0(ResourceMethodInvocationHandlerFactory.java:52) ~[jersey-server-3.0.3.jar:?]
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:124) ~[jersey-server-3.0.3.jar:?]
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:167) ~[jersey-server-3.0.3.jar:?]
at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ObjectOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:192) ~[jersey-server-3.0.3.jar:?]
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:79) ~[jersey-server-3.0.3.jar:?]
at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:475) ~[jersey-server-3.0.3.jar:?]
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:397) ~[jersey-server-3.0.3.jar:?]
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:81) ~[jersey-server-3.0.3.jar:?]
at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:255) ~[jersey-server-3.0.3.jar:?]
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:248) ~[jersey-common-3.0.3.jar:?]
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:244) ~[jersey-common-3.0.3.jar:?]
at org.glassfish.jersey.internal.Errors.process(Errors.java:292) ~[jersey-common-3.0.3.jar:?]
at org.glassfish.jersey.internal.Errors.process(Errors.java:274) ~[jersey-common-3.0.3.jar:?]
at org.glassfish.jersey.internal.Errors.process(Errors.java:244) ~[jersey-common-3.0.3.jar:?]
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:265) ~[jersey-common-3.0.3.jar:?]
at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:234) ~[jersey-server-3.0.3.jar:?]
at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:684) ~[jersey-server-3.0.3.jar:?]
at org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer.service(GrizzlyHttpContainer.java:356) ~[jersey-container-grizzly2-http-3.0.3.jar:?]
at org.glassfish.grizzly.http.server.HttpHandler$1.run(HttpHandler.java:190) ~[grizzly-http-server-3.0.1.jar:3.0.1]
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:535) ~[grizzly-framework-3.0.1.jar:3.0.1]
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:515) ~[grizzly-framework-3.0.1.jar:3.0.1]
at java.lang.Thread.run(Thread.java:829) [?:?]
U know, I did nothing to the username
and password
and even I didn't touch the username and password since using the HugeGraph.
public class BaseClientTest {
protected static final String BASE_URL = "http://192.168.10.70:8080";
protected static final String GRAPH = "hugegraph";
protected static final String USERNAME = "admin";
protected static final String PASSWORD = "pa";
...
}
The issue you're encountering, indicated by the java.lang.IllegalStateException
with the message "Missing authentication context when verifying resource permission," suggests a problem with authentication during your attempt to dynamically create a graph using the HugeGraph API. It appears that the client might not be authenticated or the authentication context is not properly established for the API call.
To address this, ensure your client is authenticated before making any API requests, especially if your HugeGraph server requires authentication. Here's a concise way to authenticate using token-based authentication, which is recommended for security:
RestClient
with your HugeGraph server's URL.LoginAPI
to log in with your admin credentials and obtain a token.Here's a simplified example:
import org.apache.hugegraph.client.RestClient;
import org.apache.hugegraph.structure.auth.Login;
import org.apache.hugegraph.structure.auth.LoginResult;
import org.apache.hugegraph.api.auth.LoginAPI;
import org.apache.hugegraph.api.auth.TokenAPI;
public class YourTestClass {
private static String BASE_URL = "http://your_hugegraph_server.com";
private static String GRAPH = "your_graph_name";
private static RestClient client;
public static void init() {
client = RestClient.builder(BASE_URL).build();
LoginAPI loginAPI = new LoginAPI(client, GRAPH);
Login login = new Login().name("admin").password("pa");
LoginResult result = loginAPI.login(login);
client.setAuthContext("Bearer " + result.token());
}
public static void main(String[] args) {
init();
// Your client is now authenticated and ready for API calls
}
}
Make sure to replace "http://your_hugegraph_server.com"
and "your_graph_name"
with your actual server URL and graph name. Also, confirm that the username and password match those expected by your server.
This setup should resolve the authentication issue. If the problem persists, it might be beneficial to check your HugeGraph server's authentication configuration, particularly the auth.authenticator
property in the rest-server.properties
file, to ensure it's correctly set up for your authentication method.
To continue the conversation, mention @dosu.
@imbajin @dosu Yeah, I did found the
org.apache.hugegraph.api.graphs.GraphsAPI.create(String name, String cloneGraphName, String configText)
in hugegraph-client project, it may works and I will give it a try.A bad news, I'm failed and here is my result. I had run the test method
test/java/org.apache.hugegraph.api.GraphsApiTest.testCreateAndDropGraph()
in hugegraph-toolchain/hugegraph-client module.@Test public void testCreateAndDropGraph() { int initialGraphNumber = graphsAPI.list().size(); // Create new graph dynamically String config; try { config = FileUtils.readFileToString(new File(CONFIG2_PATH), StandardCharsets.UTF_8); } catch (IOException e) { throw new ClientException("Failed to read config file: %s", CONFIG2_PATH); } Map<String, String> result = graphsAPI.create(GRAPH2, null, config); ... }
But a same exception as before occured.
And here is the stack of exceptions of server
java.lang.IllegalStateException: Missing authentication context when verifying resource permission at com.google.common.base.Preconditions.checkState(Preconditions.java:532) ~[guava-30.0-jre.jar:?] at org.apache.hugegraph.util.E.checkState(E.java:64) ~[hugegraph-common-1.3.0.jar:1.3.0] at org.apache.hugegraph.auth.HugeGraphAuthProxy.verifyResPermission(HugeGraphAuthProxy.java:964) ~[classes/:?] at org.apache.hugegraph.auth.HugeGraphAuthProxy.verifyResPermission(HugeGraphAuthProxy.java:955) ~[classes/:?] at org.apache.hugegraph.auth.HugeGraphAuthProxy.verifyPermission(HugeGraphAuthProxy.java:828) ~[classes/:?] at org.apache.hugegraph.auth.HugeGraphAuthProxy.create(HugeGraphAuthProxy.java:781) ~[classes/:?] at org.apache.hugegraph.core.GraphManager.createGraph(GraphManager.java:591) ~[classes/:?] at org.apache.hugegraph.core.GraphManager.createGraph(GraphManager.java:188) ~[classes/:?] at org.apache.hugegraph.api.profile.GraphsAPI.create(GraphsAPI.java:136) ~[classes/:?] at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?] at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:?] at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?] at java.lang.reflect.Method.invoke(Method.java:566) ~[?:?] at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.lambda$static$0(ResourceMethodInvocationHandlerFactory.java:52) ~[jersey-server-3.0.3.jar:?] at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:124) ~[jersey-server-3.0.3.jar:?] at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:167) ~[jersey-server-3.0.3.jar:?] at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ObjectOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:192) ~[jersey-server-3.0.3.jar:?] at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:79) ~[jersey-server-3.0.3.jar:?] at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:475) ~[jersey-server-3.0.3.jar:?] at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:397) ~[jersey-server-3.0.3.jar:?] at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:81) ~[jersey-server-3.0.3.jar:?] at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:255) ~[jersey-server-3.0.3.jar:?] at org.glassfish.jersey.internal.Errors$1.call(Errors.java:248) ~[jersey-common-3.0.3.jar:?] at org.glassfish.jersey.internal.Errors$1.call(Errors.java:244) ~[jersey-common-3.0.3.jar:?] at org.glassfish.jersey.internal.Errors.process(Errors.java:292) ~[jersey-common-3.0.3.jar:?] at org.glassfish.jersey.internal.Errors.process(Errors.java:274) ~[jersey-common-3.0.3.jar:?] at org.glassfish.jersey.internal.Errors.process(Errors.java:244) ~[jersey-common-3.0.3.jar:?] at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:265) ~[jersey-common-3.0.3.jar:?] at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:234) ~[jersey-server-3.0.3.jar:?] at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:684) ~[jersey-server-3.0.3.jar:?] at org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer.service(GrizzlyHttpContainer.java:356) ~[jersey-container-grizzly2-http-3.0.3.jar:?] at org.glassfish.grizzly.http.server.HttpHandler$1.run(HttpHandler.java:190) ~[grizzly-http-server-3.0.1.jar:3.0.1] at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:535) ~[grizzly-framework-3.0.1.jar:3.0.1] at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:515) ~[grizzly-framework-3.0.1.jar:3.0.1] at java.lang.Thread.run(Thread.java:829) [?:?]
U know, I did nothing to the
username
andpassword
and even I didn't touch the username and password since using the HugeGraph.public class BaseClientTest { protected static final String BASE_URL = "http://192.168.10.70:8080"; protected static final String GRAPH = "hugegraph"; protected static final String USERNAME = "admin"; protected static final String PASSWORD = "pa"; ... }
@BreakMeEveryday some suggestions:
Given the error message "Missing authentication context when verifying resource permission," it seems the issue is related to authentication. Here are steps to potentially resolve this:
Ensure Authentication is Configured: If your HugeGraph server has authentication enabled, you need to authenticate your client before making API calls. This involves obtaining a token using your credentials and then using this token for subsequent requests.
Check Authentication Configuration: Verify the rest-server.properties
file on your HugeGraph server to ensure that authentication is set up correctly. Look for the auth.authenticator
property and confirm it's configured as expected.
Use Correct Credentials: Make sure you are using the correct username and password when attempting to authenticate. Since you mentioned not modifying the username
and password
, ensure these default credentials are still valid and haven't been changed.
Explicitly Set Authentication in Your Test: If you're running a test that requires authentication, ensure your test code explicitly sets the authentication context. This might involve setting up a login step at the beginning of your test to authenticate and then using the obtained token for your API calls.
Disable Authentication for Testing: If authentication is not essential for your current testing purposes and you're encountering difficulties, consider temporarily disabling authentication on your HugeGraph server. This can help determine if the issue is specifically related to authentication or if there are other problems. Remember to re-enable authentication after testing to ensure your server's security.
Consult Documentation and Community: If the issue persists, consult the HugeGraph documentation and community forums for more detailed guidance on authentication and permission issues. There might be specific configurations or known issues with certain versions that the community can help you address.
Remember, changing server configurations, especially those related to security like authentication, should be done cautiously and ideally in a testing environment before applying changes to production.
To continue the conversation, mention @dosu.
Due to the lack of activity, the current issue is marked as stale and will be closed after 20 days, any update will remove the stale label
refer #2418 & I'll enhance the doc now
Problem Type (问题类型)
rest-api (结果不合预期)
Before submit
Environment (环境信息)
Your Question (问题描述)
I had tried the client.post() method failed.
Vertex/Edge example (问题点 / 边数据举例)
No response
Schema [VertexLabel, EdgeLabel, IndexLabel] (元数据结构)
No response