diff --git a/src/main/java/com/flowingcode/vaadin/addons/DevSourceRequestHandler.java b/src/main/java/com/flowingcode/vaadin/addons/DevSourceRequestHandler.java new file mode 100644 index 0000000..c3145ae --- /dev/null +++ b/src/main/java/com/flowingcode/vaadin/addons/DevSourceRequestHandler.java @@ -0,0 +1,51 @@ +package com.flowingcode.vaadin.addons; + +import com.vaadin.flow.server.RequestHandler; +import com.vaadin.flow.server.VaadinRequest; +import com.vaadin.flow.server.VaadinResponse; +import com.vaadin.flow.server.VaadinService; +import com.vaadin.flow.server.VaadinSession; +import java.io.File; +import java.io.IOException; +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; + +public class DevSourceRequestHandler implements RequestHandler { + + private static final int SC_OK = 200; + + private static final int SC_NOT_FOUND = 404; + + @Override + public boolean handleRequest(VaadinSession session, VaadinRequest request, + VaadinResponse response) throws IOException { + + if (VaadinService.getCurrent().getDeploymentConfiguration().isProductionMode()) { + return false; + } + + String path = request.getPathInfo(); + if (!path.startsWith("/src/")) { + return false; + } + + if (fileExists(path)) { + byte file[] = FileUtils.readFileToByteArray(getFile(path)); + response.setStatus(SC_OK); + IOUtils.write(file, response.getOutputStream()); + } else { + response.setStatus(SC_NOT_FOUND); + } + + return true; + } + + public static boolean fileExists(String path) { + return getFile(path).exists(); + } + + private static File getFile(String path) { + return new File(path.substring(1)); + } + +} diff --git a/src/main/java/com/flowingcode/vaadin/addons/DevSourceRequestHandlerInitializer.java b/src/main/java/com/flowingcode/vaadin/addons/DevSourceRequestHandlerInitializer.java new file mode 100644 index 0000000..41ca78f --- /dev/null +++ b/src/main/java/com/flowingcode/vaadin/addons/DevSourceRequestHandlerInitializer.java @@ -0,0 +1,15 @@ +package com.flowingcode.vaadin.addons; + +import com.vaadin.flow.server.ServiceInitEvent; +import com.vaadin.flow.server.VaadinServiceInitListener; + +public class DevSourceRequestHandlerInitializer implements VaadinServiceInitListener { + + @Override + public void serviceInit(ServiceInitEvent event) { + if (!event.getSource().getDeploymentConfiguration().isProductionMode()) { + event.addRequestHandler(new DevSourceRequestHandler()); + } + } + +} diff --git a/src/main/java/com/flowingcode/vaadin/addons/demo/SourceCodeView.java b/src/main/java/com/flowingcode/vaadin/addons/demo/SourceCodeView.java index 01112b1..73a141d 100644 --- a/src/main/java/com/flowingcode/vaadin/addons/demo/SourceCodeView.java +++ b/src/main/java/com/flowingcode/vaadin/addons/demo/SourceCodeView.java @@ -19,11 +19,13 @@ */ package com.flowingcode.vaadin.addons.demo; +import com.flowingcode.vaadin.addons.DevSourceRequestHandler; import com.vaadin.flow.component.HasSize; import com.vaadin.flow.component.dependency.JsModule; import com.vaadin.flow.component.dependency.NpmPackage; import com.vaadin.flow.component.html.Div; import com.vaadin.flow.dom.Element; +import com.vaadin.flow.server.VaadinService; @SuppressWarnings("serial") @JsModule("./code-viewer.ts") @@ -43,6 +45,13 @@ public SourceCodeView(String sourceUrl) { } private static String translateSource(String url) { + if (!VaadinService.getCurrent().getDeploymentConfiguration().isProductionMode()) { + String src = url.replaceFirst("^.*/src/", "/src/"); + if (DevSourceRequestHandler.fileExists(src)) { + return src; + } + } + if (url.startsWith("https://github.com")) { url = url.replaceFirst("github.com", "raw.githubusercontent.com"); url = url.replaceFirst("/blob", ""); diff --git a/src/main/resources/META-INF/services/com.vaadin.flow.server.VaadinServiceInitListener b/src/main/resources/META-INF/services/com.vaadin.flow.server.VaadinServiceInitListener new file mode 100644 index 0000000..49978bf --- /dev/null +++ b/src/main/resources/META-INF/services/com.vaadin.flow.server.VaadinServiceInitListener @@ -0,0 +1 @@ +com.flowingcode.vaadin.addons.DevSourceRequestHandlerInitializer \ No newline at end of file