001/*
002 * (C) Copyright 2019 Nuxeo (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.blob;
020
021import org.nuxeo.ecm.core.api.NuxeoException;
022
023/**
024 * Represents computation of blob keys based on the document id. Only the main blob can be written using this strategy.
025 *
026 * @since 11.1
027 */
028public class KeyStrategyDocId implements KeyStrategy {
029
030    private static final KeyStrategyDocId INSTANCE = new KeyStrategyDocId();
031
032    // in these low-level APIs we deal with unprefixed xpaths, so not file:content
033    protected static final String MAIN_BLOB_XPATH = "content";
034
035    public static KeyStrategyDocId instance() {
036        return INSTANCE;
037    }
038
039    @Override
040    public boolean useDeDuplication() {
041        return false;
042    }
043
044    @Override
045    public String getDigestFromKey(String key) {
046        return null;
047    }
048
049    @Override
050    public BlobWriteContext getBlobWriteContext(BlobContext blobContext) {
051        String xpath = blobContext.xpath;
052        if (!MAIN_BLOB_XPATH.equals(xpath)) {
053            throw new NuxeoException("Cannot store blob with xpath '" + xpath + "' in record blob provider");
054        }
055        String key = getKey(blobContext);
056        return new BlobWriteContext(blobContext, null, () -> key, this);
057    }
058
059    protected String getKey(BlobContext blobContext) {
060        String docId = blobContext.docId;
061        if (docId == null) {
062            throw new NuxeoException("Missing docId for key strategy");
063        }
064        return docId;
065    }
066
067    @Override
068    public boolean equals(Object obj) {
069        if (!(obj instanceof KeyStrategyDocId)) {
070            return false;
071        }
072        return true;
073    }
074
075    @Override
076    public int hashCode() {
077        return 0;
078    }
079
080}