All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.javalib.isb.UIEndpoint Maven / Gradle / Ivy

There is a newer version: 2.0.0-alpha-8
Show newest version
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> getEndpointType() {
        return null;
    }

    @Override
    public String getPath() {
        return path;
    }

    @Override
    public boolean isSensitive() {
        return true;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy