1 19 20 package taskblocks.bugzilla; 21 22 import java.io.BufferedReader ; 23 import java.io.ByteArrayInputStream ; 24 import java.io.CharArrayWriter ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.InputStreamReader ; 28 import java.io.OutputStream ; 29 import java.io.PrintWriter ; 30 import java.io.UnsupportedEncodingException ; 31 import java.net.HttpURLConnection ; 32 import java.net.MalformedURLException ; 33 import java.net.URL ; 34 import java.net.URLEncoder ; 35 import java.util.ArrayList ; 36 import java.util.HashMap ; 37 import java.util.List ; 38 import java.util.Map ; 39 import java.util.regex.Matcher ; 40 import java.util.regex.Pattern ; 41 42 import javax.xml.parsers.DocumentBuilder ; 43 import javax.xml.parsers.DocumentBuilderFactory ; 44 import javax.xml.parsers.ParserConfigurationException ; 45 46 import org.w3c.dom.Document ; 47 import org.w3c.dom.Element ; 48 import org.xml.sax.SAXException ; 49 50 import taskblocks.Utils; 51 52 59 public class BugzillaSubmitter { 60 61 62 public static final String KEYWORDS = "keywords"; 63 64 65 public static final String PRODUCT = "product"; 66 67 68 public static final String VERSION = "version"; 69 70 71 public static final String COMPONENT = "component"; 72 73 74 public static final String HARDWARE = "rep_platform"; 75 76 77 public static final String OS = "op_sys"; 78 79 80 public static final String PRIORITY = "priority"; 81 82 83 public static final String SEVERITY = "bug_severity"; 84 85 89 public static final String STATUS = "bug_status"; 90 91 92 public static final String ASSIGNED_TO = "assigned_to"; 93 94 95 public static final String SUMMARY = "short_desc"; 96 97 98 public static final String DESCRIPTION = "comment"; 99 100 101 public static final String ESTIMATED_TIME = "estimated_time"; 102 103 104 public static final String BLOCKS = "blocked"; 105 106 107 public static final String STATUS_WHITEBOARD = "status_whiteboard"; 108 109 113 public String _successRegexp = "Bug ([0-9]+) Submitted"; 114 115 119 public String _errTitleRegexp = "<title>(.*)</title>"; 120 121 125 public String _errDetailRegexp = "<div id=\"bugzilla-body\">(.*)</div>.*?<div id=\"footer\">"; 126 127 128 public String [] _errDetailRemovalRegexps = new String [] { 129 "(?s)<script.*?</script>", 130 "(?s)<div id=\"docslinks\">.*?</div>" 131 }; 132 133 140 private static String buildFormBody(Map <String , String > formData) 141 throws UnsupportedEncodingException { 142 StringBuilder body = new StringBuilder (); 143 int count = 0; 144 for (Map.Entry <String , String > e : formData.entrySet()) { 145 if (count > 0) { 146 body.append("&"); 147 } 148 body.append(URLEncoder.encode(e.getKey(), "UTF-8")); 149 body.append("="); 150 body.append(URLEncoder.encode(e.getValue(), "UTF-8")); 151 count++; 152 } 153 return body.toString(); 154 } 155 156 164 private String submit(URL url, String body) throws IOException { 165 166 HttpURLConnection conn = (HttpURLConnection ) url.openConnection(); 168 conn.setRequestMethod("POST"); 169 conn.setAllowUserInteraction(false); conn.setDoOutput(true); conn.setRequestProperty("Content-type", 173 "application/x-www-form-urlencoded; charset=utf-8"); 174 conn.setRequestProperty("Content-length", Integer.toString(body.length())); 176 177 OutputStream rawOutStream = conn.getOutputStream(); 179 PrintWriter pw = new PrintWriter (rawOutStream); 180 181 pw.print(body); pw.flush(); 183 pw.close(); 184 185 InputStream rawInStream = conn.getInputStream(); 189 190 BufferedReader rdr = new BufferedReader (new InputStreamReader (rawInStream, "UTF-8")); 193 CharArrayWriter result = new CharArrayWriter (); 194 char[] buf = new char[1024]; 195 int count = rdr.read(buf); 196 while(count > 0) { 197 result.write(buf, 0, count); 198 count = rdr.read(buf); 199 } 200 201 conn.disconnect(); 202 return result.toString(); 203 } 204 205 private void ensureDefault(Map <String , String > map, String key, 206 String defaultValue) { 207 if (!map.containsKey(key)) { 208 map.put(key, defaultValue); 209 } 210 } 211 212 235 public String submit(String baseUrl, String user, String password, 236 Map <String , String > properties) throws Exception { 237 238 ensureDefault(properties, STATUS, "NEW"); 240 ensureDefault(properties, SEVERITY, "normal"); 241 ensureDefault(properties, PRIORITY, "P2"); 242 ensureDefault(properties, "bug_file_loc", "http://"); 243 244 properties.put("form_name", "enter_bug"); 246 properties.put("Bugzilla_login", user); 247 properties.put("Bugzilla_password", password); 248 properties.put("GoAheadAndLogIn", "1"); 249 250 String formBody = buildFormBody(properties); 251 String result = submit(new URL (baseUrl + "/post_bug.cgi"), formBody); 252 254 Matcher m = Pattern.compile(_successRegexp).matcher(result); 255 if (m.find()) { 256 String bugId = m.group(1); 257 return bugId; 258 } else { 259 260 String errText = ""; 261 m = Pattern.compile(_errTitleRegexp).matcher(result); 262 if (m.find()) { 263 errText = m.group(1); 264 } 265 266 String errText2 = ""; 267 m = Pattern.compile(_errDetailRegexp, Pattern.DOTALL).matcher(result); 268 if (m.find()) { 269 errText2 = m.group(1); 270 } 271 if (errText2.length() > 0) { 272 for (String removeRegexp : _errDetailRemovalRegexps) { 273 errText2 = errText2.replaceAll(removeRegexp, ""); 274 } 275 errText2 = errText2.replaceAll("<[^>]*>", ""); 276 errText2 = errText2.replaceAll("\r?\n", " "); 277 errText2 = errText2.replaceAll(" +", " "); 278 } 279 throw new Exception (errText + ": " + errText2); 280 } 281 } 282 283 public String query(String baseUrl, String user, String password, String [] bugs) throws MalformedURLException , IOException , SAXException , ParserConfigurationException { 284 Map <String , String > formData = new HashMap <String , String >(); 285 formData.put("ctype", "xml"); 286 formData.put("excludefield", "attachmentdata"); 287 String body = buildFormBody(formData); 288 289 for(String bugId: bugs) { 290 body += "&"; 291 body += URLEncoder.encode("id", "UTF-8"); 292 body += "="; 293 body += URLEncoder.encode(bugId, "UTF-8"); 294 } 295 296 String result = submit(new URL (baseUrl + "/show_record.cgi"), body); 297 298 Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream (result.getBytes("UTF-8"))); 300 Element rootE = doc.getDocumentElement(); 301 if(!rootE.getNodeName().equals("bugzilla")) { 302 throw new IOException ("Wrong xml answer, doesn't looks like bugzilla"); 303 } 304 305 List <Map <String , String >> bugsData = new ArrayList <Map <String ,String >>(); 306 for(Element bugE: Utils.getChilds(rootE, "bug")) { 307 Map <String , String > bugData = new HashMap <String , String >(); 308 fillBugData(bugE, bugData); 309 } 310 return result; 311 } 312 313 private void fillBugData(Element bugE, Map <String , String > bugData) { 314 } 315 } 316 | Popular Tags |