Skip to content
Merged
Show file tree
Hide file tree
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
@@ -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));
}

}
Original file line number Diff line number Diff line change
@@ -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());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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", "");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.flowingcode.vaadin.addons.DevSourceRequestHandlerInitializer