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.metadata.MetadataTools;
45 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataException;
46 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataReader;
47 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataWriter;
48 import org.apache.maven.archiva.repository.project.ProjectModelException;
49 import org.apache.maven.archiva.repository.project.ProjectModelWriter;
50 import org.apache.maven.archiva.repository.project.writers.ProjectModel400Writer;
51 import org.apache.maven.archiva.security.ArchivaSecurityException;
52 import org.apache.maven.archiva.security.PrincipalNotFoundException;
53 import org.apache.maven.archiva.security.UserRepositories;
54 import org.apache.maven.archiva.security.ArchivaXworkUser;
55 import org.codehaus.plexus.xwork.action.PlexusActionSupport;
57 import com.opensymphony.xwork.ActionContext;
58 import com.opensymphony.xwork.Preparable;
59 import com.opensymphony.xwork.Validateable;
62 * Upload an artifact using Jakarta file upload in webwork. If set by the user a pom will also be generated. Metadata
63 * will also be updated if one exists, otherwise it would be created.
65 * @author <a href="mailto:wsmoak@apache.org">Wendy Smoak</a>
66 * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
67 * @plexus.component role="com.opensymphony.xwork.Action" role-hint="uploadAction"
69 public class UploadAction
70 extends PlexusActionSupport
71 implements Validateable, Preparable
74 * The groupId of the artifact to be deployed.
76 private String groupId;
79 * The artifactId of the artifact to be deployed.
81 private String artifactId;
84 * The version of the artifact to be deployed.
86 private String version;
89 * The packaging of the artifact to be deployed.
91 private String packaging;
94 * The classifier of the artifact to be deployed.
96 private String classifier;
99 * The temporary file representing the artifact to be deployed.
101 private File artifactFile;
104 * The content type of the artifact to be deployed.
106 private String artifactContentType;
109 * The original filename of the uploaded artifact file.
111 private String artifactFilename;
114 * The temporary file representing the pom to be deployed alongside the artifact.
116 private File pomFile;
119 * The content type of the pom file.
121 private String pomContentType;
124 * The original filename of the uploaded pom file.
126 private String pomFilename;
129 * The repository where the artifact is to be deployed.
131 private String repositoryId;
134 * Flag whether to generate a pom for the artifact or not.
136 private boolean generatePom;
139 * List of managed repositories to deploy to.
141 private List<String> managedRepoIdList;
144 * @plexus.requirement
146 private UserRepositories userRepositories;
149 * @plexus.requirement role-hint="default"
151 private ArchivaConfiguration configuration;
154 * @plexus.requirement
156 private RepositoryContentFactory repositoryFactory;
158 private ChecksumAlgorithm[] algorithms = new ChecksumAlgorithm[] { ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 };
160 private ProjectModelWriter pomWriter = new ProjectModel400Writer();
162 public void setArtifact( File file )
164 this.artifactFile = file;
167 public void setArtifactContentType( String contentType )
169 this.artifactContentType = contentType;
172 public void setArtifactFileName( String filename )
174 this.artifactFilename = filename;
177 public void setPom( File file )
182 public void setPomContentType( String contentType )
184 this.pomContentType = contentType;
187 public void setPomFileName( String filename )
189 this.pomFilename = filename;
192 public String getGroupId()
197 public void setGroupId( String groupId )
199 this.groupId = groupId;
202 public String getArtifactId()
207 public void setArtifactId( String artifactId )
209 this.artifactId = artifactId;
212 public String getVersion()
217 public void setVersion( String version )
219 this.version = version;
222 public String getPackaging()
227 public void setPackaging( String packaging )
229 this.packaging = packaging;
232 public String getClassifier()
237 public void setClassifier( String classifier )
239 this.classifier = classifier;
242 public String getRepositoryId()
247 public void setRepositoryId( String repositoryId )
249 this.repositoryId = repositoryId;
252 public boolean isGeneratePom()
257 public void setGeneratePom( boolean generatePom )
259 this.generatePom = generatePom;
262 public List<String> getManagedRepoIdList()
264 return managedRepoIdList;
267 public void setManagedRepoIdList( List<String> managedRepoIdList )
269 this.managedRepoIdList = managedRepoIdList;
272 public void prepare()
275 new ArrayList<String>( configuration.getConfiguration().getManagedRepositoriesAsMap().keySet() );
278 public String input()
283 public String doUpload()
287 ManagedRepositoryConfiguration repoConfig =
288 configuration.getConfiguration().findManagedRepositoryById( repositoryId );
290 ArtifactReference artifactReference = new ArtifactReference();
291 artifactReference.setArtifactId( artifactId );
292 artifactReference.setGroupId( groupId );
293 artifactReference.setVersion( version );
294 artifactReference.setClassifier( classifier );
295 artifactReference.setType( packaging );
297 ManagedRepositoryContent repository = repositoryFactory.getManagedRepositoryContent( repositoryId );
299 String artifactPath = repository.toPath( artifactReference );
301 int lastIndex = artifactPath.lastIndexOf( '/' );
303 File targetPath = new File( repoConfig.getLocation(), artifactPath.substring( 0, lastIndex ) );
305 if ( !targetPath.exists() )
312 copyFile( artifactFile, targetPath, artifactPath.substring( lastIndex + 1 ) );
314 catch ( IOException ie )
316 addActionError( "Error encountered while uploading file: " + ie.getMessage() );
324 createPom( targetPath, artifactPath.substring( lastIndex + 1 ) );
326 catch ( IOException ie )
328 addActionError( "Error encountered while writing pom file: " + ie.getMessage() );
331 catch ( ProjectModelException pe )
333 addActionError( "Error encountered while generating pom file: " + pe.getMessage() );
338 if ( pomFile != null && pomFile.length() > 0 )
343 String targetFilename = artifactPath.substring( lastIndex + 1 ).replaceAll( packaging, "pom" );
344 copyFile( pomFile, targetPath, targetFilename );
346 catch ( IOException ie )
348 addActionError( "Error encountered while uploading pom file: " + ie.getMessage() );
354 updateMetadata( getMetadata( targetPath.getAbsolutePath() ) );
356 addActionMessage( "Artifact \'" + groupId + ":" + artifactId + ":" + version +
357 "\' was successfully deployed to repository \'" + repositoryId + "\'!" );
361 catch ( RepositoryNotFoundException re )
363 addActionError( "Target repository cannot be found: " + re.getMessage() );
366 catch ( RepositoryException rep )
368 addActionError( "Repository exception: " + rep.getMessage() );
373 private String getPrincipal()
375 return ArchivaXworkUser.getActivePrincipal( ActionContext.getContext().getSession() );
378 private void copyFile( File sourceFile, File targetPath, String targetFilename )
381 FileOutputStream out = new FileOutputStream( new File( targetPath, targetFilename ) );
385 FileInputStream input = new FileInputStream( sourceFile );
387 while ( ( i = input.read() ) != -1 )
399 private void createPom( File targetPath, String filename )
400 throws IOException, ProjectModelException
402 ArchivaProjectModel projectModel = new ArchivaProjectModel();
403 projectModel.setGroupId( groupId );
404 projectModel.setArtifactId( artifactId );
405 projectModel.setVersion( version );
406 projectModel.setPackaging( packaging );
408 File pomFile = new File( targetPath, filename.replaceAll( packaging, "pom" ) );
410 pomWriter.write( projectModel, pomFile );
413 private File getMetadata( String targetPath )
415 String artifactPath = targetPath.substring( 0, targetPath.lastIndexOf( '/' ) );
417 return new File( artifactPath, MetadataTools.MAVEN_METADATA );
421 * Update artifact level metadata. If it does not exist, create the metadata.
423 * @param metadataFile
425 private void updateMetadata( File metadataFile )
426 throws RepositoryMetadataException
428 List<String> availableVersions = new ArrayList<String>();
429 ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
431 if ( metadataFile.exists() )
433 metadata = RepositoryMetadataReader.read( metadataFile );
434 availableVersions = metadata.getAvailableVersions();
436 Collections.sort( availableVersions, VersionComparator.getInstance() );
438 if ( !availableVersions.contains( version ) )
440 availableVersions.add( version );
443 String latestVersion = availableVersions.get( availableVersions.size() - 1 );
444 metadata.setLatestVersion( latestVersion );
445 metadata.setAvailableVersions( availableVersions );
446 metadata.setLastUpdatedTimestamp( Calendar.getInstance().getTime() );
448 if ( !VersionUtil.isSnapshot( version ) )
450 metadata.setReleasedVersion( latestVersion );
455 availableVersions.add( version );
457 metadata.setGroupId( groupId );
458 metadata.setArtifactId( artifactId );
459 metadata.setLatestVersion( version );
460 metadata.setLastUpdatedTimestamp( Calendar.getInstance().getTime() );
461 metadata.setAvailableVersions( availableVersions );
463 if ( !VersionUtil.isSnapshot( version ) )
465 metadata.setReleasedVersion( version );
469 RepositoryMetadataWriter.write( metadata, metadataFile );
470 ChecksummedFile checksum = new ChecksummedFile( metadataFile );
471 checksum.fixChecksums( algorithms );
474 public void validate()
478 // is this enough check for the repository permission?
479 if ( !userRepositories.isAuthorizedToUploadArtifacts( getPrincipal(), repositoryId ) )
481 addActionError( "User is not authorized to upload in repository " + repositoryId );
484 if ( artifactFile == null || artifactFile.length() == 0 )
486 addActionError( "Please add a file to upload." );
489 if ( !VersionUtil.isVersion( version ) )
491 addActionError( "Invalid version." );
494 catch ( PrincipalNotFoundException pe )
496 addActionError( pe.getMessage() );
498 catch ( ArchivaSecurityException ae )
500 addActionError( ae.getMessage() );