1 package org.apache.maven.archiva.web.tags;
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 java.io.IOException;
23 import java.io.Writer;
24 import java.text.DecimalFormat;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.HashMap;
29 import java.util.List;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.jsp.PageContext;
34 import com.opensymphony.xwork2.ActionContext;
35 import com.opensymphony.xwork2.util.ValueStack;
36 import org.apache.archiva.metadata.model.ArtifactMetadata;
37 import org.apache.archiva.metadata.repository.MetadataRepository;
38 import org.apache.commons.lang.StringEscapeUtils;
39 import org.apache.commons.lang.StringUtils;
40 import org.apache.maven.archiva.model.ArtifactReference;
41 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
42 import org.apache.maven.archiva.repository.RepositoryContentFactory;
43 import org.apache.maven.archiva.repository.RepositoryException;
44 import org.apache.maven.archiva.repository.RepositoryNotFoundException;
45 import org.apache.maven.archiva.repository.layout.LayoutException;
46 import org.apache.maven.archiva.security.ArchivaSecurityException;
47 import org.apache.maven.archiva.security.ArchivaXworkUser;
48 import org.apache.maven.archiva.security.UserRepositories;
49 import org.apache.struts2.StrutsException;
50 import org.apache.struts2.components.Component;
51 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
53 public class DownloadArtifact
56 private static final String DEFAULT_DOWNLOAD_IMAGE = "download-type-other.png";
58 private RepositoryContentFactory repositoryFactory;
60 private MetadataRepository metadataRepository;
62 private HttpServletRequest req;
64 private String groupId;
66 private String artifactId;
68 private String version;
70 private boolean mini = false;
72 private DecimalFormat decimalFormat;
74 private static final Map<String, String> DOWNLOAD_IMAGES = new HashMap<String, String>();
76 private UserRepositories userRepositories;
80 DOWNLOAD_IMAGES.put( "jar", "download-type-jar.png" );
81 DOWNLOAD_IMAGES.put( "java-source", "download-type-jar.png" );
82 DOWNLOAD_IMAGES.put( "pom", "download-type-pom.png" );
83 DOWNLOAD_IMAGES.put( "maven-plugin", "download-type-maven-plugin.png" );
84 DOWNLOAD_IMAGES.put( "maven-archetype", "download-type-archetype.png" );
85 DOWNLOAD_IMAGES.put( "maven-skin", "download-type-skin.png" );
88 public DownloadArtifact( ValueStack stack, PageContext pageContext )
91 decimalFormat = new DecimalFormat( "#,#00" );
92 this.req = (HttpServletRequest) pageContext.getRequest();
95 metadataRepository = (MetadataRepository) PlexusTagUtil.lookup( pageContext, MetadataRepository.class );
97 (RepositoryContentFactory) PlexusTagUtil.lookup( pageContext, RepositoryContentFactory.class );
98 userRepositories = (UserRepositories) PlexusTagUtil.lookup( pageContext, UserRepositories.class );
100 catch ( ComponentLookupException e )
102 throw new RuntimeException( e.getMessage(), e );
107 public boolean end( Writer writer, String body )
109 StringBuffer sb = new StringBuffer();
113 List<ArtifactMetadata> artifacts = new ArrayList<ArtifactMetadata>();
114 for ( String repoId : getObservableRepos() )
116 artifacts.addAll( metadataRepository.getArtifacts( repoId, groupId, artifactId, version ) );
119 if ( !artifacts.isEmpty() )
121 String prefix = req.getContextPath() + "/repository/";
125 // TODO: write 1 line download link for main artifact.
129 appendNormal( sb, prefix, artifacts );
133 catch ( RepositoryNotFoundException e )
135 // TODO Auto-generated catch block
138 catch ( RepositoryException e )
140 // TODO Auto-generated catch block
146 writer.write( sb.toString() );
148 catch ( IOException e )
150 throw new StrutsException( "IOError: " + e.getMessage(), e );
153 return super.end( writer, body );
156 private void appendNormal( StringBuffer sb, String prefix, List<ArtifactMetadata> relatedArtifacts )
157 throws RepositoryException
160 * <div class="download">
162 * <div class="c"></div>
166 * <-- main content goes here -->
170 * <div class="c"></div>
175 sb.append( "<div class=\"download\">" );
176 sb.append( "<div class=\"hd\"><div class=\"c\"></div></div>" );
177 sb.append( "<div class=\"bd\"><div class=\"c\">" );
181 if ( relatedArtifacts.size() > 1 )
183 sb.append( "Downloads" );
187 sb.append( "Download" );
189 sb.append( "</h2>" );
192 sb.append( "<p class=\"body\">" );
194 sb.append( "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">" );
195 for ( ArtifactMetadata artifact : relatedArtifacts )
197 String repoId = artifact.getRepositoryId();
198 ManagedRepositoryContent repo = repositoryFactory.getManagedRepositoryContent( repoId );
200 sb.append( "\n<tr>" );
202 sb.append( "<td class=\"icon\">" );
203 appendImageLink( sb, prefix + repoId, repo, artifact );
204 sb.append( "</td>" );
206 sb.append( "<td class=\"type\">" );
207 appendLink( sb, prefix + repoId, repo, artifact );
208 sb.append( "</td>" );
210 sb.append( "<td class=\"size\">" );
211 appendFilesize( sb, artifact );
212 sb.append( "</td>" );
214 sb.append( "</tr>" );
216 sb.append( "</table>" );
219 sb.append( "</div>" ); // close "downloadbox.bd.c"
220 sb.append( "</div>" ); // close "downloadbox.bd"
222 sb.append( "<div class=\"ft\"><div class=\"c\"></div></div>" );
223 sb.append( "</div>" ); // close "download"
226 private void appendImageLink( StringBuffer sb, String prefix, ManagedRepositoryContent repo,
227 ArtifactMetadata artifact )
229 String path = getPath( repo, artifact );
230 String type = getType( repo, path );
231 String linkText = "<img src=\"" + req.getContextPath() + "/images/" + getDownloadImage( type ) + "\" />";
232 appendLink( sb, prefix, artifact, linkText, path );
235 private String getType( ManagedRepositoryContent repo, String path )
240 type = repo.toArtifactReference( path ).getType();
242 catch ( LayoutException e )
244 e.printStackTrace(); //TODO
249 private String getDownloadImage( String type )
251 String name = DOWNLOAD_IMAGES.get( type );
252 return name != null ? name : DEFAULT_DOWNLOAD_IMAGE;
255 private static void appendLink( StringBuffer sb, String prefix, ArtifactMetadata artifact, String linkText,
259 StringBuffer url = new StringBuffer();
260 url.append( prefix );
261 url.append( "/" ).append( path );
263 sb.append( "<a href=\"" ).append( StringEscapeUtils.escapeXml( url.toString() ) ).append( "\"" );
264 sb.append( " title=\"" ).append( "Download " ).append( StringEscapeUtils.escapeXml( artifact.getId() ) ).append(
268 sb.append( linkText );
273 private static String getPath( ManagedRepositoryContent repo, ArtifactMetadata artifact )
275 // TODO: use metadata resolver capability instead
276 ArtifactReference ref = new ArtifactReference();
277 ref.setArtifactId( artifact.getProject() );
278 ref.setGroupId( artifact.getNamespace() );
279 ref.setVersion( artifact.getVersion() );
280 String path = repo.toPath( ref );
281 path = path.substring( 0, path.lastIndexOf( "/" ) + 1 ) + artifact.getId();
285 private void appendLink( StringBuffer sb, String prefix, ManagedRepositoryContent repo, ArtifactMetadata artifact )
287 String path = getPath( repo, artifact );
288 String type = getType( repo, path );
289 String linkText = StringUtils.capitalize( type );
291 appendLink( sb, prefix, artifact, linkText, path );
294 private void appendFilesize( StringBuffer sb, ArtifactMetadata artifact )
296 sb.append( decimalFormat.format( artifact.getSize() ) );
299 public void setArtifactId( String artifactId )
301 this.artifactId = artifactId;
304 public void setGroupId( String groupId )
306 this.groupId = groupId;
309 public void setMini( boolean mini )
314 public void setVersion( String version )
316 this.version = version;
319 public Collection<String> getObservableRepos()
323 ActionContext context = ActionContext.getContext();
324 Map session = context.getSession();
325 return userRepositories.getObservableRepositoryIds( ArchivaXworkUser.getActivePrincipal( session ) );
327 catch ( ArchivaSecurityException e )
329 e.printStackTrace(); //TODO
330 return Collections.emptyList();