1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.eclipse.aether.spi.connector.transport;
20
21 import java.io.*;
22 import java.net.URI;
23 import java.nio.charset.StandardCharsets;
24 import java.nio.file.Files;
25 import java.nio.file.Path;
26
27 /**
28 * A task to upload a resource to the remote repository.
29 *
30 * @see Transporter#put(PutTask)
31 */
32 public final class PutTask extends TransportTask {
33
34 private Path dataPath;
35
36 private byte[] dataBytes = EMPTY;
37
38 /**
39 * Creates a new task for the specified remote resource.
40 *
41 * @param location The relative location of the resource in the remote repository, must not be {@code null}.
42 */
43 public PutTask(URI location) {
44 setLocation(location);
45 }
46
47 /**
48 * Opens an input stream for the data to be uploaded. The length of the stream can be queried via
49 * {@link #getDataLength()}. It's the responsibility of the caller to close the provided stream.
50 *
51 * @return The input stream for the data, never {@code null}. The stream is unbuffered.
52 * @throws IOException If the stream could not be opened.
53 */
54 public InputStream newInputStream() throws IOException {
55 if (dataPath != null) {
56 return Files.newInputStream(dataPath);
57 }
58 return new ByteArrayInputStream(dataBytes);
59 }
60
61 /**
62 * Gets the total number of bytes to be uploaded.
63 *
64 * @return The total number of bytes to be uploaded.
65 */
66 public long getDataLength() {
67 if (dataPath != null) {
68 try {
69 return Files.size(dataPath);
70 } catch (IOException e) {
71 throw new UncheckedIOException(e);
72 }
73 }
74 return dataBytes.length;
75 }
76
77 /**
78 * Gets the file (if any) with the data to be uploaded.
79 *
80 * @return The data file or {@code null} if the data resides in memory.
81 * @deprecated Use {@link #getDataPath()} instead.
82 */
83 @Deprecated
84 public File getDataFile() {
85 return dataPath != null ? dataPath.toFile() : null;
86 }
87
88 /**
89 * Gets the file (if any) with the data to be uploaded.
90 *
91 * @return The data file or {@code null} if the data resides in memory.
92 * @since 2.0.0
93 */
94 public Path getDataPath() {
95 return dataPath;
96 }
97
98 /**
99 * Sets the file with the data to be uploaded. To upload some data residing already in memory, use
100 * {@link #setDataString(String)} or {@link #setDataBytes(byte[])}.
101 *
102 * @param dataFile The data file, may be {@code null} if the resource data is provided directly from memory.
103 * @return This task for chaining, never {@code null}.
104 * @deprecated Use {@link #setDataPath(Path)} instead.
105 */
106 @Deprecated
107 public PutTask setDataFile(File dataFile) {
108 return setDataPath(dataFile.toPath());
109 }
110
111 /**
112 * Sets the file with the data to be uploaded. To upload some data residing already in memory, use
113 * {@link #setDataString(String)} or {@link #setDataBytes(byte[])}.
114 *
115 * @param dataPath The data file, may be {@code null} if the resource data is provided directly from memory.
116 * @return This task for chaining, never {@code null}.
117 * @since 2.0.0
118 */
119 public PutTask setDataPath(Path dataPath) {
120 this.dataPath = dataPath;
121 dataBytes = EMPTY;
122 return this;
123 }
124
125 /**
126 * Sets the binary data to be uploaded.
127 *
128 * @param bytes The binary data, may be {@code null}.
129 * @return This task for chaining, never {@code null}.
130 */
131 public PutTask setDataBytes(byte[] bytes) {
132 this.dataBytes = (bytes != null) ? bytes : EMPTY;
133 dataPath = null;
134 return this;
135 }
136
137 /**
138 * Sets the textual data to be uploaded. The text is encoded using UTF-8 before transmission.
139 *
140 * @param str The textual data, may be {@code null}.
141 * @return This task for chaining, never {@code null}.
142 */
143 public PutTask setDataString(String str) {
144 return setDataBytes((str != null) ? str.getBytes(StandardCharsets.UTF_8) : null);
145 }
146
147 /**
148 * Sets the listener that is to be notified during the transfer.
149 *
150 * @param listener The listener to notify of progress, may be {@code null}.
151 * @return This task for chaining, never {@code null}.
152 */
153 public PutTask setListener(TransportListener listener) {
154 super.setListener(listener);
155 return this;
156 }
157
158 @Override
159 public String toString() {
160 return ">> " + getLocation();
161 }
162 }