Search in sources :

Example 1 with GenericRequest

use of com.pratilipi.api.shared.GenericRequest in project pratilipi by Pratilipi.

the class GenericApi method executeApi.

final Object executeApi(GenericApi api, Method apiMethod, JsonObject requestPayloadJson, Class<? extends GenericRequest> apiMethodParameterType, HttpServletRequest request) {
    try {
        GenericRequest apiRequest = new Gson().fromJson(requestPayloadJson, apiMethodParameterType);
        if (apiRequest instanceof GenericFileUploadRequest) {
            GenericFileUploadRequest gfuRequest = (GenericFileUploadRequest) apiRequest;
            try {
                ServletFileUpload upload = new ServletFileUpload();
                FileItemIterator iterator = upload.getItemIterator(request);
                while (iterator.hasNext()) {
                    FileItemStream fileItemStream = iterator.next();
                    if (!fileItemStream.isFormField()) {
                        gfuRequest.setName(fileItemStream.getName());
                        gfuRequest.setData(IOUtils.toByteArray(fileItemStream.openStream()));
                        gfuRequest.setMimeType(fileItemStream.getContentType());
                        break;
                    }
                }
            } catch (IOException | FileUploadException e) {
                throw new UnexpectedServerException();
            }
        }
        JsonObject errorMessages = apiRequest.validate();
        if (errorMessages.entrySet().size() > 0)
            return new InvalidArgumentException(errorMessages);
        else
            return apiMethod.invoke(api, apiRequest);
    } catch (JsonSyntaxException e) {
        logger.log(Level.SEVERE, "Invalid JSON in request body.", e);
        return new InvalidArgumentException("Invalid JSON in request body.");
    } catch (UnexpectedServerException e) {
        return e;
    } catch (InvocationTargetException e) {
        Throwable te = e.getTargetException();
        if (te instanceof InvalidArgumentException || te instanceof InsufficientAccessException || te instanceof UnexpectedServerException) {
            return te;
        } else {
            logger.log(Level.SEVERE, "Failed to execute API.", te);
            return new UnexpectedServerException();
        }
    } catch (IllegalAccessException | IllegalArgumentException e) {
        logger.log(Level.SEVERE, "Failed to execute API.", e);
        return new UnexpectedServerException();
    }
}
Also used : GenericFileUploadRequest(com.pratilipi.api.shared.GenericFileUploadRequest) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) JsonSyntaxException(com.google.gson.JsonSyntaxException) FileItemStream(org.apache.commons.fileupload.FileItemStream) GenericRequest(com.pratilipi.api.shared.GenericRequest) FileItemIterator(org.apache.commons.fileupload.FileItemIterator) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Example 2 with GenericRequest

use of com.pratilipi.api.shared.GenericRequest in project pratilipi by Pratilipi.

the class GenericApi method init.

@SuppressWarnings("unchecked")
@Override
public void init() throws ServletException {
    for (Method method : this.getClass().getMethods()) {
        if (getMethod == null && method.getAnnotation(Get.class) != null) {
            getMethod = method;
            getMethodParameterType = (Class<? extends GenericRequest>) method.getParameterTypes()[0];
            for (Field field : getMethodParameterType.getDeclaredFields()) if (field.getAnnotation(Sensitive.class) != null)
                getRequestSensitiveFieldList.add(field.getName());
        } else if (postMethod == null && method.getAnnotation(Post.class) != null) {
            postMethod = method;
            postMethodParameterType = (Class<? extends GenericRequest>) method.getParameterTypes()[0];
            for (Field field : postMethodParameterType.getDeclaredFields()) if (field.getAnnotation(Sensitive.class) != null)
                postRequestSensitiveFieldList.add(field.getName());
        }
    }
}
Also used : Field(java.lang.reflect.Field) Post(com.pratilipi.api.annotation.Post) Sensitive(com.pratilipi.api.annotation.Sensitive) Method(java.lang.reflect.Method) GenericRequest(com.pratilipi.api.shared.GenericRequest)

Aggregations

GenericRequest (com.pratilipi.api.shared.GenericRequest)2 Gson (com.google.gson.Gson)1 JsonObject (com.google.gson.JsonObject)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 Post (com.pratilipi.api.annotation.Post)1 Sensitive (com.pratilipi.api.annotation.Sensitive)1 GenericFileUploadRequest (com.pratilipi.api.shared.GenericFileUploadRequest)1 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)1 InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)1 UnexpectedServerException (com.pratilipi.common.exception.UnexpectedServerException)1 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 FileItemIterator (org.apache.commons.fileupload.FileItemIterator)1 FileItemStream (org.apache.commons.fileupload.FileItemStream)1 FileUploadException (org.apache.commons.fileupload.FileUploadException)1 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)1