001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.internal.impl.synccontext.named;
020
021import java.util.Collection;
022import java.util.stream.Collectors;
023
024import org.eclipse.aether.RepositorySystemSession;
025import org.eclipse.aether.artifact.Artifact;
026import org.eclipse.aether.metadata.Metadata;
027import org.eclipse.aether.util.ConfigUtils;
028import org.eclipse.aether.util.StringDigestUtil;
029
030import static java.util.Objects.requireNonNull;
031
032/**
033 * Wrapping {@link NameMapper}, that wraps another {@link NameMapper} and hashes resulting strings. It makes use of
034 * fact that (proper) Hash will create unique fixed length string for each different input string (so injection still
035 * stands). This mapper produces file system friendly names. Supports different "depths" (0-4 inclusive) where the
036 * name will contain 0 to 4 level deep directories.
037 * <p>
038 * This mapper is usable in any scenario, but intent was to produce more "compact" name mapper for file locking.
039 *
040 * @since 1.9.0
041 */
042public class HashingNameMapper implements NameMapper {
043    private static final String CONFIG_PROP_DEPTH = "aether.syncContext.named.hashing.depth";
044
045    private final NameMapper delegate;
046
047    public HashingNameMapper(final NameMapper delegate) {
048        this.delegate = requireNonNull(delegate);
049    }
050
051    @Override
052    public boolean isFileSystemFriendly() {
053        return true; // hashes delegated strings, so whatever it wrapped, it does not come through
054    }
055
056    @Override
057    public Collection<String> nameLocks(
058            RepositorySystemSession session,
059            Collection<? extends Artifact> artifacts,
060            Collection<? extends Metadata> metadatas) {
061        final int depth = ConfigUtils.getInteger(session, 2, CONFIG_PROP_DEPTH);
062        if (depth < 0 || depth > 4) {
063            throw new IllegalArgumentException("allowed depth value is between 0 and 4 (inclusive)");
064        }
065        return delegate.nameLocks(session, artifacts, metadatas).stream()
066                .map(n -> hashName(n, depth))
067                .collect(Collectors.toList());
068    }
069
070    private String hashName(final String name, final int depth) {
071        String hashedName = StringDigestUtil.sha1(name);
072        if (depth == 0) {
073            return hashedName;
074        }
075        StringBuilder prefix = new StringBuilder("");
076        int i = 0;
077        while (i < hashedName.length() && i / 2 < depth) {
078            prefix.append(hashedName, i, i + 2).append("/");
079            i += 2;
080        }
081        return prefix.append(hashedName).toString();
082    }
083}