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 org.apache.struts2.StrutsException;
23 import org.apache.struts2.components.Component;
25 import com.opensymphony.xwork2.util.ValueStack;
26 import org.apache.commons.lang.StringEscapeUtils;
27 import org.apache.commons.lang.StringUtils;
28 import org.apache.maven.archiva.database.ArchivaDAO;
29 import org.apache.maven.archiva.database.ArchivaDatabaseException;
30 import org.apache.maven.archiva.database.Constraint;
31 import org.apache.maven.archiva.database.ObjectNotFoundException;
32 import org.apache.maven.archiva.database.constraints.ArtifactsRelatedConstraint;
33 import org.apache.maven.archiva.model.ArchivaArtifact;
34 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
35 import org.apache.maven.archiva.repository.RepositoryContentFactory;
36 import org.apache.maven.archiva.repository.RepositoryException;
37 import org.apache.maven.archiva.repository.RepositoryNotFoundException;
38 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
40 import java.io.IOException;
41 import java.io.Writer;
42 import java.text.DecimalFormat;
43 import java.util.HashMap;
44 import java.util.List;
47 import javax.servlet.http.HttpServletRequest;
48 import javax.servlet.http.HttpServletResponse;
49 import javax.servlet.jsp.PageContext;
55 * @plexus.component role="org.apache.struts2.components.Component" role-hint="download-artifact"
56 * instantiation-strategy="per-lookup"
58 public class DownloadArtifact
61 private static final String DEFAULT_DOWNLOAD_IMAGE = "download-type-other.png";
64 * @plexus.requirement role-hint="jdo"
66 private ArchivaDAO dao;
71 private RepositoryContentFactory repositoryFactory;
73 private HttpServletRequest req;
75 @SuppressWarnings("unused")
76 private HttpServletResponse res;
78 private String groupId;
80 private String artifactId;
82 private String version;
84 private boolean mini = false;
86 private DecimalFormat decimalFormat;
88 private static final Map<String, String> DOWNLOAD_IMAGES = new HashMap<String,String>();
92 DOWNLOAD_IMAGES.put( "jar", "download-type-jar.png" );
93 DOWNLOAD_IMAGES.put( "java-source", "download-type-jar.png" );
94 DOWNLOAD_IMAGES.put( "pom", "download-type-pom.png" );
95 DOWNLOAD_IMAGES.put( "maven-plugin", "download-type-maven-plugin.png" );
96 DOWNLOAD_IMAGES.put( "maven-archetype", "download-type-archetype.png" );
97 DOWNLOAD_IMAGES.put( "maven-skin", "download-type-skin.png" );
100 public DownloadArtifact( ValueStack stack, PageContext pageContext )
103 decimalFormat = new DecimalFormat( "#,#00" );
104 this.req = (HttpServletRequest) pageContext.getRequest();
105 this.res = (HttpServletResponse) pageContext.getResponse();
108 dao = (ArchivaDAO) PlexusTagUtil.lookup( pageContext, ArchivaDAO.ROLE, "jdo" );
109 repositoryFactory = (RepositoryContentFactory) PlexusTagUtil.lookup( pageContext,
110 RepositoryContentFactory.class );
112 catch ( ComponentLookupException e )
114 throw new RuntimeException( e.getMessage(), e );
119 public boolean end( Writer writer, String body )
121 StringBuffer sb = new StringBuffer();
125 Constraint constraint = new ArtifactsRelatedConstraint( groupId, artifactId, version );
126 List<ArchivaArtifact> relatedArtifacts = dao.getArtifactDAO().queryArtifacts( constraint );
128 if ( relatedArtifacts != null && relatedArtifacts.size() > 0 )
130 String prefix = req.getContextPath() + "/repository/";
134 appendMini( sb, prefix, relatedArtifacts );
138 appendNormal( sb, prefix, relatedArtifacts );
142 catch ( ObjectNotFoundException e )
144 appendError( sb, e );
146 catch ( ArchivaDatabaseException e )
148 appendError( sb, e );
150 catch ( RepositoryNotFoundException e )
152 // TODO Auto-generated catch block
155 catch ( RepositoryException e )
157 // TODO Auto-generated catch block
163 writer.write( sb.toString() );
165 catch ( IOException e )
167 throw new StrutsException( "IOError: " + e.getMessage(), e );
170 return super.end( writer, body );
173 private void appendError( StringBuffer sb, Exception e )
178 private void appendMini( StringBuffer sb, String prefix, List<ArchivaArtifact> relatedArtifacts )
180 // TODO: write 1 line download link for main artifact.
183 private void appendNormal( StringBuffer sb, String prefix, List<ArchivaArtifact> relatedArtifacts )
184 throws RepositoryNotFoundException, RepositoryException
187 * <div class="download">
189 * <div class="c"></div>
193 * <-- main content goes here -->
197 * <div class="c"></div>
202 sb.append( "<div class=\"download\">" );
203 sb.append( "<div class=\"hd\"><div class=\"c\"></div></div>" );
204 sb.append( "<div class=\"bd\"><div class=\"c\">" );
208 if ( relatedArtifacts.size() > 1 )
210 sb.append( "Downloads" );
214 sb.append( "Download" );
216 sb.append( "</h2>" );
219 sb.append( "<p class=\"body\">" );
221 sb.append( "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">" );
222 for ( ArchivaArtifact artifact : relatedArtifacts )
224 String repoId = artifact.getModel().getRepositoryId();
225 ManagedRepositoryContent repo = repositoryFactory.getManagedRepositoryContent( repoId );
227 sb.append( "\n<tr>" );
229 sb.append( "<td class=\"icon\">" );
230 appendImageLink( sb, prefix + repoId, repo, artifact );
231 sb.append( "</td>" );
233 sb.append( "<td class=\"type\">" );
234 appendLink( sb, prefix + repoId, repo, artifact );
235 sb.append( "</td>" );
237 sb.append( "<td class=\"size\">" );
238 appendFilesize( sb, artifact );
239 sb.append( "</td>" );
241 sb.append( "</tr>" );
243 sb.append( "</table>" );
246 sb.append( "</div>" ); // close "downloadbox.bd.c"
247 sb.append( "</div>" ); // close "downloadbox.bd"
249 sb.append( "<div class=\"ft\"><div class=\"c\"></div></div>" );
250 sb.append( "</div>" ); // close "download"
253 private void appendImageLink( StringBuffer sb, String prefix, ManagedRepositoryContent repo,
254 ArchivaArtifact artifact )
256 String type = artifact.getType();
257 String linkText = "<img src=\"" + req.getContextPath() + "/images/" + getDownloadImage( type ) + "\" />";
258 appendLink( sb, prefix, repo, artifact, linkText );
261 private String getDownloadImage( String type )
263 String name = DOWNLOAD_IMAGES.get( type );
264 return name != null ? name : DEFAULT_DOWNLOAD_IMAGE;
267 private static void appendLink( StringBuffer sb, String prefix, ManagedRepositoryContent repo,
268 ArchivaArtifact artifact, String linkText )
270 StringBuffer url = new StringBuffer();
272 String path = repo.toPath( artifact );
274 url.append( prefix );
275 url.append( "/" ).append( path );
277 String filename = path.substring( path.lastIndexOf( "/" ) + 1 );
279 sb.append( "<a href=\"" ).append( StringEscapeUtils.escapeXml( url.toString() ) ).append( "\"" );
280 sb.append( " title=\"" ).append( "Download " ).append( StringEscapeUtils.escapeXml( filename ) ).append( "\"" );
283 sb.append( linkText );
288 private void appendLink( StringBuffer sb, String prefix, ManagedRepositoryContent repo,
289 ArchivaArtifact artifact )
291 String type = artifact.getType();
292 String linkText = StringUtils.capitalize( type );
294 appendLink( sb, prefix, repo, artifact, linkText );
297 private void appendFilesize( StringBuffer sb, ArchivaArtifact artifact )
299 sb.append( decimalFormat.format( artifact.getModel().getSize() ) );
302 public void setArtifactId( String artifactId )
304 this.artifactId = artifactId;
307 public void setGroupId( String groupId )
309 this.groupId = groupId;
312 public void setMini( boolean mini )
317 public void setVersion( String version )
319 this.version = version;