All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.addicticks.maven.httpsupload.mojo.UploadMultipleMojo Maven / Gradle / Ivy

/*
 * 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.HashMap;
import java.util.List;
import java.util.Map;
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: *

    *
  • 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" * or if multiple files are allowed: "file1", "file2", etc. * The value must be unique among the files to be uploaded. *
    Type: String, Required: Yes *

  • *
  • file. File to upload. *
    Type: java.io.File, Required: Yes *

  • *
  • 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 *

  • *
  • 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 *
  • *
* * *
*
**/ @Parameter(required=true) private List uploadFiles; @Override public void execute() throws MojoExecutionException, MojoFailureException { validateUploadFiles(); HttpsFileUploaderConfig config = getConfig(); Map newMap = new HashMap<>(); 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()); } if (newMap.put(e.getFormFieldName(), f) != null) { throw new MojoExecutionException("The value for formFieldName must be unique among uploadFiles. (value = \"" + e.getFormFieldName() + "\")"); } } } upload(config, newMap, 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