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.StringReader;
10  import java.lang.reflect.InvocationTargetException;
11  import javax.servlet.http.HttpServletRequest;
12  import javax.servlet.http.HttpServletResponse;
13  import javax.xml.parsers.ParserConfigurationException;
14  import javax.xml.transform.TransformerException;
15  import org.apache.commons.beanutils.BeanUtils;
16  import org.apache.commons.logging.Log;
17  import org.apache.commons.logging.LogFactory;
18  import org.apache.struts.action.Action;
19  import org.apache.struts.action.ActionError;
20  import org.apache.struts.action.ActionErrors;
21  import org.apache.struts.action.ActionForm;
22  import org.apache.struts.action.ActionForward;
23  import org.apache.struts.action.ActionMapping;
24  import org.apache.struts.action.ActionMessage;
25  import org.apache.struts.action.ActionMessages;
26  import org.apache.struts.action.DynaActionForm;
27  import org.repoweb.model.Artifact;
28  import org.repoweb.model.BuildFailureException;
29  import org.repoweb.model.Manager;
30  import org.repoweb.model.UnknownArtifactException;
31  import org.repoweb.pom.Project;
32  import org.repoweb.pom.XMLProject;
33  import org.repoweb.xml.XMLUtil;
34  import org.xml.sax.InputSource;
35  import org.xml.sax.SAXException;
36  /***
37   * Action class to edit/create a Project Object Model.
38   */
39  public class EditPom extends Action {
40      public static final String FROM_ARTIFACT = "EditPom.FROM_ARTIFACT";
41      private static final Log LOG = LogFactory.getLog(EditPom.class);
42      private static final String DEPENDENCIES_AS_XML = "depenciesAsXml";
43      private static final String ARTIFACT_ID = "artifactId";
44      private static final String GROUP_ID = "groupId";
45      private static final String CURRENT_VERSION = "currentVersion";
46  
47      public ActionForward execute(ActionMapping mapping, ActionForm objForm,
48          HttpServletRequest request, HttpServletResponse res)
49          throws IllegalAccessException, InvocationTargetException, BuildFailureException {
50          Project project = new Project();
51          BeanUtils.copyProperties(project, objForm);
52  
53          if ("get".equals(mapping.getParameter())) {
54              doGetPom(request, project, objForm);
55          }
56          else {
57              ActionErrors errors = new ActionErrors();
58              checkProperty(ARTIFACT_ID, project.getArtifactId(), errors);
59              checkProperty(GROUP_ID, project.getGroupId(), errors);
60              checkProperty(CURRENT_VERSION, project.getCurrentVersion(), errors);
61              if (!errors.isEmpty()) {
62                  saveErrors(request, errors);
63                  return mapping.findForward("error");
64              }
65  
66              String depsXml = (String)((DynaActionForm)objForm).get(DEPENDENCIES_AS_XML);
67              if (depsXml != null && depsXml.trim().length() != 0) {
68                  addDependenciesToProject(depsXml, project);
69              }
70              project.setFreeContent(xmlize(project.getFreeContent()));
71              Artifact pom = Manager.getRepository().buildPom(project);
72  
73              Artifact fromArtifact =
74                  (Artifact)request.getSession().getAttribute(FROM_ARTIFACT);
75              if (fromArtifact != null) {
76                  request.getSession().removeAttribute(FROM_ARTIFACT);
77                  return UriBeautifier.buildActionForwardTo(fromArtifact);
78              }
79              else {
80                  request.setAttribute(FindArtifact.RESULT_ARTIFACT, pom);
81              }
82          }
83  
84          return mapping.findForward("success");
85      }
86  
87  
88      private void fillActionFormWithDefaultPom(Project project, ActionForm objForm) {
89          try {
90              Artifact artifact =
91                  Manager.getDefaultPomRepository().getArtifact(project.getGroupId(),
92                      "pom", project.getArtifactId() + ".pom");
93              Project defaultPom = loadProject(artifact);
94              defaultPom.setCurrentVersion(project.getCurrentVersion());
95              putProjectIntoActionForm(defaultPom, objForm);
96              BeanUtils.copyProperties(project, objForm);
97          }
98          catch (UnknownArtifactException e) {
99              ; // No default available
100         }
101         catch (Exception error) {
102             LOG.error("Should never occur error occured :)", error); // Should never occur
103         }
104     }
105 
106 
107     private void addDependenciesToProject(String depsXml, Project project)
108         throws BuildFailureException {
109         try {
110             XMLProject.dependenciesToBean(new InputSource(
111                     new StringReader(xmlize(depsXml))), project);
112         }
113         catch (Exception error) {
114             LOG.error("Unable to understand dependency tag ! : " + depsXml, error);
115             throw new BuildFailureException("Unable to understand dependency tag", error);
116         }
117     }
118 
119 
120     private String xmlize(String pseudoXml) {
121         if (pseudoXml == null) {
122             return null;
123         }
124         return pseudoXml.replaceAll("&lt;", "<").replaceAll("&gt;", ">");
125     }
126 
127 
128     private void doGetPom(HttpServletRequest request, Project project, ActionForm objForm) {
129         try {
130             Artifact art = getPomArtifact(project);
131             request.setAttribute(FindArtifact.RESULT_ARTIFACT, art);
132 
133             Project old = loadProject(art);
134             putProjectIntoActionForm(old, objForm);
135         }
136         catch (UnknownArtifactException e) {
137             fillActionFormWithDefaultPom(project, objForm);
138 
139             ActionMessages messages = new ActionMessages();
140             messages.add(ActionMessages.GLOBAL_MESSAGE,
141                 new ActionMessage("explorer.pom.new", project.getArtifactId()));
142             saveMessages(request, messages);
143         }
144         catch (Exception error) {
145             LOG.error("Unable to retrieve POM data !", error);
146             ActionErrors errors = new ActionErrors();
147             errors.add(ActionErrors.GLOBAL_MESSAGE,
148                 new ActionError("explorer.pom.load.error", error.getLocalizedMessage()));
149             saveErrors(request, errors);
150         }
151     }
152 
153 
154     private void putProjectIntoActionForm(Project old, ActionForm objForm)
155         throws TransformerException, ParserConfigurationException, IllegalAccessException, 
156             InvocationTargetException {
157         old.setFreeContent(XMLUtil.format(old.getFreeContent()));
158         ((DynaActionForm)objForm).set(DEPENDENCIES_AS_XML,
159             XMLUtil.format(XMLProject.dependenciesToXml(old)));
160         BeanUtils.copyProperties(objForm, old);
161     }
162 
163 
164     private Artifact getPomArtifact(Project project)
165         throws UnknownArtifactException {
166         return Manager.getRepository().getArtifact(project.getGroupId(), "pom",
167             project.getArtifactId() + "-" + project.getCurrentVersion() + ".pom");
168     }
169 
170 
171     private Project loadProject(Artifact art)
172         throws IOException, ParserConfigurationException, SAXException, 
173             TransformerException {
174         final InputStream stream = art.getInputStream();
175         try {
176             return XMLProject.toBean(new InputSource(stream));
177         }
178         finally {
179             stream.close();
180         }
181     }
182 
183 
184     private static void checkProperty(final String propName, final String propValue,
185         ActionErrors errors) {
186         if (isInvalid(propValue)) {
187             errors.add(propName, new ActionError("explorer.pom.data.empty", propName));
188         }
189     }
190 
191 
192     private static boolean isInvalid(String value) {
193         return value == null || "".equals(value.trim());
194     }
195 }