diff --git a/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/GraphSpaceFilter.java b/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/GraphSpaceFilter.java new file mode 100644 index 0000000000..97e0cec935 --- /dev/null +++ b/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/GraphSpaceFilter.java @@ -0,0 +1,126 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hugegraph.api.filter; + +import java.io.IOException; +import java.net.URI; +import java.util.Arrays; +import java.util.stream.Collectors; + +import org.apache.hugegraph.config.HugeConfig; +import org.apache.hugegraph.config.ServerOptions; +import org.apache.hugegraph.util.Log; +import org.slf4j.Logger; + +import jakarta.inject.Singleton; +import jakarta.ws.rs.container.ContainerRequestContext; +import jakarta.ws.rs.container.ContainerRequestFilter; +import jakarta.ws.rs.container.PreMatching; +import jakarta.ws.rs.core.Context; +import jakarta.ws.rs.core.UriBuilder; +import jakarta.ws.rs.ext.Provider; + +/** + * TODO: Change the adaptor logic to keep compatibility with the non-"GraphSpace" version after we + * support "GraphSpace" + */ +@Provider +@Singleton +@PreMatching +public class GraphSpaceFilter implements ContainerRequestFilter { + + private static final Logger LOG = Log.logger(GraphSpaceFilter.class); + + private static final String GRAPHSPACES_PATH = "graphspaces/"; + + @Context + private jakarta.inject.Provider configProvider; + + /** + * Filters incoming HTTP requests to modify the request URI if it matches certain criteria. + *

+ * This filter checks if the request URI starts with the {@link #GRAPHSPACES_PATH} path + * segment. If it does, + * the filter removes the {@link #GRAPHSPACES_PATH} segment along with the following segment + * and then reconstructs + * the remaining URI. The modified URI is set back into the request context. This is useful for + * supporting legacy paths or adapting to new API structures. + *

+ * + *

Example:

+ *
+     * URI baseUri = URI.create("http://localhost:8080/");
+     * URI requestUri = URI.create("http://localhost:8080/graphspaces/DEFAULT/graphs");
+     *
+     * // Before filter:
+     * context.getUriInfo().getRequestUri();  // returns http://localhost:8080/graphspaces/DEFAULT/graphs
+     *
+     * // After filter:
+     * context.getUriInfo().getRequestUri();  // returns http://localhost:8080/graphs
+     * 
+ * + * @param context The {@link ContainerRequestContext} which provides access to the request + * details. + * @throws IOException If an input or output exception occurs. + */ + @Override + public void filter(ContainerRequestContext context) throws IOException { + HugeConfig config = configProvider.get(); + if (!config.get(ServerOptions.REST_SERVER_ENABLE_GRAPHSPACES_FILTER)) { + return; + } + + // Step 1: Get relativePath + URI baseUri = context.getUriInfo().getBaseUri(); + URI requestUri = context.getUriInfo().getRequestUri(); + URI relativePath = baseUri.relativize(requestUri); + + String relativePathStr = relativePath.getPath(); + // TODO: remember remove the logic after we support "GraphSpace" + if (!relativePathStr.startsWith(GRAPHSPACES_PATH)) { + return; + } + + // Step 2: Extract the next substring after {@link #GRAPHSPACES_PATH} + String[] parts = relativePathStr.split("/"); + if (parts.length <= 1) { + return; + } + + String ignoredPart = Arrays.stream(parts) + .limit(2) // Ignore the first two segments + .collect(Collectors.joining("/")); + + // Reconstruct the remaining path + String newPath = Arrays.stream(parts) + .skip(2) // Skip the first two segments + .collect(Collectors.joining("/")); + + // Step 3: Modify RequestUri and log the ignored part + URI newUri = UriBuilder.fromUri(baseUri) + .path(newPath) + .replaceQuery(requestUri.getRawQuery()) + .build(); + context.setRequestUri(newUri); + + // Log the ignored part + if (LOG.isDebugEnabled()) { + LOG.debug("Ignored graphspaces segment: {}", ignoredPart); + } + } +} diff --git a/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/config/ServerOptions.java b/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/config/ServerOptions.java index 63da169c99..084e9a338f 100644 --- a/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/config/ServerOptions.java +++ b/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/config/ServerOptions.java @@ -47,6 +47,14 @@ public static synchronized ServerOptions instance() { "http://127.0.0.1:8080" ); + public static final ConfigOption REST_SERVER_ENABLE_GRAPHSPACES_FILTER = + new ConfigOption<>( + "restserver.enable_graphspaces_filter", + "Whether to enable graphspaces url filter.", + disallowEmpty(), + false + ); + public static final ConfigOption SERVER_ID = new ConfigOption<>( "server.id", diff --git a/hugegraph-server/hugegraph-dist/src/assembly/static/conf/rest-server.properties b/hugegraph-server/hugegraph-dist/src/assembly/static/conf/rest-server.properties index 507867380c..f89966e6cd 100644 --- a/hugegraph-server/hugegraph-dist/src/assembly/static/conf/rest-server.properties +++ b/hugegraph-server/hugegraph-dist/src/assembly/static/conf/rest-server.properties @@ -1,6 +1,7 @@ # bind url # could use '0.0.0.0' or specified (real)IP to expose external network access restserver.url=http://127.0.0.1:8080 +#restserver.enable_graphspaces_filter=false # gremlin server url, need to be consistent with host and port in gremlin-server.yaml #gremlinserver.url=http://127.0.0.1:8182 diff --git a/hugegraph-server/hugegraph-dist/src/assembly/travis/run-api-test.sh b/hugegraph-server/hugegraph-dist/src/assembly/travis/run-api-test.sh index 8008f39cdb..bd821712dc 100755 --- a/hugegraph-server/hugegraph-dist/src/assembly/travis/run-api-test.sh +++ b/hugegraph-server/hugegraph-dist/src/assembly/travis/run-api-test.sh @@ -41,6 +41,7 @@ fi # config rest-server sed -i 's/#auth.authenticator=/auth.authenticator=org.apache.hugegraph.auth.StandardAuthenticator/' $REST_SERVER_CONF sed -i 's/#auth.admin_token=/auth.admin_token=pa/' $REST_SERVER_CONF +sed -i 's/#restserver.enable_graphspaces_filter=false/restserver.enable_graphspaces_filter=true/' $REST_SERVER_CONF # config hugegraph.properties sed -i 's/gremlin.graph=.*/gremlin.graph=org.apache.hugegraph.auth.HugeFactoryAuthProxy/' $CONF diff --git a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/ApiTestSuite.java b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/ApiTestSuite.java index 11d3e64095..cca27a78c2 100644 --- a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/ApiTestSuite.java +++ b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/ApiTestSuite.java @@ -17,6 +17,7 @@ package org.apache.hugegraph.api; +import org.apache.hugegraph.api.graphspaces.GraphSpaceApiTestSuite; import org.apache.hugegraph.api.traversers.TraversersApiTestSuite; import org.apache.hugegraph.dist.RegisterUtil; import org.junit.BeforeClass; @@ -40,7 +41,8 @@ ProjectApiTest.class, TraversersApiTestSuite.class, CypherApiTest.class, - ArthasApiTest.class + ArthasApiTest.class, + GraphSpaceApiTestSuite.class }) public class ApiTestSuite { diff --git a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/BaseApiTest.java b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/BaseApiTest.java index 4b6c0ed7f4..72821ecb1a 100644 --- a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/BaseApiTest.java +++ b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/BaseApiTest.java @@ -55,7 +55,7 @@ public class BaseApiTest { - private static final String BASE_URL = "http://127.0.0.1:8080"; + protected static final String BASE_URL = "http://127.0.0.1:8080"; private static final String GRAPH = "hugegraph"; private static final String USERNAME = "admin"; private static final String PASSWORD = "pa"; @@ -71,7 +71,7 @@ public class BaseApiTest { protected static final String TRAVERSERS_API = URL_PREFIX + "/traversers"; - private static RestClient client; + protected static RestClient client; private static final ObjectMapper MAPPER = new ObjectMapper(); @@ -84,6 +84,7 @@ public static void init() { @AfterClass public static void clear() throws Exception { client.close(); + client = null; } @After diff --git a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/graphspaces/GraphSpaceApiTestSuite.java b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/graphspaces/GraphSpaceApiTestSuite.java new file mode 100644 index 0000000000..d5090058b1 --- /dev/null +++ b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/graphspaces/GraphSpaceApiTestSuite.java @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hugegraph.api.graphspaces; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +@RunWith(Suite.class) +@Suite.SuiteClasses({ + GraphSpacePropertyKeyApiTest.class, + GraphSpaceVertexLabelApiTest.class, + GraphSpaceEdgeLabelApiTest.class, + GraphSpaceIndexLabelApiTest.class, + GraphSpaceEdgeApiTest.class, + GraphSpaceVertexApiTest.class +}) +public class GraphSpaceApiTestSuite { + +} diff --git a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/graphspaces/GraphSpaceEdgeApiTest.java b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/graphspaces/GraphSpaceEdgeApiTest.java new file mode 100644 index 0000000000..643888a953 --- /dev/null +++ b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/graphspaces/GraphSpaceEdgeApiTest.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hugegraph.api.graphspaces; + +import java.util.Objects; + +import org.apache.hugegraph.api.BaseApiTest; +import org.apache.hugegraph.api.EdgeApiTest; +import org.junit.BeforeClass; + +public class GraphSpaceEdgeApiTest extends EdgeApiTest { + + @BeforeClass + public static void init() { + if (Objects.nonNull(client)) { + client.close(); + } + client = new RestClient(String.join("/", BASE_URL, "graphspaces", "DEFAULT")); + BaseApiTest.clearData(); + } +} diff --git a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/graphspaces/GraphSpaceEdgeLabelApiTest.java b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/graphspaces/GraphSpaceEdgeLabelApiTest.java new file mode 100644 index 0000000000..80e21b1631 --- /dev/null +++ b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/graphspaces/GraphSpaceEdgeLabelApiTest.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hugegraph.api.graphspaces; + +import java.util.Objects; + +import org.apache.hugegraph.api.BaseApiTest; +import org.apache.hugegraph.api.EdgeLabelApiTest; +import org.junit.BeforeClass; + +public class GraphSpaceEdgeLabelApiTest extends EdgeLabelApiTest { + + @BeforeClass + public static void init() { + if (Objects.nonNull(client)) { + client.close(); + } + client = new RestClient(String.join("/", BASE_URL, "graphspaces", "DEFAULT")); + BaseApiTest.clearData(); + } +} diff --git a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/graphspaces/GraphSpaceIndexLabelApiTest.java b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/graphspaces/GraphSpaceIndexLabelApiTest.java new file mode 100644 index 0000000000..f5f3e4c4d8 --- /dev/null +++ b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/graphspaces/GraphSpaceIndexLabelApiTest.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hugegraph.api.graphspaces; + +import java.util.Objects; + +import org.apache.hugegraph.api.BaseApiTest; +import org.apache.hugegraph.api.IndexLabelApiTest; +import org.junit.BeforeClass; + +public class GraphSpaceIndexLabelApiTest extends IndexLabelApiTest { + + @BeforeClass + public static void init() { + if (Objects.nonNull(client)) { + client.close(); + } + client = new RestClient(String.join("/", BASE_URL, "graphspaces", "DEFAULT")); + BaseApiTest.clearData(); + } +} diff --git a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/graphspaces/GraphSpacePropertyKeyApiTest.java b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/graphspaces/GraphSpacePropertyKeyApiTest.java new file mode 100644 index 0000000000..6096c10ee2 --- /dev/null +++ b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/graphspaces/GraphSpacePropertyKeyApiTest.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hugegraph.api.graphspaces; + +import java.util.Objects; + +import org.apache.hugegraph.api.BaseApiTest; +import org.apache.hugegraph.api.PropertyKeyApiTest; +import org.junit.BeforeClass; + +public class GraphSpacePropertyKeyApiTest extends PropertyKeyApiTest { + + @BeforeClass + public static void init() { + if (Objects.nonNull(client)) { + client.close(); + } + client = new RestClient(String.join("/", BASE_URL, "graphspaces", "DEFAULT")); + BaseApiTest.clearData(); + } +} diff --git a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/graphspaces/GraphSpaceVertexApiTest.java b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/graphspaces/GraphSpaceVertexApiTest.java new file mode 100644 index 0000000000..f967540e1d --- /dev/null +++ b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/graphspaces/GraphSpaceVertexApiTest.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hugegraph.api.graphspaces; + +import java.util.Objects; + +import org.apache.hugegraph.api.BaseApiTest; +import org.apache.hugegraph.api.VertexApiTest; +import org.junit.BeforeClass; + +public class GraphSpaceVertexApiTest extends VertexApiTest { + + @BeforeClass + public static void init() { + if (Objects.nonNull(client)) { + client.close(); + } + client = new RestClient(String.join("/", BASE_URL, "graphspaces", "DEFAULT")); + BaseApiTest.clearData(); + } +} diff --git a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/graphspaces/GraphSpaceVertexLabelApiTest.java b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/graphspaces/GraphSpaceVertexLabelApiTest.java new file mode 100644 index 0000000000..5b12576a67 --- /dev/null +++ b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/graphspaces/GraphSpaceVertexLabelApiTest.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hugegraph.api.graphspaces; + +import java.util.Objects; + +import org.apache.hugegraph.api.BaseApiTest; +import org.apache.hugegraph.api.VertexLabelApiTest; +import org.junit.BeforeClass; + +public class GraphSpaceVertexLabelApiTest extends VertexLabelApiTest { + + @BeforeClass + public static void init() { + if (Objects.nonNull(client)) { + client.close(); + } + client = new RestClient(String.join("/", BASE_URL, "graphspaces", "DEFAULT")); + BaseApiTest.clearData(); + } +}