PL-8888 AssertError - CubaVaadinServletService reads VaadinSession without lock

This commit is contained in:
Yuriy Artamonov 2017-04-02 11:44:16 +04:00
parent 106f91afbc
commit b015a30612
4 changed files with 66 additions and 115 deletions

View File

@ -25,7 +25,6 @@ import com.haulmont.cuba.core.sys.SecurityContext;
import com.haulmont.cuba.gui.executors.*;
import com.haulmont.cuba.gui.executors.impl.TaskExecutor;
import com.haulmont.cuba.gui.executors.impl.TaskHandlerImpl;
import com.haulmont.cuba.security.global.UserSession;
import com.haulmont.cuba.web.App;
import com.haulmont.cuba.web.AppUI;
import com.haulmont.cuba.web.WebConfig;
@ -135,32 +134,6 @@ public class WebBackgroundWorker implements BackgroundWorker {
}
}
protected static void withUserSessionAsync(UI ui, Runnable handler) {
ui.access(() ->
executeOnUiThread(ui, handler)
);
}
protected static void withUserSessionInvoke(UI ui, Runnable handler) {
ui.accessSynchronously(() ->
executeOnUiThread(ui, handler)
);
}
protected static void executeOnUiThread(UI ui, Runnable handler) {
SecurityContext oldSecurityContext = AppContext.getSecurityContext();
try {
UserSession userSession = ui.getSession().getAttribute(UserSession.class);
if (userSession != null) {
AppContext.setSecurityContext(new SecurityContext(userSession));
}
handler.run();
} finally {
AppContext.setSecurityContext(oldSecurityContext);
}
}
private class WebTaskExecutor<T, V> implements TaskExecutor<T, V>, Callable<V> {
private AppUI ui;
@ -194,7 +167,7 @@ public class WebBackgroundWorker implements BackgroundWorker {
this.future = new FutureTask<V>(this) {
@Override
protected void done() {
withUserSessionAsync(() ->
WebTaskExecutor.this.ui.access(() ->
handleDone()
);
}
@ -242,7 +215,7 @@ public class WebBackgroundWorker implements BackgroundWorker {
@Override
public final void handleProgress(T... changes) {
if (changes != null) {
withUserSessionAsync(() ->
ui.access(() ->
process(Arrays.asList(changes))
);
}
@ -367,7 +340,9 @@ public class WebBackgroundWorker implements BackgroundWorker {
@Override
public final void startExecution() {
// Start thread
executorService.execute(() -> future.run());
executorService.execute(() ->
future.run()
);
}
@Override
@ -403,10 +378,6 @@ public class WebBackgroundWorker implements BackgroundWorker {
public FutureTask<V> getFuture() {
return future;
}
protected final void withUserSessionAsync(Runnable handler) {
WebBackgroundWorker.withUserSessionAsync(ui, handler);
}
}
private static class WebUIAccessor implements UIAccessor {
@ -418,12 +389,12 @@ public class WebBackgroundWorker implements BackgroundWorker {
@Override
public void access(Runnable runnable) {
WebBackgroundWorker.withUserSessionAsync(ui, runnable);
ui.access(runnable);
}
@Override
public void accessSynchronously(Runnable runnable) {
WebBackgroundWorker.withUserSessionInvoke(ui, runnable);
ui.accessSynchronously(runnable);
}
}
}

View File

@ -22,7 +22,6 @@ import com.haulmont.cuba.core.global.Configuration;
import com.haulmont.cuba.core.global.GlobalConfig;
import com.haulmont.cuba.core.global.Messages;
import com.haulmont.cuba.core.sys.AppContext;
import com.haulmont.cuba.core.sys.SecurityContext;
import com.haulmont.cuba.security.global.UserSession;
import com.haulmont.cuba.web.App;
import com.haulmont.cuba.web.ScreenProfiler;
@ -157,9 +156,7 @@ public class CubaVaadinServletService extends VaadinServletService {
List<RequestHandler> cubaRequestHandlers = new ArrayList<>();
for (RequestHandler handler : requestHandlers) {
if (handler instanceof ServletUIInitHandler) {
cubaRequestHandlers.add(new CubaServletUiInitHandler());
} else if (handler instanceof UidlRequestHandler) {
if (handler instanceof UidlRequestHandler) {
// replace UidlRequestHandler with CubaUidlRequestHandler
cubaRequestHandlers.add(new CubaUidlRequestHandler());
} else if (handler instanceof PublishedFileHandler) {
@ -174,10 +171,8 @@ public class CubaVaadinServletService extends VaadinServletService {
cubaRequestHandlers.add(new CubaHeartbeatHandler());
} else if (handler instanceof FileUploadHandler) {
// add support for jquery file upload
cubaRequestHandlers.add(new ExtFileUploadHandler());
cubaRequestHandlers.add(new FileUploadHandler());
cubaRequestHandlers.add(new CubaFileUploadHandler());
} else if (handler instanceof ConnectorResourceHandler) {
cubaRequestHandlers.add(new CubaConnectorResourceHandler());
} else {
cubaRequestHandlers.add(handler);
}
@ -186,40 +181,19 @@ public class CubaVaadinServletService extends VaadinServletService {
return cubaRequestHandlers;
}
protected static boolean withUserSession(VaadinSession session, RequestHandlerAction<Boolean> handler) throws IOException {
UserSession userSession = session.getAttribute(UserSession.class);
if (userSession != null) {
AppContext.setSecurityContext(new SecurityContext(userSession));
}
try {
return handler.handle();
} finally {
AppContext.setSecurityContext(null);
}
}
// Add ability to load JS and CSS resources from VAADIN directory
protected static class CubaPublishedFileHandler extends PublishedFileHandler {
@Override
protected InputStream getApplicationResourceAsStream(Class<?> contextClass, String fileName) {
return VaadinServlet.getCurrent().getServletContext().getResourceAsStream("/VAADIN/" + fileName);
}
}
// For com.vaadin.ui.Upload instances
protected static class ExtFileUploadHandler extends FileUploadHandler {
@Override
public boolean handleRequest(VaadinSession session, VaadinRequest request, VaadinResponse response)
throws IOException {
return withUserSession(session, () -> super.handleRequest(session, request, response));
ServletContext servletContext = VaadinServlet.getCurrent().getServletContext();
return servletContext.getResourceAsStream("/VAADIN/" + fileName);
}
}
// Add support for CubaFileUpload component with XHR upload mechanism
protected static class CubaFileUploadHandler extends FileUploadHandler {
private Logger log = LoggerFactory.getLogger(CubaFileUploadHandler.class);
private final Logger log = LoggerFactory.getLogger(CubaFileUploadHandler.class);
@Override
protected boolean isSuitableUploadComponent(ClientConnector source) {
@ -252,19 +226,12 @@ public class CubaVaadinServletService extends VaadinServletService {
writer.write(json.toJson());
writer.close();
}
@Override
public boolean handleRequest(VaadinSession session, VaadinRequest request, VaadinResponse response)
throws IOException {
return withUserSession(session, () -> super.handleRequest(session, request, response));
}
}
/**
* Add ability to redirect to base application URL if we have unparsable path tail
*/
protected static class CubaServletBootstrapHandler extends ServletBootstrapHandler {
@Override
public boolean handleRequest(VaadinSession session, VaadinRequest request, VaadinResponse response)
throws IOException {
@ -284,38 +251,29 @@ public class CubaVaadinServletService extends VaadinServletService {
// Add ability to handle hearbeats in App
protected static class CubaHeartbeatHandler extends HeartbeatHandler {
private Logger log = LoggerFactory.getLogger(CubaHeartbeatHandler.class);
private final Logger log = LoggerFactory.getLogger(CubaHeartbeatHandler.class);
@Override
public boolean synchronizedHandleRequest(VaadinSession session, VaadinRequest request, VaadinResponse response)
throws IOException {
return withUserSession(session, () -> {
boolean result = super.synchronizedHandleRequest(session, request, response);
boolean result = super.synchronizedHandleRequest(session, request, response);
if (log.isTraceEnabled()) {
log.trace("Handle heartbeat {} {}", request.getRemoteHost(), request.getRemoteAddr());
}
if (log.isTraceEnabled()) {
log.trace("Handle heartbeat {} {}", request.getRemoteHost(), request.getRemoteAddr());
}
if (result && App.isBound()) {
App.getInstance().onHeartbeat();
}
if (result && App.isBound()) {
App.getInstance().onHeartbeat();
}
return result;
});
return result;
}
}
// Set security context to AppContext for normal UI requests
protected static class CubaUidlRequestHandler extends UidlRequestHandler {
protected ScreenProfiler profiler = AppBeans.get(ScreenProfiler.NAME);
@Override
public boolean synchronizedHandleRequest(VaadinSession session, VaadinRequest request, VaadinResponse response)
throws IOException {
return withUserSession(session, () -> super.synchronizedHandleRequest(session, request, response));
}
@Override
protected UidlWriter createUidlWriter() {
return new UidlWriter() {
@ -335,24 +293,6 @@ public class CubaVaadinServletService extends VaadinServletService {
}
}
// Set security context to AppContext for UI init requests
protected static class CubaServletUiInitHandler extends ServletUIInitHandler {
@Override
public boolean synchronizedHandleRequest(VaadinSession session, VaadinRequest request, VaadinResponse response)
throws IOException {
return withUserSession(session, () -> super.synchronizedHandleRequest(session, request, response));
}
}
// Set security context to AppContext for connector resources
protected static class CubaConnectorResourceHandler extends ConnectorResourceHandler {
@Override
public boolean handleRequest(VaadinSession session, VaadinRequest request, VaadinResponse response)
throws IOException {
return withUserSession(session, () -> super.handleRequest(session, request, response));
}
}
@Override
protected VaadinSession createVaadinSession(VaadinRequest request) throws ServiceException {
if (testMode && !webAuthConfig.getExternalAuthentication()) {
@ -392,8 +332,4 @@ public class CubaVaadinServletService extends VaadinServletService {
return super.createVaadinSession(request);
}
}
protected interface RequestHandlerAction<T> {
T handle() throws IOException;
}
}

View File

@ -25,7 +25,6 @@ import com.haulmont.cuba.gui.AppConfig;
/**
* {@link AppContext} loader of the web client application.
*
*/
public class WebAppContextLoader extends AbstractWebAppContextLoader {
@ -39,6 +38,8 @@ public class WebAppContextLoader extends AbstractWebAppContextLoader {
super.beforeInitAppContext();
AppContext.setProperty(AppConfig.CLIENT_TYPE_PROP, ClientType.WEB.toString());
AppContext.Internals.setSecurityContextHolder(new WebVaadinCompatibleSecurityContextHolder());
}
@Override

View File

@ -0,0 +1,43 @@
/*
* Copyright (c) 2008-2017 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 com.haulmont.cuba.web.sys;
import com.haulmont.cuba.core.sys.SecurityContext;
import com.haulmont.cuba.core.sys.ThreadLocalSecurityContextHolder;
import com.vaadin.server.VaadinSession;
public class WebVaadinCompatibleSecurityContextHolder extends ThreadLocalSecurityContextHolder {
@Override
public SecurityContext get() {
VaadinSession vaadinSession = VaadinSession.getCurrent();
if (vaadinSession != null && vaadinSession.hasLock()) {
return vaadinSession.getAttribute(SecurityContext.class);
}
return super.get();
}
@Override
public void set(SecurityContext securityContext) {
VaadinSession vaadinSession = VaadinSession.getCurrent();
if (vaadinSession != null && vaadinSession.hasLock()) {
vaadinSession.setAttribute(SecurityContext.class, securityContext);
} else {
super.set(securityContext);
}
}
}