001/*
002 * (C) Copyright 2006-2014 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Florent Guillaume
018 */
019package org.nuxeo.ecm.core.opencmis.bindings;
020
021import org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException;
022import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
023import org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException;
024import org.apache.chemistry.opencmis.commons.server.CmisService;
025import org.apache.chemistry.opencmis.server.support.wrapper.ConformanceCmisServiceWrapper;
026import org.nuxeo.ecm.core.api.RecoverableClientException;
027import org.nuxeo.ecm.core.query.QueryParseException;
028import org.nuxeo.runtime.transaction.TransactionHelper;
029import org.slf4j.Logger;
030import org.slf4j.LoggerFactory;
031
032/**
033 * CMIS Conformance Service Wrapper that has better exception handling than the default.
034 */
035public class NuxeoCmisServiceWrapper extends ConformanceCmisServiceWrapper {
036
037    private static final Logger LOG = LoggerFactory.getLogger(NuxeoCmisServiceWrapper.class);
038
039    public NuxeoCmisServiceWrapper(CmisService service) {
040        super(service);
041    }
042
043    /**
044     * Converts the given exception into a CMIS exception.
045     */
046    @Override
047    protected CmisBaseException createCmisException(Exception e) {
048        // make sure that the transaction is marked rollback-only, as higher layers in the
049        // CMIS services stack will swallow it and turn it into a high-level HTTP error
050        TransactionHelper.setTransactionRollbackOnly();
051
052        // map exception into CmisBaseException
053        if (e == null) {
054            return new CmisRuntimeException("Unknown exception!");
055        } else if (e instanceof CmisBaseException) {
056            return (CmisBaseException) e;
057        } else if (e instanceof RecoverableClientException) {
058            return new CmisRuntimeException(e.getMessage(), e);
059        } else if (e instanceof QueryParseException) {
060            return new CmisInvalidArgumentException(e.getMessage(), e);
061        } else {
062            // should not happen if the connector works correctly
063            // it's alarming enough to log the exception
064            LOG.warn(e.toString(), e);
065            return new CmisRuntimeException(e.getMessage(), e);
066        }
067    }
068
069}