001/*
002 * (C) Copyright 2020 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 */
016package org.nuxeo.ecm.core.blob;
017
018/**
019 * Represents trusted managed blob key computation with a fallback {@link KeyStrategy}
020 *
021 * @since 11.2
022 */
023public class KeyStrategyManaged implements KeyStrategy {
024
025    protected final KeyStrategy strategy;
026
027    public KeyStrategyManaged(KeyStrategy strategy) {
028        this.strategy = strategy;
029    }
030
031    @Override
032    public boolean useDeDuplication() {
033        return false;
034    }
035
036    @Override
037    public String getDigestFromKey(String key) {
038        return null;
039    }
040
041    @Override
042    public BlobWriteContext getBlobWriteContext(BlobContext blobContext)  {
043        if (blobContext.blob instanceof ManagedBlob) {
044            // strip version id if part of file key
045            String key = ((ManagedBlob) blobContext.blob).getKey().split(String.valueOf(VER_SEP))[0];
046            return new BlobWriteContext(blobContext, null, () -> key, this);
047        } else {
048            return strategy.getBlobWriteContext(blobContext);
049        }
050    }
051
052    @Override
053    public boolean equals(Object obj) {
054        if (!(obj instanceof KeyStrategyManaged)) {
055            return false;
056        }
057        return strategy.equals(((KeyStrategyManaged) obj).strategy);
058    }
059
060    @Override
061    public int hashCode() {
062        return 0;
063    }
064
065}