use MultipartBody as a parameter for the FileUpload service and parse various parameters

git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1307153 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Olivier Lamy 2012-03-29 22:52:48 +00:00
parent 08002b92eb
commit f832e697a0
4 changed files with 56 additions and 8 deletions

View File

@ -21,7 +21,10 @@ package org.apache.archiva.webapp.ui.services.api;
import org.apache.archiva.rest.api.services.ArchivaRestServiceException; import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
import org.apache.archiva.webapp.ui.services.model.FileMetadata; import org.apache.archiva.webapp.ui.services.model.FileMetadata;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.BooleanUtils;
import org.apache.commons.lang.SystemUtils; import org.apache.commons.lang.SystemUtils;
import org.apache.cxf.jaxrs.ext.multipart.Attachment;
import org.apache.cxf.jaxrs.ext.multipart.MultipartBody;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -70,6 +73,44 @@ public class DefaultFileUploadService
} }
} }
public FileMetadata post( MultipartBody multipartBody )
throws ArchivaRestServiceException
{
try
{
String groupId =
IOUtils.toString( multipartBody.getAttachment( "groupId" ).getDataHandler().getInputStream() );
String artifactId =
IOUtils.toString( multipartBody.getAttachment( "artifactId" ).getDataHandler().getInputStream() );
String version =
IOUtils.toString( multipartBody.getAttachment( "version" ).getDataHandler().getInputStream() );
String packaging =
IOUtils.toString( multipartBody.getAttachment( "packaging" ).getDataHandler().getInputStream() );
boolean generatePom = BooleanUtils.toBoolean(
IOUtils.toString( multipartBody.getAttachment( "generatePom" ).getDataHandler().getInputStream() ) );
String classifier =
IOUtils.toString( multipartBody.getAttachment( "classifier" ).getDataHandler().getInputStream() );
log.info( "uploading file:" + groupId + ":" + artifactId + ":" + version );
Attachment file = multipartBody.getAttachment( "files[]" );
File tmpFile = File.createTempFile( "upload-artifact", "tmp" );
tmpFile.deleteOnExit();
IOUtils.copy( file.getDataHandler().getInputStream(), new FileOutputStream( tmpFile ) );
FileMetadata fileMetadata = new FileMetadata( "thefile", tmpFile.length(), "theurl" );
fileMetadata.setDeleteUrl( tmpFile.getName() );
return fileMetadata;
}
catch ( IOException e )
{
throw new ArchivaRestServiceException( e.getMessage(),
Response.Status.INTERNAL_SERVER_ERROR.getStatusCode() );
}
}
public Boolean deleteFile( String fileName ) public Boolean deleteFile( String fileName )
throws ArchivaRestServiceException throws ArchivaRestServiceException
{ {

View File

@ -20,6 +20,7 @@ package org.apache.archiva.webapp.ui.services.api;
import org.apache.archiva.rest.api.services.ArchivaRestServiceException; import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
import org.apache.archiva.webapp.ui.services.model.FileMetadata; import org.apache.archiva.webapp.ui.services.model.FileMetadata;
import org.apache.cxf.jaxrs.ext.multipart.MultipartBody;
import org.codehaus.plexus.redback.authorization.RedbackAuthorization; import org.codehaus.plexus.redback.authorization.RedbackAuthorization;
import javax.ws.rs.Consumes; import javax.ws.rs.Consumes;
@ -28,7 +29,6 @@ import javax.ws.rs.POST;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.PathParam; import javax.ws.rs.PathParam;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
/** /**
@ -39,15 +39,15 @@ import javax.ws.rs.core.MediaType;
public interface FileUploadService public interface FileUploadService
{ {
//@Path( "upload" )
@POST @POST
@Consumes( MediaType.MULTIPART_FORM_DATA ) @Consumes( MediaType.MULTIPART_FORM_DATA )
@Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } ) @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } )
@RedbackAuthorization( noRestriction = true ) @RedbackAuthorization( noRestriction = true )
FileMetadata post( @QueryParam( "g" ) String groupId, @QueryParam( "a" ) String artifactId, //FileMetadata post( @FormParam( "groupId" ) String groupId, @FormParam( "artifactId" ) String artifactId,
@QueryParam( "v" ) String version, @QueryParam( "p" ) String packaging, // @FormParam( "version" ) String version, @FormParam( "packaging" ) String packaging,
@QueryParam( "c" ) String classifier, @QueryParam( "r" ) String repositoryId, // @FormParam( "classifier" ) String classifier, @FormParam( "repositoryId" ) String repositoryId,
@QueryParam( "generatePom" ) String generatePom ) // @FormParam( "generatePom" ) String generatePom )
FileMetadata post( MultipartBody multipartBody )// @Multipart( value = "files[]", type = "*/*" ) Attachment file )
throws ArchivaRestServiceException; throws ArchivaRestServiceException;
@Path( "{fileName}" ) @Path( "{fileName}" )

View File

@ -27,7 +27,14 @@ define("archiva.artifacts-management",["jquery","i18n","order!utils","order!jque
dataType: 'json', dataType: 'json',
success: function(data) { success: function(data) {
mainContent.html($("#file-upload-tmpl" ).tmpl({managedRepositories: data})); mainContent.html($("#file-upload-tmpl" ).tmpl({managedRepositories: data}));
$('#fileupload').fileupload(); $('#fileupload').fileupload({
add: function (e, data) {
data.timeStamp = $.now();
$.blueimpUI.fileupload.prototype
.options.add.call(this, e, data);
}
}
);
} }
}); });

View File

@ -94,7 +94,7 @@
{% for (var i=0, file; file=o.files[i]; i++) { %} {% for (var i=0, file; file=o.files[i]; i++) { %}
<tr class="template-upload"> <tr class="template-upload">
<td class="name"><span>{%=file.name%}</span></td> <td class="name"><span>{%=file.name%}</span></td>
<td><input type="text" placeholder="classifier"></td> <td><input type="text" id="classifier" name="classifier" placeholder="classifier" value=""></td>
<td class="size"><span>{%=o.formatFileSize(file.size)%}</span></td> <td class="size"><span>{%=o.formatFileSize(file.size)%}</span></td>
{% if (file.error) { %} {% if (file.error) { %}
<td class="error" colspan="2"> <td class="error" colspan="2">