public RestClient (URI baseURI) { ObjectMapper mapper = Jackson.newObjectMapper () .registerModule (new SupernodeModule ()); Validator validator = Validation.buildDefaultValidatorFactory ().getValidator (); ClientConfig cc = new DefaultClientConfig (JsonProcessingExceptionMapper.class); cc.getProperties ().put (ClientConfig.PROPERTY_CONNECT_TIMEOUT, 5000); cc.getProperties ().put (ClientConfig.PROPERTY_READ_TIMEOUT, 5000); cc.getSingletons ().add (new JacksonMessageBodyProvider (mapper, validator)); Client client = Client.create (cc); client.addFilter (new LoggingFilter ()); this.client = client; this.baseURI = baseURI; }
@Override public void run(ApiConfig configuration, Environment environment) throws Exception { LOGGER.info("api started up"); injector = guiceBundle.getInjector(); JerseyEnvironment jersey = environment.jersey(); register(environment.lifecycle(), REFLECTIONS.getSubTypesOf(Managed.class)); // registers NbdServer // injector.getInstance(SessionFactory.class); //init DB installCorsFilter(environment); //init all Singletons semi-eagerly REFLECTIONS.getTypesAnnotatedWith(Singleton.class).forEach(injector::getInstance); final Set<Class<?>> resources = REFLECTIONS.getTypesAnnotatedWith(Path.class); register(jersey, resources); jersey.register(new LoggingExceptionMapper<Throwable>() { @Override protected String formatErrorMessage(long id, Throwable exception) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); exception.printStackTrace(pw); return sw.toString(); } }); jersey.register(new JsonProcessingExceptionMapper(true)); jersey.register(new EarlyEofExceptionMapper()); final TrivialAuthenticator instance = injector.getInstance(TrivialAuthenticator.class); environment.jersey().register(new AuthDynamicFeature( new BasicCredentialAuthFilter.Builder<Principal>() .setAuthenticator(instance) .setAuthorizer((principal, role) -> false) .buildAuthFilter())); environment.jersey().register(RolesAllowedDynamicFeature.class); }
@Override protected void configure() { bind(new LoggingExceptionMapper<Throwable>() { }).to(ExceptionMapper.class); bind(new JerseyViolationExceptionMapper()).to(ExceptionMapper.class); bind(new JsonProcessingExceptionMapper(isShowDetails())).to(ExceptionMapper.class); bind(new EarlyEofExceptionMapper()).to(ExceptionMapper.class); // bind(new EmptyOptionalExceptionMapper()).to(ExceptionMapper.class); }
@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; }