1 package com.eviware.soapui.support.components;
2
3 import com.eviware.soapui.SoapUI;
4 import com.eviware.soapui.settings.ProxySettings;
5 import com.eviware.soapui.support.DefaultHyperlinkListener;
6 import com.eviware.soapui.support.UISupport;
7 import com.eviware.soapui.support.StringUtils;
8
9 import javax.swing.*;
10 import javax.swing.text.html.HTMLEditorKit;
11 import java.awt.*;
12 import java.io.ByteArrayInputStream;
13 import java.io.IOException;
14 import java.net.URL;
15
16 public class BrowserComponent
17 {
18 private JEditorPane editorPane;
19 private JScrollPane scrollPane;
20
21
22
23
24
25
26
27
28 public BrowserComponent()
29 {
30 editorPane = new JEditorPane();
31 editorPane.setEditorKit( new HTMLEditorKit() );
32 editorPane.setEditable( false );
33 editorPane.addHyperlinkListener( new DefaultHyperlinkListener( editorPane ) );
34 }
35
36 public Component getComponent()
37 {
38
39
40
41
42
43
44 if( scrollPane == null )
45 {
46 scrollPane = new JScrollPane( editorPane );
47 UISupport.addPreviewCorner( scrollPane, false );
48 }
49 return scrollPane;
50 }
51
52 private void initBrowser()
53 {
54
55
56
57
58
59
60
61
62
63
64
65
66 }
67
68 public void release()
69 {
70
71
72
73
74 }
75
76 public void setContent( String contentAsString, String contentType, String contextUri )
77 {
78 editorPane.setContentType( contentType );
79 try
80 {
81 editorPane.read( new ByteArrayInputStream( contentAsString.getBytes() ), editorPane.getEditorKit().createDefaultDocument() );
82 }
83 catch( IOException e )
84 {
85 e.printStackTrace();
86 }
87
88
89
90
91
92
93 }
94
95 public void setContent( String content, String contentType )
96 {
97 setContent( content, contentType, null );
98
99
100
101
102
103
104 }
105
106 public boolean navigate( String url, String errorPage )
107 {
108 try
109 {
110 String proxyHost = SoapUI.getSettings().getString( ProxySettings.HOST, System.getProperty( "http.proxyHost", null ) );
111 if( StringUtils.hasContent(proxyHost) )
112 {
113 System.setProperty( "http.proxyHost", proxyHost );
114 }
115
116 String proxyPort = SoapUI.getSettings().getString( ProxySettings.PORT, System.getProperty( "http.proxyPort", null ) );
117 if( StringUtils.hasContent(proxyPort) )
118 {
119 System.setProperty( "http.proxyPort", proxyPort );
120 }
121
122 editorPane.setPage( new URL( url ) );
123 return true;
124 }
125 catch( Exception e )
126 {
127 SoapUI.logError( new Exception( "Failed to access [" + url + "]", e ));
128 }
129
130 if( errorPage != null )
131 {
132 navigate( errorPage, null );
133 }
134
135 return false;
136
137
138
139
140
141
142
143 }
144
145 public String getContent()
146 {
147
148 return editorPane.getText();
149 }
150 }