Skip to content

Instantly share code, notes, and snippets.

@banterCZ
Created August 2, 2016 08:48
Show Gist options
  • Save banterCZ/8f021c91fbb1a4b9f04ed59838094811 to your computer and use it in GitHub Desktop.
Save banterCZ/8f021c91fbb1a4b9f04ed59838094811 to your computer and use it in GitHub Desktop.
Vaadin Servlet
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.cdi.server.VaadinCDIServlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
/**
* PM custom Vaadin servlet.
* <p>Vaading config <code>productionMode</code> is set by default to <code>true</code>.
* You may change it by setting the system property <code>productionMode</code> (done in {@link #init(ServletConfig)})
*/
@WebServlet(urlPatterns = "/*")
@VaadinServletConfiguration(ui = AddressbookUI.class, productionMode = true)
public class CustomUIServlet extends VaadinCDIServlet {
private static final String PRODUCTION_MODE = "productionMode";
@Override
public void init(ServletConfig servletConfig) throws ServletException {
setupProductionMode(servletConfig);
super.init(servletConfig);
}
@Override
protected void servletInitialized() throws ServletException {
getService().addSessionInitListener(new ErrorHandlerSessionListener());
}
private void setupProductionMode(ServletConfig servletConfig) {
String productionMode = System.getProperty(PRODUCTION_MODE);
if (productionMode != null) {
servletConfig.getServletContext().setInitParameter(PRODUCTION_MODE, productionMode);
}
}
}
import com.vaadin.server.*;
import com.vaadin.ui.Notification;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Date;
/**
* The error handler shows an error notification and logs the error identifier.
*/
public class ErrorHandlerSessionListener implements SessionInitListener {
private static final Logger LOGGER = LoggerFactory.getLogger(ErrorHandlerSessionListener.class);
@Override
public void sessionInit(SessionInitEvent event) throws ServiceException {
VaadinSession session = event.getSession();
session.setErrorHandler(new DefaultErrorHandler() {
@Override
public void error(ErrorEvent event) {
Throwable relevantThrowable = DefaultErrorHandler.findRelevantThrowable(event.getThrowable());
String errorIdentifier = createErrorIdentifier();
LOGGER.error("Error - {}", errorIdentifier, relevantThrowable);
Notification.show("Error", "An error has occurred. The error identifier is: " + errorIdentifier, Notification.Type.ERROR_MESSAGE);
}
});
}
/**
* Creates an error identifier, it is last 12 digits of timestamp formatted to four digits chunks, e.g. 4413-7312-5282
* @return
*/
private String createErrorIdentifier() {
String errorIdString = Long.toString(new Date().getTime());
errorIdString = errorIdString.substring(errorIdString.length() - 12);
StringBuilder builder = new StringBuilder(15);
for (int i = 0; i < errorIdString.length(); i++) {
builder.append(errorIdString.charAt(i));
if (i % 4 == 3 && i != errorIdString.length() - 1) {
builder.append("-");
}
}
return builder.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment