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 java.util.function.Supplier;
022
023import org.apache.chemistry.opencmis.commons.data.ExtensionsData;
024import org.apache.chemistry.opencmis.commons.data.FailedToDeleteData;
025import org.apache.chemistry.opencmis.commons.enums.UnfileObject;
026import org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException;
027import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
028import org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException;
029import org.apache.chemistry.opencmis.commons.exceptions.CmisUpdateConflictException;
030import org.apache.chemistry.opencmis.commons.server.CmisService;
031import org.apache.chemistry.opencmis.server.support.wrapper.ConformanceCmisServiceWrapper;
032import org.nuxeo.ecm.core.api.ConcurrentUpdateException;
033import org.nuxeo.ecm.core.api.RecoverableClientException;
034import org.nuxeo.ecm.core.query.QueryParseException;
035import org.nuxeo.runtime.transaction.TransactionHelper;
036import org.slf4j.Logger;
037import org.slf4j.LoggerFactory;
038
039/**
040 * CMIS Conformance Service Wrapper that has better exception handling than the default.
041 */
042public class NuxeoCmisServiceWrapper extends ConformanceCmisServiceWrapper {
043
044    private static final Logger LOG = LoggerFactory.getLogger(NuxeoCmisServiceWrapper.class);
045
046    public NuxeoCmisServiceWrapper(CmisService service) {
047        super(service);
048    }
049
050    /**
051     * Converts the given exception into a CMIS exception.
052     */
053    @Override
054    protected CmisBaseException createCmisException(Exception e) {
055        // make sure that the transaction is marked rollback-only, as higher layers in the
056        // CMIS services stack will swallow it and turn it into a high-level HTTP error
057        TransactionHelper.setTransactionRollbackOnly();
058
059        // map exception into CmisBaseException
060        if (e == null) {
061            return new CmisRuntimeException("Unknown exception!");
062        } else if (e instanceof CmisBaseException) {
063            return (CmisBaseException) e;
064        } else if (e instanceof RecoverableClientException) {
065            return new CmisRuntimeException(e.getMessage(), e);
066        } else if (e instanceof QueryParseException) {
067            return new CmisInvalidArgumentException(e.getMessage(), e);
068        } else if (e instanceof ConcurrentUpdateException) {
069            return new CmisUpdateConflictException(e.getMessage(), e);
070        } else {
071            // should not happen if the connector works correctly
072            // it's alarming enough to log the exception
073            LOG.warn(e.toString(), e);
074            return new CmisRuntimeException(e.getMessage(), e);
075        }
076    }
077
078    protected void runWithRetryOnConflict(Runnable runnable) {
079        runWithRetryOnConflict(() -> {
080            runnable.run();
081            return null;
082        });
083    }
084
085    protected <R> R runWithRetryOnConflict(Supplier<R> supplier) {
086        try {
087            return supplier.get();
088        } catch (CmisUpdateConflictException e) {
089            // retry once
090            TransactionHelper.setTransactionRollbackOnly();
091            TransactionHelper.commitOrRollbackTransaction();
092            TransactionHelper.startTransaction();
093            return supplier.get();
094        }
095    }
096
097    @Override
098    public void deleteObject(String repositoryId, String objectId, Boolean allVersions, ExtensionsData extension) {
099        runWithRetryOnConflict(() -> super.deleteObject(repositoryId, objectId, allVersions, extension));
100    }
101
102    @Override
103    public void deleteObjectOrCancelCheckOut(String repositoryId, String objectId, Boolean allVersions,
104            ExtensionsData extension) {
105        runWithRetryOnConflict(
106                () -> super.deleteObjectOrCancelCheckOut(repositoryId, objectId, allVersions, extension));
107    }
108
109    @Override
110    public FailedToDeleteData deleteTree(String repositoryId, String folderId, Boolean allVersions,
111            UnfileObject unfileObjects, Boolean continueOnFailure, ExtensionsData extension) {
112        return runWithRetryOnConflict(() -> super.deleteTree(repositoryId, folderId, allVersions, unfileObjects,
113                continueOnFailure, extension));
114    }
115
116}