1 package org.apache.maven.archiva.web.action;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
23 import java.io.FileInputStream;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.Calendar;
28 import java.util.Collections;
29 import java.util.List;
31 import org.apache.archiva.checksum.ChecksumAlgorithm;
32 import org.apache.archiva.checksum.ChecksummedFile;
33 import org.apache.maven.archiva.common.utils.VersionComparator;
34 import org.apache.maven.archiva.common.utils.VersionUtil;
35 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
36 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
37 import org.apache.maven.archiva.model.ArchivaProjectModel;
38 import org.apache.maven.archiva.model.ArchivaRepositoryMetadata;
39 import org.apache.maven.archiva.model.ArtifactReference;
40 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
41 import org.apache.maven.archiva.repository.RepositoryContentFactory;
42 import org.apache.maven.archiva.repository.RepositoryException;
43 import org.apache.maven.archiva.repository.RepositoryNotFoundException;
44 import org.apache.maven.archiva.repository.scanner.RepositoryContentConsumers;
45 import org.apache.maven.archiva.repository.metadata.MetadataTools;
46 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataException;
47 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataReader;
48 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataWriter;
49 import org.apache.maven.archiva.repository.project.ProjectModelException;
50 import org.apache.maven.archiva.repository.project.ProjectModelWriter;
51 import org.apache.maven.archiva.repository.project.writers.ProjectModel400Writer;
52 import org.apache.maven.archiva.security.ArchivaSecurityException;
53 import org.apache.maven.archiva.security.PrincipalNotFoundException;
54 import org.apache.maven.archiva.security.UserRepositories;
55 import org.apache.maven.archiva.security.ArchivaXworkUser;
56 import org.codehaus.plexus.xwork.action.PlexusActionSupport;
58 import com.opensymphony.xwork.ActionContext;
59 import com.opensymphony.xwork.Preparable;
60 import com.opensymphony.xwork.Validateable;
63 * Upload an artifact using Jakarta file upload in webwork. If set by the user a pom will also be generated. Metadata
64 * will also be updated if one exists, otherwise it would be created.
66 * @author <a href="mailto:wsmoak@apache.org">Wendy Smoak</a>
67 * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
68 * @plexus.component role="com.opensymphony.xwork.Action" role-hint="uploadAction"
70 public class UploadAction
71 extends PlexusActionSupport
72 implements Validateable, Preparable
77 private RepositoryContentConsumers consumers;
80 * The groupId of the artifact to be deployed.
82 private String groupId;
85 * The artifactId of the artifact to be deployed.
87 private String artifactId;
90 * The version of the artifact to be deployed.
92 private String version;
95 * The packaging of the artifact to be deployed.
97 private String packaging;
100 * The classifier of the artifact to be deployed.
102 private String classifier;
105 * The temporary file representing the artifact to be deployed.
107 private File artifactFile;
110 * The content type of the artifact to be deployed.
112 private String artifactContentType;
115 * The original filename of the uploaded artifact file.
117 private String artifactFilename;
120 * The temporary file representing the pom to be deployed alongside the artifact.
122 private File pomFile;
125 * The content type of the pom file.
127 private String pomContentType;
130 * The original filename of the uploaded pom file.
132 private String pomFilename;
135 * The repository where the artifact is to be deployed.
137 private String repositoryId;
140 * Flag whether to generate a pom for the artifact or not.
142 private boolean generatePom;
145 * List of managed repositories to deploy to.
147 private List<String> managedRepoIdList;
150 * @plexus.requirement
152 private UserRepositories userRepositories;
155 * @plexus.requirement role-hint="default"
157 private ArchivaConfiguration configuration;
160 * @plexus.requirement
162 private RepositoryContentFactory repositoryFactory;
164 private ChecksumAlgorithm[] algorithms = new ChecksumAlgorithm[] { ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 };
166 private ProjectModelWriter pomWriter = new ProjectModel400Writer();
168 public void setArtifact( File file )
170 this.artifactFile = file;
173 public void setArtifactContentType( String contentType )
175 this.artifactContentType = contentType;
178 public void setArtifactFileName( String filename )
180 this.artifactFilename = filename;
183 public void setPom( File file )
188 public void setPomContentType( String contentType )
190 this.pomContentType = contentType;
193 public void setPomFileName( String filename )
195 this.pomFilename = filename;
198 public String getGroupId()
203 public void setGroupId( String groupId )
205 this.groupId = groupId;
208 public String getArtifactId()
213 public void setArtifactId( String artifactId )
215 this.artifactId = artifactId;
218 public String getVersion()
223 public void setVersion( String version )
225 this.version = version;
228 public String getPackaging()
233 public void setPackaging( String packaging )
235 this.packaging = packaging;
238 public String getClassifier()
243 public void setClassifier( String classifier )
245 this.classifier = classifier;
248 public String getRepositoryId()
253 public void setRepositoryId( String repositoryId )
255 this.repositoryId = repositoryId;
258 public boolean isGeneratePom()
263 public void setGeneratePom( boolean generatePom )
265 this.generatePom = generatePom;
268 public List<String> getManagedRepoIdList()
270 return managedRepoIdList;
273 public void setManagedRepoIdList( List<String> managedRepoIdList )
275 this.managedRepoIdList = managedRepoIdList;
278 public void prepare()
281 new ArrayList<String>( configuration.getConfiguration().getManagedRepositoriesAsMap().keySet() );
284 public String input()
289 public String doUpload()
293 ManagedRepositoryConfiguration repoConfig =
294 configuration.getConfiguration().findManagedRepositoryById( repositoryId );
296 ArtifactReference artifactReference = new ArtifactReference();
297 artifactReference.setArtifactId( artifactId );
298 artifactReference.setGroupId( groupId );
299 artifactReference.setVersion( version );
300 artifactReference.setClassifier( classifier );
301 artifactReference.setType( packaging );
303 ManagedRepositoryContent repository = repositoryFactory.getManagedRepositoryContent( repositoryId );
305 String artifactPath = repository.toPath( artifactReference );
307 int lastIndex = artifactPath.lastIndexOf( '/' );
309 File targetPath = new File( repoConfig.getLocation(), artifactPath.substring( 0, lastIndex ) );
311 if ( !targetPath.exists() )
318 copyFile( artifactFile, targetPath, artifactPath.substring( lastIndex + 1 ) );
320 catch ( IOException ie )
322 addActionError( "Error encountered while uploading file: " + ie.getMessage() );
330 createPom( targetPath, artifactPath.substring( lastIndex + 1 ) );
332 catch ( IOException ie )
334 addActionError( "Error encountered while writing pom file: " + ie.getMessage() );
337 catch ( ProjectModelException pe )
339 addActionError( "Error encountered while generating pom file: " + pe.getMessage() );
344 if ( pomFile != null && pomFile.length() > 0 )
349 String targetFilename = artifactPath.substring( lastIndex + 1 ).replaceAll( packaging, "pom" );
350 copyFile( pomFile, targetPath, targetFilename );
352 catch ( IOException ie )
354 addActionError( "Error encountered while uploading pom file: " + ie.getMessage() );
360 updateMetadata( getMetadata( targetPath.getAbsolutePath() ) );
362 String msg = "Artifact \'" + groupId + ":" + artifactId + ":" + version +
363 "\' was successfully deployed to repository \'" + repositoryId + "\'";
365 //TODO: MRM-810 (this writes to archiva.log, should be audit.log)
366 getLogger().info( msg + " by " + getPrincipal() );
368 //TODO: MRM-785 (success message does not display on web page)
369 addActionMessage( msg );
371 consumers.executeConsumers( repoConfig, repository.toFile( artifactReference ) );
375 catch ( RepositoryNotFoundException re )
377 addActionError( "Target repository cannot be found: " + re.getMessage() );
380 catch ( RepositoryException rep )
382 addActionError( "Repository exception: " + rep.getMessage() );
387 private String getPrincipal()
389 return ArchivaXworkUser.getActivePrincipal( ActionContext.getContext().getSession() );
392 private void copyFile( File sourceFile, File targetPath, String targetFilename )
395 FileOutputStream out = new FileOutputStream( new File( targetPath, targetFilename ) );
399 FileInputStream input = new FileInputStream( sourceFile );
401 while ( ( i = input.read() ) != -1 )
413 private void createPom( File targetPath, String filename )
414 throws IOException, ProjectModelException
416 ArchivaProjectModel projectModel = new ArchivaProjectModel();
417 projectModel.setGroupId( groupId );
418 projectModel.setArtifactId( artifactId );
419 projectModel.setVersion( version );
420 projectModel.setPackaging( packaging );
422 File pomFile = new File( targetPath, filename.replaceAll( packaging, "pom" ) );
424 pomWriter.write( projectModel, pomFile );
427 private File getMetadata( String targetPath )
429 String artifactPath = targetPath.substring( 0, targetPath.lastIndexOf( '/' ) );
431 return new File( artifactPath, MetadataTools.MAVEN_METADATA );
435 * Update artifact level metadata. If it does not exist, create the metadata.
437 * @param metadataFile
439 private void updateMetadata( File metadataFile )
440 throws RepositoryMetadataException
442 List<String> availableVersions = new ArrayList<String>();
443 ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
445 if ( metadataFile.exists() )
447 metadata = RepositoryMetadataReader.read( metadataFile );
448 availableVersions = metadata.getAvailableVersions();
450 Collections.sort( availableVersions, VersionComparator.getInstance() );
452 if ( !availableVersions.contains( version ) )
454 availableVersions.add( version );
457 String latestVersion = availableVersions.get( availableVersions.size() - 1 );
458 metadata.setLatestVersion( latestVersion );
459 metadata.setAvailableVersions( availableVersions );
460 metadata.setLastUpdatedTimestamp( Calendar.getInstance().getTime() );
462 if ( !VersionUtil.isSnapshot( version ) )
464 metadata.setReleasedVersion( latestVersion );
469 availableVersions.add( version );
471 metadata.setGroupId( groupId );
472 metadata.setArtifactId( artifactId );
473 metadata.setLatestVersion( version );
474 metadata.setLastUpdatedTimestamp( Calendar.getInstance().getTime() );
475 metadata.setAvailableVersions( availableVersions );
477 if ( !VersionUtil.isSnapshot( version ) )
479 metadata.setReleasedVersion( version );
483 RepositoryMetadataWriter.write( metadata, metadataFile );
484 ChecksummedFile checksum = new ChecksummedFile( metadataFile );
485 checksum.fixChecksums( algorithms );
488 public void validate()
492 // is this enough check for the repository permission?
493 if ( !userRepositories.isAuthorizedToUploadArtifacts( getPrincipal(), repositoryId ) )
495 addActionError( "User is not authorized to upload in repository " + repositoryId );
498 if ( artifactFile == null || artifactFile.length() == 0 )
500 addActionError( "Please add a file to upload." );
503 if ( !VersionUtil.isVersion( version ) )
505 addActionError( "Invalid version." );
508 catch ( PrincipalNotFoundException pe )
510 addActionError( pe.getMessage() );
512 catch ( ArchivaSecurityException ae )
514 addActionError( ae.getMessage() );