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.FileWriter;
26 import java.io.IOException;
27 import java.text.DateFormat;
28 import java.text.SimpleDateFormat;
29 import java.util.ArrayList;
30 import java.util.Calendar;
31 import java.util.Collections;
32 import java.util.Date;
33 import java.util.List;
34 import java.util.TimeZone;
36 import com.opensymphony.xwork2.Preparable;
37 import com.opensymphony.xwork2.Validateable;
38 import org.apache.archiva.checksum.ChecksumAlgorithm;
39 import org.apache.archiva.checksum.ChecksummedFile;
40 import org.apache.archiva.scheduler.ArchivaTaskScheduler;
41 import org.apache.archiva.scheduler.repository.RepositoryTask;
42 import org.apache.commons.io.FilenameUtils;
43 import org.apache.commons.lang.StringUtils;
44 import org.apache.maven.archiva.common.utils.VersionComparator;
45 import org.apache.maven.archiva.common.utils.VersionUtil;
46 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
47 import org.apache.maven.archiva.configuration.Configuration;
48 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
49 import org.apache.maven.archiva.model.ArchivaRepositoryMetadata;
50 import org.apache.maven.archiva.model.ArtifactReference;
51 import org.apache.maven.archiva.model.SnapshotVersion;
52 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
53 import org.apache.maven.archiva.repository.RepositoryContentFactory;
54 import org.apache.maven.archiva.repository.RepositoryException;
55 import org.apache.maven.archiva.repository.RepositoryNotFoundException;
56 import org.apache.maven.archiva.repository.audit.AuditEvent;
57 import org.apache.maven.archiva.repository.audit.Auditable;
58 import org.apache.maven.archiva.repository.metadata.MetadataTools;
59 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataException;
60 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataReader;
61 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataWriter;
62 import org.apache.maven.archiva.security.AccessDeniedException;
63 import org.apache.maven.archiva.security.ArchivaSecurityException;
64 import org.apache.maven.archiva.security.PrincipalNotFoundException;
65 import org.apache.maven.archiva.security.UserRepositories;
66 import org.apache.maven.model.Model;
67 import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
68 import org.codehaus.plexus.taskqueue.TaskQueueException;
69 import org.codehaus.plexus.util.IOUtil;
72 * Upload an artifact using Jakarta file upload in webwork. If set by the user a pom will also be generated. Metadata
73 * will also be updated if one exists, otherwise it would be created.
75 * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="uploadAction" instantiation-strategy="per-lookup"
77 public class UploadAction
78 extends PlexusActionSupport
79 implements Validateable, Preparable, Auditable
82 * The groupId of the artifact to be deployed.
84 private String groupId;
87 * The artifactId of the artifact to be deployed.
89 private String artifactId;
92 * The version of the artifact to be deployed.
94 private String version;
97 * The packaging of the artifact to be deployed.
99 private String packaging;
102 * The classifier of the artifact to be deployed.
104 private String classifier;
107 * The temporary file representing the artifact to be deployed.
109 private File artifactFile;
112 * The temporary file representing the pom to be deployed alongside the artifact.
114 private File pomFile;
117 * The repository where the artifact is to be deployed.
119 private String repositoryId;
122 * Flag whether to generate a pom for the artifact or not.
124 private boolean generatePom;
127 * List of managed repositories to deploy to.
129 private List<String> managedRepoIdList;
132 * @plexus.requirement
134 private UserRepositories userRepositories;
137 * @plexus.requirement role-hint="default"
139 private ArchivaConfiguration configuration;
142 * @plexus.requirement
144 private RepositoryContentFactory repositoryFactory;
147 * @plexus.requirement role="org.apache.archiva.scheduler.ArchivaTaskScheduler" role-hint="repository"
149 private ArchivaTaskScheduler scheduler;
151 private ChecksumAlgorithm[] algorithms = new ChecksumAlgorithm[]{ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5};
153 public void setArtifact( File file )
155 this.artifactFile = file;
158 public void setArtifactContentType( String contentType )
160 StringUtils.trim( contentType );
163 public void setArtifactFileName( String filename )
165 StringUtils.trim( filename );
168 public void setPom( File file )
173 public void setPomContentType( String contentType )
175 StringUtils.trim( contentType );
178 public void setPomFileName( String filename )
180 StringUtils.trim( filename );
183 public String getGroupId()
188 public void setGroupId( String groupId )
190 this.groupId = StringUtils.trim( groupId );
193 public String getArtifactId()
198 public void setArtifactId( String artifactId )
200 this.artifactId = StringUtils.trim( artifactId );
203 public String getVersion()
208 public void setVersion( String version )
210 this.version = StringUtils.trim( version );
213 public String getPackaging()
218 public void setPackaging( String packaging )
220 this.packaging = StringUtils.trim( packaging );
223 public String getClassifier()
228 public void setClassifier( String classifier )
230 this.classifier = StringUtils.trim( classifier );
233 public String getRepositoryId()
238 public void setRepositoryId( String repositoryId )
240 this.repositoryId = repositoryId;
243 public boolean isGeneratePom()
248 public void setGeneratePom( boolean generatePom )
250 this.generatePom = generatePom;
253 public List<String> getManagedRepoIdList()
255 return managedRepoIdList;
258 public void setManagedRepoIdList( List<String> managedRepoIdList )
260 this.managedRepoIdList = managedRepoIdList;
263 public void prepare()
265 managedRepoIdList = getManagableRepos();
268 public String input()
275 // reset the fields so the form is clear when
276 // the action returns to the jsp page
288 public String doUpload()
292 Configuration config = configuration.getConfiguration();
293 ManagedRepositoryConfiguration repoConfig = config.findManagedRepositoryById( repositoryId );
295 ArtifactReference artifactReference = new ArtifactReference();
296 artifactReference.setArtifactId( artifactId );
297 artifactReference.setGroupId( groupId );
298 artifactReference.setVersion( version );
299 artifactReference.setClassifier( classifier );
300 artifactReference.setType( packaging );
302 ManagedRepositoryContent repository = repositoryFactory.getManagedRepositoryContent( repositoryId );
304 String artifactPath = repository.toPath( artifactReference );
306 int lastIndex = artifactPath.lastIndexOf( '/' );
308 File targetPath = new File( repoConfig.getLocation(), artifactPath.substring( 0, lastIndex ) );
310 Date lastUpdatedTimestamp = Calendar.getInstance().getTime();
311 int newBuildNumber = -1;
312 String timestamp = null;
314 File metadataFile = getMetadata( targetPath.getAbsolutePath() );
315 ArchivaRepositoryMetadata metadata = getMetadata( metadataFile );
317 if ( VersionUtil.isSnapshot( version ) )
319 TimeZone timezone = TimeZone.getTimeZone( "UTC" );
320 DateFormat fmt = new SimpleDateFormat( "yyyyMMdd.HHmmss" );
321 fmt.setTimeZone( timezone );
322 timestamp = fmt.format( lastUpdatedTimestamp );
323 if ( metadata.getSnapshotVersion() != null )
325 newBuildNumber = metadata.getSnapshotVersion().getBuildNumber() + 1;
329 metadata.setSnapshotVersion( new SnapshotVersion() );
334 if ( !targetPath.exists() )
339 String filename = artifactPath.substring( lastIndex + 1 );
340 if ( VersionUtil.isSnapshot( version ) )
342 filename = filename.replaceAll( "SNAPSHOT", timestamp + "-" + newBuildNumber );
345 boolean fixChecksums =
346 !( config.getRepositoryScanning().getKnownContentConsumers().contains( "create-missing-checksums" ) );
350 File targetFile = new File( targetPath, filename );
351 if ( targetFile.exists() && !VersionUtil.isSnapshot( version ) && repoConfig.isBlockRedeployments() )
354 "Overwriting released artifacts in repository '" + repoConfig.getId() + "' is not allowed." );
359 copyFile( artifactFile, targetPath, filename, fixChecksums );
360 queueRepositoryTask( repository.getId(), repository.toFile( artifactReference ) );
363 catch ( IOException ie )
365 addActionError( "Error encountered while uploading file: " + ie.getMessage() );
369 String pomFilename = filename;
370 if ( classifier != null && !"".equals( classifier ) )
372 pomFilename = StringUtils.remove( pomFilename, "-" + classifier );
374 pomFilename = FilenameUtils.removeExtension( pomFilename ) + ".pom";
380 File generatedPomFile = createPom( targetPath, pomFilename );
383 fixChecksums( generatedPomFile );
385 queueRepositoryTask( repoConfig.getId(), generatedPomFile );
387 catch ( IOException ie )
389 addActionError( "Error encountered while writing pom file: " + ie.getMessage() );
394 if ( pomFile != null && pomFile.length() > 0 )
398 copyFile( pomFile, targetPath, pomFilename, fixChecksums );
399 queueRepositoryTask( repoConfig.getId(), new File( targetPath, pomFilename ) );
401 catch ( IOException ie )
403 addActionError( "Error encountered while uploading pom file: " + ie.getMessage() );
409 // explicitly update only if metadata-updater consumer is not enabled!
410 if ( !config.getRepositoryScanning().getKnownContentConsumers().contains( "metadata-updater" ) )
412 updateMetadata( metadata, metadataFile, lastUpdatedTimestamp, timestamp, newBuildNumber, fixChecksums );
415 String msg = "Artifact \'" + groupId + ":" + artifactId + ":" + version +
416 "\' was successfully deployed to repository \'" + repositoryId + "\'";
418 triggerAuditEvent( repositoryId, artifactPath, AuditEvent.UPLOAD_FILE );
420 addActionMessage( msg );
425 catch ( RepositoryNotFoundException re )
427 addActionError( "Target repository cannot be found: " + re.getMessage() );
430 catch ( RepositoryException rep )
432 addActionError( "Repository exception: " + rep.getMessage() );
437 private void fixChecksums( File file )
439 ChecksummedFile checksum = new ChecksummedFile( file );
440 checksum.fixChecksums( algorithms );
443 private void copyFile( File sourceFile, File targetPath, String targetFilename, boolean fixChecksums )
446 FileOutputStream out = new FileOutputStream( new File( targetPath, targetFilename ) );
447 FileInputStream input = new FileInputStream( sourceFile );
452 while ( ( i = input.read() ) != -1 )
466 fixChecksums( new File( targetPath, targetFilename ) );
470 private File createPom( File targetPath, String filename )
473 Model projectModel = new Model();
474 projectModel.setModelVersion( "4.0.0" );
475 projectModel.setGroupId( groupId );
476 projectModel.setArtifactId( artifactId );
477 projectModel.setVersion( version );
478 projectModel.setPackaging( packaging );
480 File pomFile = new File( targetPath, filename );
481 MavenXpp3Writer writer = new MavenXpp3Writer();
482 FileWriter w = new FileWriter( pomFile );
485 writer.write( w, projectModel );
495 private File getMetadata( String targetPath )
497 String artifactPath = targetPath.substring( 0, targetPath.lastIndexOf( File.separatorChar ) );
499 return new File( artifactPath, MetadataTools.MAVEN_METADATA );
502 private ArchivaRepositoryMetadata getMetadata( File metadataFile )
503 throws RepositoryMetadataException
505 ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
506 if ( metadataFile.exists() )
508 metadata = RepositoryMetadataReader.read( metadataFile );
514 * Update artifact level metadata. If it does not exist, create the metadata and
515 * fix checksums if necessary.
517 private void updateMetadata( ArchivaRepositoryMetadata metadata, File metadataFile, Date lastUpdatedTimestamp,
518 String timestamp, int buildNumber, boolean fixChecksums )
519 throws RepositoryMetadataException
521 List<String> availableVersions = new ArrayList<String>();
522 String latestVersion = version;
524 if ( metadataFile.exists() )
526 availableVersions = metadata.getAvailableVersions();
528 Collections.sort( availableVersions, VersionComparator.getInstance() );
530 if ( !availableVersions.contains( version ) )
532 availableVersions.add( version );
535 latestVersion = availableVersions.get( availableVersions.size() - 1 );
539 availableVersions.add( version );
541 metadata.setGroupId( groupId );
542 metadata.setArtifactId( artifactId );
545 if ( metadata.getGroupId() == null )
547 metadata.setGroupId( groupId );
549 if ( metadata.getArtifactId() == null )
551 metadata.setArtifactId( artifactId );
554 metadata.setLatestVersion( latestVersion );
555 metadata.setLastUpdatedTimestamp( lastUpdatedTimestamp );
556 metadata.setAvailableVersions( availableVersions );
558 if ( !VersionUtil.isSnapshot( version ) )
560 metadata.setReleasedVersion( latestVersion );
564 metadata.getSnapshotVersion().setBuildNumber( buildNumber );
566 metadata.getSnapshotVersion().setTimestamp( timestamp );
569 RepositoryMetadataWriter.write( metadata, metadataFile );
573 fixChecksums( metadataFile );
577 public void validate()
581 // is this enough check for the repository permission?
582 if ( !userRepositories.isAuthorizedToUploadArtifacts( getPrincipal(), repositoryId ) )
584 addActionError( "User is not authorized to upload in repository " + repositoryId );
587 if ( artifactFile == null || artifactFile.length() == 0 )
589 addActionError( "Please add a file to upload." );
592 if ( version == null || !VersionUtil.isVersion( version ) )
594 addActionError( "Invalid version." );
597 catch ( PrincipalNotFoundException pe )
599 addActionError( pe.getMessage() );
601 catch ( ArchivaSecurityException ae )
603 addActionError( ae.getMessage() );
607 private List<String> getManagableRepos()
611 return userRepositories.getManagableRepositoryIds( getPrincipal() );
613 catch ( PrincipalNotFoundException e )
615 log.warn( e.getMessage(), e );
617 catch ( AccessDeniedException e )
619 log.warn( e.getMessage(), e );
620 // TODO: pass this onto the screen.
622 catch ( ArchivaSecurityException e )
624 log.warn( e.getMessage(), e );
626 return Collections.emptyList();
629 private void queueRepositoryTask( String repositoryId, File localFile )
631 RepositoryTask task = new RepositoryTask();
632 task.setRepositoryId( repositoryId );
633 task.setResourceFile( localFile );
634 task.setUpdateRelatedArtifacts( true );
635 task.setScanAll( true );
639 scheduler.queueTask( task );
641 catch ( TaskQueueException e )
644 "Unable to queue repository task to execute consumers on resource file ['" + localFile.getName() +
649 public void setScheduler( ArchivaTaskScheduler scheduler )
651 this.scheduler = scheduler;
654 public void setRepositoryFactory( RepositoryContentFactory repositoryFactory )
656 this.repositoryFactory = repositoryFactory;
659 public void setConfiguration( ArchivaConfiguration configuration )
661 this.configuration = configuration;