Rath Common
===========
Utility toolkit shared across Rath services. It ships lightweight helpers for HTTP calls, JSON-friendly response wrappers, time handling pinned to Asia/Kolkata, string/number utilities, multipart argument resolution, and a small concurrency primitive.
Installation
- Add the GitHub Packages repo (requires a GitHub token with read access):
<repositories>
<repository>
<id>github</id>
<url>https://maven.pkg.github.com/tranztech/rath-common</url>
</repository>
</repositories>
- Depend on the library:
<dependency>
<groupId>com.tranztechnologies</groupId>
<artifactId>rath-common</artifactId>
<version>1.0.2</version>
</dependency>
HTTP helper (HttpUtil)
- Single-entry
fetchfor GET/POST/etc. with query params, JSON bodies, headers, and optional multipart files (OkHttp-based, 30m timeouts). - Quick JSON parsing helpers:
toMap(String url)andtoList(String url)wrap responses inMapResponse/ListResponse.
HashMap<String, String> params = new HashMap<>();
params.put("q", "test");
JSONObject body = new JSONObject().put("flag", true);
String data = HttpUtil.fetch(
"https://example.com/api",
"POST",
params,
body,
null,
List.of(new HttpUtil.FileParam("file", "a.txt", "hi".getBytes(), "text/plain"))
);
Response wrappers (MapResponse, ListResponse)
MapResponseextendsHashMap<String, Object>with fluentput, typed getters, andsuccess/failurefactories.ListResponseextendsArrayList<Object>with typed getters andputfor chaining.
Select service (RathSelectService)
- Base class for building parametrized selects with simple in-memory caching (opt-out via
getCacheIgnores). - Appends criteria as
column = ?clauses and uses deterministic cache keys based on criteria ordering. - Returns a
MapResponsewith the results underoptions.
class CountrySelectService extends RathSelectService {
CountrySelectService() {
super(Map.of("countries", "Select id, name From country Where active = ?"));
}
@Override
public Set<String> getCacheIgnores() {
return Set.of("countries"); // optionally skip cache for volatile selects
}
}
CountrySelectService service = new CountrySelectService();
MapResponse response = service.cachedSelect("countries", Map.of("active", true));
List<MapResponse> options = (List<MapResponse>) response.get("options");
Common utilities (CommonUtil)
- GST helpers:
addGst,removeGst, numericround. - Null/empty helpers:
nullIfEmpty. - Request context helpers:
getRequest,getIp(honors proxy headers),getUserAgent. - External lookups:
ifsc(String)via Razorpay IFSC API,postal(String)via India Post API (returnsMapResponsewithpostallist).
String utilities (StringUtil)
- Validators:
isEmail,isPhone,isPostcode,isNumber,isBoolean. - Parsers:
isTime/isDatewithTimeUtilparsing,parseFilterValue(coerces booleans/timestamps). - Helpers:
nullify,coalesce,seoEncode,isJsonObject.
Time utilities (TimeUtil, ISTDateFormat, ISTCalender)
- All time operations pinned to
Asia/Kolkata. - Formatting/parsing helpers:
toString,toDateString,parse,parseDate. - Convenience:
today,start(long), duration helpers (minutes,hours,days,weeks), FY calculation (getFY), week arithmetic (getWeekDifference,getPreviousWeek,getNextWeek). ISTDateFormat/ISTCalenderensure consistent timezone handling.
Multipart mix helper (MixMultipartFileAndString)
- Annotation + argument resolver to accept multipart files and strings in the same parameter name.
@Configuration
class WebConfig implements WebMvcConfigurer {
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
resolvers.add(new MixMultiPartFileAndStringArgumentResolver());
}
}
@PostMapping("/upload")
public void upload(@MixMultipartFileAndString List<Object> payload) {
// payload may contain MultipartFile instances and String form values
}
Concurrency (BucketSemaphore)
- Thin wrapper over
Semaphorewith bucketed permits and a global cap; use carefully to avoid deadlocks.
BucketSemaphore sem = new BucketSemaphore(5); // max concurrent acquisitions
sem.acquire(2); // blocks until a slot is available
try {
// critical section
} finally {
sem.release(2);
}
Numbers (NumberToWords)
convertToWords(double amount)formats INR amounts to words (Rupees/Paise, Indian numbering system).
Scripts
scripts/deploy.sh/scripts/deploy.bat: helper scripts for publishing to GitHub Packages (adjust credentials/env vars before use).