net.javalib.isb.UIEndpoint Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of isb Show documentation
Show all versions of isb Show documentation
Instruments spring boot applications
package net.javalib.isb;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.BeansException;
import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;
@Component
@ConditionalOnProperty(name = "isb.ui", matchIfMissing = true)
public class UIEndpoint implements MvcEndpoint, ApplicationContextAware {
private String path = "/";
private ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
Resource resource = new ClassPathResource("/isb-ui/");
List locations = new ArrayList();
locations.add(resource);
handler.setApplicationContext(applicationContext);
handler.setLocations(locations);
try {
handler.afterPropertiesSet();
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@RequestMapping(value = "/**", method = RequestMethod.GET)
public ModelAndView handle(HttpServletRequest request, HttpServletResponse response) throws Exception {
String reqPath = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
if (reqPath.startsWith("/manage/")) {
reqPath = reqPath.substring(7);
}
reqPath = reqPath.substring(path.length());
if ("".equals(reqPath)) {
reqPath = "index.html";
}
if (reqPath.startsWith("ui")) {
reqPath = "/index.html";
}
request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, reqPath);
handler.handleRequest(request, response);
return null;
}
@Override
public Class extends Endpoint>> getEndpointType() {
return null;
}
@Override
public String getPath() {
return path;
}
@Override
public boolean isSensitive() {
return true;
}
}