1 package com.jsql.view.swing.panel.consoles;
2
3 import com.jsql.model.bean.util.HttpHeader;
4 import com.jsql.model.injection.vendor.model.VendorYaml;
5 import com.jsql.util.I18nUtil;
6 import com.jsql.util.LogLevelUtil;
7 import com.jsql.util.StringUtil;
8 import com.jsql.view.swing.panel.util.HTMLEditorKitTextPaneWrap;
9 import com.jsql.view.swing.tab.TabbedPaneWheeled;
10 import com.jsql.view.swing.text.JPopupTextComponent;
11 import com.jsql.view.swing.text.JTextPanePlaceholder;
12 import com.jsql.view.swing.text.SyntaxTextArea;
13 import com.jsql.view.swing.util.I18nViewUtil;
14 import com.jsql.view.swing.util.MediatorHelper;
15 import com.jsql.view.swing.util.UiUtil;
16 import org.apache.commons.lang3.StringUtils;
17 import org.apache.logging.log4j.LogManager;
18 import org.apache.logging.log4j.Logger;
19 import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
20 import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
21 import org.fife.ui.rtextarea.RTextScrollPane;
22 import org.jsoup.Jsoup;
23 import org.jsoup.safety.Safelist;
24
25 import javax.swing.*;
26 import javax.swing.text.DefaultCaret;
27 import java.awt.*;
28 import java.awt.event.FocusAdapter;
29 import java.awt.event.FocusEvent;
30 import java.util.AbstractMap.SimpleEntry;
31 import java.util.Arrays;
32 import java.util.Map;
33 import java.util.stream.Stream;
34
35 public class TabbedPaneNetworkTab extends TabbedPaneWheeled {
36
37 private static final Logger LOGGER = LogManager.getRootLogger();
38
39 private final RSyntaxTextArea textAreaUrl = new SyntaxTextArea(I18nUtil.valueByKey("NETWORK_LINE_PLACEHOLDER_URL"));
40 private final RSyntaxTextArea textAreaResponse = new SyntaxTextArea(I18nUtil.valueByKey("NETWORK_LINE_PLACEHOLDER_RESPONSE"));
41 private final RSyntaxTextArea textAreaSource = new SyntaxTextArea(I18nUtil.valueByKey("NETWORK_LINE_PLACEHOLDER_SOURCE"));
42 private final JTextPane textPanePreview = new JPopupTextComponent<>(new JTextPanePlaceholder(I18nUtil.valueByKey("NETWORK_LINE_PLACEHOLDER_PREVIEW")){
43 @Override
44 public boolean isEditable() {
45 return false;
46 }
47 }).getProxy();
48 private final RSyntaxTextArea textAreaHeader = new SyntaxTextArea(I18nUtil.valueByKey("NETWORK_LINE_PLACEHOLDER_HEADERS"));
49 private final RSyntaxTextArea textAreaRequest = new SyntaxTextArea(I18nUtil.valueByKey("NETWORK_LINE_PLACEHOLDER_REQUEST"));
50 private final JCheckBox checkBoxDecode = new JCheckBox("Decode", MediatorHelper.model().getMediatorUtils().getPreferencesUtil().isUrlDecodeNetworkTab());
51
52 public TabbedPaneNetworkTab() {
53 this.setName("tabNetwork");
54 var panelDecode = new JPanel(new BorderLayout());
55 panelDecode.add(this.checkBoxDecode, BorderLayout.LINE_END);
56 this.putClientProperty("JTabbedPane.trailingComponent", panelDecode);
57
58 I18nViewUtil.addComponentForKey("NETWORK_LINE_PLACEHOLDER_URL", this.textAreaUrl);
59 I18nViewUtil.addComponentForKey("NETWORK_LINE_PLACEHOLDER_RESPONSE", this.textAreaResponse);
60 I18nViewUtil.addComponentForKey("NETWORK_LINE_PLACEHOLDER_SOURCE", this.textAreaSource);
61 I18nViewUtil.addComponentForKey("NETWORK_LINE_PLACEHOLDER_PREVIEW", this.textPanePreview);
62 I18nViewUtil.addComponentForKey("NETWORK_LINE_PLACEHOLDER_HEADERS", this.textAreaHeader);
63 I18nViewUtil.addComponentForKey("NETWORK_LINE_PLACEHOLDER_REQUEST", this.textAreaRequest);
64 Stream.of(
65 new SimpleEntry<>("NETWORK_TAB_URL_LABEL", this.textAreaUrl),
66 new SimpleEntry<>("NETWORK_TAB_HEADERS_LABEL", this.textAreaHeader),
67 new SimpleEntry<>("NETWORK_TAB_PARAMS_LABEL", this.textAreaRequest),
68 new SimpleEntry<>("NETWORK_TAB_RESPONSE_LABEL", this.textAreaResponse),
69 new SimpleEntry<>("NETWORK_TAB_SOURCE_LABEL", this.textAreaSource),
70 new SimpleEntry<>("NETWORK_TAB_PREVIEW_LABEL", this.textPanePreview)
71 )
72 .forEach(entry -> {
73 this.addTab(
74 I18nUtil.valueByKey(entry.getKey()),
75 entry.getValue() == this.textAreaSource
76 ? new RTextScrollPane(entry.getValue(), false)
77 : new JScrollPane(entry.getValue())
78 );
79 var label = new JLabel(I18nUtil.valueByKey(entry.getKey()));
80 label.setName("label"+ entry.getKey());
81 this.setTabComponentAt(
82 this.indexOfTab(I18nUtil.valueByKey(entry.getKey())),
83 label
84 );
85 I18nViewUtil.addComponentForKey(entry.getKey(), label);
86
87 entry.getValue().setName("text"+ entry.getKey());
88
89 DefaultCaret caret = (DefaultCaret) entry.getValue().getCaret();
90 caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
91 });
92
93 Arrays.asList(this.textAreaUrl, this.textAreaHeader, this.textAreaRequest, this.textAreaResponse, this.textAreaSource).forEach(entry -> {
94 entry.setEditable(false);
95 entry.setLineWrap(true);
96 });
97 this.textAreaResponse.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_HTML);
98 this.textAreaSource.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_HTML);
99 this.textAreaSource.setHighlightSecondaryLanguages(true);
100 this.applyTheme();
101
102 this.textPanePreview.setEditorKit(new HTMLEditorKitTextPaneWrap());
103 this.textPanePreview.setContentType("text/html");
104 this.textPanePreview.setEditable(false);
105 this.textPanePreview.getCaret().setBlinkRate(0);
106 this.textPanePreview.addFocusListener(new FocusAdapter() {
107 @Override
108 public void focusGained(FocusEvent focusEvent) {
109 TabbedPaneNetworkTab.this.textPanePreview.getCaret().setVisible(true);
110 TabbedPaneNetworkTab.this.textPanePreview.getCaret().setSelectionVisible(true);
111 }
112 });
113 }
114
115 public void changeTextNetwork(HttpHeader networkData) {
116 this.textAreaRequest.setText(this.getDecodedValue(this.checkBoxDecode.isSelected(), networkData.getPost()));
117 this.textAreaUrl.setText(this.getDecodedValue(this.checkBoxDecode.isSelected(), networkData.getUrl()));
118 this.updateTextArea(this.textAreaHeader, networkData.getHeader());
119 this.updateTextArea(this.textAreaResponse, networkData.getResponse());
120
121
122
123 try {
124 this.textAreaSource.setText(
125 StringUtil.detectUtf8(networkData.getSource())
126 .replaceAll(VendorYaml.CALIBRATOR_SQL +"{5,}", VendorYaml.CALIBRATOR_SQL +"*")
127 .trim()
128 );
129 } catch (ArrayIndexOutOfBoundsException | NullPointerException e) {
130 LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
131 }
132
133 this.textPanePreview.getEditorKit().createDefaultDocument();
134
135
136
137
138
139 try {
140 this.textPanePreview.setText(
141 Jsoup.clean(
142 String.format(
143 "<html>%s</html>",
144 StringUtil.detectUtf8(networkData.getSource())
145 )
146 .replaceAll("<img[^>]*>",StringUtils.EMPTY)
147 .replaceAll("<input[^>]*type=\"?hidden\"?.*>", StringUtils.EMPTY)
148 .replaceAll(
149 "<input[^>]*type=\"?(submit|button)\"?.*>",
150 "<div style=\"background-color:#eeeeee;text-align:center;border:1px solid black;width:100px;\">button</div>"
151 )
152 .replaceAll(
153 "<input[^>]*>",
154 "<div style=\"text-align:center;border:1px solid black;width:100px;\">input</div>"
155 ),
156 Safelist.relaxed().addTags("center", "div", "span").addAttributes(":all", "style")
157 )
158 );
159 } catch (Exception | ExceptionInInitializerError e) {
160 LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
161 }
162 }
163
164 private void updateTextArea(JTextArea textArea, Map<String, String> httpData) {
165 textArea.setText(StringUtils.EMPTY);
166 if (httpData != null) {
167 httpData.forEach((key, value) -> {
168 String decodedValue = this.getDecodedValue(this.checkBoxDecode.isSelected(), value);
169 textArea.append(key + ": " + decodedValue + "\n");
170 });
171 }
172 }
173
174 private String getDecodedValue(boolean isSelected, String value) {
175
176 try {
177 return isSelected ? StringUtil.fromUrl(value) : value;
178 } catch (IllegalArgumentException e) {
179 LOGGER.log(LogLevelUtil.CONSOLE_JAVA, "Decoding failure: {}", e.getMessage());
180 return value;
181 }
182 }
183
184 public void reset() {
185 this.textAreaUrl.setText(StringUtils.EMPTY);
186 this.textAreaHeader.setText(StringUtils.EMPTY);
187 this.textAreaRequest.setText(StringUtils.EMPTY);
188 this.textAreaResponse.setText(StringUtils.EMPTY);
189
190
191 try {
192 this.textAreaSource.setText(StringUtils.EMPTY);
193 } catch (NullPointerException e) {
194 LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
195 }
196
197
198 try {
199 this.textPanePreview.setText(StringUtils.EMPTY);
200 } catch (ArrayIndexOutOfBoundsException e) {
201 LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
202 }
203 }
204
205 public void applyTheme() {
206 Arrays.asList(
207 this.textAreaUrl, this.textAreaHeader, this.textAreaRequest, this.textAreaResponse, this.textAreaSource
208 ).forEach(UiUtil::applySyntaxTheme);
209 }
210
211 public JCheckBox getCheckBoxDecode() {
212 return this.checkBoxDecode;
213 }
214 }