PageRenderTime 22ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/JAVA/src/APJP/HTTPS/HTTPSServer.java

http://apjp.googlecode.com/
Java | 265 lines | 208 code | 47 blank | 10 comment | 8 complexity | 1cbb9c80042a5bacadbba31401511f23 MD5 | raw file
  1. /*
  2. APJP, A PHP/JAVA PROXY
  3. Copyright (C) 2009-2011 Jeroen Van Steirteghem
  4. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
  5. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  6. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  7. */
  8. package APJP.HTTPS;
  9. import java.net.InetSocketAddress;
  10. import java.util.Collections;
  11. import java.util.LinkedList;
  12. import java.util.List;
  13. import javax.net.ssl.SSLServerSocket;
  14. import javax.net.ssl.SSLSocket;
  15. import APJP.APJP;
  16. import APJP.Logger;
  17. public class HTTPSServer implements Runnable
  18. {
  19. private static Logger logger;
  20. private Thread thread;
  21. private SSLServerSocket sslServerSocket;
  22. private List<HTTPSServerWorker> httpsServerWorkers;
  23. private String remoteAddress;
  24. private int remotePort;
  25. static
  26. {
  27. logger = Logger.getLogger(APJP.APJP_LOCAL_HTTPS_SERVER_LOGGER_ID);
  28. }
  29. public HTTPSServer(String remoteAddress, int remotePort)
  30. {
  31. httpsServerWorkers = Collections.synchronizedList(new LinkedList<HTTPSServerWorker>());
  32. this.remoteAddress = remoteAddress;
  33. this.remotePort = remotePort;
  34. }
  35. public String getLocalAddress()
  36. {
  37. return APJP.APJP_LOCAL_HTTPS_SERVER_ADDRESS;
  38. }
  39. public int getLocalPort()
  40. {
  41. return sslServerSocket.getLocalPort();
  42. }
  43. public String getRemoteAddress()
  44. {
  45. return remoteAddress;
  46. }
  47. public int getRemotePort()
  48. {
  49. return remotePort;
  50. }
  51. public synchronized void start() throws HTTPSServerException
  52. {
  53. logger.log(2, "HTTPS_SERVER/START");
  54. try
  55. {
  56. startHTTPSServer();
  57. startHTTPSServerWorkers();
  58. }
  59. catch(Exception e)
  60. {
  61. logger.log(2, "HTTPS_SERVER/START: EXCEPTION", e);
  62. throw new HTTPSServerException("HTTPS_SERVER/START", e);
  63. }
  64. }
  65. public synchronized void stop() throws HTTPSServerException
  66. {
  67. logger.log(2, "HTTPS_SERVER/STOP");
  68. try
  69. {
  70. stopHTTPSServer();
  71. stopHTTPSServerWorkers();
  72. }
  73. catch(Exception e)
  74. {
  75. logger.log(2, "HTTPS_SERVER/STOP: EXCEPTION", e);
  76. throw new HTTPSServerException("HTTPS_SERVER/STOP", e);
  77. }
  78. }
  79. protected synchronized void startHTTPSServerWorker(HTTPSServerWorker httpsServerWorker) throws HTTPSServerException
  80. {
  81. logger.log(2, "HTTPS_SERVER/START_HTTPS_SERVER_WORKER");
  82. try
  83. {
  84. httpsServerWorker.start();
  85. httpsServerWorkers.add(httpsServerWorker);
  86. }
  87. catch(Exception e)
  88. {
  89. logger.log(2, "HTTPS_SERVER/START_HTTPS_SERVER_WORKER: EXCEPTION", e);
  90. throw new HTTPSServerException("HTTPS_SERVER/START_HTTPS_SERVER_WORKER", e);
  91. }
  92. }
  93. protected synchronized void stopHTTPSServerWorker(HTTPSServerWorker httpsServerWorker) throws HTTPSServerException
  94. {
  95. logger.log(2, "HTTPS_SERVER/STOP_HTTPS_SERVER_WORKER");
  96. try
  97. {
  98. httpsServerWorker.stop();
  99. httpsServerWorkers.remove(httpsServerWorker);
  100. }
  101. catch(Exception e)
  102. {
  103. logger.log(2, "HTTPS_SERVER/STOP_HTTPS_SERVER_WORKER: EXCEPTION", e);
  104. throw new HTTPSServerException("HTTPS_SERVER/STOP_HTTPS_SERVER_WORKER", e);
  105. }
  106. }
  107. protected synchronized void startHTTPSServer() throws HTTPSServerException
  108. {
  109. logger.log(2, "HTTPS_SERVER/START_HTTPS_SERVER");
  110. try
  111. {
  112. sslServerSocket = HTTPS.createSSLServerSocket(remoteAddress, remotePort);
  113. int i = 0;
  114. while(i <= 100)
  115. {
  116. try
  117. {
  118. sslServerSocket.bind(new InetSocketAddress(APJP.APJP_LOCAL_HTTPS_SERVER_ADDRESS, APJP.APJP_LOCAL_HTTPS_SERVER_PORT + i));
  119. break;
  120. }
  121. catch(Exception e)
  122. {
  123. if(i == 100)
  124. {
  125. throw e;
  126. }
  127. }
  128. i = i + 1;
  129. }
  130. thread = new Thread(this);
  131. thread.start();
  132. }
  133. catch(Exception e)
  134. {
  135. logger.log(2, "HTTPS_SERVER/START_HTTPS_SERVER: EXCEPTION", e);
  136. throw new HTTPSServerException("HTTPS_SERVER/START_HTTPS_SERVER", e);
  137. }
  138. }
  139. protected synchronized void stopHTTPSServer() throws HTTPSServerException
  140. {
  141. logger.log(2, "HTTPS_SERVER/STOP_HTTPS_SERVER");
  142. try
  143. {
  144. thread = null;
  145. try
  146. {
  147. SSLSocket outputSSLSocket = HTTPS.createSSLSocket();
  148. outputSSLSocket.connect(new InetSocketAddress(APJP.APJP_LOCAL_HTTPS_SERVER_ADDRESS, sslServerSocket.getLocalPort()));
  149. outputSSLSocket.close();
  150. }
  151. catch(Exception e)
  152. {
  153. }
  154. try
  155. {
  156. sslServerSocket.close();
  157. }
  158. catch(Exception e)
  159. {
  160. }
  161. }
  162. catch(Exception e)
  163. {
  164. logger.log(2, "HTTPS_SERVER/STOP_HTTPS_SERVER: EXCEPTION", e);
  165. throw new HTTPSServerException("HTTPS_SERVER/STOP_HTTPS_SERVER", e);
  166. }
  167. }
  168. protected synchronized void startHTTPSServerWorkers() throws HTTPSServerException
  169. {
  170. logger.log(2, "HTTPS_SERVER/START_HTTPS_SERVER_WORKERS");
  171. }
  172. protected synchronized void stopHTTPSServerWorkers() throws HTTPSServerException
  173. {
  174. logger.log(2, "HTTPS_SERVER/STOP_HTTPS_SERVER_WORKERS");
  175. try
  176. {
  177. for(HTTPSServerWorker httpsServerWorker2: httpsServerWorkers)
  178. {
  179. httpsServerWorker2.stop();
  180. }
  181. httpsServerWorkers.removeAll(httpsServerWorkers);
  182. }
  183. catch(Exception e)
  184. {
  185. logger.log(2, "HTTPS_SERVER/STOP_HTTPS_SERVER_WORKERS: EXCEPTION", e);
  186. throw new HTTPSServerException("HTTPS_SERVER/STOP_HTTPS_SERVER_WORKERS", e);
  187. }
  188. }
  189. public void run()
  190. {
  191. while(thread != null)
  192. {
  193. try
  194. {
  195. SSLSocket inputSSLSocket = (SSLSocket) sslServerSocket.accept();
  196. if(thread != null)
  197. {
  198. HTTPSServerWorker httpsServerWorker = new HTTPSServerWorker(this, inputSSLSocket);
  199. startHTTPSServerWorker(httpsServerWorker);
  200. }
  201. else
  202. {
  203. inputSSLSocket.close();
  204. }
  205. }
  206. catch(Exception e)
  207. {
  208. if(thread != null)
  209. {
  210. logger.log(2, "HTTPS_SERVER: EXCEPTION", e);
  211. }
  212. }
  213. }
  214. }
  215. }