org.xlcloud.service.provider.WebApplicationExceptionMapper.java Source code

Java tutorial

Introduction

Here is the source code for org.xlcloud.service.provider.WebApplicationExceptionMapper.java

Source

/*
 * Copyright 2012 Bull SAS
 * Copyright 2012 AMG.net S.A.
 * 
 * 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.xlcloud.service.provider;

import javax.annotation.ManagedBean;
import javax.inject.Inject;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.log4j.Logger;

import org.xlcloud.config.ConfigParam;
import org.xlcloud.openstack.model.exceptions.OpenStackAuthenticationException;
import org.xlcloud.openstack.model.exceptions.OpenStackException;
import org.xlcloud.service.exception.VcmsException;
import org.xlcloud.service.exception.VcmsObjectNotFoundException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
import com.sun.jersey.api.NotFoundException;

/**
 * {@link ExceptionMapper} implementation responsible for wrapping every
 * exception into xml/json serializable Response.
 * 
 * @author Maciej Osyda, AMG.net
 */
@Provider
@ManagedBean
public class WebApplicationExceptionMapper implements ExceptionMapper<Exception> {

    private static final Logger LOG = Logger.getLogger(WebApplicationExceptionMapper.class);

    @Inject
    @ConfigParam
    private Boolean showExceptionStackTraces;

    private VcmsException map(WebApplicationException exception) {
        VcmsException vcmsException;
        if (exception instanceof VcmsException) {
            vcmsException = (VcmsException) exception;
        } else if (exception instanceof NotFoundException) {
            vcmsException = new VcmsObjectNotFoundException("Not found.");
        } else {
            int statusCode = ((WebApplicationException) exception).getResponse().getStatus();
            String message = exception.getMessage();
            vcmsException = new VcmsException(statusCode, message);
        }
        return vcmsException;
    }

    private VcmsException map(UnrecognizedPropertyException exception) {
        LOG.debug(exception.getMessage());
        return new VcmsException(400, String.format("Bad data format ('%s' property not recognized)",
                exception.getUnrecognizedPropertyName()));
    }

    private VcmsException map(JsonProcessingException exception) {
        LOG.debug(exception.getMessage());
        return new VcmsException(400, "Bad data format");
    }

    private VcmsException map(OpenStackException exception) {
        LOG.debug(exception.getMessage());

        if (exception instanceof OpenStackAuthenticationException) {
            return new VcmsException(403, exception.getMessage());
        } else {
            return new VcmsException(exception.getStatusCode(), exception.getMessage());
        }
    }

    /** {@inheritDoc} */
    @Override
    public Response toResponse(Exception exception) {
        VcmsException vcmsException;
        if (exception instanceof WebApplicationException) {
            vcmsException = map((WebApplicationException) exception);
        } else if (exception instanceof UnrecognizedPropertyException) {
            vcmsException = map((UnrecognizedPropertyException) exception);
        } else if (exception instanceof JsonProcessingException) {
            vcmsException = map((JsonProcessingException) exception);
        } else if (exception instanceof OpenStackException) {
            vcmsException = map((OpenStackException) exception);
        } else {
            LOG.warn("Unrecognized exception!", exception);
            vcmsException = new VcmsException("Internal application error.");
        }
        if (showExceptionStackTraces) {
            vcmsException.setStackTrace(ExceptionUtils.getStackTrace(exception));
        }
        return vcmsException.getResponse();
    }

}