PageRenderTime 29ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

/octanebrowser/src/org/bresearch/octane/browse/lib/BrowserHandler.java

http://websecuritynotebook.googlecode.com/
Java | 294 lines | 164 code | 63 blank | 67 comment | 17 complexity | f3a3dba6cef129a472d32b34156ae169 MD5 | raw file
Possible License(s): Apache-2.0
  1. /**
  2. * Copyright (c) 2006-2007 Berlin Brown and botnode.com/Berlin Research All
  3. * Rights Reserved
  4. *
  5. * http://www.opensource.org/licenses/bsd-license.php
  6. *
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright notice, this
  13. * list of conditions and the following disclaimer. * Redistributions in binary
  14. * form must reproduce the above copyright notice, this list of conditions and
  15. * the following disclaimer in the documentation and/or other materials provided
  16. * with the distribution. * Neither the name of the Botnode.com (Berlin Brown)
  17. * nor the names of its contributors may be used to endorse or promote products
  18. * derived from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  24. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. * Date: 12/15/2009
  33. *
  34. * Home Page: http://botnode.com/
  35. *
  36. * Contact: Berlin Brown <berlin dot brown at gmail.com>
  37. */
  38. package org.bresearch.octane.browse.lib;
  39. import javax.swing.JTextArea;
  40. import javax.swing.JTextField;
  41. import javax.swing.SwingUtilities;
  42. import org.bresearch.octane.browse.utils.SwingWorker;
  43. import org.bresearch.octane.net.ConnectHistory;
  44. import org.bresearch.octane.net.ConnectResult;
  45. import org.bresearch.octane.net.ConnectSettingsBean;
  46. import org.bresearch.octane.net.HistoryNode;
  47. import org.bresearch.octane.net.HttpConnect;
  48. import org.bresearch.octane.net.IHttpConnect;
  49. import org.bresearch.octane.net.SystemSettingsBean;
  50. import org.bresearch.octane.parse.ParseHtml;
  51. import org.bresearch.octane.parse.html.HtmlLink;
  52. import org.bresearch.octane.parse.html.RenderBasicHtml;
  53. public class BrowserHandler {
  54. private ConnectHistory history = new ConnectHistory();
  55. private IHttpConnect connection = new HttpConnect(null, null);
  56. private JTextField urlField;
  57. private JTextArea responseArea;
  58. private JTextField commandField;
  59. private JTextField statusArea;
  60. /**
  61. * Constructor for ConnectFrame.
  62. *
  63. * @param url
  64. * JTextField
  65. * @param response
  66. * JTextArea
  67. * @param actionButton
  68. * JButton
  69. */
  70. public BrowserHandler(final JTextField url, final JTextArea response, final JTextField command, final JTextField statusArea) {
  71. this.urlField = url;
  72. this.responseArea = response;
  73. this.commandField = command;
  74. this.statusArea = statusArea;
  75. }
  76. // ///////////////////////////////////////////////////////////////
  77. public SwingWorker buildCommandWorker() {
  78. return new HandleCommandWorker();
  79. }
  80. public SwingWorker buildConnectWorker() {
  81. return new HandleConnectWorker();
  82. }
  83. public SwingWorker buildConnectWorkerPrev() {
  84. return new HandleCommandWorkerPrev();
  85. }
  86. public class HandleCommandWorkerPrev extends SwingWorker {
  87. public Object construct() {
  88. BrowserHandler.this.handleConnectPrev();
  89. return "" + Thread.currentThread();
  90. }
  91. }
  92. public class HandleCommandWorker extends SwingWorker {
  93. public Object construct() {
  94. BrowserHandler.this.handleCommand(BrowserHandler.this.commandField.getText());
  95. return "" + Thread.currentThread();
  96. }
  97. }
  98. public class HandleConnectWorker extends SwingWorker {
  99. private long tstart = 0;
  100. private long tend;
  101. private long tdiff;
  102. public Object construct() {
  103. BrowserHandler.this.handleConnect();
  104. return "" + Thread.currentThread();
  105. }
  106. public void start() {
  107. this.tstart = System.currentTimeMillis();
  108. System.out.println("Executing...");
  109. super.start();
  110. }
  111. public void execTime() {
  112. this.tend = System.currentTimeMillis();
  113. this.tdiff = this.tend - tstart;
  114. }
  115. public void finished() {
  116. System.out.println("Operation complete [!]");
  117. this.execTime();
  118. SwingUtilities.invokeLater(new Runnable() {
  119. public void run() {
  120. BrowserHandler.this.statusArea.setText("Operation complete in " + tdiff + " ms");
  121. }
  122. });
  123. super.finished();
  124. }
  125. } // End of the Class //
  126. public void parseAndConnect(final int inputLinkId) {
  127. final ParseHtml parser = this.connection.getParser();
  128. final HtmlLink link = parser.getHtmlLink(inputLinkId);
  129. if (link != null) {
  130. // Attempt another connect //
  131. System.out.println("Attempting connect to link : " + link.getHrefLink());
  132. this.urlField.setText(link.getHrefLink());
  133. this.handleConnect();
  134. } else {
  135. System.out.println("Invalid link id : " + inputLinkId);
  136. } // End of the if - else //
  137. }
  138. /**
  139. * Connect to the previously connected URL address.
  140. *
  141. * @return
  142. */
  143. public ConnectSettingsBean prevHistoryLink() {
  144. // Use the history
  145. if (this.history.getHistoryPointer() != null) {
  146. final HistoryNode historyNode = this.history.getHistoryPointer();
  147. return historyNode.next();
  148. } else {
  149. System.out.println("No history object");
  150. } // End of the if - else //
  151. return null;
  152. }
  153. /**
  154. * Connect to the previously connected URL address.
  155. *
  156. * @return
  157. */
  158. public void connectPrevHistory() {
  159. final ConnectSettingsBean prevConnect = this.prevHistoryLink();
  160. if (prevConnect != null) {
  161. final SystemSettingsBean proxySettings = new SystemSettingsBean(SystemSettingsBean.USER_AGENT, false, "proxy", 9999);
  162. final ConnectSettingsBean connectSettings = new ConnectSettingsBean(prevConnect.getUrl());
  163. this.connect(proxySettings, connectSettings);
  164. // Display connect prev message
  165. System.out.println("Connecting to previous URL request, url=" + prevConnect.getUrl());
  166. final StringBuilder buf = new StringBuilder(100);
  167. buf.append(connection.getLastResult().buildHeaderResponse());
  168. buf.append(">>>>-------------------\n");
  169. final ConnectResult result = connection.getLastResult();
  170. final RenderBasicHtml renderHtml = result.getRenderHtml();
  171. buf.append(renderHtml.getRenderHtmlData());
  172. this.responseArea.setText(buf.toString());
  173. } // End of the if //
  174. }
  175. public void handleConnectPrev() {
  176. // Convert to integer, use as the link id //
  177. try {
  178. final ConnectResult result = connection.getLastResult();
  179. if (result != null) {
  180. connectPrevHistory();
  181. } else {
  182. System.out.println("No html results from previous request");
  183. } // End of the if - else //
  184. } catch (NumberFormatException e) {
  185. e.printStackTrace();
  186. } // End of the try - catch //
  187. }
  188. public void handleCommand(final String text) {
  189. // Convert to integer, use as the link id //
  190. try {
  191. final int inputLinkId = Integer.parseInt(text);
  192. System.out.println("Processing command link Id : " + inputLinkId);
  193. final ConnectResult result = connection.getLastResult();
  194. if (result != null) {
  195. // First check if the link id is negative //
  196. if (inputLinkId == -1) {
  197. connectPrevHistory();
  198. } else {
  199. this.parseAndConnect(inputLinkId);
  200. } // End of the if - else //
  201. } else {
  202. System.out.println("No html results from previous request");
  203. } // End of the if - else //
  204. } catch (NumberFormatException e) {
  205. e.printStackTrace();
  206. } // End of the try - catch //
  207. }
  208. public void connect(final SystemSettingsBean proxySettings, final ConnectSettingsBean connectSettings) {
  209. this.connection = new HttpConnect(connectSettings, proxySettings);
  210. connection.buildConnectProperties();
  211. connection.connect(connection.buildURL());
  212. connection.parse();
  213. }
  214. /**
  215. * Connect to URL.
  216. */
  217. public void handleConnect() {
  218. System.out.println("Executing Connect");
  219. final SystemSettingsBean proxySettings = new SystemSettingsBean(SystemSettingsBean.USER_AGENT, false, "proxy", 9999);
  220. final ConnectSettingsBean connectSettings = new ConnectSettingsBean(this.urlField.getText());
  221. this.history.addConnectInfo(connectSettings);
  222. this.connect(proxySettings, connectSettings);
  223. final StringBuilder buf = new StringBuilder(100);
  224. buf.append(connection.getLastResult().buildHeaderResponse());
  225. buf.append(">>>>-------------------\n");
  226. final ConnectResult result = connection.getLastResult();
  227. final RenderBasicHtml renderHtml = result.getRenderHtml();
  228. buf.append(renderHtml.getRenderHtmlData());
  229. this.responseArea.setText(buf.toString());
  230. }
  231. } // End of the Class //