use of com.pratilipi.common.exception.InvalidArgumentException in project pratilipi by Pratilipi.
the class GenericApi method service.
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Logging
if (SystemProperty.STAGE != SystemProperty.STAGE_PROD)
logHeaders(request);
logRequestParams(request);
// Creating request payload json
JsonObject requestPayloadJson = createRequestPayloadJson(request);
String method = request.getMethod();
Object apiResponse = null;
// Invoking get/put method for API response
if (method.equals("GET") && getMethod != null)
apiResponse = executeApi(this, getMethod, requestPayloadJson, getMethodParameterType, request);
else if (method.equals("POST") && postMethod != null)
apiResponse = executeApi(this, postMethod, requestPayloadJson, postMethodParameterType, request);
else
apiResponse = new InvalidArgumentException("Invalid resource or method.");
// Dispatching API response
dispatchApiResponse(apiResponse, request, response);
}
use of com.pratilipi.common.exception.InvalidArgumentException in project pratilipi by Pratilipi.
the class GenericBatchApi method service.
@Override
protected final void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Logging
if (SystemProperty.STAGE != SystemProperty.STAGE_PROD)
logHeaders(request);
logRequestParams(request);
String method = request.getMethod();
Object apiResponse = null;
if (method.equals("GET")) {
Gson gson = new Gson();
JsonObject apiReqs = gson.fromJson(// e.g. {"req1":"/page?uri=/munshi-premchand/godan","req2":"/pratilipi?pratilipiId=$req1.primaryContentId"}
request.getParameter("requests"), JsonElement.class).getAsJsonObject();
Map<String, Object> apiResps = new HashMap<>();
for (Entry<String, JsonElement> apiReq : apiReqs.entrySet()) {
// e.g. /pratilipi?pratilipiId=$req1.primaryContentId
String reqUri = apiReq.getValue().getAsString();
int index = reqUri.indexOf('?');
GenericApi api = // e.g. /pratilipi
index == -1 ? ApiRegistry.getApi(reqUri) : ApiRegistry.getApi(reqUri.substring(0, index));
JsonObject reqPayloadJson = // e.g. pratilipiId=$req1.primaryContentId
index == -1 ? new JsonObject() : createRequestPayloadJson(reqUri.substring(index + 1), request);
// Replacing variables by values
for (Entry<String, JsonElement> paramVal : reqPayloadJson.entrySet()) {
if (paramVal.getValue() == null)
continue;
String var = paramVal.getValue().getAsString();
if (!var.startsWith("$"))
continue;
Object varResp = apiResps.get(var.substring(1, var.indexOf(".")));
if (varResp == null)
reqPayloadJson.add(paramVal.getKey(), null);
else if (varResp instanceof JsonElement)
reqPayloadJson.add(paramVal.getKey(), ((JsonElement) varResp).getAsJsonObject().get(var.substring(var.indexOf(".") + 1)));
else
reqPayloadJson.add(paramVal.getKey(), null);
}
Object apiResp = executeApi(api, api.getMethod, reqPayloadJson, api.getMethodParameterType, request);
if (apiResp instanceof GenericResponse)
apiResp = gson.toJsonTree(apiResp);
apiResps.put(apiReq.getKey(), apiResp);
}
dispatchApiResponse(apiResps, request, response);
} else {
apiResponse = new InvalidArgumentException("Invalid resource or method.");
dispatchApiResponse(apiResponse, request, response);
}
}
Aggregations