Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

F xyp redirect #19

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

import com.ice.server.nio.IceNioServerInit;
import com.ice.server.nio.ha.IceNioServerHa;
import io.netty.util.internal.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;

/**
* @author waitmoon
Expand All @@ -19,24 +23,51 @@ public class IceServerCommonFilter implements Filter {
@Autowired(required = false)
private IceNioServerHa serverHa;

public static final String HTTP_PREFIX = "http://";

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

if (!IceNioServerInit.ready) {
servletResponse.getWriter().print("server not ready...please retry later");
return;
}
//leader
// leader
if (serverHa != null && !serverHa.isLeader()) {
servletResponse.setCharacterEncoding("UTF-8");
servletResponse.setContentType("text/html; charset=utf-8");
try {
servletResponse.getWriter().print("current leader page: " + serverHa.getLeaderWebAddress());
String leaderWebAddress = serverHa.getLeaderWebAddress();
log.info("redirect to leader, {}", leaderWebAddress);

HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
String queryString = getQueryString(httpServletRequest);

String requestFullUrl = getProtocol(httpServletRequest) +
leaderWebAddress +
getUri(httpServletRequest) +
queryString;

((HttpServletResponse) servletResponse).sendRedirect(requestFullUrl);
} catch (Exception e) {
log.error("not leader response error", e);
}
} else {
filterChain.doFilter(servletRequest, servletResponse);
}
}

private String getUri(HttpServletRequest request) {
String requestURI = request.getRequestURI();
return Objects.equals("/", requestURI) ? "" : requestURI;
}

private String getProtocol(HttpServletRequest request) {
return request.getScheme() + "://";
}

private String getQueryString(HttpServletRequest request) {
String queryString = request.getQueryString();
queryString = StringUtil.isNullOrEmpty(queryString) ? "" : "?" + queryString;
return queryString;
}

}