chore(api): support ignore graphspaces segment in url (#2612)

For forwards compatibility with the "graphspace" mode

---------

Co-authored-by: imbajin <jin@apache.org>
This commit is contained in:
V_Galaxy 2024-08-13 20:56:53 +08:00 committed by GitHub
parent 4f0e4442ab
commit 1c577de9ba
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 392 additions and 3 deletions

View File

@ -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);
}
}
}

View File

@ -47,6 +47,14 @@ public class ServerOptions extends OptionHolder {
"http://127.0.0.1:8080"
);
public static final ConfigOption<Boolean> REST_SERVER_ENABLE_GRAPHSPACES_FILTER =
new ConfigOption<>(
"restserver.enable_graphspaces_filter",
"Whether to enable graphspaces url filter.",
disallowEmpty(),
false
);
public static final ConfigOption<String> SERVER_ID =
new ConfigOption<>(
"server.id",

View File

@ -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

View File

@ -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

View File

@ -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 @@ import org.junit.runners.Suite;
ProjectApiTest.class,
TraversersApiTestSuite.class,
CypherApiTest.class,
ArthasApiTest.class
ArthasApiTest.class,
GraphSpaceApiTestSuite.class
})
public class ApiTestSuite {

View File

@ -55,7 +55,7 @@ import jakarta.ws.rs.core.Response;
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 class BaseApiTest {
@AfterClass
public static void clear() throws Exception {
client.close();
client = null;
}
@After

View File

@ -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 {
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}