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.apache.maven.enforcer.rules.checksum;
020
021import javax.inject.Inject;
022import javax.inject.Named;
023
024import java.io.IOException;
025import java.io.InputStream;
026import java.io.Reader;
027import java.nio.charset.Charset;
028import java.nio.file.Files;
029import java.util.Objects;
030
031import org.apache.commons.io.input.ReaderInputStream;
032import org.apache.commons.lang3.StringUtils;
033import org.apache.maven.enforcer.rule.api.EnforcerRuleError;
034import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
035import org.apache.maven.enforcer.rules.checksum.NormalizeLineSeparatorReader.LineSeparator;
036import org.apache.maven.project.MavenProject;
037
038/**
039 * Rule to validate a text file to match the specified checksum.
040 *
041 * @author Konrad Windszus
042 * @see RequireFileChecksum
043 */
044@Named("requireTextFileChecksum")
045public final class RequireTextFileChecksum extends RequireFileChecksum {
046
047    private NormalizeLineSeparatorReader.LineSeparator normalizeLineSeparatorTo = LineSeparator.UNIX;
048
049    private Charset encoding;
050
051    private final MavenProject project;
052
053    @Inject
054    public RequireTextFileChecksum(MavenProject project) {
055        this.project = Objects.requireNonNull(project);
056    }
057
058    public void setNormalizeLineSeparatorTo(NormalizeLineSeparatorReader.LineSeparator normalizeLineSeparatorTo) {
059        this.normalizeLineSeparatorTo = normalizeLineSeparatorTo;
060    }
061
062    public void setEncoding(String encoding) {
063        this.encoding = Charset.forName(encoding);
064    }
065
066    public Charset getEncoding() {
067        return encoding;
068    }
069
070    @Override
071    public void execute() throws EnforcerRuleException {
072        // set defaults
073        if (encoding == null) {
074            // https://maven.apache.org/plugins/maven-resources-plugin/examples/encoding.html
075            String projectEncoding = project.getProperties().getProperty("project.build.sourceEncoding", null);
076            if (StringUtils.isBlank(projectEncoding)) {
077                encoding = Charset.defaultCharset();
078                getLog().warn(
079                                "File encoding has not been set, using platform encoding " + encoding.displayName()
080                                        + ". Build is platform dependent! - https://maven.apache.org/general.html#How_do_I_prevent_.E2.80.9C.5BWARNING.5D_Using_platform_encoding_.28Cp1252_actually.29_to_copy_filtered_resources.2C_i.e._build_is_platform_dependent.21.E2.80.9D");
081            } else {
082                encoding = Charset.forName(projectEncoding);
083            }
084        }
085        super.execute();
086    }
087
088    @Override
089    protected String calculateChecksum() throws EnforcerRuleException {
090        try (Reader reader = new NormalizeLineSeparatorReader(
091                        Files.newBufferedReader(getFile().toPath(), encoding), normalizeLineSeparatorTo);
092                InputStream inputStream = new ReaderInputStream(reader, encoding)) {
093            return super.calculateChecksum(inputStream);
094        } catch (IOException e) {
095            throw new EnforcerRuleError("Unable to calculate checksum (with normalized line separators)", e);
096        }
097    }
098
099    @Override
100    public String toString() {
101        return String.format(
102                "RequireFileChecksum[message=%s, file=%s, checksum=%s, type=%s, encoding=%s, normalizeLineSeparatorTo=%s, nonexistentFileMessage=%s, level=%s]",
103                getMessage(),
104                getFile(),
105                getChecksum(),
106                getType(),
107                encoding,
108                normalizeLineSeparatorTo,
109                getNonexistentFileMessage(),
110                getLevel());
111    }
112}