public int getPort(Environment environment) { int defaultPort = 9090; MutableServletContextHandler h = environment.getApplicationContext(); if (h == null) { return defaultPort; } Server s = h.getServer(); if (s == null) { return defaultPort; } Connector[] c = s.getConnectors(); if (c != null && c.length > 0) { AbstractNetworkConnector anc = (AbstractNetworkConnector) c[0]; if (anc != null) { return anc.getLocalPort(); } } return defaultPort; }
@Override public void start() throws Exception { for (MutableServletContextHandler environment : listeners.keySet()) { final SessionHandler sessionHandler = environment.getSessionHandler(); if (sessionHandler == null) { final String msg = String.format( "Can't register session listeners for %s because sessions support is not enabled: %s", environment.getDisplayName().toLowerCase(), Joiner.on(',').join(listeners.get(environment).stream() .map(it -> FeatureUtils.getInstanceClass(it).getSimpleName()) .collect(Collectors.toList()))); if (failWithoutSession) { throw new IllegalStateException(msg); } else { logger.warn(msg); } } else { listeners.get(environment).forEach(sessionHandler::addEventListener); } } }
@Override public void run(Environment environment) { MutableServletContextHandler applicationContext = environment.getApplicationContext(); FilterHolder filterHolder = applicationContext .addFilter(CrossOriginFilter.class, "/*", EnumSet.allOf(DispatcherType.class)); filterHolder.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, "*"); filterHolder.setInitParameter(CrossOriginFilter.ALLOWED_HEADERS_PARAM, "X-Requested-With,Content-Type,Accept,Origin"); filterHolder.setInitParameter(CrossOriginFilter.ALLOWED_METHODS_PARAM, "GET,HEAD"); filterHolder.setInitParameter(CrossOriginFilter.ALLOW_CREDENTIALS_PARAM, "false"); }
public WebsocketContainer initialize(WebsocketConfiguration configuration, MutableServletContextHandler contextHandler) { try { return new WebsocketContainer(configuration, WebSocketServerContainerInitializer.configureContext(contextHandler)); } catch (Exception e) { throw new IllegalStateException("Could not initialize contexthandler to enable Websockets", e); } }
public static ServerContainer configureContext(final MutableServletContextHandler context, final MetricRegistry metrics) throws ServletException { WebSocketUpgradeFilter filter = WebSocketUpgradeFilter.configureContext(context); NativeWebSocketConfiguration wsConfig = filter.getConfiguration(); ServerContainer wsContainer = new ServerContainer(wsConfig, context.getServer().getThreadPool()); EventDriverFactory edf = wsConfig.getFactory().getEventDriverFactory(); edf.clearImplementations(); edf.addImplementation(new InstJsrServerEndpointImpl(metrics)); edf.addImplementation(new InstJsrServerExtendsEndpointImpl(metrics)); context.addBean(wsContainer); context.setAttribute(javax.websocket.server.ServerContainer.class.getName(), wsContainer); context.setAttribute(WebSocketUpgradeFilter.class.getName(), filter); return wsContainer; }
private void configure(final MutableServletContextHandler environment, final EventListener listener, final boolean context, final boolean session) { if (session) { support.add(environment, listener); } if (context) { environment.addEventListener(listener); } }
@Before public void init() { when(containerInitializer.initialize(any(WebsocketConfiguration.class), any(MutableServletContextHandler.class))).thenReturn(container); when(endpointRegistration.getRegisteredEndpoints()).thenReturn(endpoints); }
public MutableServletContextHandler getApplicationContext() { return this.environment.getApplicationContext(); }
public MutableServletContextHandler getAdminContext() { return this.environment.getAdminContext(); }
@Override protected Handler createAppServlet(Server server, JerseyEnvironment jersey, ObjectMapper objectMapper, Validator validator, MutableServletContextHandler handler, @Nullable Servlet jerseyContainer, MetricRegistry metricRegistry) { if (jerseyContainer != null) { jersey.register(new LoggingExceptionMapper<Throwable>() { @Override public Response toResponse(Throwable exception) { final int status; final StatusEntity statusEntity; if (exception instanceof WebApplicationException) { final Response response = ((WebApplicationException) exception).getResponse(); if (response.getStatusInfo().getFamily().equals(Response.Status.Family.SERVER_ERROR)) { logException(exception); } status = response.getStatus(); statusEntity = new StatusEntity(status, new StatusMessage(µService.WEB_APPLICATION_ERROR, exception.getLocalizedMessage())); } else { final long id = logException(exception); status = Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(); StatusMessage message = new StatusMessage(StatusMessage.Type.ERROR, String.format("%016x", id), µService.INTERNAL_SERVER_ERROR, formatErrorMessage(id, exception), null); statusEntity = new StatusEntity(status, message); } return Response.status(status) .type(MediaType.APPLICATION_JSON_TYPE) .entity(statusEntity) .build(); } }); jersey.register(new ConstraintViolationExceptionMapper()); jersey.register(new JsonProcessingExceptionMapper()); jersey.register(new EarlyEofExceptionMapper()); } Handler appServlet = super.createAppServlet(server, jersey, objectMapper, validator, handler, jerseyContainer, metricRegistry); return appServlet; }
public void add(final MutableServletContextHandler environment, final EventListener listener) { listeners.put(environment, listener); }
private void nameContext(final MutableServletContextHandler context, final String name) { if (context.getDisplayName() == null) { context.setDisplayName(name); } }
/** * Override if needed, but prefer to exploit * {@link Pac4jFactory#setSessionEnabled(boolean)} first. * * @param environment * the dropwizard {@link Environment} * @since 1.1.0 */ protected void setupJettySession(Environment environment) { MutableServletContextHandler contextHandler = environment .getApplicationContext(); if (contextHandler.getSessionHandler() == null) { contextHandler.setSessionHandler(new SessionHandler()); } }