com.addicticks.maven.httpsupload.mojo.UploadMultipleMojo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of httpsupload-maven-plugin Show documentation
Show all versions of httpsupload-maven-plugin Show documentation
Maven plugin which allows to upload one or several files
to a remote site by using HTTP/HTTPS.
/*
* Copyright Addicticks 2015.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.addicticks.maven.httpsupload.mojo;
import com.addicticks.net.httpsupload.HttpsFileUploaderConfig;
import com.addicticks.net.httpsupload.UploadItem;
import com.addicticks.net.httpsupload.UploadItemFile;
import java.util.ArrayList;
import java.util.List;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
/**
* Uploads multiple file to a remote server using HTTP or HTTPS. This
* goal should only be used with servers that accepts multiple file
* to be uploaded in a single operation.
*
* Files are sent using POST method and using
* {@code multipart/form-data} encoding (RFC2388).
*
* @author Addicticks
*/
@Mojo( name = "multiple", requiresProject = false)
public class UploadMultipleMojo extends UploadAbstractMojo {
/**
* Files to upload.
*
*
* Example:
*
* <uploadFiles>
* <uploadFile>
* <formFieldName>file1</formFieldName>
* <file>${project.build.directory}/myfile.jar</file>
* </uploadFile>
* <uploadFile>
* <formFieldName>file2</formFieldName>
* <file>c://mydir/whatsup.dat</file>
* <hintFilename>whatsup.xls</hintFilename>
* </uploadFile>
* </uploadFiles>
*
*
*
* There must be at least one uploadFile
element.
*
*
* The elements of an uploadFile
are as follows:
*
* file
. File to upload.
*
Type: java.io.File
, Required: Yes
*
* hintFilename
. Hint given to the server as to what the
* server should name the file. If this value is not set explicitly
* it will be derived from the filename of the {@code file} element.
*
Type: String
, Required: No
*
* mimeType
. This is the http Content-Type
* used when the file is uploaded. If not given explicitly here it
* will be derived from the file name extension using Java's
* {@link java.net.URLConnection#guessContentTypeFromName(java.lang.String)}
* method.
*
Type: String
, Required: No
*
* formFieldName
. The form field name to POST the file to.
* This value depends entirely on
* the server you're uploading to. Typically such a field is named "file".
* There's no requirement that the value is unique among the files
* to be uploaded. If you don't know what this value should be
* then leave it out.
*
Type: String
, Required: No
*
*
*
*
*
*
**/
@Parameter(required=true)
private List uploadFiles;
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
validateUploadFiles();
HttpsFileUploaderConfig config = getConfig();
List newList = new ArrayList<>();
if (uploadFiles != null) {
for (UploadFile e : uploadFiles) {
UploadItem f;
boolean hintFileNameSpecified = true;
boolean mimeTypeSpecified = true;
if (e.getHintFilename() == null) {
hintFileNameSpecified = false;
}
if (e.getMimeType() == null) {
mimeTypeSpecified = false;
}
if (mimeTypeSpecified && hintFileNameSpecified) {
f = new UploadItemFile(
e.getFile(),
e.getHintFilename(),
e.getMimeType());
} else if (!mimeTypeSpecified && hintFileNameSpecified) {
f = new UploadItemFile(
e.getFile(),
e.getHintFilename());
} else {
f = new UploadItemFile(
e.getFile());
}
f.setFormFieldName(e.getFormFieldName());
newList.add(f);
}
}
upload(config, newList, extraFields);
}
private void validateUploadFiles() throws MojoExecutionException {
for (UploadFile e : uploadFiles) {
if (e.getFormFieldName() == null || e.getFormFieldName().isEmpty()) {
throw new MojoExecutionException("'formFieldName' is required on 'uploadFile'");
}
if (e.getFile() == null) {
throw new MojoExecutionException("'file' is required on 'uploadFile'");
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy