View Javadoc
1   /*
2    * REPOWEB, repository manager.
3    *
4    * Terms of license - http://opensource.org/licenses/apachepl.php
5    */
6   package org.repoweb.action;
7   import java.io.IOException;
8   import java.io.InputStream;
9   import java.io.OutputStream;
10  import javax.servlet.http.HttpServletRequest;
11  import javax.servlet.http.HttpServletResponse;
12  import org.apache.struts.action.ActionForm;
13  import org.apache.struts.action.ActionForward;
14  import org.apache.struts.action.ActionMapping;
15  import org.apache.struts.action.DynaActionForm;
16  import org.repoweb.model.Artifact;
17  import org.repoweb.model.Manager;
18  import org.repoweb.model.UnknownGroupIdException;
19  /***
20   * Action to donwload an artifact.
21   */
22  public class DonwloadArtifact extends FindArtifact {
23      public ActionForward execute(ActionMapping mapping, ActionForm objForm,
24          HttpServletRequest request, HttpServletResponse res)
25          throws UnknownGroupIdException, IOException {
26          String forward =
27              findOneArtifact((DynaActionForm)objForm, Manager.getRepository(), null,
28                  request);
29          if (forward == null) {
30              Artifact artifact = (Artifact)request.getAttribute(RESULT_ARTIFACT);
31  
32              if (LOG.isInfoEnabled()) {
33                  LOG.info("DonwloadArtifact : " + artifact + " [length="
34                      + artifact.getLength() + "]" + " [mime="
35                      + determineMimeType(artifact) + "]");
36              }
37  
38              res.setContentLength((int)artifact.getLength());
39              res.setContentType(determineMimeType(artifact));
40  
41              donwload(artifact, res);
42          }
43          else {
44              LOG.error("Unable to donwload an unknown Artifact : " + objForm);
45              res.sendError(404);
46          }
47  
48          return null;
49      }
50  
51  
52      private void donwload(Artifact artifact, HttpServletResponse res)
53          throws IOException {
54          OutputStream out = res.getOutputStream();
55          try {
56              InputStream in = artifact.getInputStream();
57              try {
58                  int nread;
59                  byte[] buffer = new byte[1024];
60                  while ((nread = in.read(buffer)) > 0) {
61                      out.write(buffer, 0, nread);
62                  }
63              }
64              finally {
65                  in.close();
66              }
67          }
68          finally {
69              out.close();
70          }
71      }
72  
73  
74      private String determineMimeType(Artifact artifact) {
75          final String extension = getExtension(artifact.getFileName());
76  
77          if ("jar".equalsIgnoreCase(extension)) {
78              return "application/java-archive";
79          }
80          else if ("pom".equalsIgnoreCase(extension)) {
81              return "text/xml";
82          }
83  
84          String mimeType = getServlet().getServletContext().getMimeType(extension);
85          if (mimeType == null) {
86              mimeType = "application/octet-stream";
87          }
88          return mimeType;
89      }
90  
91  
92      /***
93       * return the extension for a file.
94       * @param filename
95       * @return file extension
96       */
97      private String getExtension(String filename) {
98          int pos = filename.lastIndexOf('.');
99          if (pos < 0 || filename.length() == pos + 1) {
100             return "";
101         }
102         return filename.substring(pos + 1);
103     }
104 }