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
22 import org.codehaus.plexus.xwork.action.PlexusActionSupport;
23 import org.apache.maven.archiva.common.utils.Checksums;
24 import org.apache.maven.archiva.common.utils.VersionComparator;
25 import org.apache.maven.archiva.common.utils.VersionUtil;
26 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
27 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
28 import org.apache.maven.archiva.model.ArchivaProjectModel;
29 import org.apache.maven.archiva.model.ArchivaRepositoryMetadata;
30 import org.apache.maven.archiva.model.ArtifactReference;
31 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
32 import org.apache.maven.archiva.repository.RepositoryContentFactory;
33 import org.apache.maven.archiva.repository.RepositoryException;
34 import org.apache.maven.archiva.repository.RepositoryNotFoundException;
35 import org.apache.maven.archiva.repository.metadata.MetadataTools;
36 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataException;
37 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataReader;
38 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataWriter;
39 import org.apache.maven.archiva.repository.project.ProjectModelException;
40 import org.apache.maven.archiva.repository.project.ProjectModelWriter;
41 import org.apache.maven.archiva.security.ArchivaSecurityException;
42 import org.apache.maven.archiva.security.ArchivaUser;
43 import org.apache.maven.archiva.security.PrincipalNotFoundException;
44 import org.apache.maven.archiva.security.UserRepositories;
46 import com.opensymphony.xwork.Preparable;
47 import com.opensymphony.xwork.Validateable;
50 import java.io.FileInputStream;
51 import java.io.FileOutputStream;
52 import java.io.IOException;
53 import java.util.ArrayList;
54 import java.util.Calendar;
55 import java.util.Collections;
56 import java.util.List;
59 * Upload an artifact using Jakarta file upload in webwork. If set by the user a pom will also be generated. Metadata
60 * will also be updated if one exists, otherwise it would be created.
62 * @author <a href="mailto:wsmoak@apache.org">Wendy Smoak</a>
63 * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
64 * @plexus.component role="com.opensymphony.xwork.Action" role-hint="uploadAction"
66 public class UploadAction
67 extends PlexusActionSupport
68 implements Validateable, Preparable
71 * The groupId of the artifact to be deployed.
73 private String groupId;
76 * The artifactId of the artifact to be deployed.
78 private String artifactId;
81 * The version of the artifact to be deployed.
83 private String version;
86 * The packaging of the artifact to be deployed.
88 private String packaging;
91 * The classifier of the artifact to be deployed.
93 private String classifier;
96 * The artifact to be deployed.
101 * The content type of the artifact to be deployed.
103 private String contentType;
106 * The temporary filename of the artifact to be deployed.
108 private String filename;
111 * The repository where the artifact is to be deployed.
113 private String repositoryId;
116 * Flag whether to generate a pom for the artifact or not.
118 private boolean generatePom;
121 * List of managed repositories to deploy to.
123 private List<String> managedRepoIdList;
126 * @plexus.requirement role-hint="xwork"
128 private ArchivaUser archivaUser;
131 * @plexus.requirement
133 private UserRepositories userRepositories;
136 * @plexus.requirement role-hint="default"
138 private ArchivaConfiguration configuration;
141 * @plexus.requirement
143 private RepositoryContentFactory repositoryFactory;
146 * @plexus.requirement role-hint="model400"
148 private ProjectModelWriter pomWriter;
151 * @plexus.requirement
153 private Checksums checksums;
155 public void setUpload( File file )
160 public void setUploadContentType( String contentType )
162 this.contentType = contentType;
165 public void setUploadFileName( String filename )
167 this.filename = filename;
170 public String getGroupId()
175 public void setGroupId( String groupId )
177 this.groupId = groupId;
180 public String getArtifactId()
185 public void setArtifactId( String artifactId )
187 this.artifactId = artifactId;
190 public String getVersion()
195 public void setVersion( String version )
197 this.version = version;
200 public String getPackaging()
205 public void setPackaging( String packaging )
207 this.packaging = packaging;
210 public String getClassifier()
215 public void setClassifier( String classifier )
217 this.classifier = classifier;
220 public String getRepositoryId()
225 public void setRepositoryId( String repositoryId )
227 this.repositoryId = repositoryId;
230 public boolean isGeneratePom()
235 public void setGeneratePom( boolean generatePom )
237 this.generatePom = generatePom;
240 public List<String> getManagedRepoIdList()
242 return managedRepoIdList;
245 public void setManagedRepoIdList( List<String> managedRepoIdList )
247 this.managedRepoIdList = managedRepoIdList;
250 public void prepare()
253 new ArrayList<String>( configuration.getConfiguration().getManagedRepositoriesAsMap().keySet() );
256 public String input()
261 public String doUpload()
265 ManagedRepositoryConfiguration repoConfig =
266 configuration.getConfiguration().findManagedRepositoryById( repositoryId );
268 ArtifactReference artifactReference = new ArtifactReference();
269 artifactReference.setArtifactId( artifactId );
270 artifactReference.setGroupId( groupId );
271 artifactReference.setVersion( version );
272 artifactReference.setClassifier( classifier );
273 artifactReference.setType( packaging );
275 ManagedRepositoryContent repository = repositoryFactory.getManagedRepositoryContent( repositoryId );
277 String artifactPath = repository.toPath( artifactReference );
279 int lastIndex = artifactPath.lastIndexOf( '/' );
281 File targetPath = new File( repoConfig.getLocation(), artifactPath.substring( 0, lastIndex ) );
283 if ( !targetPath.exists() )
290 copyFile( targetPath, artifactPath.substring( lastIndex + 1 ) );
292 catch ( IOException ie )
294 addActionError( "Error encountered while uploading file: " + ie.getMessage() );
302 createPom( targetPath, artifactPath.substring( lastIndex + 1 ) );
304 catch ( IOException ie )
306 addActionError( "Error encountered while writing pom file: " + ie.getMessage() );
309 catch ( ProjectModelException pe )
311 addActionError( "Error encountered while generating pom file: " + pe.getMessage() );
316 updateMetadata( getMetadata( targetPath.getAbsolutePath() ) );
318 addActionMessage( "Artifact \'" + groupId + ":" + artifactId + ":" + version +
319 "\' was successfully deployed to repository \'" + repositoryId + "\'!" );
323 catch ( RepositoryNotFoundException re )
325 addActionError( "Target repository cannot be found: " + re.getMessage() );
328 catch ( RepositoryException rep )
330 addActionError( "Repository exception: " + rep.getMessage() );
335 private String getPrincipal()
337 return archivaUser.getActivePrincipal();
340 private void copyFile( File targetPath, String artifactFilename )
343 FileOutputStream out = new FileOutputStream( new File( targetPath, artifactFilename ) );
347 FileInputStream input = new FileInputStream( file );
349 while ( ( i = input.read() ) != -1 )
361 private void createPom( File targetPath, String filename )
362 throws IOException, ProjectModelException
364 ArchivaProjectModel projectModel = new ArchivaProjectModel();
365 projectModel.setGroupId( groupId );
366 projectModel.setArtifactId( artifactId );
367 projectModel.setVersion( version );
368 projectModel.setPackaging( packaging );
370 File pomFile = new File( targetPath, filename.replaceAll( packaging, "pom" ) );
372 pomWriter.write( projectModel, pomFile );
375 private File getMetadata( String targetPath )
377 String artifactPath = targetPath.substring( 0, targetPath.lastIndexOf( '/' ) );
379 return new File( artifactPath, MetadataTools.MAVEN_METADATA );
383 * Update artifact level metadata. If it does not exist, create the metadata.
387 private void updateMetadata( File metadataFile )
388 throws RepositoryMetadataException
390 List<String> availableVersions = new ArrayList<String>();
391 ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
393 if ( metadataFile.exists() )
395 metadata = RepositoryMetadataReader.read( metadataFile );
396 availableVersions = metadata.getAvailableVersions();
398 Collections.sort( availableVersions, VersionComparator.getInstance() );
400 if ( !availableVersions.contains( version ) )
402 availableVersions.add( version );
405 String latestVersion = availableVersions.get( availableVersions.size() - 1 );
406 metadata.setLatestVersion( latestVersion );
407 metadata.setAvailableVersions( availableVersions );
408 metadata.setLastUpdatedTimestamp( Calendar.getInstance().getTime() );
410 if ( !VersionUtil.isSnapshot( version ) )
412 metadata.setReleasedVersion( latestVersion );
417 availableVersions.add( version );
419 metadata.setGroupId( groupId );
420 metadata.setArtifactId( artifactId );
421 metadata.setLatestVersion( version );
422 metadata.setLastUpdatedTimestamp( Calendar.getInstance().getTime() );
423 metadata.setAvailableVersions( availableVersions );
425 if ( !VersionUtil.isSnapshot( version ) )
427 metadata.setReleasedVersion( version );
431 RepositoryMetadataWriter.write( metadata, metadataFile );
433 checksums.update( metadataFile );
436 public void validate()
440 // is this enough check for the repository permission?
441 if ( !userRepositories.isAuthorizedToUploadArtifacts( getPrincipal(), repositoryId ) )
443 addActionError( "User is not authorized to upload in repository " + repositoryId );
446 if ( file == null || file.length() == 0 )
448 addActionError( "Please add a file to upload." );
451 if ( !VersionUtil.isVersion( version ) )
453 addActionError( "Invalid version." );
456 catch ( PrincipalNotFoundException pe )
458 addActionError( pe.getMessage() );
460 catch ( ArchivaSecurityException ae )
462 addActionError( ae.getMessage() );