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.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030
031/**
032 * CMIS Conformance Service Wrapper that has better exception handling than the default.
033 */
034public class NuxeoCmisServiceWrapper extends ConformanceCmisServiceWrapper {
035
036    private static final Logger LOG = LoggerFactory.getLogger(NuxeoCmisServiceWrapper.class);
037
038    public NuxeoCmisServiceWrapper(CmisService service) {
039        super(service);
040    }
041
042    /**
043     * Converts the given exception into a CMIS exception.
044     */
045    protected CmisBaseException createCmisException(Exception e) {
046        if (e == null) {
047            return new CmisRuntimeException("Unknown exception!");
048        } else if (e instanceof CmisBaseException) {
049            return (CmisBaseException) e;
050        } else if (e instanceof RecoverableClientException) {
051            throw new CmisRuntimeException("error", e);
052        } else if (e instanceof QueryParseException) {
053            throw new CmisInvalidArgumentException(e.getMessage(), e);
054        } else {
055            // should not happen if the connector works correctly
056            // it's alarming enough to log the exception
057            LOG.warn(e.toString(), e);
058            return new CmisRuntimeException(e.getMessage(), e);
059        }
060    }
061
062}