{"id":24901751,"url":"https://github.com/antonsjava/web-filter","last_synced_at":"2025-03-27T18:19:52.392Z","repository":{"id":57732430,"uuid":"184930137","full_name":"antonsjava/web-filter","owner":"antonsjava","description":null,"archived":false,"fork":false,"pushed_at":"2023-12-17T17:51:41.000Z","size":46,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-01T21:17:26.847Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/antonsjava.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-05-04T18:21:25.000Z","updated_at":"2021-12-15T20:27:53.000Z","dependencies_parsed_at":"2023-02-13T01:31:28.923Z","dependency_job_id":null,"html_url":"https://github.com/antonsjava/web-filter","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antonsjava%2Fweb-filter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antonsjava%2Fweb-filter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antonsjava%2Fweb-filter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antonsjava%2Fweb-filter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/antonsjava","download_url":"https://codeload.github.com/antonsjava/web-filter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245898314,"owners_count":20690466,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-02-01T21:17:35.479Z","updated_at":"2025-03-27T18:19:52.369Z","avatar_url":"https://github.com/antonsjava.png","language":"Java","readme":"\n# web-filter\n\n Set of classes usefull for Servlet filter implementation.\n  - RequestLimiter. API for filter usage configuration \n  - LogFilter. Implementation of request response logging filter. \n    Implemented as example of RequestLimiter usage.\n\n## RequestLimiter\n\n Implementation of various configuations and check for ServletRequests\n and its state. \n \n It is quit easy to setup Filter for your API. But is is complicated to \n implement configuration of filter in a way where you want to limit filter\n functionality on som request condition.\n\n RequestLimiter can provide such configuration for your filter implementation.\n \n As example of usage you add limiter as atttribute of filter \n```java\n  public class MyFilter implements Filter {\n    private RequestLimiter limiter = new RequestLimiter();\n    public RequestLimiter limit() { return limiter; }\n```\n\n Now you can configure the filter\n \n```java\n  MyFilter filter = MyFilter.instance();\n  filter.limit()\n    .path()\n      .include(\"/foo/**\") \n      .exclude(\"/foo.bar\", \"POST\");\n  ;\n```\n\n And in MyFilter implementation you can use something like \n\n```java\n  if (limiter.allow(request)) { ... do something }\n  chain.doFilter(request, response);\n  if (limiter.allow(request)) { ... do something }\n```\n\n See LogFilter doc for examples of usage.\n\n## LogFilter\n\n Implements logging filter for ServletRequests. It can show usage of \n RequestLimiter usage.\n\n LogFilter logs content of http requests and responses. You can use them \n to debug your web API. \n \n It logs following events \n - start or the request with method and path (and 'vvv' sequence indicating \n   that relevant log is bellow)\n - possible unhandled exception (ussualy not present)\n - request path headers and payload\n - response status headers and payload\n \n It enable variety of configure 'where', 'what' and 'how' to log messages. \n And also there is possible to configure when to log and when not. This \n is usefull as possibility to configure filter presence itself is ussually \n pretty complicated.\n \n### Filter configuration - common\n \n There is only API way to configure the filter. So if you have no possibility \n to use API directy you must inherit from LogFilter configure and use your \n inherited (and configured) instance. \n \n### Filter configuration - where\n \n Consumer is abstraction target for log message. By default fileter log \n messages using jdk14 logging API. It uses sk.antons.web.filter.log.LogFilter\n logger and produce messages to FINEST level.\n \n If you want to use different API for logging you simply implement Consumer \n and ConsumerStatus (check if logging is enabled) interfaces and configure \n filter\n Example for slf4j and debug level.\n\n```java\n   filter.consumer(\n               (message) -\u003e { log.debug(message); } \n               , () -\u003e { return log.isDebugEnabled();} );\n```\n \n### Filter configuration - when \n \n LogFilter was implemented as example of usage RequestLimiter class/lib. By \n default is LogFilter configured with empty RequestLimiter so it is applied \n for all requests where is configured.\n \n But it is possible to limit LogFilter usage by configuration of RequestLimiter.\n See this class for configuration possibilities.\n Example for limit LogFilter functionality using path and method\n\n```java\n   filter.limit()\n               .path()\n                  .include(\"/foo/**\")\n                  .exclude(\"/foo/bar\", \"POST\");\n```\n \n### Filter configuration - what\n - filter.requestBeforePrefix(\"REQ\") If it is set to null no start request \n   message will be displayed\n - filter.requestPrefix(\"REQ\") If it is set to null no request message \n   will be displayed\n - filter.responsePrefix(\"RES\") If it is set to null no response message \n   will be displayed\n \n### Filter configuration - how\n - filter.logHeaders(true) If it is set to false no header information is \n   included in request message and response message\n - filter.logPayload(true) If it is set to false no payload information is \n   included in request message and response message. There are also \n   filter.printable() which define if payload can be displayed depending \n   on contentType.\n - filter.truncateTo(0) If it is set to value \u003e0 printable payload will be \n   truncated to this value. (usefull for variety of base64 values....)\n - filter.truncateLineTo(0) If it is set to value \u003e0 each line of printable \n   payload will be truncated to this value. (usefull for variety of \n   base64 values followed by another usefful information where truncateTo \n   skip that information)\n - filter.truncateJsonelementTo(0) If it is set to value \u003e0 and content is\n   filter.jsonable() each string literal will be truncated to this value.\n   (usefull for variety of base64 attributes in json)\n - filter.forceOneLine(true) If it is set to false content is not formated\n   otherwise itis formated to one line. New line characters are escaped \n   with \\\\n. Json content is formated to one line in inative form.\n \n### Filter configuration - example\n \n This is simple example for springboot and jdk14 default logging\n\n```java\n   @Bean\n   public Filter requestResponseDumpFilter() {\n       return LogFilter.instance();\n   }\n```\n \n This is simple example for springboot and slf4j and some configuration\n \n```java\n   @Bean\n   public Filter requestResponseDumpFilter() {\n     LogFilter filter = LogFilter.instance();\n     filter\n       .consumer(\n          (String string) -\u003e { log.debug(string); } \n          , () -\u003e { return log.isDebugEnabled();} )\n       .truncateTo(10000)\n       .truncateLineTo(100)\n       .truncateJsonLiteral(100)\n       //.oneLine(false)\n       .limit()\n         .path()\n           .include(\"/foo/**\", \"POST\") // - allow path pattern together with POST method\n           .include(\"/dummy/**\" //path - allow only this pattern\n             , null //method - allow all methos\n             , null //ip - allow all incoming ips\n             , null //host - allow all incoming host names\n             , MediaType.APPLICATION_JSON_VALUE //contentType - allow only json\n             , (status) -\u003e { return status \u003e= 400;}) //respons status check - allow bad statuses \n           .exclude(\"/foo/bar/**\" //path - disallow only this pattern\n             , \"PUT\" //method - disallow POT \n             , \"127.0.0.1\" //ip - localhost request\n             , null //host - disallow all incoming host names\n             , null //contentType - disallow all\n             , null) //respons status check - disallow all\n           //.exclude(\"/dummy/**\", \"GET\")\n     ;\n     return filter;\n   }\n```\n\n## Dependencies\n \n Implementation depends on Servlet API (j2ee 7) and \n  - com.github.antonsjava:web-path-matcher small library for path patterns\n  - com.github.antonsjava:json small library for parsing json\n\n## Maven usage\n\n```\n   \u003cdependency\u003e\n      \u003cgroupId\u003eio.github.antonsjava\u003c/groupId\u003e\n      \u003cartifactId\u003eweb-filter\u003c/artifactId\u003e\n      \u003cversion\u003eLASTVERSION\u003c/version\u003e\n   \u003c/dependency\u003e\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fantonsjava%2Fweb-filter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fantonsjava%2Fweb-filter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fantonsjava%2Fweb-filter/lists"}