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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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.text.MessageFormat;
  61. import java.util.Collection;
  62. import java.util.Collections;
  63. import java.util.HashMap;
  64. import java.util.HashSet;
  65. import java.util.LinkedHashMap;
  66. import java.util.List;
  67. import java.util.Map;
  68. import java.util.Set;
  69. import java.util.TreeSet;
  70. import java.util.regex.Matcher;
  71. import java.util.regex.Pattern;
  72. import org.eclipse.jgit.util.HttpSupport;
  73. /** A crude interface to query IPzilla. */
  74. class IPZillaQuery {
  75. private static final String RE_EPL = "^.*(Eclipse Public License|EPL).*$";
  76. private final URL base;
  77. private final String username;
  78. private final String password;
  79. private final ProxySelector proxySelector = ProxySelector.getDefault();
  80. IPZillaQuery(URL base, String username, String password) {
  81. this.base = base;
  82. this.username = username;
  83. this.password = password;
  84. }
  85. Set<CQ> getCQs(Collection<Project> projects) throws IOException {
  86. try {
  87. login();
  88. Set<CQ> cqs = new HashSet<CQ>();
  89. for (Project project : projects)
  90. cqs.addAll(queryOneProject(project));
  91. return cqs;
  92. } finally {
  93. // Kill the IPzilla session and log us out from there.
  94. logout();
  95. }
  96. }
  97. private Set<CQ> queryOneProject(Project project) throws IOException {
  98. Map<String, String> p = new LinkedHashMap<String, String>();
  99. p.put("bugidtype", "include");
  100. p.put("chfieldto", "Now");
  101. p.put("component", project.getID());
  102. p.put("field-1-0-0", "component");
  103. p.put("type-1-0-0", "anyexact");
  104. p.put("value-1-0-0", project.getID());
  105. p.put("ctype", "csv");
  106. StringBuilder req = new StringBuilder();
  107. for (Map.Entry<String, String> e : p.entrySet()) {
  108. if (req.length() > 0)
  109. req.append('&');
  110. req.append(URLEncoder.encode(e.getKey(), "UTF-8"));
  111. req.append('=');
  112. req.append(URLEncoder.encode(e.getValue(), "UTF-8"));
  113. }
  114. URL csv = new URL(new URL(base, "buglist.cgi").toString() + "?" + req);
  115. req = new StringBuilder();
  116. for (String name : new String[] { "bug_severity", "bug_status",
  117. "resolution", "short_desc", "cf_license", "keywords" }) {
  118. if (req.length() > 0)
  119. req.append("%20");
  120. req.append(name);
  121. }
  122. setCookie(csv, "COLUMNLIST", req.toString());
  123. HttpURLConnection conn = open(csv);
  124. if (HttpSupport.response(conn) != HttpURLConnection.HTTP_OK) {
  125. throw new IOException(MessageFormat.format(IpLogText.get().queryFailed
  126. , csv, conn.getResponseCode() + " " + conn.getResponseMessage()));
  127. }
  128. BufferedReader br = reader(conn);
  129. try {
  130. Set<CQ> cqs = new HashSet<CQ>();
  131. CSV in = new CSV(br);
  132. Map<String, String> row;
  133. while ((row = in.next()) != null) {
  134. CQ cq = parseOneCQ(row);
  135. if (cq != null)
  136. cqs.add(cq);
  137. }
  138. return cqs;
  139. } finally {
  140. br.close();
  141. }
  142. }
  143. private BufferedReader reader(HttpURLConnection conn)
  144. throws UnsupportedEncodingException, IOException {
  145. String encoding = conn.getContentEncoding();
  146. InputStream in = conn.getInputStream();
  147. if (encoding != null && !encoding.equals(""))
  148. return new BufferedReader(new InputStreamReader(in, encoding));
  149. return new BufferedReader(new InputStreamReader(in));
  150. }
  151. private void login() throws MalformedURLException,
  152. UnsupportedEncodingException, ConnectException, IOException {
  153. final URL login = new URL(base, "index.cgi");
  154. StringBuilder req = new StringBuilder();
  155. req.append("Bugzilla_login=");
  156. req.append(URLEncoder.encode(username, "UTF-8"));
  157. req.append('&');
  158. req.append("Bugzilla_password=");
  159. req.append(URLEncoder.encode(password, "UTF-8"));
  160. byte[] reqbin = req.toString().getBytes("UTF-8");
  161. HttpURLConnection c = open(login);
  162. c.setDoOutput(true);
  163. c.setFixedLengthStreamingMode(reqbin.length);
  164. c.setRequestProperty(HttpSupport.HDR_CONTENT_TYPE,
  165. "application/x-www-form-urlencoded");
  166. OutputStream out = c.getOutputStream();
  167. out.write(reqbin);
  168. out.close();
  169. if (HttpSupport.response(c) != HttpURLConnection.HTTP_OK) {
  170. throw new IOException(MessageFormat.format(IpLogText.get().loginFailed
  171. , username, login, c.getResponseCode() + " " + 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(MessageFormat.format(IpLogText.get().loginFailed
  178. , username, login, IpLogText.get().responseNotHTMLAsExpected));
  179. }
  180. String title = matcher.group(1);
  181. if (!"IPZilla Main Page".equals(title)) {
  182. throw new IOException(MessageFormat.format(IpLogText.get().loginFailed
  183. , username, login
  184. , MessageFormat.format(IpLogText.get().pageTitleWas, title)));
  185. }
  186. }
  187. private String readFully(HttpURLConnection c) throws IOException {
  188. String enc = c.getContentEncoding();
  189. Reader reader;
  190. if (enc != null) {
  191. reader = new InputStreamReader(c.getInputStream(), enc);
  192. } else {
  193. reader = new InputStreamReader(c.getInputStream(), "ISO-8859-1");
  194. }
  195. try {
  196. StringBuilder b = new StringBuilder();
  197. BufferedReader r = new BufferedReader(reader);
  198. String line;
  199. while ((line = r.readLine()) != null) {
  200. b.append(line).append('\n');
  201. }
  202. return b.toString();
  203. } finally {
  204. reader.close();
  205. }
  206. }
  207. private void logout() throws MalformedURLException, ConnectException,
  208. IOException {
  209. HttpSupport.response(open(new URL(base, "relogin.cgi")));
  210. }
  211. private HttpURLConnection open(URL url) throws ConnectException,
  212. IOException {
  213. Proxy proxy = HttpSupport.proxyFor(proxySelector, url);
  214. HttpURLConnection c = (HttpURLConnection) url.openConnection(proxy);
  215. c.setUseCaches(false);
  216. return c;
  217. }
  218. private void setCookie(URL url, String name, String value)
  219. throws IOException {
  220. Map<String, List<String>> cols = new HashMap<String, List<String>>();
  221. cols.put("Set-Cookie", Collections.singletonList(name + "=" + value));
  222. try {
  223. CookieHandler.getDefault().put(url.toURI(), cols);
  224. } catch (URISyntaxException e) {
  225. IOException err = new IOException(MessageFormat.format(IpLogText.get().invalidURIFormat, url));
  226. err.initCause(e);
  227. throw err;
  228. }
  229. }
  230. private CQ parseOneCQ(Map<String, String> row) {
  231. long id = Long.parseLong(row.get("bug_id"));
  232. String state = row.get("bug_severity");
  233. String bug_status = row.get("bug_status");
  234. String resolution = row.get("resolution");
  235. String short_desc = row.get("short_desc");
  236. String license = row.get("cf_license");
  237. Set<String> keywords = new TreeSet<String>();
  238. for (String w : row.get("keywords").split(", *"))
  239. keywords.add(w);
  240. // Skip any CQs that were not accepted.
  241. //
  242. if ("closed".equalsIgnoreCase(state)
  243. || "rejected".equalsIgnoreCase(state)
  244. || "withdrawn".equalsIgnoreCase(state))
  245. return null;
  246. // Skip any CQs under the EPL without nonepl keyword
  247. // Skip any CQs with the EPL keyword
  248. //
  249. if (!keywords.contains("nonepl") && license.matches(RE_EPL))
  250. return null;
  251. if (keywords.contains("epl"))
  252. return null;
  253. // Work around CQs that were closed in the wrong state.
  254. //
  255. if ("new".equalsIgnoreCase(state)
  256. || "under_review".equalsIgnoreCase(state)
  257. || state.startsWith("awaiting_")) {
  258. if ("RESOLVED".equalsIgnoreCase(bug_status)
  259. || "CLOSED".equalsIgnoreCase(bug_status)) {
  260. if ("FIXED".equalsIgnoreCase(resolution))
  261. state = "approved";
  262. else
  263. return null;
  264. }
  265. }
  266. StringBuilder use = new StringBuilder();
  267. for (String n : new String[] { "unmodified", "modified", "source",
  268. "binary" }) {
  269. if (keywords.contains(n)) {
  270. if (use.length() > 0)
  271. use.append(' ');
  272. use.append(n);
  273. }
  274. }
  275. if (keywords.contains("sourceandbinary")) {
  276. if (use.length() > 0)
  277. use.append(' ');
  278. use.append("source & binary");
  279. }
  280. CQ cq = new CQ(id);
  281. cq.setDescription(short_desc);
  282. cq.setLicense(license);
  283. cq.setState(state);
  284. if (use.length() > 0)
  285. cq.setUse(use.toString().trim());
  286. return cq;
  287. }
  288. }