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 com.opensymphony.webwork.WebWorkException;
23 import com.opensymphony.webwork.components.Component;
24 import com.opensymphony.xwork.util.OgnlValueStack;
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.model.ArchivaRepository;
35 import org.apache.maven.archiva.repository.layout.BidirectionalRepositoryLayout;
36 import org.apache.maven.archiva.repository.layout.BidirectionalRepositoryLayoutFactory;
37 import org.apache.maven.archiva.repository.layout.LayoutException;
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.Iterator;
44 import java.util.List;
46 import javax.servlet.http.HttpServletRequest;
47 import javax.servlet.http.HttpServletResponse;
48 import javax.servlet.jsp.PageContext;
53 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
56 * @plexus.component role="com.opensymphony.webwork.components.Component" role-hint="download-artifact"
57 * instantiation-strategy="per-lookup"
59 public class DownloadArtifact
63 * @plexus.requirement role-hint="jdo"
65 private ArchivaDAO dao;
70 private BidirectionalRepositoryLayoutFactory layoutFactory;
72 private HttpServletRequest req;
74 private HttpServletResponse res;
76 private String groupId;
78 private String artifactId;
80 private String version;
82 private boolean mini = false;
84 private DecimalFormat decimalFormat;
86 public DownloadArtifact( OgnlValueStack stack, PageContext pageContext )
89 decimalFormat = new DecimalFormat( "#,#00" );
90 this.req = (HttpServletRequest) pageContext.getRequest();
91 this.res = (HttpServletResponse) pageContext.getResponse();
94 dao = (ArchivaDAO) PlexusTagUtil.lookup( pageContext, ArchivaDAO.ROLE, "jdo" );
95 layoutFactory = (BidirectionalRepositoryLayoutFactory) PlexusTagUtil
96 .lookup( pageContext, BidirectionalRepositoryLayoutFactory.class );
98 catch ( ComponentLookupException e )
100 throw new RuntimeException( e.getMessage(), e );
104 public boolean end( Writer writer, String body )
106 StringBuffer sb = new StringBuffer();
110 Constraint constraint = new ArtifactsRelatedConstraint( groupId, artifactId, version );
111 List relatedArtifacts = dao.getArtifactDAO().queryArtifacts( constraint );
113 if ( relatedArtifacts != null )
115 String repoId = ( (ArchivaArtifact) relatedArtifacts.get( 0 ) ).getModel().getRepositoryId();
116 ArchivaRepository repo = dao.getRepositoryDAO().getRepository( repoId );
117 BidirectionalRepositoryLayout layout = layoutFactory.getLayout( repo.getLayoutType() );
119 String prefix = req.getContextPath() + "/repository/" + repoId;
123 appendMini( sb, prefix, repo, layout, relatedArtifacts );
127 appendNormal( sb, prefix, repo, layout, relatedArtifacts );
131 catch ( ObjectNotFoundException e )
133 appendError( sb, e );
135 catch ( ArchivaDatabaseException e )
137 appendError( sb, e );
139 catch ( LayoutException e )
141 appendError( sb, e );
146 writer.write( sb.toString() );
148 catch ( IOException e )
150 throw new WebWorkException( "IOError: " + e.getMessage(), e );
153 return super.end( writer, body );
156 private void appendError( StringBuffer sb, Exception e )
161 private void appendMini( StringBuffer sb, String prefix, ArchivaRepository repo,
162 BidirectionalRepositoryLayout layout, List relatedArtifacts )
167 private void appendNormal( StringBuffer sb, String prefix, ArchivaRepository repo,
168 BidirectionalRepositoryLayout layout, List relatedArtifacts )
171 * <div class="download">
173 * <div class="c"></div>
177 * <-- main content goes here -->
181 * <div class="c"></div>
186 sb.append( "<div class=\"download\">" );
187 sb.append( "<div class=\"hd\"><div class=\"c\"></div></div>" );
188 sb.append( "<div class=\"bd\"><div class=\"c\">" );
192 if ( relatedArtifacts.size() > 1 )
194 sb.append( "Downloads" );
198 sb.append( "Download" );
200 sb.append( "</h2>" );
203 sb.append( "<p class=\"body\">" );
205 sb.append( "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">" );
206 Iterator it = relatedArtifacts.iterator();
207 while ( it.hasNext() )
209 ArchivaArtifact artifact = (ArchivaArtifact) it.next();
212 sb.append( "<td class=\"icon\">" );
213 appendImageLink( sb, prefix, layout, artifact );
214 sb.append( "</td>" );
216 sb.append( "<td class=\"type\">" );
217 appendLink( sb, prefix, layout, artifact );
218 sb.append( "</td>" );
220 sb.append( "<td class=\"size\">" );
221 appendFilesize( sb, artifact );
222 sb.append( "</td>" );
226 sb.append( "</table>" );
229 sb.append( "</div>" ); // close "downloadbox.bd.c"
230 sb.append( "</div>" ); // close "downloadbox.bd"
232 sb.append( "<div class=\"ft\"><div class=\"c\"></div></div>" );
233 sb.append( "</div>" ); // close "download"
236 private void appendImageLink( StringBuffer sb, String prefix, BidirectionalRepositoryLayout layout, ArchivaArtifact artifact )
238 StringBuffer url = new StringBuffer();
239 String path = layout.toPath( artifact );
240 String type = artifact.getType();
242 url.append( prefix );
243 url.append( "/" ).append( path );
245 String filename = path.substring( path.lastIndexOf( "/" ) + 1 );
247 sb.append( "<a href=\"" ).append( StringEscapeUtils.escapeXml( url.toString() ) ).append( "\"" );
248 sb.append( " title=\"" ).append( "Download " ).append( StringEscapeUtils.escapeXml( filename ) ).append( "\"" );
251 sb.append( "<img src=\"" ).append( req.getContextPath() );
252 sb.append( "/images/download-type-" ).append( type ).append( ".png\" />" );
257 private void appendLink( StringBuffer sb, String prefix, BidirectionalRepositoryLayout layout,
258 ArchivaArtifact artifact )
260 StringBuffer url = new StringBuffer();
261 String path = layout.toPath( artifact );
262 String type = artifact.getType();
264 url.append( prefix );
265 url.append( "/" ).append( path );
267 String filename = path.substring( path.lastIndexOf( "/" ) + 1 );
269 sb.append( "<a href=\"" ).append( StringEscapeUtils.escapeXml( url.toString() ) ).append( "\"" );
270 sb.append( " title=\"" ).append( "Download " ).append( StringEscapeUtils.escapeXml( filename ) ).append( "\"" );
273 sb.append( StringUtils.capitalize( type ) );
277 private void appendFilesize( StringBuffer sb, ArchivaArtifact artifact )
279 sb.append( decimalFormat.format( artifact.getModel().getSize() ) );
282 public void setArtifactId( String artifactId )
284 this.artifactId = artifactId;
287 public void setGroupId( String groupId )
289 this.groupId = groupId;
292 public void setMini( boolean mini )
297 public void setVersion( String version )
299 this.version = version;