1 /*
2  * RequestLogDetail.java
3  *
4  * Copyright (C) 2021 by RStudio, PBC
5  *
6  * Unless you have received this program directly from RStudio pursuant
7  * to the terms of a commercial license agreement with RStudio, then
8  * this program is licensed to you under the terms of version 3 of the
9  * GNU Affero General Public License. This program is distributed WITHOUT
10  * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
11  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
12  * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
13  *
14  */
15 package org.rstudio.studio.client.application.ui;
16 
17 import com.google.gwt.dom.client.Style.Overflow;
18 import com.google.gwt.user.client.ui.Composite;
19 import com.google.gwt.user.client.ui.FlowPanel;
20 import com.google.gwt.user.client.ui.HTML;
21 import org.rstudio.core.client.jsonrpc.RequestLogEntry;
22 
23 public class RequestLogDetail extends Composite
24 {
tryPrettyJson(String raw)25    private native String tryPrettyJson(String raw) /*-{
26       var pretty = raw;
27 
28       try {
29         pretty = JSON.stringify(JSON.parse(raw), null, 2);
30       }
31       catch(e) {
32       }
33 
34       return pretty;
35    }-*/;
36 
RequestLogDetail(RequestLogEntry entry)37    public RequestLogDetail(RequestLogEntry entry)
38    {
39       String req = entry.getRequestData();
40       String resp = entry.getResponseData();
41 
42       final FlowPanel panel = new FlowPanel();
43       panel.getElement().getStyle().setOverflow(Overflow.AUTO);
44 
45       HTML html = new HTML();
46       html.setText("Request ID: " + entry.getRequestId() + "\n\n"
47                    + "== REQUEST ======\n"
48                    + tryPrettyJson(req)
49                    + "\n\n"
50                    + "== RESPONSE ======\n"
51                    + tryPrettyJson(resp)
52                    + "\n");
53       html.getElement().getStyle().setProperty("whiteSpace", "pre-wrap");
54       html.getElement().getStyle().setProperty("userSelect", "text");
55 
56       panel.add(html);
57 
58       initWidget(panel);
59    }
60 }
61