com.baidu.formula.discovery.serviceregistry.FormulaServletRegistrationCustomer Maven / Gradle / Ivy
package com.baidu.formula.discovery.serviceregistry;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.util.StringUtils;
import javax.servlet.ServletContext;
/**
* @author Bowu Dong ([email protected])
*/
public class FormulaServletRegistrationCustomer implements FormulaRegistrationCustomer {
private ObjectProvider servletContext;
public FormulaServletRegistrationCustomer(ObjectProvider servletContext) {
this.servletContext = servletContext;
}
@Override
public void customize(FormulaRegistration registration) {
if (servletContext == null) {
return;
}
ServletContext sc = servletContext.getIfAvailable();
if (sc != null
&& StringUtils.hasText(sc.getContextPath())
&& StringUtils.hasText(sc.getContextPath().replaceAll("/", ""))) {
registration.getInstance().getCustoms().put("contextPath", sc.getContextPath());
registration.getInstance().setPath("/" + sc.getContextPath().replaceAll("/", ""));
} else {
registration.getInstance().setPath("/");
}
}
}