Search in sources :

Example 36 with InvalidArgumentException

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);
}
Also used : InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) JsonObject(com.google.gson.JsonObject) JsonObject(com.google.gson.JsonObject)

Example 37 with InvalidArgumentException

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);
    }
}
Also used : InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) HashMap(java.util.HashMap) GenericResponse(com.pratilipi.api.shared.GenericResponse) JsonElement(com.google.gson.JsonElement) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) JsonObject(com.google.gson.JsonObject)

Aggregations

InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)37 JsonObject (com.google.gson.JsonObject)21 DataAccessor (com.pratilipi.data.DataAccessor)19 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)11 UnexpectedServerException (com.pratilipi.common.exception.UnexpectedServerException)9 User (com.pratilipi.data.type.User)9 Gson (com.google.gson.Gson)7 GenericResponse (com.pratilipi.api.shared.GenericResponse)7 AuditLog (com.pratilipi.data.type.AuditLog)7 Author (com.pratilipi.data.type.Author)6 JsonElement (com.google.gson.JsonElement)5 Post (com.pratilipi.api.annotation.Post)5 UserData (com.pratilipi.data.client.UserData)5 Page (com.pratilipi.data.type.Page)5 Pratilipi (com.pratilipi.data.type.Pratilipi)5 Date (java.util.Date)5 HashMap (java.util.HashMap)5 Language (com.pratilipi.common.type.Language)3 UserPratilipi (com.pratilipi.data.type.UserPratilipi)3 PrintWriter (java.io.PrintWriter)3