cleanup upload rest service

git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1307530 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Olivier Lamy 2012-03-30 16:47:34 +00:00
parent c0d576e45e
commit 8312f1e85a
5 changed files with 154 additions and 62 deletions

View File

@ -30,12 +30,14 @@ import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* @author Olivier Lamy
@ -49,28 +51,11 @@ public class DefaultFileUploadService
@Context
private HttpServletRequest httpServletRequest;
@Context
private HttpServletResponse httpServletResponse;
public FileMetadata post( String groupId, String artifactId, String version, String packaging, String classifier,
String repositoryId, String generatePom )
throws ArchivaRestServiceException
private String getStringValue( MultipartBody multipartBody, String attachmentId )
throws IOException
{
log.info( "uploading file:" + groupId + ":" + artifactId + ":" + version );
try
{
File file = File.createTempFile( "upload-artifact", "tmp" );
file.deleteOnExit();
IOUtils.copy( httpServletRequest.getInputStream(), new FileOutputStream( file ) );
FileMetadata fileMetadata = new FileMetadata( "thefile", file.length(), "theurl" );
fileMetadata.setDeleteUrl( file.getName() );
return fileMetadata;
}
catch ( IOException e )
{
throw new ArchivaRestServiceException( e.getMessage(),
Response.Status.INTERNAL_SERVER_ERROR.getStatusCode() );
}
Attachment attachment = multipartBody.getAttachment( attachmentId );
return attachment == null ? "" : IOUtils.toString( attachment.getDataHandler().getInputStream() );
}
public FileMetadata post( MultipartBody multipartBody )
@ -79,40 +64,31 @@ public class DefaultFileUploadService
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() );
String groupId = getStringValue( multipartBody, "groupId" );
String repositoryId =
IOUtils.toString( multipartBody.getAttachment( "repositoryId" ).getDataHandler().getInputStream() );
String artifactId = getStringValue( multipartBody, "artifactId" );
Attachment generatePomAttachment = multipartBody.getAttachment( "generatePom" );
boolean generatePom = BooleanUtils.toBoolean( generatePomAttachment == null
? Boolean.FALSE.toString()
: IOUtils.toString(
generatePomAttachment.getDataHandler().getInputStream() ) );
String version = getStringValue( multipartBody, "version" );
String classifier =
IOUtils.toString( multipartBody.getAttachment( "classifier" ).getDataHandler().getInputStream() );
String packaging = getStringValue( multipartBody, "packaging" );
Attachment pomFileAttachment = multipartBody.getAttachment( "pomFile" );
String repositoryId = getStringValue( multipartBody, "repositoryId" );
boolean pomFile = BooleanUtils.toBoolean( pomFileAttachment == null
? Boolean.FALSE.toString()
: IOUtils.toString(
pomFileAttachment.getDataHandler().getInputStream() ) );
boolean generatePom = BooleanUtils.toBoolean( getStringValue( multipartBody, "generatePom" ) );
String classifier = getStringValue( multipartBody, "classifier" );
boolean pomFile = BooleanUtils.toBoolean( getStringValue( multipartBody, "pomFile" ) );
log.info( "uploading file:" + groupId + ":" + artifactId + ":" + version );
Attachment file = multipartBody.getAttachment( "files[]" );
//Content-Disposition: form-data; name="files[]"; filename="org.apache.karaf.features.command-2.2.2.jar"
String fileName = file.getContentDisposition().getParameter( "filename" );
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 fileMetadata = new FileMetadata( fileName, tmpFile.length(), "theurl" );
fileMetadata.setServerFileName( tmpFile.getName() );
fileMetadata.setGroupId( groupId );
fileMetadata.setArtifactId( artifactId );
fileMetadata.setVersion( version );
@ -123,6 +99,18 @@ public class DefaultFileUploadService
fileMetadata.setDeleteUrl( tmpFile.getName() );
fileMetadata.setRepositoryId( repositoryId );
fileMetadata.setPomFile( pomFile );
log.info( "uploading file:{}", fileMetadata );
List<FileMetadata> fileMetadatas =
(List<FileMetadata>) httpServletRequest.getSession().getAttribute( FILES_SESSION_KEY );
if ( fileMetadatas == null )
{
fileMetadatas = new ArrayList<FileMetadata>( 1 );
}
fileMetadatas.add( fileMetadata );
httpServletRequest.getSession().setAttribute( FILES_SESSION_KEY, fileMetadatas );
return fileMetadata;
}
catch ( IOException e )
@ -138,10 +126,21 @@ public class DefaultFileUploadService
{
File file = new File( SystemUtils.getJavaIoTmpDir(), fileName );
log.debug( "delete file:{},exists:{}", file.getPath(), file.exists() );
boolean removed = getSessionFileMetadatas().remove( new FileMetadata( fileName ) );
if ( file.exists() )
{
return file.delete();
}
return Boolean.FALSE;
}
public List<FileMetadata> getSessionFileMetadatas()
throws ArchivaRestServiceException
{
List<FileMetadata> fileMetadatas =
(List<FileMetadata>) httpServletRequest.getSession().getAttribute( FILES_SESSION_KEY );
return fileMetadatas == null ? Collections.<FileMetadata>emptyList() : fileMetadatas;
}
}

View File

@ -19,17 +19,20 @@ package org.apache.archiva.webapp.ui.services.api;
*/
import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
import org.apache.archiva.security.common.ArchivaRoleConstants;
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 javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.List;
/**
* @author Olivier Lamy
@ -39,21 +42,27 @@ import javax.ws.rs.core.MediaType;
public interface FileUploadService
{
String FILES_SESSION_KEY = FileUploadService.class.getName() + "files_session_key";
@POST
@Consumes( MediaType.MULTIPART_FORM_DATA )
@Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } )
@RedbackAuthorization( noRestriction = true )
//FileMetadata post( @FormParam( "groupId" ) String groupId, @FormParam( "artifactId" ) String artifactId,
// @FormParam( "version" ) String version, @FormParam( "packaging" ) String packaging,
// @FormParam( "classifier" ) String classifier, @FormParam( "repositoryId" ) String repositoryId,
// @FormParam( "generatePom" ) String generatePom )
FileMetadata post( MultipartBody multipartBody )// @Multipart( value = "files[]", type = "*/*" ) Attachment file )
@RedbackAuthorization( permissions = ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD )
FileMetadata post( MultipartBody multipartBody )
throws ArchivaRestServiceException;
@Path( "{fileName}" )
@DELETE
@Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } )
@RedbackAuthorization( noRestriction = true )
@RedbackAuthorization( permissions = ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD )
Boolean deleteFile( @PathParam( "fileName" ) String fileName )
throws ArchivaRestServiceException;
@Path( "sessionFileMetadatas" )
@GET
@Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } )
@RedbackAuthorization( permissions = ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD )
List<FileMetadata> getSessionFileMetadatas()
throws ArchivaRestServiceException;
}

View File

@ -31,6 +31,8 @@ public class FileMetadata
{
private String name;
private String serverFileName;
private long size;
private String url;
@ -62,9 +64,14 @@ public class FileMetadata
// no op
}
public FileMetadata( String filename, long size, String url )
public FileMetadata( String serverFileName )
{
this.name = filename;
this.serverFileName = serverFileName;
}
public FileMetadata( String name, long size, String url )
{
this.name = name;
this.size = size;
this.url = url;
this.deleteUrl = url;
@ -211,4 +218,65 @@ public class FileMetadata
{
this.pomFile = pomFile;
}
public String getServerFileName()
{
return serverFileName;
}
public void setServerFileName( String serverFileName )
{
this.serverFileName = serverFileName;
}
@Override
public boolean equals( Object o )
{
if ( this == o )
{
return true;
}
if ( !( o instanceof FileMetadata ) )
{
return false;
}
FileMetadata that = (FileMetadata) o;
if ( !serverFileName.equals( that.serverFileName ) )
{
return false;
}
return true;
}
@Override
public int hashCode()
{
return serverFileName.hashCode();
}
@Override
public String toString()
{
final StringBuilder sb = new StringBuilder();
sb.append( "FileMetadata" );
sb.append( "{name='" ).append( name ).append( '\'' );
sb.append( ", size=" ).append( size );
sb.append( ", url='" ).append( url ).append( '\'' );
sb.append( ", deleteUrl='" ).append( deleteUrl ).append( '\'' );
sb.append( ", deleteType='" ).append( deleteType ).append( '\'' );
sb.append( ", errorKey='" ).append( errorKey ).append( '\'' );
sb.append( ", groupId='" ).append( groupId ).append( '\'' );
sb.append( ", artifactId='" ).append( artifactId ).append( '\'' );
sb.append( ", version='" ).append( version ).append( '\'' );
sb.append( ", packaging='" ).append( packaging ).append( '\'' );
sb.append( ", generatePom=" ).append( generatePom );
sb.append( ", classifier='" ).append( classifier ).append( '\'' );
sb.append( ", repositoryId='" ).append( repositoryId ).append( '\'' );
sb.append( ", pomFile=" ).append( pomFile );
sb.append( '}' );
return sb.toString();
}
}

View File

@ -34,30 +34,46 @@ define("archiva.artifacts-management",["jquery","i18n","order!utils","order!jque
this.packaging=ko.observable();
this.generatePom=ko.observable();
saveArtifacts=function(){
$.log("saveArtifacts");
}
}
displayUploadArtifact=function(){
var mainContent=$("#main-content");
mainContent.html(mediumSpinnerImg());
mainContent.html($("#file-upload-screen" ).html());
$.ajax("restServices/archivaServices/browseService/userRepositories", {
type: "GET",
dataType: 'json',
success: function(data) {
mainContent.html($("#file-upload-tmpl" ).tmpl({managedRepositories: data}));
var artifactUploadViewModel=new ArtifactUploadViewModel(data);
ko.applyBindings(artifactUploadViewModel,mainContent.find("#file-upload-main" ).get(0));
mainContent.find("#fileupload-save-files" ).on("click",function(){
$.log("fileupload-save-files click");
});
$('#fileupload').fileupload({
add: function (e, data) {
data.timeStamp = $.now();
data.formData = {
groupId: artifactUploadViewModel.groupId(),
artifactId: artifactUploadViewModel.artifactId(),
version: artifactUploadViewModel.version(),
packaging: artifactUploadViewModel.packaging(),
generatePom: artifactUploadViewModel.generatePom(),
repositoryId: artifactUploadViewModel.repositoryId()
};
$.log("fileupload add file");
$.blueimpUI.fileupload.prototype.options.add.call(this, e, data);
}
}
);
$('#fileupload').bind('fileuploadsubmit', function (e, data) {
var pomFile = data.context.find('#pomFile' ).val();
var classifier = data.context.find('#classifier' ).val();
$.log("pomFile:"+pomFile+",classifier:"+classifier);
data.formData.pomFile = pomFile;
data.formData.classifier = classifier;
});
}
});

View File

@ -1,5 +1,5 @@
<script id="file-upload-screen" type="text/html">
<div id="file-upload-main" ddata-bind='template:{name:"file-upload-tmpl"}'></div>
<div id="file-upload-main" data-bind='template:{name:"file-upload-tmpl"}'></div>
</script>
<script id="file-upload-tmpl" type="text/html">
@ -84,7 +84,7 @@
</div>
<div>
<a href="#" id="fileupload-save-files">
<span class="btn btn-info">
<span class="btn btn-info" data-bind='click: saveArtifacts'>
<i class="icon-file icon-white"></i>
<span>${$.i18n.prop('fileupload.save')}</span>
</span>