def doFilter(self, request: Request, response: Response, chain: FilterChain): try: chain.doFilter(request, response) except Exception: response.status = 500 response.body = "An error occurred while handling this request." self.LOGGER.error(traceback.format_exc())
def doFilter(self, request: Request, response: Response, chain: FilterChain): chain.doFilter(request, response) if self.registry.is_annotated( response.body.__class__, Serializable) or type( response.body) in TYPES_TO_BE_SERIALIZED: response.body = self.serializer.serialize(response.body)
def doFilter(self, request: Request, response: Response, chain: FilterChain): if request.headers['Content-Type'] == 'application/json' and len(request.body) > 0: request.body = json.loads(request.body) chain.doFilter(request, response) if isinstance(response.body, dict) or isinstance(response.body, list): response.body = json.dumps(response.body) response.headers['Content-Type'] = 'application/json'
def doFilter(self, request: Request, response: Response, chain: FilterChain): try: chain.doFilter(request, response) except Exception as e: response.status = 500 response.body = "{}\n{}".format(e.__class__.__name__, traceback.format_exc()) self.LOGGER.error(traceback.format_exc())
def doFilter(self, request: Request, response: Response, chain: FilterChain): try: chain.doFilter(request, response) except Exception as e: exception_mapper = Stream(self.exception_mappers).firstMatch( lambda mapper: mapper.handles(e)) if exception_mapper.isPresent(): response.copy(exception_mapper.get().create_response(e)) else: raise e
def doFilter(self, request: Request, response: Response, chain: FilterChain): try: self.temporary_file_store.init() chain.doFilter(request, response) finally: files = self.temporary_file_store.get() Stream(files) \ .map(lambda f: glob(f"{f}*")) \ .flat() \ .forEach(os.remove) self.temporary_file_store.clear()
def doFilter(self, request: Request, response: Response, chain: FilterChain): chain.doFilter(request, response) if isinstance(response.body, RenderedView): rendered_view = response.body template_text = self.view_template_repository.get_template(rendered_view.view_file) template_parameters = rendered_view.data if self.deserializer.is_deserializable_type(rendered_view.data.__class__): template_parameters = self.serializer.serialize(rendered_view.data) response.body = Template(template_text).render(**template_parameters) response.headers['Content-Type'] = "text/html"
def doFilter(self, request: Request, response: Response, chain: FilterChain): start_time = self._now() self.telemetry_client.track_start() try: chain.doFilter(request, response) if response.status > 400: if response.status != 404: # Ignore random crawlers self.telemetry_client.track_failure(request.method, self._now() - start_time) else: self.telemetry_client.track_request(request.method, self._now() - start_time) finally: self.telemetry_client.track_end()
class FilterChainTest(unittest.TestCase): def setUp(self): self.resourceInvokerFactoryMock: RouteHandlerFactory = mock.create_autospec( RouteHandlerFactory) self.resourceInvokerMock: ResourceInvoker = mock.create_autospec( ResourceInvoker) self.filterMock: Filter = mock.create_autospec(Filter) self.secondFilterMock: Filter = mock.create_autospec(Filter) self.filterChain = FilterChain( [self.filterMock, self.secondFilterMock], self.resourceInvokerFactoryMock) self.resourceInvokerFactoryMock.create_route_handlers.return_value = [ self.resourceInvokerMock ] def test_givenEmptyFilterChain_whenApplyingFilterChain_thenInvokeResourceInvocator( self): self.filterChain = FilterChain([], self.resourceInvokerFactoryMock) self.filterChain.doFilter(A_REQUEST, A_RESPONSE) self.resourceInvokerFactoryMock.create_route_handlers.assert_called_with( A_REQUEST) def test_givenNonEmptyFilterChain_whenGettingNextChain_thenReturnANewChainWithOneFewerFilter( self): next_chain = self.filterChain.getNextChain() self.assertEqual(1, len(next_chain.filters)) self.assertEqual(self.secondFilterMock, next_chain.filters[0]) def test_whenApplyingFilterChain_thenInvokeNextFilterInTheChain(self): self.filterChain.doFilter(A_REQUEST, A_RESPONSE) self.filterMock.doFilter.assert_called_once() self.secondFilterMock.doFilter.assert_not_called() def test_givenExceptionInOneOfTheResourceInvokers_whenInvoking_thenSilenceTheException( self): self.second_resource_invoker_mock: ResourceInvoker = mock.create_autospec( ResourceInvoker) self.second_resource_invoker_mock.invoke.side_effect = IncorrectResourceParametersException( ) self.resourceInvokerFactoryMock.create_route_handlers.return_value = [ self.second_resource_invoker_mock, self.resourceInvokerMock ] self.filterChain.filters = [] self.filterChain.doFilter(A_REQUEST, A_RESPONSE) self.resourceInvokerMock.invoke.assert_called_with(A_REQUEST)
def doFilter(self, request: Request, response: Response, chain: FilterChain): self.session.set(request.headers["Authorization"]) chain.doFilter(request, response)
def doFilter(self, request: Request, response: Response, chain: FilterChain): if request.headers['Content-Type'] == 'application/x-www-form-urlencoded': request.body = parse_urlencoded_form(request.body.decode("utf-8")) chain.doFilter(request, response)
def doFilter(self, request: Request, response: Response, chain: FilterChain): try: chain.doFilter(request, response) finally: self.request_scope.clear()
def doFilter(self, request: Request, response: Response, chain: FilterChain): chain.doFilter(request, response) if isinstance(response.body, StreamingResponseBody): response.headers["Transfer-Encoding"] = "chunked"
def doFilter(self, request: Request, response: Response, chain: FilterChain): chain.doFilter(request, response) if response.status != 404: self.cors_handler.inject_cors_headers(request.path, response.headers)
def doFilter(self, request: Request, response: Response, chain: FilterChain): chain.doFilter(request, response) response.headers['X-Powered-By'] = f"Jivago {jivago.__version__}"
def doFilter(self, request: Request, response: Response, chain: FilterChain): chain.doFilter(request, response)
def doFilter(self, request: Request, response: Response, chain: FilterChain): self.anachronos.store(FILTER) chain.doFilter(request, response)