You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IPZillaQuery.java 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * Copyright (C) 2010, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.iplog;
  44. import java.io.BufferedReader;
  45. import java.io.IOException;
  46. import java.io.InputStream;
  47. import java.io.InputStreamReader;
  48. import java.io.OutputStream;
  49. import java.io.Reader;
  50. import java.io.UnsupportedEncodingException;
  51. import java.net.ConnectException;
  52. import java.net.CookieHandler;
  53. import java.net.HttpURLConnection;
  54. import java.net.MalformedURLException;
  55. import java.net.Proxy;
  56. import java.net.ProxySelector;
  57. import java.net.URISyntaxException;
  58. import java.net.URL;
  59. import java.net.URLEncoder;
  60. import java.util.Collection;
  61. import java.util.Collections;
  62. import java.util.HashMap;
  63. import java.util.HashSet;
  64. import java.util.LinkedHashMap;
  65. import java.util.List;
  66. import java.util.Map;
  67. import java.util.Set;
  68. import java.util.TreeSet;
  69. import java.util.regex.Matcher;
  70. import java.util.regex.Pattern;
  71. import org.eclipse.jgit.util.HttpSupport;
  72. /** A crude interface to query IPzilla. */
  73. class IPZillaQuery {
  74. private static final String RE_EPL = "^.*(Eclipse Public License|EPL).*$";
  75. private final URL base;
  76. private final String username;
  77. private final String password;
  78. private final ProxySelector proxySelector = ProxySelector.getDefault();
  79. IPZillaQuery(URL base, String username, String password) {
  80. this.base = base;
  81. this.username = username;
  82. this.password = password;
  83. }
  84. Set<CQ> getCQs(Collection<Project> projects) throws IOException {
  85. try {
  86. login();
  87. Set<CQ> cqs = new HashSet<CQ>();
  88. for (Project project : projects)
  89. cqs.addAll(queryOneProject(project));
  90. return cqs;
  91. } finally {
  92. // Kill the IPzilla session and log us out from there.
  93. logout();
  94. }
  95. }
  96. private Set<CQ> queryOneProject(Project project) throws IOException {
  97. Map<String, String> p = new LinkedHashMap<String, String>();
  98. p.put("bugidtype", "include");
  99. p.put("chfieldto", "Now");
  100. p.put("component", project.getID());
  101. p.put("field-1-0-0", "component");
  102. p.put("type-1-0-0", "anyexact");
  103. p.put("value-1-0-0", project.getID());
  104. p.put("ctype", "csv");
  105. StringBuilder req = new StringBuilder();
  106. for (Map.Entry<String, String> e : p.entrySet()) {
  107. if (req.length() > 0)
  108. req.append('&');
  109. req.append(URLEncoder.encode(e.getKey(), "UTF-8"));
  110. req.append('=');
  111. req.append(URLEncoder.encode(e.getValue(), "UTF-8"));
  112. }
  113. URL csv = new URL(new URL(base, "buglist.cgi").toString() + "?" + req);
  114. req = new StringBuilder();
  115. for (String name : new String[] { "bug_severity", "bug_status",
  116. "resolution", "short_desc", "cf_license", "keywords" }) {
  117. if (req.length() > 0)
  118. req.append("%20");
  119. req.append(name);
  120. }
  121. setCookie(csv, "COLUMNLIST", req.toString());
  122. HttpURLConnection conn = open(csv);
  123. if (HttpSupport.response(conn) != HttpURLConnection.HTTP_OK) {
  124. throw new IOException("Query " + csv + " failed: "
  125. + conn.getResponseCode() + " " + conn.getResponseMessage());
  126. }
  127. BufferedReader br = reader(conn);
  128. try {
  129. Set<CQ> cqs = new HashSet<CQ>();
  130. CSV in = new CSV(br);
  131. Map<String, String> row;
  132. while ((row = in.next()) != null) {
  133. CQ cq = parseOneCQ(row);
  134. if (cq != null)
  135. cqs.add(cq);
  136. }
  137. return cqs;
  138. } finally {
  139. br.close();
  140. }
  141. }
  142. private BufferedReader reader(HttpURLConnection conn)
  143. throws UnsupportedEncodingException, IOException {
  144. String encoding = conn.getContentEncoding();
  145. InputStream in = conn.getInputStream();
  146. if (encoding != null && !encoding.equals(""))
  147. return new BufferedReader(new InputStreamReader(in, encoding));
  148. return new BufferedReader(new InputStreamReader(in));
  149. }
  150. private void login() throws MalformedURLException,
  151. UnsupportedEncodingException, ConnectException, IOException {
  152. final URL login = new URL(base, "index.cgi");
  153. StringBuilder req = new StringBuilder();
  154. req.append("Bugzilla_login=");
  155. req.append(URLEncoder.encode(username, "UTF-8"));
  156. req.append('&');
  157. req.append("Bugzilla_password=");
  158. req.append(URLEncoder.encode(password, "UTF-8"));
  159. byte[] reqbin = req.toString().getBytes("UTF-8");
  160. HttpURLConnection c = open(login);
  161. c.setDoOutput(true);
  162. c.setFixedLengthStreamingMode(reqbin.length);
  163. c.setRequestProperty(HttpSupport.HDR_CONTENT_TYPE,
  164. "application/x-www-form-urlencoded");
  165. OutputStream out = c.getOutputStream();
  166. out.write(reqbin);
  167. out.close();
  168. if (HttpSupport.response(c) != HttpURLConnection.HTTP_OK) {
  169. throw new IOException("Login as " + username + " to " + login
  170. + " failed: " + c.getResponseCode() + " "
  171. + c.getResponseMessage());
  172. }
  173. String content = readFully(c);
  174. Matcher matcher = Pattern.compile("<title>(.*)</title>",
  175. Pattern.CASE_INSENSITIVE).matcher(content);
  176. if (!matcher.find()) {
  177. throw new IOException("Login as " + username + " to " + login
  178. + " failed: Response not HTML as expected");
  179. }
  180. String title = matcher.group(1);
  181. if (!"IPZilla Main Page".equals(title)) {
  182. throw new IOException("Login as " + username + " to " + login
  183. + " failed; page title was \"" + title + "\"");
  184. }
  185. }
  186. private String readFully(HttpURLConnection c) throws IOException {
  187. String enc = c.getContentEncoding();
  188. Reader reader;
  189. if (enc != null) {
  190. reader = new InputStreamReader(c.getInputStream(), enc);
  191. } else {
  192. reader = new InputStreamReader(c.getInputStream(), "ISO-8859-1");
  193. }
  194. try {
  195. StringBuilder b = new StringBuilder();
  196. BufferedReader r = new BufferedReader(reader);
  197. String line;
  198. while ((line = r.readLine()) != null) {
  199. b.append(line).append('\n');
  200. }
  201. return b.toString();
  202. } finally {
  203. reader.close();
  204. }
  205. }
  206. private void logout() throws MalformedURLException, ConnectException,
  207. IOException {
  208. HttpSupport.response(open(new URL(base, "relogin.cgi")));
  209. }
  210. private HttpURLConnection open(URL url) throws ConnectException,
  211. IOException {
  212. Proxy proxy = HttpSupport.proxyFor(proxySelector, url);
  213. HttpURLConnection c = (HttpURLConnection) url.openConnection(proxy);
  214. c.setUseCaches(false);
  215. return c;
  216. }
  217. private void setCookie(URL url, String name, String value)
  218. throws IOException {
  219. Map<String, List<String>> cols = new HashMap<String, List<String>>();
  220. cols.put("Set-Cookie", Collections.singletonList(name + "=" + value));
  221. try {
  222. CookieHandler.getDefault().put(url.toURI(), cols);
  223. } catch (URISyntaxException e) {
  224. IOException err = new IOException("Invalid URI format:" + url);
  225. err.initCause(e);
  226. throw err;
  227. }
  228. }
  229. private CQ parseOneCQ(Map<String, String> row) {
  230. long id = Long.parseLong(row.get("bug_id"));
  231. String state = row.get("bug_severity");
  232. String bug_status = row.get("bug_status");
  233. String resolution = row.get("resolution");
  234. String short_desc = row.get("short_desc");
  235. String license = row.get("cf_license");
  236. Set<String> keywords = new TreeSet<String>();
  237. for (String w : row.get("keywords").split(", *"))
  238. keywords.add(w);
  239. // Skip any CQs that were not accepted.
  240. //
  241. if ("closed".equalsIgnoreCase(state)
  242. || "rejected".equalsIgnoreCase(state)
  243. || "withdrawn".equalsIgnoreCase(state))
  244. return null;
  245. // Skip any CQs under the EPL without nonepl keyword
  246. // Skip any CQs with the EPL keyword
  247. //
  248. if (!keywords.contains("nonepl") && license.matches(RE_EPL))
  249. return null;
  250. if (keywords.contains("epl"))
  251. return null;
  252. // Work around CQs that were closed in the wrong state.
  253. //
  254. if ("new".equalsIgnoreCase(state)
  255. || "under_review".equalsIgnoreCase(state)
  256. || state.startsWith("awaiting_")) {
  257. if ("RESOLVED".equalsIgnoreCase(bug_status)
  258. || "CLOSED".equalsIgnoreCase(bug_status)) {
  259. if ("FIXED".equalsIgnoreCase(resolution))
  260. state = "approved";
  261. else
  262. return null;
  263. }
  264. }
  265. StringBuilder use = new StringBuilder();
  266. for (String n : new String[] { "unmodified", "modified", "source",
  267. "binary" }) {
  268. if (keywords.contains(n)) {
  269. if (use.length() > 0)
  270. use.append(' ');
  271. use.append(n);
  272. }
  273. }
  274. if (keywords.contains("sourceandbinary")) {
  275. if (use.length() > 0)
  276. use.append(' ');
  277. use.append("source & binary");
  278. }
  279. CQ cq = new CQ(id);
  280. cq.setDescription(short_desc);
  281. cq.setLicense(license);
  282. cq.setState(state);
  283. if (use.length() > 0)
  284. cq.setUse(use.toString().trim());
  285. return cq;
  286. }
  287. }