Improve Web API implementation with enhanced security and thread-safety
This pull request introduces a set of significant refactoring and improvements to the Fess API implementation, focusing on modularity, maintainability, performance, and code quality. Key changes include the introduction of new utility classes for content type and JSON response handling, centralization of API constants, thread-safety improvements in the API manager factory, and simplification of content type detection logic. These changes are fully backward compatible and lay the groundwork for future enhancements.
API Infrastructure Improvements
- Introduced
ApiConstants.javato centralize all API-related constants (request/response field names, HTTP methods, MIME types, error messages, etc.), eliminating magic strings and improving maintainability across the codebase. - Refactored
WebApiManagerFactory.javato use a thread-safeCopyOnWriteArrayListfor storing API managers, added null validation in theadd()method, and implemented asize()method for introspection. This improves performance and safety during concurrent initialization. [1] [2]
Utility and Code Simplification
- Added
ContentTypeUtil.java, a new utility class that centralizes MIME type detection using a map-based approach, replacing long if-else chains for file extension handling. This makes content type management extensible and maintainable. - Refactored content type detection in
SearchEngineApiManager.javato useContentTypeUtil, reducing ~30 lines of repetitive if-else code to ~7 lines and improving readability and extensibility.
API Manager Improvements
- Updated
BaseApiManager.javato use the newApiConstantsfor attribute keys and replaced the format type detection if-else chain with efficient enum-based detection usingFormatType.valueOf(), improving code clarity and performance. [1] [2] [3]
For a detailed overview and rationale behind these changes, see the new documentation in API_IMPROVEMENTS.md.