aboutsummaryrefslogtreecommitdiff
path: root/basebuilder-3.6.2/org.eclipse.releng.basebuilder/plugins/org.eclipse.build.tools/src_bugzilla/org/eclipse/releng/services/bugzilla/UpdateBugStateTask.java
blob: 5e120cb0db131d10d4e6a9a1ad0bf834506d0027 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
/**
 * Copyright (c) 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

package org.eclipse.releng.services.bugzilla;


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.net.ssl.HttpsURLConnection;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

import org.eclipse.releng.util.bugzilla.Messages;


public class UpdateBugStateTask extends Task
{
  private static final String UTF_8 = "UTF-8"; //$NON-NLS-1$

  private static final String GET = "GET"; //$NON-NLS-1$

  private static final String CTYPE_RDF = "&ctype=rdf"; //$NON-NLS-1$

  private static final String URL_TARGET_MILESTONE = "&target_milestone="; //$NON-NLS-1$

  private static final String URL_CHFIELDTO = "&chfieldto="; //$NON-NLS-1$

  private static final String URL_BUG_STATUS = "&bug_status="; //$NON-NLS-1$

  private static final String HTTPS_BUGS_ECLIPSE_ORG_BUGS_BUGLIST_CGI_PRODUCT = "https://bugs.eclipse.org/bugs/buglist.cgi?product="; //$NON-NLS-1$

  private static final String COOKIE = "Cookie"; //$NON-NLS-1$

  private static final String APPLICATION_X_WWW_FORM_URLENCODED = "application/x-www-form-urlencoded"; //$NON-NLS-1$

  private static final String CONTENT_TYPE = "Content-type"; //$NON-NLS-1$

  private static final String POST = "POST"; //$NON-NLS-1$

  private static final String HTTPS_BUGS_ECLIPSE_ORG_BUGS_PROCESS_BUG_CGI = "https://bugs.eclipse.org/bugs/process_bug.cgi"; //$NON-NLS-1$

  private static final String BUG_STATUS = "bug_status"; //$NON-NLS-1$

  private static final String AMP = "&"; //$NON-NLS-1$

  private static final String EQ = "="; //$NON-NLS-1$

  private static final String HTTPS_BUGS_ECLIPSE_ORG_BUGS_SHOW_BUG_CGI_ID = "https://bugs.eclipse.org/bugs/show_bug.cgi?id="; //$NON-NLS-1$

  private static final String CTYPE_XML = "&ctype=xml"; //$NON-NLS-1$

  private static final String RESOLVE = "resolve"; //$NON-NLS-1$

  private static final String RESOLUTION = "resolution"; //$NON-NLS-1$

  private static final String KNOB = "knob"; //$NON-NLS-1$

  private static final String LONGDESCLENGTH = "longdesclength"; //$NON-NLS-1$

  private static final String SHORT_DESC = "short_desc"; //$NON-NLS-1$

  private static final String BUG_FILE_LOC = "bug_file_loc"; //$NON-NLS-1$

  private static final String BUG_SEVERITY = "bug_severity"; //$NON-NLS-1$

  private static final String PRIORITY = "priority"; //$NON-NLS-1$

  private static final String OP_SYS = "op_sys"; //$NON-NLS-1$

  private static final String REP_PLATFORM = "rep_platform"; //$NON-NLS-1$

  private static final String TARGET_MILESTONE = "target_milestone"; //$NON-NLS-1$

  private static final String COMPONENT = "component"; //$NON-NLS-1$

  private static final String VERSION = "version"; //$NON-NLS-1$

  private static final String PRODUCT = "product"; //$NON-NLS-1$

  private static final String ID = "id"; //$NON-NLS-1$

  private static final String COMMENT = "comment"; //$NON-NLS-1$

  private static final String PROCESS_BUG = "process_bug"; //$NON-NLS-1$

  private static final String FORM_NAME = "form_name"; //$NON-NLS-1$

  private static final String BUGZILLA_LOGINCOOKIE = "; Bugzilla_logincookie="; //$NON-NLS-1$

  private static final String BUGZILLA_LOGIN = "Bugzilla_login="; //$NON-NLS-1$

  private static final String DIGITS_REGEX = "(\\d+)"; //$NON-NLS-1$

  private static final String COLON = ":"; //$NON-NLS-1$

  private static final String DASH = "-"; //$NON-NLS-1$

  private static final String BUGID_REGEX = "<bz:id(?: nc:parseType=\"Integer\")>(\\d+)</bz:id>"; //$NON-NLS-1$

  private static final String BUILDID_REGEX = "([IMNRS]?-?)(\\d{4})(\\d{2})(\\d{2})-?(\\d{2})(\\d{2})"; //$NON-NLS-1$

  private static final String TIMESTAMP_REGEX = "(\\d{4})(\\d{2})(\\d{2})(\\d{2})(\\d{2})"; //$NON-NLS-1$

  private static final String JS = "Java said:"; //$NON-NLS-1$

  private static final String SP = " "; //$NON-NLS-1$

  private static final String XML_REGEX = "<(\\S+)>([^<]+)</\\1>"; //$NON-NLS-1$

  private static final String NL = "\n"; //$NON-NLS-1$

  private static final String CSO = ", or "; //$NON-NLS-1$

  private static final String CS = ", "; //$NON-NLS-1$

  private static final String BZ_IV = "INVALID"; //$NON-NLS-1$

  private static final String BZ_WF = "WONTFIX"; //$NON-NLS-1$

  private static final String BZ_LT = "LATER"; //$NON-NLS-1$

  private static final String BZ_RM = "REMIND"; //$NON-NLS-1$

  private static final String BZ_WK = "WORKSFORME"; //$NON-NLS-1$

  private static final String BZ_FX = "FIXED"; //$NON-NLS-1$

  private static final String BZ_RE = "REOPENED"; //$NON-NLS-1$

  private static final String BZ_AS = "ASSIGNED"; //$NON-NLS-1$

  private static final String BZ_NEW = "NEW"; //$NON-NLS-1$

  private static final String BZ_UC = "UNCONFIRMED"; //$NON-NLS-1$

  private static final String EMPTY = ""; //$NON-NLS-1$

  private static final String LT = "<"; //$NON-NLS-1$

  private static final String GT = ">"; //$NON-NLS-1$

  private static final String QUOT = "\""; //$NON-NLS-1$

  private static final String APOS = "'"; //$NON-NLS-1$

  private static final String HTML_APOS = "&apos;"; //$NON-NLS-1$

  private static final String HTML_QUOT = "&quot;"; //$NON-NLS-1$

  private static final String HTML_LT = "&lt;"; //$NON-NLS-1$

  private static final String HTML_GT = "&gt;"; //$NON-NLS-1$

  private static final String HTML_NBSP = "&nbsp;"; //$NON-NLS-1$

  private static final String HTML_AMP = "&amp;"; //$NON-NLS-1$

  private int debug;

  private int login;

  private int loginCookie;

  private String product;

  private String status;

  private String buildID;

  private String buildAlias;

  private String milestone;

  private String bugList;

  private String resolution;

  private String endDate;

  private LinkedHashMap trans;

  public UpdateBugStateTask()
  {
    debug = 1;

    login = 0;
    loginCookie = 0;
    product = EMPTY;
    status = EMPTY;
    buildID = EMPTY;
    buildAlias = EMPTY;
    endDate = EMPTY;
    milestone = EMPTY;
    bugList = EMPTY;
    resolution = BZ_FX;

    trans = new LinkedHashMap(8, 0.75f, false);
    trans.put(HTML_APOS, APOS);
    trans.put(HTML_QUOT, QUOT);
    trans.put(HTML_LT, LT);
    trans.put(HTML_GT, GT);
    trans.put(HTML_NBSP, SP);
    trans.put(HTML_AMP, AMP);
  }

  public void setDebug(int d)
  {
    debug = d;
  }

  public void setBugList(String b)
  {
    bugList = b;
  }

  public void setProduct(String p)
  {
    product = p;
  }

  public void setStatus(String s)
  {
    if (s.equals(BZ_UC) || s.equals(BZ_NEW) || s.equals(BZ_AS) || s.equals(BZ_RE))
    {
      status = s;
    }
    else
    {
      throw new BuildException(Messages.getString("UpdateBugStateTask.invalidStatus") + "!" + SP + //$NON-NLS-1$ //$NON-NLS-2$
        Messages.getString("UpdateBugStateTask.expectedOne") + SP + BZ_UC + CS + BZ_NEW + CS + BZ_AS + CSO + BZ_RE); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    }
  }

  public void setLogin(int l)
  {
    login = l;
  }

  public void setLoginCookie(int lc)
  {
    loginCookie = lc;
  }

  public void setResolution(String r)
  {
    if (r.equals(BZ_FX) || r.equals(BZ_IV) || r.equals(BZ_WF) || r.equals(BZ_LT) || r.equals(BZ_RM) || r.equals(BZ_WK))
    {
      resolution = r;
    }
    else
    {
      System.err.println(Messages.getString("UpdateBugStateTask.invalidResolution") + "!" + SP + //$NON-NLS-1$ //$NON-NLS-2$
        Messages.getString("UpdateBugStateTask.expected") //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        + SP + BZ_FX + CS + BZ_IV + CS + BZ_WF + CS + BZ_LT + CS + BZ_RM + CSO + BZ_WK + SP
        + "(" + Messages.getString("UpdateBugStateTask.default") + COLON + SP + BZ_FX + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    }
  }

  public void setEndDate(String t)
  {
    Pattern p = Pattern.compile(TIMESTAMP_REGEX);
    Matcher m = p.matcher(t);
    if (m.matches())
    {
      endDate = m.group(1) + DASH + m.group(2) + DASH + m.group(3) + SP + m.group(4) + COLON + m.group(5);
    }
    else
    {
      throw new BuildException(Messages.getString("UpdateBugStateTask.invalidTimestamp") + COLON + SP + t + "!"); //$NON-NLS-1$ //$NON-NLS-2$
    }
  }

  public void setBuildID(String t)
  {
    Pattern p = Pattern.compile(BUILDID_REGEX);
    Matcher m = p.matcher(t);
    if (m.matches())
    {
      buildID = m.group(1) + m.group(2) + m.group(3) + m.group(4) + m.group(5) + m.group(6);
    }
    else
    {
      throw new BuildException(Messages.getString("UpdateBugStateTask.invalidBuildID") + COLON + SP + t + "!"); //$NON-NLS-1$ //$NON-NLS-2$
    }
  }

  public void setBuildAlias(String b)
  {
    buildAlias = b;
  }

  public void setMilestone(String m)
  {
    milestone = m;
  }

public void execute() throws BuildException
  {
    if (login == 0)
    {
      throw new BuildException(Messages.getString("UpdateBugStateTask.expectingLogin") + "!"); //$NON-NLS-1$ //$NON-NLS-2$
    }
    if (loginCookie == 0)
    {
      throw new BuildException(Messages.getString("UpdateBugStateTask.expectingLogincookie") + "!"); //$NON-NLS-1$ //$NON-NLS-2$
    }
    if (status.equals(EMPTY))
    {
      throw new BuildException(Messages.getString("UpdateBugStateTask.expectingStatus") + "!"); //$NON-NLS-1$ //$NON-NLS-2$
    }

    /* we take an explicit list OR do a query, not both */
    if (!bugList.equals(EMPTY) && endDate.equals(EMPTY) && milestone.equals(EMPTY) && product.equals(EMPTY))
    {
      if (debug > 1)
      {
        System.err.println(Messages.getString("UpdateBugStateTask.usingBugList")); //$NON-NLS-1$
      }
      Pattern p = Pattern.compile(DIGITS_REGEX);
      Matcher m = p.matcher(bugList);
      while (m.find())
      {
        int bugID = Integer.parseInt(m.group(1));
        if (debug > 1)
        {
          System.err.println(Messages.getString("UpdateBugStateTask.found") + SP + bugID); //$NON-NLS-1$
        }
        doBug(bugID);
      }
    }
    else if (bugList.equals(EMPTY))
    {
      if (product.equals(EMPTY))
      {
        throw new BuildException(Messages.getString("UpdateBugStateTask.expectingProduct") + "!"); //$NON-NLS-1$ //$NON-NLS-2$
      }

      if (debug > 1)
      {
        System.err.println(Messages.getString("UpdateBugStateTask.queryingFor") + SP + //$NON-NLS-1$ 
          (!status.equals(EMPTY) ? status + SP : EMPTY) + 
          (!product.equals(EMPTY) ? product + SP : EMPTY) + 
          (!milestone.equals(EMPTY) ? milestone + SP : EMPTY) + 
          Messages.getString("UpdateBugStateTask.bugs")); //$NON-NLS-1$ //$NON-NLS-2$
      }

      /* the Bugzilla search form generates a massive URL, but thankfully doesn't
       * demand all sorts of superfluous fields like when updating a bug */
      String url = HTTPS_BUGS_ECLIPSE_ORG_BUGS_BUGLIST_CGI_PRODUCT + urlEncode(product) + URL_BUG_STATUS + urlEncode(status)
        + URL_CHFIELDTO + urlEncode(endDate) + URL_TARGET_MILESTONE + urlEncode(milestone) + CTYPE_RDF;
      if (debug > 1)
      {
        System.err.println(Messages.getString("UpdateBugStateTask.connectingTo") + SP + //$NON-NLS-1$
          url + SP + "..."); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
      }
      HttpsURLConnection bugsconn = getConn(url, GET, true, false, EMPTY);
      String bugs = slurpStream(bugsconn);
      if (debug > 1)
      {
        System.err.println(Messages.getString("UpdateBugStateTask.gotBugList") + COLON); //$NON-NLS-1$
        System.err.println(bugs);
      }

      Pattern p = Pattern.compile(BUGID_REGEX);
      Matcher m = p.matcher(bugs);
      if (m.find()) {
        while (m.find())
        {
          int bugID = Integer.parseInt(m.group(1));
          if (debug > 1)
          {
            System.out.println(Messages.getString("UpdateBugStateTask.found") + SP + bugID); //$NON-NLS-1$
          }
          doBug(bugID);
        }
      }
      else
      {
        System.out.println("No bugs found matching specified state" + SP + "(" + status + "). Nothing to do!");
      }
    }
    else
    {
      throw new BuildException(Messages.getString("UpdateBugStateTask.ambiguousRequest") + CS + //$NON-NLS-1$
        Messages.getString("UpdateBugStateTask.mutuallyExclusive") + "!"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    }
  }  private void doBug(int bugID) throws BuildException
  {
    if (bugID == 0)
    {
      throw new BuildException(Messages.getString("UpdateBugStateTask.invalidBugID") + SP + bugID + "!"); //$NON-NLS-1$ //$NON-NLS-2$
    }

    String bugcookie = BUGZILLA_LOGIN + login + BUGZILLA_LOGINCOOKIE + loginCookie;
    String buildstring = EMPTY;
    if (buildAlias.equals(EMPTY) && buildID.equals(EMPTY))
    {
      buildstring = Messages.getString("UpdateBugStateTask.latestBuild"); //$NON-NLS-1$
    }
    else if (!buildAlias.equals(EMPTY) && !buildID.equals(EMPTY))
    {
      buildstring = buildAlias + SP + "(" + buildID + ")"; //$NON-NLS-1$ //$NON-NLS-2$
    }
    else
    {
      buildstring = (!buildAlias.equals(EMPTY) ? buildAlias : buildID);
    }

    Hashtable args = new Hashtable();
    args.put(FORM_NAME, PROCESS_BUG);
    args.put(COMMENT, Messages.getString("UpdateBugStateTask.fixedIn") + SP + buildstring + "."); //$NON-NLS-1$ //$NON-NLS-2$
    args.put(ID, new Integer(bugID));
    args.put(PRODUCT, EMPTY);
    args.put(VERSION, EMPTY);
    args.put(COMPONENT, EMPTY);
    args.put(TARGET_MILESTONE, EMPTY);
    args.put(REP_PLATFORM, EMPTY);
    args.put(OP_SYS, EMPTY);
    args.put(PRIORITY, EMPTY);
    args.put(BUG_SEVERITY, EMPTY);
    args.put(BUG_FILE_LOC, EMPTY);
    args.put(SHORT_DESC, EMPTY);
    args.put(LONGDESCLENGTH, new Integer(1)); //Bugzilla doesn't seem to use this, but demands it anyways
    args.put(KNOB, RESOLVE);
    args.put(RESOLUTION, resolution);

    if (debug > 1)
    {
      System.err.println(Messages.getString("UpdateBugStateTask.usingCookie") + COLON + SP + bugcookie); //$NON-NLS-1$
      System.err.println(Messages.getString("UpdateBugStateTask.usingComment") + COLON + SP + args.get(COMMENT).toString()); //$NON-NLS-1$
    }

    /* slurp xml for bugID */
    String url = HTTPS_BUGS_ECLIPSE_ORG_BUGS_SHOW_BUG_CGI_ID + urlEncode(args.get(ID).toString()) + CTYPE_XML;
    if (debug > 1)
    {
      System.err.println(Messages.getString("UpdateBugStateTask.connectingTo") + SP + //$NON-NLS-1$
        url + SP + "..."); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
    }
    HttpsURLConnection xmlconn = getConn(url, GET, true, false, EMPTY);
    String xml = slurpStream(xmlconn);
    if (debug > 1)
    {
      System.err.println(Messages.getString("UpdateBugStateTask.gotXML") + COLON); //$NON-NLS-1$
      System.err.println(xml);
    }
    xmlconn.disconnect();

    /* parse xml, build post string */
    String req = EMPTY;
    Hashtable pxml = parseXML(xml);
    for (Enumeration e = args.keys(); e.hasMoreElements();)
    {
      String elem = e.nextElement().toString();
      /* sometimes Bugzilla omits bug_file_loc if it's blank... */
      if (args.get(elem).equals(EMPTY) && pxml.get(elem) != null)
      {
        args.put(elem, pxml.get(elem));
      }

      req += urlEncode(elem) + EQ + urlEncode(args.get(elem).toString()) + AMP;
    }

    req = req.substring(0, req.length() - 1);

    /* update bug, if applicable */
    if (pxml.get(BUG_STATUS) == null)
    {
      if (debug > 0)
      {
        System.out.println(Messages.getString("UpdateBugStateTask.noBugStatus") + SP + //$NON-NLS-1$
          bugID + CS + Messages.getString("UpdateBugStateTask.missingBug")); //$NON-NLS-1$ //$NON-NLS-2$
      }
    }
    else if (pxml.get(BUG_STATUS).equals(status))
    {
      String bugurl = HTTPS_BUGS_ECLIPSE_ORG_BUGS_PROCESS_BUG_CGI;
      if (debug > 1)
      {
        System.err.println(Messages.getString("UpdateBugStateTask.connectingTo") + SP + //$NON-NLS-1$
          bugurl + SP + "..."); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
      }
      HttpsURLConnection bugconn = getConn(bugurl, POST, true, true, bugcookie);

      if (debug > 1)
      {
        System.err.println(Messages.getString("UpdateBugStateTask.postingData") + COLON); //$NON-NLS-1$
        System.err.println(req);
      }
      sendStream(bugconn, req);
      String response = slurpStream(bugconn);

      // trap for invalid login cookie 
      if (response.indexOf(Messages.getString("UpdateBugStateTask.legitimateLoginAndPassword")) > 0) //$NON-NLS-1$
      {
        System.err.println(Messages.getString("UpdateBugStateTask.couldNotLogIn")); //$NON-NLS-1$
        System.err.println(Messages.getString("UpdateBugStateTask.BugzillaReplied") + COLON + SP + //$NON-NLS-1$
          "\"" + Messages.getString("UpdateBugStateTask.legitimateLoginAndPassword") + "\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ 
        if (debug > 1)
        {
          System.err.println(Messages.getString("UpdateBugStateTask.gotResponse") + COLON); //$NON-NLS-1$
          System.err.println(response);
        }
        bugconn.disconnect();
        System.err.println(Messages.getString("UpdateBugStateTask.setBugFailed") + SP + bugID + SP + //$NON-NLS-1$
          Messages.getString("UpdateBugStateTask.to") + SP + resolution + SP + //$NON-NLS-1$
          "(" + Messages.getString("UpdateBugStateTask.was") + SP + pxml.get(BUG_STATUS) + ")" + COLON + SP + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
          "\"" + Messages.getString("UpdateBugStateTask.fixedIn") + SP + buildstring + ".\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
      }
      else
      {
        if (debug > 1)
        {
          System.err.println(Messages.getString("UpdateBugStateTask.gotResponse") + COLON); //$NON-NLS-1$
          System.err.println(response);
        }
        bugconn.disconnect();
        if (debug > 0)
        {
          System.out.println(Messages.getString("UpdateBugStateTask.setBug") + SP + bugID + SP + //$NON-NLS-1$
            Messages.getString("UpdateBugStateTask.to") + SP + resolution + SP + //$NON-NLS-1$
            "(" + Messages.getString("UpdateBugStateTask.was") + SP + pxml.get(BUG_STATUS) + ")" + COLON + SP + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            "\"" + Messages.getString("UpdateBugStateTask.fixedIn") + SP + buildstring + ".\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        }
      }
    }
    else
    {
      if (debug > 0)
      {
        System.out.println(Messages.getString("UpdateBugStateTask.ignoreBug") + SP + args.get(ID).toString() + SP + //$NON-NLS-1$
          "(" + Messages.getString("UpdateBugStateTask.notInExpectedState") + SP + status + ")" + COLON + SP + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
          Messages.getString("UpdateBugStateTask.was") + SP + pxml.get(BUG_STATUS).toString() + "."); //$NON-NLS-1$ //$NON-NLS-2$
      }
    }
  }

  private String urlEncode(String elem)
  {
    elem = htmlDecode(elem);

    try
    {
      elem = URLEncoder.encode(elem, UTF_8);
    }
    catch (java.io.UnsupportedEncodingException e)
    {
      throw new BuildException(Messages.getString("UpdateBugStateTask.couldntEncode") + SP + //$NON-NLS-1$
        "'" + elem + "'" + "!" + SP + JS + SP + e.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    }

    return elem;
  }

  private HttpsURLConnection getConn(String url, String method, boolean in, boolean out, String cookie)
  {
    URL u = null;
    try
    {
      u = new URL(url);
    }
    catch (java.net.MalformedURLException e)
    {
      throw new BuildException(Messages.getString("UpdateBugStateTask.badURL") + CS + //$NON-NLS-1$
        url + "!" + SP + JS + SP + e.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
    }

    URLConnection conn = null;
    try
    {
      conn = u.openConnection();
    }
    catch (java.io.IOException e)
    {
      throw new BuildException(Messages.getString("UpdateBugStateTask.failedConnection") + "!" + SP + JS + SP + e.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$
    }
    HttpsURLConnection sconn = (HttpsURLConnection)conn;

    try
    {
      sconn.setRequestMethod(method);
    }
    catch (java.net.ProtocolException e)
    {
      throw new BuildException(Messages.getString("UpdateBugStateTask.badHTTPMethod") + "!" + SP + JS + SP + e.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$
    }

    if (method.equals(POST))
    {
      sconn.setRequestProperty(CONTENT_TYPE, APPLICATION_X_WWW_FORM_URLENCODED);
    }

    if (!cookie.equals(EMPTY))
    {
      sconn.setRequestProperty(COOKIE, cookie);
    }

    sconn.setDoInput(in);
    sconn.setDoOutput(out);

    try
    {
      sconn.connect();
    }
    catch (java.io.IOException e)
    {
      throw new BuildException(Messages.getString("UpdateBugStateTask.connectError") + SP + //$NON-NLS-1$
        url + "!" + SP + JS + SP + e.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
    }

    return sconn;
  }

  private void sendStream(HttpsURLConnection conn, String req)
  {
    try
    {
      PrintWriter out = new PrintWriter(conn.getOutputStream());
      out.print(req);
      out.flush();
      out.close();
    }
    catch (java.io.IOException e)
    {
      throw new BuildException(Messages.getString("UpdateBugStateTask.streamWriteError") + "!" + SP + JS + SP + e.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$
    }
  }

  private String slurpStream(HttpsURLConnection conn)
  {
    String ret = EMPTY;
    try
    {
      BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
      String tmp;
      while ((tmp = in.readLine()) != EMPTY && tmp != null)
      {
        ret += tmp + NL;
      }

      in.close();
    }
    catch (java.io.IOException e)
    {
      throw new BuildException(Messages.getString("UpdateBugStateTask.streamReadError") + "!" + SP + JS + SP + e.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$
    }

    return ret;
  }

  /* this will only keep the last comment, but we don't use the comments anyways */
  private Hashtable parseXML(String xml)
  {
    if (debug > 1)
    {
      System.err.println(Messages.getString("UpdateBugStateTask.parsingXML") + "..."); //$NON-NLS-1$ //$NON-NLS-2$
    }
    Hashtable pxml = new Hashtable();
    Pattern p = Pattern.compile(XML_REGEX);
    Matcher m = p.matcher(xml);
    while (m.find())
    {
      if (debug > 1)
      {
        System.err.println(Messages.getString("UpdateBugStateTask.found") + SP + m.group(1) + SP + EQ + SP + m.group(2)); //$NON-NLS-1$
      }
      pxml.put(m.group(1), m.group(2));
    }

    return pxml;
  }

  private String htmlDecode(String str)
  {
    for (Iterator i = trans.keySet().iterator(); i.hasNext();)
    {
      String elem = i.next().toString();

      str = Pattern.compile(elem).matcher(str).replaceAll(trans.get(elem).toString());
    }

    return str;
  }

  public static void main(String args[])
  {
    new UpdateBugStateTask();
  }
}