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 javax.servlet.http.HttpServletRequest;
10  import javax.servlet.http.HttpServletResponse;
11  import org.apache.commons.logging.Log;
12  import org.apache.commons.logging.LogFactory;
13  import org.apache.struts.action.Action;
14  import org.apache.struts.action.ActionError;
15  import org.apache.struts.action.ActionErrors;
16  import org.apache.struts.action.ActionForm;
17  import org.apache.struts.action.ActionForward;
18  import org.apache.struts.action.ActionMapping;
19  import org.apache.struts.action.ActionMessage;
20  import org.apache.struts.action.ActionMessages;
21  import org.apache.struts.action.DynaActionForm;
22  import org.repoweb.model.Artifact;
23  import org.repoweb.model.ArtifactList;
24  import org.repoweb.model.Group;
25  import org.repoweb.model.Manager;
26  import org.repoweb.model.Repository;
27  import org.repoweb.model.UnknownArtifactException;
28  import org.repoweb.model.UnknownGroupIdException;
29  import org.repoweb.pom.Project;
30  import org.repoweb.pom.XMLProject;
31  import org.xml.sax.InputSource;
32  /***
33   * Find by id (or attached to a group) artifacts defined in the repository.
34   *
35   * <p>
36   * <b>IN</b>
37   * </p>
38   *
39   * <ul>
40   * <li>
41   * <code>QuickFind.SEARCH_VALUE</code> (request/optional): the pattern to search for.
42   * </li>
43   * <li>
44   * <code>FindArtifact.SEARCH_BY_GROUP_KEY</code> (request/optional): the group Id.
45   * </li>
46   * </ul>
47   *
48   * <p>
49   * <b>OUT (only one of these)</b>
50   * </p>
51   *
52   * <ul>
53   * <li>
54   * <code>FindArtifact.RESULT</code> (request): result of the search.
55   * (if the action parameter is different to 'byGroupAndTypeAndFilename')
56   * </li>
57   * <li>
58   * <code>FindArtifact.RESULT_ARTIFACT</code> (request): artifact found by search
59   *  (if the action parameter is set to 'byGroupAndTypeAndFilename').
60   * </li>
61   * <li>
62   * <code>FindArtifact.RESULT_POM</code> (request): pom bean attached to the
63   *   artifact (if any)
64   * </li>
65   * </ul>
66   */
67  public class FindArtifact extends Action {
68      public static final String RESULT = "FindArtifact.result";
69      public static final String RESULT_ARTIFACT = "FindArtifact.artifact";
70      public static final String RESULT_POM = "FindArtifact.pom";
71      public static final String SEARCH_BY_GROUP_KEY = "group";
72      public static final String SEARCH_BY_TYPE_KEY = "type";
73      public static final String SEARCH_BY_FILE_KEY = "filename";
74      private static final String ALL = "*";
75      protected static final Log LOG = LogFactory.getLog(FindArtifact.class);
76  
77      public ActionForward execute(ActionMapping mapping, ActionForm objForm,
78          HttpServletRequest request, HttpServletResponse res)
79          throws UnknownGroupIdException, IOException {
80          Repository repo = Manager.getRepository();
81          DynaActionForm form = (DynaActionForm)objForm;
82          String forward = "success";
83  
84          if ("byType".equals(mapping.getParameter())) {
85              findArtifactByType(form, repo, request);
86          }
87          else if ("byGroupAndTypeAndFilename".equals(mapping.getParameter())) {
88              forward = findOneArtifact(form, repo, forward, request);
89          }
90          else if ("byName".equals(mapping.getParameter())) {
91              findArtifactLike(form, repo, request);
92          }
93          else if ("byClass".equals(mapping.getParameter())) {
94              findArtifactByClass(form, repo, request);
95          }
96          else {
97              String searchPattern = (String)form.get(SEARCH_BY_GROUP_KEY);
98  
99              Group group = repo.getGroup(searchPattern.trim());
100             request.setAttribute(RESULT, group.getArtifacts());
101         }
102         return mapping.findForward(forward);
103     }
104 
105 
106     private void findArtifactLike(DynaActionForm form, Repository repo,
107         HttpServletRequest request) {
108         String searchPattern = (String)form.get(QuickFind.SEARCH_VALUE);
109         ArtifactList list = repo.findArtifacts(ALL, ALL, ALL + searchPattern + ALL);
110         request.setAttribute(RESULT, list);
111     }
112 
113 
114     private void findArtifactByClass(DynaActionForm form, Repository repo,
115         HttpServletRequest request) {
116         String searchPattern = (String)form.get(QuickFind.SEARCH_VALUE);
117         ArtifactList list = repo.findArtifactByClass(searchPattern);
118         request.setAttribute(RESULT, list);
119     }
120 
121 
122     private void findArtifactByType(DynaActionForm form, Repository repo,
123         HttpServletRequest request) {
124         String groupId = (String)form.get(SEARCH_BY_GROUP_KEY);
125         String typeId = (String)form.get(SEARCH_BY_TYPE_KEY);
126 
127         request.setAttribute(RESULT, repo.findArtifacts(groupId, typeId + "s", ALL));
128     }
129 
130 
131     protected String findOneArtifact(final DynaActionForm form, final Repository repo,
132         final String forward, HttpServletRequest request) {
133         String groupId = (String)form.get(SEARCH_BY_GROUP_KEY);
134         String typeId = (String)form.get(SEARCH_BY_TYPE_KEY);
135         String filename = (String)form.get(SEARCH_BY_FILE_KEY);
136 
137         try {
138             Artifact artifact = repo.getArtifact(groupId, typeId, filename);
139             request.setAttribute(RESULT_ARTIFACT, artifact);
140             if (artifact.getPom() != null) {
141                 putPomInContext(artifact.getPom(), request);
142             }
143             else {
144                 try {
145                     Artifact defaultPom =
146                         Manager.getDefaultPomRepository().getArtifact(artifact.getGroupId(),
147                             "pom", artifact.getArtifactId() + ".pom");
148                     putPomInContext(defaultPom, request);
149                 }
150                 catch (UnknownArtifactException error) {
151                     if (LOG.isDebugEnabled()) {
152                         LOG.debug("No default POM for " + artifact);
153                     }
154                 }
155             }
156             return forward;
157         }
158         catch (UnknownArtifactException e) {
159             ActionErrors errors = new ActionErrors();
160             errors.add(ActionErrors.GLOBAL_ERROR,
161                 new ActionError("explorer.unknown.artifact", groupId, typeId, filename));
162             saveErrors(request, errors);
163             return "no_artifact";
164         }
165     }
166 
167 
168     private void putPomInContext(Artifact pomArtifact, HttpServletRequest request) {
169         try {
170             final InputStream stream = pomArtifact.getInputStream();
171             try {
172                 Project pom = XMLProject.toBean(new InputSource(stream));
173                 request.setAttribute(RESULT_POM, pom);
174             }
175             finally {
176                 stream.close();
177             }
178         }
179         catch (Exception e) {
180             LOG.error("Unable to put Pom In Context !", e);
181             ActionMessages messages = new ActionMessages();
182             messages.add(ActionMessages.GLOBAL_MESSAGE,
183                 new ActionMessage("explorer.pom.load.error"));
184             saveMessages(request, messages);
185         }
186     }
187 }