mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Jean-Noel Rouvignac
17.33.2015 8b024c96ec114562c17b9f090fc8b561e7a7b34f
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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
 * or http://forgerock.org/license/CDDLv1.0.html.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at legal-notices/CDDLv1_0.txt.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Copyright 2006-2009 Sun Microsystems, Inc.
 *      Portions Copyright 2014-2015 ForgeRock AS
 */
package org.opends.quicksetup.util;
 
import static org.opends.messages.QuickSetupMessages.*;
 
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
 
import org.forgerock.i18n.LocalizableMessage;
import org.forgerock.i18n.LocalizableMessageBuilder;
import org.forgerock.i18n.slf4j.LocalizedLogger;
import org.opends.quicksetup.Constants;
import org.opends.quicksetup.ui.UIFactory;
 
/**
 * This is an implementation of the ProgressMessageFormatter class that
 * provides format in HTML.
 */
public class HtmlProgressMessageFormatter implements ProgressMessageFormatter
{
  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
 
  private LocalizableMessage doneHtml;
  private LocalizableMessage errorHtml;
 
  /** The constant used to separate parameters in an URL. */
  private static final String PARAM_SEPARATOR = "&&&&";
  /** The space in HTML. */
  private static final LocalizableMessage SPACE = LocalizableMessage.raw(" ");
 
  /**
   * The line break.
   * The extra char is necessary because of bug:
   * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4988885
   */
   private static final LocalizableMessage LINE_BREAK=
     LocalizableMessage.raw("
"+Constants.HTML_LINE_BREAK);
 
   private static final LocalizableMessage TAB = new LocalizableMessageBuilder(SPACE)
   .append(SPACE)
   .append(SPACE)
   .append(SPACE)
   .append(SPACE)
   .toMessage();
 
  /**
   * Returns the HTML representation of the text without providing any style.
   * @param text the source text from which we want to get the HTML
   * representation
   * @return the HTML representation for the given text.
   */
  @Override
  public LocalizableMessage getFormattedText(LocalizableMessage text)
  {
    return LocalizableMessage.raw(Utils.getHtml(String.valueOf(text)));
  }
 
  /**
   * Returns the HTML representation of the text that is the summary of the
   * installation process (the one that goes in the UI next to the progress
   * bar).
   * @param text the source text from which we want to get the formatted
   * representation
   * @return the HTML representation of the summary for the given text.
   */
  @Override
  public LocalizableMessage getFormattedSummary(LocalizableMessage text)
  {
    return new LocalizableMessageBuilder("<html>")
            .append(UIFactory.applyFontToHtml(
                    String.valueOf(text), UIFactory.PROGRESS_FONT))
            .toMessage();
  }
 
  /**
   * Returns the HTML representation of an error for a given text.
   * @param text the source text from which we want to get the HTML
   * representation
   * @param applyMargin specifies whether we apply a margin or not to the
   * resulting HTML.
   * @return the HTML representation of an error for the given text.
   */
  @Override
  public LocalizableMessage getFormattedError(LocalizableMessage text, boolean applyMargin)
  {
    String html;
    if (!Utils.containsHtml(String.valueOf(text))) {
      html = UIFactory.getIconHtml(UIFactory.IconType.ERROR_LARGE)
          + SPACE
          + SPACE
          + UIFactory.applyFontToHtml(Utils.getHtml(String.valueOf(text)),
              UIFactory.PROGRESS_ERROR_FONT);
    } else {
      html =
          UIFactory.getIconHtml(UIFactory.IconType.ERROR_LARGE) + SPACE
          + SPACE + UIFactory.applyFontToHtml(
                  String.valueOf(text), UIFactory.PROGRESS_FONT);
    }
 
    String result = UIFactory.applyErrorBackgroundToHtml(html);
    if (applyMargin)
    {
      result =
          UIFactory.applyMargin(result,
              UIFactory.TOP_INSET_ERROR_MESSAGE, 0, 0, 0);
    }
    return LocalizableMessage.raw(result);
  }
 
  /**
   * Returns the HTML representation of a warning for a given text.
   * @param text the source text from which we want to get the HTML
   * representation
   * @param applyMargin specifies whether we apply a margin or not to the
   * resulting HTML.
   * @return the HTML representation of a warning for the given text.
   */
  @Override
  public LocalizableMessage getFormattedWarning(LocalizableMessage text, boolean applyMargin)
  {
    String html;
    if (!Utils.containsHtml(String.valueOf(text))) {
      html =
        UIFactory.getIconHtml(UIFactory.IconType.WARNING_LARGE)
            + SPACE
            + SPACE
            + UIFactory.applyFontToHtml(Utils.getHtml(String.valueOf(text)),
                UIFactory.PROGRESS_WARNING_FONT);
    } else {
      html =
          UIFactory.getIconHtml(UIFactory.IconType.WARNING_LARGE) + SPACE
          + SPACE + UIFactory.applyFontToHtml(
                  String.valueOf(text), UIFactory.PROGRESS_FONT);
    }
 
    String result = UIFactory.applyWarningBackgroundToHtml(html);
    if (applyMargin)
    {
      result =
          UIFactory.applyMargin(result,
              UIFactory.TOP_INSET_ERROR_MESSAGE, 0, 0, 0);
    }
    return LocalizableMessage.raw(result);
  }
 
  /**
   * Returns the HTML representation of a success message for a given text.
   * @param text the source text from which we want to get the HTML
   * representation
   * @return the HTML representation of a success message for the given text.
   */
  @Override
  public LocalizableMessage getFormattedSuccess(LocalizableMessage text)
  {
    // Note: the text we get already is in HTML form
    String html =
        UIFactory.getIconHtml(UIFactory.IconType.INFORMATION_LARGE) + SPACE
        + SPACE + UIFactory.applyFontToHtml(String.valueOf(text),
                UIFactory.PROGRESS_FONT);
 
    return LocalizableMessage.raw(UIFactory.applySuccessfulBackgroundToHtml(html));
  }
 
  /**
   * Returns the HTML representation of a log error message for a given
   * text.
   * @param text the source text from which we want to get the HTML
   * representation
   * @return the HTML representation of a log error message for the given
   * text.
   */
  @Override
  public LocalizableMessage getFormattedLogError(LocalizableMessage text)
  {
    String html = Utils.getHtml(String.valueOf(text));
    return LocalizableMessage.raw(UIFactory.applyFontToHtml(html,
        UIFactory.PROGRESS_LOG_ERROR_FONT));
  }
 
 
  /**
   * Returns the HTML representation of a log message for a given text.
   * @param text the source text from which we want to get the HTML
   * representation
   * @return the HTML representation of a log message for the given text.
   */
  @Override
  public LocalizableMessage getFormattedLog(LocalizableMessage text)
  {
    String html = Utils.getHtml(String.valueOf(text));
    return LocalizableMessage.raw(UIFactory.applyFontToHtml(html,
            UIFactory.PROGRESS_LOG_FONT));
  }
 
  /**
   * Returns the HTML representation of the 'Done' text string.
   * @return the HTML representation of the 'Done' text string.
   */
  @Override
  public LocalizableMessage getFormattedDone()
  {
    if (doneHtml == null)
    {
      String html = Utils.getHtml(INFO_PROGRESS_DONE.get().toString());
      doneHtml = LocalizableMessage.raw(UIFactory.applyFontToHtml(html,
          UIFactory.PROGRESS_DONE_FONT));
    }
    return LocalizableMessage.raw(doneHtml);
  }
 
  /**
   * Returns the HTML representation of the 'Error' text string.
   * @return the HTML representation of the 'Error' text string.
   */
  @Override
  public LocalizableMessage getFormattedError() {
    if (errorHtml == null)
    {
      String html = Utils.getHtml(INFO_PROGRESS_ERROR.get().toString());
      errorHtml = LocalizableMessage.raw(UIFactory.applyFontToHtml(html,
          UIFactory.PROGRESS_ERROR_FONT));
    }
    return LocalizableMessage.raw(errorHtml);
  }
 
  /**
   * Returns the HTML representation of the argument text to which we add
   * points.  For instance if we pass as argument 'Configuring Server' the
   * return value will be 'Configuring Server <B>.....</B>'.
   * @param text the String to which add points.
   * @return the HTML representation of the '.....' text string.
   */
  @Override
  public LocalizableMessage getFormattedWithPoints(LocalizableMessage text)
  {
    String html = Utils.getHtml(String.valueOf(text));
    String points = SPACE +
            Utils.getHtml(INFO_PROGRESS_POINTS.get().toString()) + SPACE;
 
    LocalizableMessageBuilder buf = new LocalizableMessageBuilder();
    buf.append(UIFactory.applyFontToHtml(html, UIFactory.PROGRESS_FONT))
        .append(
            UIFactory.applyFontToHtml(points, UIFactory.PROGRESS_POINTS_FONT));
 
    return buf.toMessage();
  }
 
  /**
   * Returns the formatted representation of a point.
   * @return the formatted representation of the '.' text string.
   */
  @Override
  public LocalizableMessage getFormattedPoint()
  {
    return LocalizableMessage.raw(UIFactory.applyFontToHtml(".",
        UIFactory.PROGRESS_POINTS_FONT));
  }
 
  /**
   * Returns the formatted representation of a space.
   * @return the formatted representation of the ' ' text string.
   */
  @Override
  public LocalizableMessage getSpace()
  {
    return LocalizableMessage.raw(SPACE);
  }
 
  /**
   * Returns the formatted representation of a progress message for a given
   * text.
   * @param text the source text from which we want to get the formatted
   * representation
   * @return the formatted representation of a progress message for the given
   * text.
   */
  @Override
  public LocalizableMessage getFormattedProgress(LocalizableMessage text)
  {
    return LocalizableMessage.raw(UIFactory.applyFontToHtml(
        Utils.getHtml(String.valueOf(text)),
        UIFactory.PROGRESS_FONT));
  }
 
  /**
   * Returns the HTML representation of an error message for a given throwable.
   * This method applies a margin if the applyMargin parameter is
   * <CODE>true</CODE>.
   * @param t the throwable.
   * @param applyMargin specifies whether we apply a margin or not to the
   * resulting HTML.
   * @return the HTML representation of an error message for the given
   * exception.
   */
  @Override
  public LocalizableMessage getFormattedError(Throwable t, boolean applyMargin)
  {
    String openDiv = "<div style=\"margin-left:5px; margin-top:10px\">";
    String hideText =
        UIFactory.applyFontToHtml(INFO_HIDE_EXCEPTION_DETAILS.get().toString(),
            UIFactory.PROGRESS_FONT);
    String showText =
        UIFactory.applyFontToHtml(INFO_SHOW_EXCEPTION_DETAILS.get().toString(),
            UIFactory.PROGRESS_FONT);
    String closeDiv = "</div>";
 
    StringBuilder stackBuf = new StringBuilder();
    stackBuf.append(getHtmlStack(t));
    Throwable root = t.getCause();
    while (root != null)
    {
      stackBuf.append(Utils.getHtml(INFO_EXCEPTION_ROOT_CAUSE.get().toString()))
              .append(getLineBreak());
      stackBuf.append(getHtmlStack(root));
      root = root.getCause();
    }
    String stackText =
        UIFactory.applyFontToHtml(stackBuf.toString(), UIFactory.STACK_FONT);
 
    StringBuilder buf = new StringBuilder();
 
    String msg = t.getMessage();
    if (msg != null)
    {
      buf.append(UIFactory.applyFontToHtml(Utils.getHtml(t.getMessage()),
              UIFactory.PROGRESS_ERROR_FONT)).append(getLineBreak());
    } else
    {
      buf.append(t).append(getLineBreak());
    }
    buf.append(getErrorWithStackHtml(openDiv, hideText, showText, stackText,
        closeDiv, false));
 
    String html = UIFactory.getIconHtml(UIFactory.IconType.ERROR_LARGE) + SPACE + SPACE + buf;
 
    String result;
    if (applyMargin)
    {
      result =
          UIFactory.applyMargin(UIFactory.applyErrorBackgroundToHtml(html),
              UIFactory.TOP_INSET_ERROR_MESSAGE, 0, 0, 0);
    } else
    {
      result = UIFactory.applyErrorBackgroundToHtml(html);
    }
    return LocalizableMessage.raw(result);
  }
 
  /**
   * Returns the line break in HTML.
   * @return the line break in HTML.
   */
  @Override
  public LocalizableMessage getLineBreak()
  {
    return LINE_BREAK;
  }
 
  /**
   * Returns the tab in HTML.
   * @return the tab in HTML.
   */
  @Override
  public LocalizableMessage getTab()
  {
    return TAB;
  }
 
  /**
   * Returns the task separator in HTML.
   * @return the task separator in HTML.
   */
  @Override
  public LocalizableMessage getTaskSeparator()
  {
    return LocalizableMessage.raw(UIFactory.HTML_SEPARATOR);
  }
 
  /**
   * Returns the log HTML representation after the user has clicked on a url.
   *
   * @see HtmlProgressMessageFormatter#getErrorWithStackHtml
   * @param url that has been clicked
   * @param lastText the HTML representation of the log before clicking on the
   * url.
   * @return the log HTML representation after the user has clicked on a url.
   */
  @Override
  public LocalizableMessage getFormattedAfterUrlClick(String url, LocalizableMessage lastText)
  {
    String urlText = getErrorWithStackHtml(url, false);
    String newUrlText = getErrorWithStackHtml(url, true);
    String lastTextStr = String.valueOf(lastText);
 
    int index = lastTextStr.indexOf(urlText);
    if (index == -1)
    {
      logger.trace("lastText: " + lastText +
              "does not contain: " + urlText);
    } else
    {
      lastTextStr =
          lastTextStr.substring(0, index) + newUrlText
              + lastTextStr.substring(index + urlText.length());
    }
    return LocalizableMessage.raw(lastTextStr);
  }
 
  /**
   * Returns a HTML representation of the stack trace of a Throwable object.
   * @param ex the throwable object from which we want to obtain the stack
   * trace HTML representation.
   * @return a HTML representation of the stack trace of a Throwable object.
   */
  private String getHtmlStack(Throwable ex)
  {
    StringBuilder buf = new StringBuilder();
    buf.append(SPACE)
    .append(SPACE)
    .append(SPACE)
    .append(SPACE)
    .append(SPACE)
    .append(SPACE)
    .append(SPACE)
    .append(SPACE)
    .append(SPACE)
    .append(SPACE)
    .append(Utils.getHtml(ex.toString()))
    .append(getLineBreak());
    StackTraceElement[] stack = ex.getStackTrace();
    for (StackTraceElement aStack : stack) {
      buf.append(SPACE)
              .append(SPACE)
              .append(SPACE)
              .append(SPACE)
              .append(SPACE)
              .append(SPACE)
              .append(SPACE)
              .append(SPACE)
              .append(SPACE)
              .append(SPACE)
              .append(Utils.getHtml(aStack.toString()))
              .append(getLineBreak());
    }
    return buf.toString();
  }
 
  /**
   * Returns the HTML representation of an exception in the
   * progress log.<BR>
   * We can have something of type:<BR><BR>
   *
   * An error occurred.  java.io.IOException could not connect to server.<BR>
   * <A HREF="">Show Details</A>
   *
   * When the user clicks on 'Show Details' the whole stack will be displayed.
   *
   * An error occurred.  java.io.IOException could not connect to server.<BR>
   * <A HREF="">Hide Details</A><BR>
   * ... And here comes all the stack trace representation<BR>
   *
   *
   * As the object that listens to this hyperlink events is not here (it is
   * QuickSetupStepPanel) we must include all the information somewhere.  The
   * chosen solution is to include everything in the URL using parameters.
   * This everything consists of:
   * The open div tag for the text.
   * The text that we display when we do not display the exception.
   * The text that we display when we display the exception.
   * The stack trace text.
   * The closing div.
   * A boolean informing if we are hiding the exception or not (to know in the
   * next event what must be displayed).
   *
   * @param openDiv the open div tag for the text.
   * @param hideText the text that we display when we do not display the
   * exception.
   * @param showText the text that we display when we display the exception.
   * @param stackText the stack trace text.
   * @param closeDiv the closing div.
   * @param hide a boolean informing if we are hiding the exception or not.
   * @return the HTML representation of an error message with an stack trace.
   */
  private String getErrorWithStackHtml(String openDiv, String hideText,
      String showText, String stackText, String closeDiv, boolean hide)
  {
    StringBuilder buf = new StringBuilder();
 
    String params =
        getUrlParams(openDiv, hideText, showText, stackText, closeDiv, hide);
    try
    {
      String text = hide ? hideText : showText;
      buf.append(openDiv).append("<a href=\"http://")
              .append(URLEncoder.encode(params, "UTF-8"))
              .append("\">").append(text).append("</a>");
      if (hide)
      {
        buf.append(getLineBreak()).append(stackText);
      }
      buf.append(closeDiv);
 
    } catch (UnsupportedEncodingException uee)
    {
      // Bug
      throw new IllegalStateException("UTF-8 is not supported ", uee);
    }
 
    return buf.toString();
  }
 
  /**
   * Gets the url parameters of the href we construct in getErrorWithStackHtml.
   * @see HtmlProgressMessageFormatter#getErrorWithStackHtml
   * @param openDiv the open div tag for the text.
   * @param hideText the text that we display when we do not display the
   * exception.
   * @param showText the text that we display when we display the exception.
   * @param stackText the stack trace text.
   * @param closeDiv the closing div.
   * @param hide a boolean informing if we are hiding the exception or not.
   * @return the url parameters of the href we construct in getHrefString.
   */
  private String getUrlParams(String openDiv, String hideText,
      String showText, String stackText, String closeDiv, boolean hide)
  {
    StringBuilder buf = new StringBuilder();
    buf.append(openDiv).append(PARAM_SEPARATOR);
    buf.append(hideText).append(PARAM_SEPARATOR);
    buf.append(showText).append(PARAM_SEPARATOR);
    buf.append(stackText).append(PARAM_SEPARATOR);
    buf.append(closeDiv).append(PARAM_SEPARATOR);
    buf.append(hide);
    return buf.toString();
  }
 
  /**
   * Returns the HTML representation of an exception in the
   * progress log for a given url.
   * @param url the url containing all the information required to retrieve
   * the HTML representation.
   * @param inverse indicates whether we want to 'inverse' the representation
   * or not.  For instance if the url specifies that the stack is being hidden
   * and this parameter is <CODE>true</CODE> the resulting HTML will display
   * the stack.
   * @return the HTML representation of an exception in the progress log for a
   * given url.
   */
  private String getErrorWithStackHtml(String url, boolean inverse)
  {
    String p = url.substring("http://".length());
    try
    {
      p = URLDecoder.decode(p, "UTF-8");
    } catch (UnsupportedEncodingException uee)
    {
      // Bug
      throw new IllegalStateException("UTF-8 is not supported ", uee);
    }
    String params[] = p.split(PARAM_SEPARATOR);
    int i = 0;
    String openDiv = params[i++];
    String hideText = params[i++];
    String showText = params[i++];
    String stackText = params[i++];
    String closeDiv = params[i++];
    boolean isHide = Boolean.parseBoolean(params[i]);
 
    if (isHide)
    {
      return getErrorWithStackHtml(openDiv, hideText, showText, stackText,
          closeDiv, !inverse);
    } else
    {
      return getErrorWithStackHtml(openDiv, hideText, showText, stackText,
          closeDiv, inverse);
    }
  }
 
}