forked from jmix-framework/jmix
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move Vaadin servlet from Studio jmix-framework#1554
- Loading branch information
Mikhail Fedoseev
committed
Apr 14, 2023
1 parent
6074335
commit 142cd7d
Showing
15 changed files
with
1,157 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
jmix-flowui/flowui-devserver/src/main/java/io/jmix/flowui/devserver/FlowJettyServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* Copyright 2022 Haulmont. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.jmix.flowui.devserver; | ||
|
||
import com.vaadin.flow.server.startup.ServletContextListeners; | ||
import io.jmix.flowui.devserver.servlet.JmixErrorHandler; | ||
import io.jmix.flowui.devserver.servlet.JmixServletContextListener; | ||
import io.jmix.flowui.devserver.servlet.JmixSystemPropertiesLifeCycleListener; | ||
import io.jmix.flowui.devserver.servlet.JmixVaadinServlet; | ||
import org.eclipse.jetty.annotations.AnnotationConfiguration; | ||
import org.eclipse.jetty.server.Server; | ||
import org.eclipse.jetty.server.handler.HandlerList; | ||
import org.eclipse.jetty.server.handler.ShutdownHandler; | ||
import org.eclipse.jetty.webapp.Configuration; | ||
import org.eclipse.jetty.webapp.JettyWebXmlConfiguration; | ||
import org.eclipse.jetty.webapp.WebAppContext; | ||
import org.eclipse.jetty.websocket.jsr356.server.deploy.WebSocketServerContainerInitializer; | ||
|
||
import javax.servlet.ServletException; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
|
||
/** | ||
* Used in Studio. | ||
*/ | ||
@SuppressWarnings("unused") | ||
public class FlowJettyServer extends Server { | ||
|
||
private final Map<String, Object> params; | ||
|
||
public FlowJettyServer(int port, Map<String, Object> params) { | ||
super(port); | ||
this.params = params; | ||
} | ||
|
||
public void initAndStart() throws Exception { | ||
init(); | ||
start(); | ||
} | ||
|
||
private void init() throws ServletException { | ||
WebAppContext context = createContext(); | ||
this.setHandler( | ||
new HandlerList( | ||
context, | ||
new ShutdownHandler("studio") | ||
) | ||
); | ||
this.addLifeCycleListener( | ||
new JmixSystemPropertiesLifeCycleListener( | ||
(String) params.get("ProjectBaseDir"), | ||
(String) params.get("IsPnpmEnabled"), | ||
(Properties) params.get("Properties") | ||
) | ||
); | ||
Configuration.ClassList | ||
.setServerDefault(this) | ||
.addBefore( | ||
JettyWebXmlConfiguration.class.getName(), | ||
AnnotationConfiguration.class.getName() | ||
); | ||
} | ||
|
||
private WebAppContext createContext() throws ServletException { | ||
WebAppContext context = new WebAppContext(); | ||
context.setContextPath("/"); | ||
context.setClassLoader(FlowJettyServer.class.getClassLoader()); | ||
context.setExtraClasspath((String) params.get("ExtraClassPath")); | ||
context.setResourceBase((String) params.get("ResourceBaseDir")); | ||
context.addServlet(JmixVaadinServlet.class, "/*"); | ||
context.setConfigurationDiscovered(true); | ||
context.getServletContext().setExtendedListenerTypes(true); | ||
context.addEventListener(new ServletContextListeners()); | ||
WebSocketServerContainerInitializer.initialize(context); | ||
context.setErrorHandler(new JmixErrorHandler()); | ||
context.addEventListener(new JmixServletContextListener(params)); | ||
return context; | ||
} | ||
|
||
private FlowJettyServer() { | ||
this.params = Collections.emptyMap(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...wui/flowui-devserver/src/main/java/io/jmix/flowui/devserver/servlet/JmixErrorHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright 2022 Haulmont. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.jmix.flowui.devserver.servlet; | ||
|
||
import org.eclipse.jetty.server.Request; | ||
import org.eclipse.jetty.server.handler.ErrorHandler; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import java.io.Writer; | ||
|
||
public class JmixErrorHandler extends ErrorHandler { | ||
@Override | ||
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { | ||
super.handle(target, baseRequest, request, response); | ||
} | ||
|
||
@Override | ||
protected void handleErrorPage(HttpServletRequest request, Writer writer, int code, String message) throws IOException { | ||
super.handleErrorPage(request, writer, code, message); | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
...server/src/main/java/io/jmix/flowui/devserver/servlet/JmixJavaScriptBootstrapHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright 2022 Haulmont. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.jmix.flowui.devserver.servlet; | ||
|
||
import com.vaadin.flow.component.PushConfiguration; | ||
import com.vaadin.flow.component.UI; | ||
import com.vaadin.flow.component.page.Push; | ||
import com.vaadin.flow.server.AppShellRegistry; | ||
import com.vaadin.flow.server.VaadinRequest; | ||
import com.vaadin.flow.server.VaadinResponse; | ||
import com.vaadin.flow.server.VaadinSession; | ||
import com.vaadin.flow.server.communication.JavaScriptBootstrapHandler; | ||
import com.vaadin.flow.shared.communication.PushMode; | ||
import elemental.json.JsonObject; | ||
|
||
import java.util.Optional; | ||
|
||
public class JmixJavaScriptBootstrapHandler extends JavaScriptBootstrapHandler { | ||
|
||
public JmixJavaScriptBootstrapHandler() { | ||
super(); | ||
} | ||
|
||
@Override | ||
protected BootstrapContext createAndInitUI(Class<? extends UI> uiClass, | ||
VaadinRequest request, | ||
VaadinResponse response, | ||
VaadinSession session) { | ||
UI ui = new JmixJavaScriptBootstrapUI(); | ||
ui.getInternals().setContextRoot(request.getService().getContextRootRelativePath(request)); | ||
|
||
PushConfiguration pushConfiguration = ui.getPushConfiguration(); | ||
|
||
ui.getInternals().setSession(session); | ||
ui.setLocale(session.getLocale()); | ||
|
||
BootstrapContext context = createBootstrapContext( | ||
request, | ||
response, | ||
ui, | ||
vaadinRequest -> vaadinRequest.getService().getContextRootRelativePath(request) | ||
); | ||
|
||
|
||
Optional<Push> push = context.getPageConfigurationAnnotation(Push.class); | ||
|
||
setupPushConnectionFactory(pushConfiguration, context); | ||
pushConfiguration.setPushMode | ||
(PushMode.MANUAL); | ||
push.stream() | ||
.map(Push::transport) | ||
.forEach(pushConfiguration::setTransport); | ||
|
||
// Set thread local here so it is available in init | ||
UI.setCurrent(ui); | ||
ui.doInit(request, session.getNextUIid(), context.getAppId()); | ||
session.addUI(ui); | ||
|
||
// After init and adding UI to session fire init listeners. | ||
session.getService().fireUIInitListeners(ui); | ||
|
||
initializeUIWithRouter(context, ui); | ||
|
||
JsonObject config = context.getApplicationParameters(); | ||
|
||
String requestURL = getRequestUrl(request); | ||
|
||
AppShellRegistry registry = AppShellRegistry.getInstance(session.getService().getContext()); | ||
registry.modifyPushConfiguration(pushConfiguration); | ||
|
||
config.put("requestURL", requestURL); | ||
|
||
return context; | ||
} | ||
} |
Oops, something went wrong.