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.List;
45 import javax.servlet.http.HttpServletRequest;
46 import javax.servlet.http.HttpServletResponse;
47 import javax.servlet.jsp.PageContext;
53 * @plexus.component role="org.apache.struts2.components.Component" role-hint="download-artifact"
54 * instantiation-strategy="per-lookup"
56 public class DownloadArtifact
60 * @plexus.requirement role-hint="jdo"
62 private ArchivaDAO dao;
67 private RepositoryContentFactory repositoryFactory;
69 private HttpServletRequest req;
71 @SuppressWarnings("unused")
72 private HttpServletResponse res;
74 private String groupId;
76 private String artifactId;
78 private String version;
80 private boolean mini = false;
82 private DecimalFormat decimalFormat;
84 public DownloadArtifact( ValueStack stack, PageContext pageContext )
87 decimalFormat = new DecimalFormat( "#,#00" );
88 this.req = (HttpServletRequest) pageContext.getRequest();
89 this.res = (HttpServletResponse) pageContext.getResponse();
92 dao = (ArchivaDAO) PlexusTagUtil.lookup( pageContext, ArchivaDAO.ROLE, "jdo" );
93 repositoryFactory = (RepositoryContentFactory) PlexusTagUtil.lookup( pageContext,
94 RepositoryContentFactory.class );
96 catch ( ComponentLookupException e )
98 throw new RuntimeException( e.getMessage(), e );
103 public boolean end( Writer writer, String body )
105 StringBuffer sb = new StringBuffer();
109 Constraint constraint = new ArtifactsRelatedConstraint( groupId, artifactId, version );
110 List<ArchivaArtifact> relatedArtifacts = dao.getArtifactDAO().queryArtifacts( constraint );
112 if ( relatedArtifacts != null && relatedArtifacts.size() > 0 )
114 String repoId = ( (ArchivaArtifact) relatedArtifacts.get( 0 ) ).getModel().getRepositoryId();
115 ManagedRepositoryContent repo = repositoryFactory.getManagedRepositoryContent( repoId );
117 String prefix = req.getContextPath() + "/repository/" + repoId;
121 appendMini( sb, prefix, repo, relatedArtifacts );
125 appendNormal( sb, prefix, repo, relatedArtifacts );
129 catch ( ObjectNotFoundException e )
131 appendError( sb, e );
133 catch ( ArchivaDatabaseException e )
135 appendError( sb, e );
137 catch ( RepositoryNotFoundException e )
139 // TODO Auto-generated catch block
142 catch ( RepositoryException e )
144 // TODO Auto-generated catch block
150 writer.write( sb.toString() );
152 catch ( IOException e )
154 throw new StrutsException( "IOError: " + e.getMessage(), e );
157 return super.end( writer, body );
160 private void appendError( StringBuffer sb, Exception e )
165 private void appendMini( StringBuffer sb, String prefix, ManagedRepositoryContent repo,
166 List<ArchivaArtifact> relatedArtifacts )
168 // TODO: write 1 line download link for main artifact.
171 private void appendNormal( StringBuffer sb, String prefix, ManagedRepositoryContent repo,
172 List<ArchivaArtifact> relatedArtifacts )
175 * <div class="download">
177 * <div class="c"></div>
181 * <-- main content goes here -->
185 * <div class="c"></div>
190 sb.append( "<div class=\"download\">" );
191 sb.append( "<div class=\"hd\"><div class=\"c\"></div></div>" );
192 sb.append( "<div class=\"bd\"><div class=\"c\">" );
196 if ( relatedArtifacts.size() > 1 )
198 sb.append( "Downloads" );
202 sb.append( "Download" );
204 sb.append( "</h2>" );
207 sb.append( "<p class=\"body\">" );
209 sb.append( "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">" );
210 for ( ArchivaArtifact artifact : relatedArtifacts )
212 sb.append( "\n<tr>" );
214 sb.append( "<td class=\"icon\">" );
215 appendImageLink( sb, prefix, repo, artifact );
216 sb.append( "</td>" );
218 sb.append( "<td class=\"type\">" );
219 appendLink( sb, prefix, repo, artifact );
220 sb.append( "</td>" );
222 sb.append( "<td class=\"size\">" );
223 appendFilesize( sb, artifact );
224 sb.append( "</td>" );
226 sb.append( "</tr>" );
228 sb.append( "</table>" );
231 sb.append( "</div>" ); // close "downloadbox.bd.c"
232 sb.append( "</div>" ); // close "downloadbox.bd"
234 sb.append( "<div class=\"ft\"><div class=\"c\"></div></div>" );
235 sb.append( "</div>" ); // close "download"
238 private void appendImageLink( StringBuffer sb, String prefix, ManagedRepositoryContent repo,
239 ArchivaArtifact artifact )
241 String type = artifact.getType();
242 String linkText = "<img src=\"" + req.getContextPath() + "/images/download-type-" + type + ".png\" />";
243 appendLink( sb, prefix, repo, artifact, linkText );
246 private static void appendLink( StringBuffer sb, String prefix, ManagedRepositoryContent repo,
247 ArchivaArtifact artifact, String linkText )
249 StringBuffer url = new StringBuffer();
251 String path = repo.toPath( artifact );
253 url.append( prefix );
254 url.append( "/" ).append( path );
256 String filename = path.substring( path.lastIndexOf( "/" ) + 1 );
258 sb.append( "<a href=\"" ).append( StringEscapeUtils.escapeXml( url.toString() ) ).append( "\"" );
259 sb.append( " title=\"" ).append( "Download " ).append( StringEscapeUtils.escapeXml( filename ) ).append( "\"" );
262 sb.append( linkText );
267 private void appendLink( StringBuffer sb, String prefix, ManagedRepositoryContent repo,
268 ArchivaArtifact artifact )
270 String type = artifact.getType();
271 String linkText = StringUtils.capitalize( type );
273 appendLink( sb, prefix, repo, artifact, linkText );
276 private void appendFilesize( StringBuffer sb, ArchivaArtifact artifact )
278 sb.append( decimalFormat.format( artifact.getModel().getSize() ) );
281 public void setArtifactId( String artifactId )
283 this.artifactId = artifactId;
286 public void setGroupId( String groupId )
288 this.groupId = groupId;
291 public void setMini( boolean mini )
296 public void setVersion( String version )
298 this.version = version;