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.spi.connector.transport;
020
021import java.io.ByteArrayInputStream;
022import java.io.File;
023import java.io.IOException;
024import java.io.InputStream;
025import java.net.URI;
026import java.nio.charset.StandardCharsets;
027import java.nio.file.Files;
028
029/**
030 * A task to upload a resource to the remote repository.
031 *
032 * @see Transporter#put(PutTask)
033 */
034public final class PutTask extends TransportTask {
035
036    private File dataFile;
037
038    private byte[] dataBytes = EMPTY;
039
040    /**
041     * Creates a new task for the specified remote resource.
042     *
043     * @param location The relative location of the resource in the remote repository, must not be {@code null}.
044     */
045    public PutTask(URI location) {
046        setLocation(location);
047    }
048
049    /**
050     * Opens an input stream for the data to be uploaded. The length of the stream can be queried via
051     * {@link #getDataLength()}. It's the responsibility of the caller to close the provided stream.
052     *
053     * @return The input stream for the data, never {@code null}. The stream is unbuffered.
054     * @throws IOException If the stream could not be opened.
055     */
056    public InputStream newInputStream() throws IOException {
057        if (dataFile != null) {
058            return Files.newInputStream(dataFile.toPath());
059        }
060        return new ByteArrayInputStream(dataBytes);
061    }
062
063    /**
064     * Gets the total number of bytes to be uploaded.
065     *
066     * @return The total number of bytes to be uploaded.
067     */
068    public long getDataLength() {
069        if (dataFile != null) {
070            return dataFile.length();
071        }
072        return dataBytes.length;
073    }
074
075    /**
076     * Gets the file (if any) with the data to be uploaded.
077     *
078     * @return The data file or {@code null} if the data resides in memory.
079     */
080    public File getDataFile() {
081        return dataFile;
082    }
083
084    /**
085     * Sets the file with the data to be uploaded. To upload some data residing already in memory, use
086     * {@link #setDataString(String)} or {@link #setDataBytes(byte[])}.
087     *
088     * @param dataFile The data file, may be {@code null} if the resource data is provided directly from memory.
089     * @return This task for chaining, never {@code null}.
090     */
091    public PutTask setDataFile(File dataFile) {
092        this.dataFile = dataFile;
093        dataBytes = EMPTY;
094        return this;
095    }
096
097    /**
098     * Sets the binary data to be uploaded.
099     *
100     * @param bytes The binary data, may be {@code null}.
101     * @return This task for chaining, never {@code null}.
102     */
103    public PutTask setDataBytes(byte[] bytes) {
104        this.dataBytes = (bytes != null) ? bytes : EMPTY;
105        dataFile = null;
106        return this;
107    }
108
109    /**
110     * Sets the textual data to be uploaded. The text is encoded using UTF-8 before transmission.
111     *
112     * @param str The textual data, may be {@code null}.
113     * @return This task for chaining, never {@code null}.
114     */
115    public PutTask setDataString(String str) {
116        return setDataBytes((str != null) ? str.getBytes(StandardCharsets.UTF_8) : null);
117    }
118
119    /**
120     * Sets the listener that is to be notified during the transfer.
121     *
122     * @param listener The listener to notify of progress, may be {@code null}.
123     * @return This task for chaining, never {@code null}.
124     */
125    public PutTask setListener(TransportListener listener) {
126        super.setListener(listener);
127        return this;
128    }
129
130    @Override
131    public String toString() {
132        return ">> " + getLocation();
133    }
134}