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
@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 )
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 repositoryId =
- IOUtils.toString( multipartBody.getAttachment( "repositoryId" ).getDataHandler().getInputStream() );
-
- Attachment generatePomAttachment = multipartBody.getAttachment( "generatePom" );
- boolean generatePom = BooleanUtils.toBoolean( generatePomAttachment == null
- ? Boolean.FALSE.toString()
- : IOUtils.toString(
- generatePomAttachment.getDataHandler().getInputStream() ) );
-
- String classifier =
- IOUtils.toString( multipartBody.getAttachment( "classifier" ).getDataHandler().getInputStream() );
-
- Attachment pomFileAttachment = multipartBody.getAttachment( "pomFile" );
-
- boolean pomFile = BooleanUtils.toBoolean( pomFileAttachment == null
- ? Boolean.FALSE.toString()
- : IOUtils.toString(
- pomFileAttachment.getDataHandler().getInputStream() ) );
-
- log.info( "uploading file:" + groupId + ":" + artifactId + ":" + version );
+ String groupId = getStringValue( multipartBody, "groupId" );
+
+ String artifactId = getStringValue( multipartBody, "artifactId" );
+
+ String version = getStringValue( multipartBody, "version" );
+
+ String packaging = getStringValue( multipartBody, "packaging" );
+
+ String repositoryId = getStringValue( multipartBody, "repositoryId" );
+
+ boolean generatePom = BooleanUtils.toBoolean( getStringValue( multipartBody, "generatePom" ) );
+
+ String classifier = getStringValue( multipartBody, "classifier" );
+ boolean pomFile = BooleanUtils.toBoolean( getStringValue( multipartBody, "pomFile" ) );
+
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 );
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 )
{
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;
+ }
+
}
*/
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
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;
}
{
private String name;
+ private String serverFileName;
+
private long size;
private String url;
// 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;
{
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();
+ }
}
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;
+ });
}
});
<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">
</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>