/*
 *  Copyright WSO2 Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.wso2.carbon.apimgt.rest.api.util.exception;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.exc.UnrecognizedPropertyException;
import org.wso2.carbon.apimgt.rest.api.util.RestApiConstants;
import org.wso2.carbon.apimgt.rest.api.util.dto.ErrorDTO;
import org.wso2.carbon.apimgt.rest.api.util.utils.RestApiUtil;

import javax.ws.rs.ClientErrorException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;

public class GlobalThrowableMapper implements ExceptionMapper<Throwable> {

    private static final Log log = LogFactory.getLog(GlobalThrowableMapper.class);

    private ErrorDTO e500 = new ErrorDTO();

    GlobalThrowableMapper() {
        e500.setCode((long) 500);
        e500.setMessage("Internal server error. Please contact administrator.");
    }

    @Override
    public Response toResponse(Throwable e) {

        if (e instanceof ClientErrorException) {
            return ((ClientErrorException) e).getResponse();
        }

        if (e instanceof NotFoundException) {
            return ((NotFoundException) e).getResponse();
        }

        if (e instanceof PreconditionFailedException) {
            return ((PreconditionFailedException) e).getResponse();
        }

        if (e instanceof BadRequestException) {
            return ((BadRequestException) e).getResponse();
        }

        if (e instanceof ConstraintViolationException) {
            return ((ConstraintViolationException) e).getResponse();
        }

        if (e instanceof ForbiddenException) {
            return ((ForbiddenException) e).getResponse();
        }

        if (e instanceof ConflictException) {
            return ((ConflictException) e).getResponse();
        }

        if (e instanceof MethodNotAllowedException) {
            return ((MethodNotAllowedException) e).getResponse();
        }

        if (e instanceof JsonParseException) {
            //noinspection ThrowableResultOfMethodCallIgnored
            return RestApiUtil.buildBadRequestException("Malformed request body.").getResponse();
        }

        if (e instanceof JsonMappingException) {
            if (e instanceof UnrecognizedPropertyException) {
                UnrecognizedPropertyException unrecognizedPropertyException = (UnrecognizedPropertyException) e;
                String unrecognizedProperty = unrecognizedPropertyException.getUnrecognizedPropertyName();
                //noinspection ThrowableResultOfMethodCallIgnored
                return RestApiUtil
                        .buildBadRequestException("Unrecognized property '" + unrecognizedProperty + "'")
                        .getResponse();
            } else {
                //noinspection ThrowableResultOfMethodCallIgnored
                return RestApiUtil
                        .buildBadRequestException("One or more request body parameters contain disallowed values.")
                        .getResponse();
            }
        }

        //unknown exception log and return
        log.error("An Unknown exception has been captured by global exception mapper.", e);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).header("Content-Type", "application/json")
                .entity(e500).build();
    }
}

@@ -907,70 +907,8 @@
 on;%0A
-import org.wso2.carbon.apimgt.rest.api.util.RestApiConstants;%0A
 impo
@@ -1139,16 +1139,45 @@
 nMapper;
+%0Aimport java.io.EOFException;
 %0A%0Apublic
@@ -3669,32 +3669,388 @@
    %7D%0A        %7D%0A%0A
+        //This occurs when received an empty body in an occasion where the body is mandatory%0A        if (e instanceof EOFException) %7B%0A            //noinspection ThrowableResultOfMethodCallIgnored%0A            return RestApiUtil%0A                    .buildBadRequestException(%22Request payload cannot be empty.%22)%0A                    .getResponse();%0A        %7D%0A%0A
         //unknow

fix validation for not empty body where body is mandatory