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

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