001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.mail2.jakarta.activation;
019
020import java.io.IOException;
021import java.io.InputStream;
022import java.io.OutputStream;
023import java.nio.file.Files;
024import java.nio.file.OpenOption;
025import java.nio.file.Path;
026import java.util.Objects;
027
028import jakarta.activation.DataSource;
029import jakarta.activation.FileTypeMap;
030import jakarta.activation.MimetypesFileTypeMap;
031
032/**
033 * A JavaBeans Activation Framework {@link DataSource} that encapsulates a {@link Path}. It provides data typing services via a {@link FileTypeMap} object.
034 *
035 * @see jakarta.activation.DataSource
036 * @see jakarta.activation.FileTypeMap
037 * @see jakarta.activation.MimetypesFileTypeMap
038 *
039 * @since 1.6.0
040 */
041public final class PathDataSource implements DataSource {
042
043    /**
044     * The source.
045     */
046    private final Path path;
047
048    /**
049     * Defaults to {@link FileTypeMap#getDefaultFileTypeMap()}.
050     */
051    private final FileTypeMap typeMap;
052
053    /**
054     * NIO options to open the source Path.
055     */
056    private final OpenOption[] options;
057
058    /**
059     * Creates a new instance from a Path.
060     * <p>
061     * The file will not actually be opened until a method is called that requires the path to be opened.
062     * </p>
063     * <p>
064     * The type map defaults to {@link FileTypeMap#getDefaultFileTypeMap()}.
065     *
066     * @param path the path
067     */
068    public PathDataSource(final Path path) {
069        this(path, FileTypeMap.getDefaultFileTypeMap());
070    }
071
072    /**
073     * Creates a new instance from a Path.
074     * <p>
075     * The file will not actually be opened until a method is called that requires the path to be opened.
076     * </p>
077     *
078     * @param path    the path, non-null.
079     * @param typeMap the type map, non-null.
080     * @param options options for opening file streams.
081     */
082    public PathDataSource(final Path path, final FileTypeMap typeMap, final OpenOption... options) {
083        this.path = Objects.requireNonNull(path, "path");
084        this.typeMap = Objects.requireNonNull(typeMap, "typeMap");
085        this.options = options;
086    }
087
088    /**
089     * Gets the MIME type of the data as a String. This method uses the currently installed FileTypeMap. If there is no FileTypeMap explicitly set, the
090     * FileDataSource will call the {@link FileTypeMap#getDefaultFileTypeMap} method to acquire a default FileTypeMap.
091     * <p>
092     * By default, the {@link FileTypeMap} used will be a {@link MimetypesFileTypeMap}.
093     * </p>
094     *
095     * @return the MIME Type
096     * @see FileTypeMap#getDefaultFileTypeMap
097     */
098    @Override
099    public String getContentType() {
100        return typeMap.getContentType(getName());
101    }
102
103    /**
104     * Gets an InputStream representing the the data and will throw an IOException if it can not do so. This method will return a new instance of InputStream
105     * with each invocation.
106     *
107     * @return an InputStream
108     */
109    @Override
110    public InputStream getInputStream() throws IOException {
111        return Files.newInputStream(path, options);
112    }
113
114    /**
115     * Gets the <em>name</em> of this object. The FileDataSource will return the file name of the object.
116     *
117     * @return the name of the object or null.
118     * @see jakarta.activation.DataSource
119     */
120    @Override
121    public String getName() {
122        return Objects.toString(path.getFileName(), null);
123    }
124
125    /**
126     * Gets an OutputStream representing the the data and will throw an IOException if it can not do so. This method will return a new instance of OutputStream
127     * with each invocation.
128     *
129     * @return an OutputStream
130     */
131    @Override
132    public OutputStream getOutputStream() throws IOException {
133        return Files.newOutputStream(path, options);
134    }
135
136    /**
137     * Gets the File object that corresponds to this PathDataSource.
138     *
139     * @return the File object for the file represented by this object.
140     */
141    public Path getPath() {
142        return path;
143    }
144
145}