View Javadoc

1   /*
2    * LICENSE
3    *
4    * "THE BEER-WARE LICENSE" (Revision 42):
5    * "Sven Strittmatter" <ich@weltraumschaf.de> wrote this file.
6    * As long as you retain this notice you can do whatever you want with
7    * this stuff. If we meet some day, and you think this stuff is worth it,
8    * you can buy me a beer in return.
9    */
10  package org.jenkinsci.plugins.darcs.browsers;
11  
12  import org.jenkinsci.plugins.darcs.DarcsChangeSet;
13  
14  import java.io.IOException;
15  import java.net.URL;
16  import java.net.MalformedURLException;
17  import java.util.regex.Pattern;
18  import javax.servlet.ServletException;
19  
20  import hudson.Extension;
21  import hudson.Util;
22  import hudson.model.Descriptor;
23  import hudson.scm.RepositoryBrowser;
24  import hudson.util.FormValidation;
25  
26  import net.sf.json.JSONObject;
27  
28  import org.kohsuke.stapler.DataBoundConstructor;
29  import org.kohsuke.stapler.QueryParameter;
30  import org.kohsuke.stapler.StaplerRequest;
31  
32  /**
33   *
34   * @author Sven Strittmatter <ich@weltraumschaf.de>
35   */
36  public class DarcsWeb extends DarcsRepositoryBrowser {
37  
38      /**
39       * Serial version UID.
40       */
41      private static final long serialVersionUID = 1L;
42      public final URL url;
43      public final String repo;
44  
45      @DataBoundConstructor
46      public DarcsWeb(final URL url, final String repo) throws MalformedURLException {
47          this.url  = url;
48          this.repo = repo;
49      }
50  
51      protected DarcsQueryBuilder createDefaultQuery() {
52          final DarcsQueryBuilder query = new DarcsQueryBuilder(DarcsQueryBuilder.SeparatorType.SEMICOLONS);
53          return query.add("r=" + repo);
54      }
55  
56      protected DarcsQueryBuilder createDefaultQuery(final String action) {
57          final DarcsQueryBuilder query = createDefaultQuery();
58          return query.add("a=" + action);
59      }
60  
61      /**
62       * Get the change set URI.
63       *
64       * Format: {@literal http://localhost/cgi-bin/darcsweb.cgi?r=REPO;a=commit;h=HASH}
65       *
66       * @param changeSet
67       * @return
68       * @throws IOException
69       */
70      public URL getChangeSetLink(final DarcsChangeSet changeSet) throws IOException {
71          final DarcsQueryBuilder query = createDefaultQuery("commit");
72          query.add("h=" + changeSet.getHash());
73  
74          return new URL(url + query.toString());
75      }
76  
77      /**
78       * Get file difference link.
79       *
80       * Format: {@literal http://localhost/cgi-bin/darcsweb.cgi?r=REPO;a=filediff;h=HASH;f=FILE}
81       *
82       * @param changeSet
83       * @param file
84       * @return
85       * @throws IOException
86       */
87      public URL getFileDiffLink(final DarcsChangeSet changeSet, final String file) throws IOException {
88          final DarcsQueryBuilder query = createDefaultQuery("filediff");
89          query.add("h=" + changeSet.getHash());
90          query.add("f=" + file);
91  
92          return new URL(url + query.toString());
93      }
94  
95      /**
96       * DarcsWeb repository browser description.
97       */
98      @Extension
99      public static class DescriptorImpl extends Descriptor<RepositoryBrowser<?>> {
100 
101         private static final Pattern URI_PATTERN = Pattern.compile(".+/cgi-bin/darcsweb.cgi");
102 
103         public String getDisplayName() {
104             return "Darcsweb";
105         }
106 
107         /**
108          * Validates the URL given in the config formular.
109          *
110          * @todo implement check.
111          *
112          * @param value
113          * @return
114          * @throws IOException
115          * @throws ServletException
116          */
117         public FormValidation doCheck(@QueryParameter final String value) throws IOException, ServletException {
118 
119             return (new FormValidation.URLCheck() {
120 
121                 @Override
122                 protected FormValidation check() throws IOException, ServletException {
123                     final String uri = Util.fixEmpty(value);
124 
125                     if (null == uri) { // nothing entered yet
126                         return FormValidation.ok();
127                     }
128 
129                     if (!uri.startsWith("http://") && ! uri.startsWith("https://")) {
130                         return FormValidation.errorWithMarkup("The URI should start either with <tt>http://</tt> or "
131                                 + "<tt>https://</tt>!");
132                     }
133 
134                     if (!URI_PATTERN.matcher(uri).matches()) {
135                         return FormValidation.errorWithMarkup("The URI should look like <tt>"
136                                 + "http://.../cgi-bin/darcsweb.cgi</tt>!");
137                     }
138 
139                     try {
140                         if (!findText(open(new URL(uri)), "Crece desde el pueblo el futuro / crece desde el pie")) {
141                             return FormValidation.error("This is a valid URI but it doesn't look like DarcsWeb!");
142                         }
143                     } catch (IOException e) {
144                         handleIOException(uri, e);
145                     }
146 
147                     return FormValidation.ok();
148                 }
149             }).check();
150         }
151 
152         @Override
153         public DarcsWeb newInstance(StaplerRequest req, JSONObject formData) throws FormException {
154             return req.bindParameters(DarcsWeb.class, "darcsweb.darcs.");
155         }
156     }
157 
158 }