Skip to content

Commit

Permalink
fix: 401
Browse files Browse the repository at this point in the history
  • Loading branch information
zyxxoo committed Mar 1, 2023
1 parent c9f0c7b commit 5d065c8
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,43 @@
import java.lang.annotation.RetentionPolicy;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import jakarta.ws.rs.NameBinding;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.client.Invocation;
import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.container.ContainerRequestFilter;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.core.Response;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.utils.URIBuilder;
import org.apache.hugegraph.election.GlobalMasterInfo;
import org.apache.hugegraph.util.Log;
import org.glassfish.jersey.message.internal.HeaderUtils;
import org.slf4j.Logger;

public class RedirectFilter implements ContainerRequestFilter {

private static final Logger LOG = Log.logger(RedirectFilter.class);
public static final String X_HG_REDIRECT = "x-hg-redirect";

private static volatile Client client = null;

private static final Set<String> MUST_BE_NULL = new HashSet<>();

static {
MUST_BE_NULL.add("DELETE");
MUST_BE_NULL.add("GET");
MUST_BE_NULL.add("HEAD");
MUST_BE_NULL.add("TRACE");
}

@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
Expand All @@ -44,6 +67,11 @@ public void filter(ContainerRequestContext requestContext) throws IOException {
return;
}

String redirectTag = requestContext.getHeaderString(X_HG_REDIRECT);
if (StringUtils.isNotEmpty(redirectTag)) {
return;
}

String url = "";
synchronized (instance) {
if (instance.isMaster() || StringUtils.isEmpty(instance.url())) {
Expand All @@ -54,7 +82,7 @@ public void filter(ContainerRequestContext requestContext) throws IOException {

URI redirectUri = null;
try {
URIBuilder redirectURIBuilder = new URIBuilder(requestContext.getUriInfo().getAbsolutePath());
URIBuilder redirectURIBuilder = new URIBuilder(requestContext.getUriInfo().getRequestUri());
String[] host = url.split(":");
redirectURIBuilder.setHost(host[0]);
if (host.length == 2 && StringUtils.isNotEmpty(host[1].trim())) {
Expand All @@ -66,7 +94,47 @@ public void filter(ContainerRequestContext requestContext) throws IOException {
LOG.error("Redirect request exception occurred", e);
return;
}
requestContext.abortWith(Response.temporaryRedirect(redirectUri).build());
this.initClientIfNeeded();
Response response = this.forwardRequest(requestContext, redirectUri);
requestContext.abortWith(response);
}

private Response forwardRequest(ContainerRequestContext requestContext, URI redirectUri) {
MultivaluedMap<String, String> headers = requestContext.getHeaders();
MultivaluedMap<String, Object> newHeaders = HeaderUtils.createOutbound();
if (headers != null) {
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
for (String value : entry.getValue()) {
newHeaders.add(entry.getKey(), value);
}
}
}
newHeaders.add(X_HG_REDIRECT, new Date().getTime());
Invocation.Builder builder = client.target(redirectUri)
.request()
.headers(newHeaders);
Response response = null;
if (MUST_BE_NULL.contains(requestContext.getMethod())) {
response = builder.method(requestContext.getMethod());
} else {
response = builder.method(requestContext.getMethod(),
Entity.json(requestContext.getEntityStream()));
}
return response;
}

private void initClientIfNeeded() {
if (client != null) {
return;
}

synchronized (RedirectFilter.class) {
if (client != null) {
return;
}

client = ClientBuilder.newClient();
}
}

@NameBinding
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public class GremlinAPI extends API {
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed({"admin", "$owner=$graph $action=gremlin_execute"})
@RedirectFilter.RedirectMasterRole
public Map<String, Id> post(@Context GraphManager manager,
@PathParam("graph") String graph,
GremlinRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class RebuildAPI extends API {
@Status(Status.ACCEPTED)
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed({"admin", "$owner=$graph $action=index_write"})
@RedirectFilter.RedirectMasterRole
public Map<String, Id> vertexLabelRebuild(@Context GraphManager manager,
@PathParam("graph") String graph,
@PathParam("name") String name) {
Expand All @@ -69,6 +70,7 @@ public Map<String, Id> vertexLabelRebuild(@Context GraphManager manager,
@Status(Status.ACCEPTED)
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed({"admin", "$owner=$graph $action=index_write"})
@RedirectFilter.RedirectMasterRole
public Map<String, Id> edgeLabelRebuild(@Context GraphManager manager,
@PathParam("graph") String graph,
@PathParam("name") String name) {
Expand All @@ -85,6 +87,7 @@ public Map<String, Id> edgeLabelRebuild(@Context GraphManager manager,
@Status(Status.ACCEPTED)
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed({"admin", "$owner=$graph $action=index_write"})
@RedirectFilter.RedirectMasterRole
public Map<String, Id> indexLabelRebuild(@Context GraphManager manager,
@PathParam("graph") String graph,
@PathParam("name") String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.Context;

import org.apache.hugegraph.api.filter.RedirectFilter;
import org.apache.hugegraph.core.GraphManager;
import org.slf4j.Logger;

Expand Down Expand Up @@ -151,6 +152,7 @@ public Map<String, String> setLeader(@Context GraphManager manager,
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed({"admin"})
@RedirectFilter.RedirectMasterRole
public Map<String, Id> addPeer(@Context GraphManager manager,
@PathParam("graph") String graph,
@QueryParam("group") @DefaultValue("default")
Expand Down Expand Up @@ -180,6 +182,7 @@ public Map<String, Id> addPeer(@Context GraphManager manager,
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed({"admin"})
@RedirectFilter.RedirectMasterRole
public Map<String, Id> removePeer(@Context GraphManager manager,
@PathParam("graph") String graph,
@QueryParam("group")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,19 @@ public StandardStateMachineCallback(TaskManager taskManager) {
public void onAsRoleMaster(StateMachineContext context) {
if (!isMaster) {
this.taskManager.onAsRoleMaster();
this.initGlobalMasterInfo(context);
LOG.info("Server {} change to master role", context.config().node());
}
this.initGlobalMasterInfo(context);
this.isMaster = true;
}

@Override
public void onAsRoleWorker(StateMachineContext context) {
if (isMaster) {
this.taskManager.onAsRoleWorker();
this.initGlobalMasterInfo(context);
LOG.info("Server {} change to worker role", context.config().node());
}

this.initGlobalMasterInfo(context);
this.isMaster = false;
}

Expand All @@ -66,9 +65,9 @@ public void onAsRoleCandidate(StateMachineContext context) {
public void unknown(StateMachineContext context) {
if (isMaster) {
this.taskManager.onAsRoleWorker();
this.initGlobalMasterInfo(context);
LOG.info("Server {} change to worker role", context.config().node());
}
this.initGlobalMasterInfo(context);

isMaster = false;
}
Expand All @@ -77,9 +76,9 @@ public void unknown(StateMachineContext context) {
public void onAsRoleAbdication(StateMachineContext context) {
if (isMaster) {
this.taskManager.onAsRoleWorker();
this.initGlobalMasterInfo(context);
LOG.info("Server {} change to worker role", context.config().node());
}
this.initGlobalMasterInfo(context);

isMaster = false;
}
Expand Down

0 comments on commit 5d065c8

Please sign in to comment.