-
Notifications
You must be signed in to change notification settings - Fork 521
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(api): support ignore graphspaces segment in url (#2612)
For forwards compatibility with the "graphspace" mode --------- Co-authored-by: imbajin <[email protected]>
- Loading branch information
Showing
13 changed files
with
392 additions
and
3 deletions.
There are no files selected for viewing
126 changes: 126 additions & 0 deletions
126
...-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/GraphSpaceFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<HugeConfig> configProvider; | ||
|
||
/** | ||
* Filters incoming HTTP requests to modify the request URI if it matches certain criteria. | ||
* <p> | ||
* 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. | ||
* </p> | ||
* | ||
* <p><b>Example:</b></p> | ||
* <pre> | ||
* 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 | ||
* </pre> | ||
* | ||
* @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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
hugegraph-server/hugegraph-dist/src/assembly/static/conf/rest-server.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
...graph-test/src/main/java/org/apache/hugegraph/api/graphspaces/GraphSpaceApiTestSuite.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 { | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
...egraph-test/src/main/java/org/apache/hugegraph/api/graphspaces/GraphSpaceEdgeApiTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...h-test/src/main/java/org/apache/hugegraph/api/graphspaces/GraphSpaceEdgeLabelApiTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...-test/src/main/java/org/apache/hugegraph/api/graphspaces/GraphSpaceIndexLabelApiTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...test/src/main/java/org/apache/hugegraph/api/graphspaces/GraphSpacePropertyKeyApiTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
Oops, something went wrong.