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

/projects/tomcat-7.0.2/java/org/apache/coyote/http11/AbstractHttp11Protocol.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 431 lines | 260 code | 94 blank | 77 comment | 17 complexity | c9111f0394fa54bbcd4d25ad3736321b MD5 | raw file
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.coyote.http11;
  18. import java.net.InetAddress;
  19. import java.net.URLEncoder;
  20. import java.util.HashMap;
  21. import java.util.Iterator;
  22. import java.util.concurrent.Executor;
  23. import javax.management.MBeanRegistration;
  24. import javax.management.MBeanServer;
  25. import javax.management.ObjectName;
  26. import org.apache.coyote.Adapter;
  27. import org.apache.coyote.ProtocolHandler;
  28. import org.apache.juli.logging.Log;
  29. import org.apache.tomcat.util.modeler.Registry;
  30. import org.apache.tomcat.util.net.AbstractEndpoint;
  31. import org.apache.tomcat.util.net.SSLImplementation;
  32. import org.apache.tomcat.util.res.StringManager;
  33. public abstract class AbstractHttp11Protocol implements ProtocolHandler, MBeanRegistration {
  34. /**
  35. * The string manager for this package.
  36. */
  37. protected static final StringManager sm = StringManager.getManager(Constants.Package);
  38. protected abstract Log getLog();
  39. protected ObjectName tpOname = null;
  40. protected ObjectName rgOname = null;
  41. protected AbstractEndpoint endpoint=null;
  42. protected SSLImplementation sslImplementation = null;
  43. /**
  44. * The adapter, used to call the connector.
  45. */
  46. protected Adapter adapter;
  47. public void setAdapter(Adapter adapter) { this.adapter = adapter; }
  48. public Adapter getAdapter() { return adapter; }
  49. protected HashMap<String, Object> attributes = new HashMap<String, Object>();
  50. /**
  51. * Pass config info
  52. */
  53. public void setAttribute(String name, Object value) {
  54. if (getLog().isTraceEnabled()) {
  55. getLog().trace(sm.getString("http11protocol.setattribute", name, value));
  56. }
  57. attributes.put(name, value);
  58. }
  59. public Object getAttribute(String key) {
  60. return attributes.get(key);
  61. }
  62. public Iterator<String> getAttributeNames() {
  63. return attributes.keySet().iterator();
  64. }
  65. /**
  66. * Set a property.
  67. */
  68. public boolean setProperty(String name, String value) {
  69. setAttribute(name, value); //store all settings
  70. if ( name!=null && (name.startsWith("socket.") ||name.startsWith("selectorPool.")) ){
  71. return endpoint.setProperty(name, value);
  72. } else {
  73. return endpoint.setProperty(name,value); //make sure we at least try to set all properties
  74. }
  75. }
  76. /**
  77. * Get a property
  78. */
  79. public String getProperty(String name) {
  80. return (String)getAttribute(name);
  81. }
  82. public InetAddress getAddress() { return endpoint.getAddress(); }
  83. public void setAddress(InetAddress ia) {
  84. endpoint.setAddress( ia );
  85. setAttribute("address", "" + ia);
  86. }
  87. public String getName() {
  88. String encodedAddr = "";
  89. if (getAddress() != null) {
  90. encodedAddr = "" + getAddress();
  91. if (encodedAddr.startsWith("/"))
  92. encodedAddr = encodedAddr.substring(1);
  93. encodedAddr = URLEncoder.encode(encodedAddr) + "-";
  94. }
  95. return ("http-" + encodedAddr + endpoint.getPort());
  96. }
  97. public void pause() throws Exception {
  98. try {
  99. endpoint.pause();
  100. } catch (Exception ex) {
  101. getLog().error(sm.getString("http11protocol.endpoint.pauseerror"), ex);
  102. throw ex;
  103. }
  104. if(getLog().isInfoEnabled())
  105. getLog().info(sm.getString("http11protocol.pause", getName()));
  106. }
  107. public void resume() throws Exception {
  108. try {
  109. endpoint.resume();
  110. } catch (Exception ex) {
  111. getLog().error(sm.getString("http11protocol.endpoint.resumeerror"), ex);
  112. throw ex;
  113. }
  114. if(getLog().isInfoEnabled())
  115. getLog().info(sm.getString("http11protocol.resume", getName()));
  116. }
  117. public void destroy() throws Exception {
  118. if(getLog().isInfoEnabled())
  119. getLog().info(sm.getString("http11protocol.stop", getName()));
  120. endpoint.destroy();
  121. if( tpOname!=null )
  122. Registry.getRegistry(null, null).unregisterComponent(tpOname);
  123. if( rgOname != null )
  124. Registry.getRegistry(null, null).unregisterComponent(rgOname);
  125. }
  126. public boolean isSSLEnabled() { return endpoint.isSSLEnabled();}
  127. public void setSSLEnabled(boolean SSLEnabled) { endpoint.setSSLEnabled(SSLEnabled);}
  128. private boolean secure;
  129. public boolean getSecure() { return secure; }
  130. public void setSecure(boolean b) {
  131. secure = b;
  132. setAttribute("secure", "" + b);
  133. }
  134. /**
  135. * Processor cache.
  136. */
  137. private int processorCache = 200;
  138. public int getProcessorCache() { return this.processorCache; }
  139. public void setProcessorCache(int processorCache) { this.processorCache = processorCache; }
  140. private int socketBuffer = 9000;
  141. public int getSocketBuffer() { return socketBuffer; }
  142. public void setSocketBuffer(int socketBuffer) { this.socketBuffer = socketBuffer; }
  143. // HTTP
  144. /**
  145. * Maximum number of requests which can be performed over a keepalive
  146. * connection. The default is the same as for Apache HTTP Server.
  147. */
  148. public int getMaxKeepAliveRequests() { return endpoint.getMaxKeepAliveRequests(); }
  149. public void setMaxKeepAliveRequests(int mkar) {
  150. endpoint.setMaxKeepAliveRequests(mkar);
  151. setAttribute("maxKeepAliveRequests", "" + mkar);
  152. }
  153. /**
  154. * Return the Keep-Alive policy for the connection.
  155. */
  156. public boolean getKeepAlive() {
  157. return ((endpoint.getMaxKeepAliveRequests() != 0) && (endpoint.getMaxKeepAliveRequests() != 1));
  158. }
  159. /**
  160. * Set the keep-alive policy for this connection.
  161. */
  162. public void setKeepAlive(boolean keepAlive) {
  163. if (!keepAlive) {
  164. setMaxKeepAliveRequests(1);
  165. }
  166. }
  167. public void setKeepAliveTimeout(int keepAliveTimeout) {
  168. endpoint.setKeepAliveTimeout(keepAliveTimeout);
  169. }
  170. public int getKeepAliveTimeout() {
  171. return endpoint.getKeepAliveTimeout();
  172. }
  173. public int getTimeout() {
  174. return getSoTimeout();
  175. }
  176. public void setTimeout( int timeout ) {
  177. setSoTimeout(timeout);
  178. }
  179. public int getConnectionTimeout() {
  180. return getSoTimeout();
  181. }
  182. public void setConnectionTimeout( int timeout ) {
  183. setSoTimeout(timeout);
  184. }
  185. public int getSoTimeout() {
  186. return endpoint.getSoTimeout();
  187. }
  188. public void setSoTimeout( int i ) {
  189. endpoint.setSoTimeout(i);
  190. setAttribute("soTimeout", "" + i);
  191. setAttribute("timeout", "" + i);
  192. setAttribute("connectionTimeout", "" + i);
  193. }
  194. // *
  195. /**
  196. * Maximum size of the post which will be saved when processing certain
  197. * requests, such as a POST.
  198. */
  199. private int maxSavePostSize = 4 * 1024;
  200. public int getMaxSavePostSize() { return maxSavePostSize; }
  201. public void setMaxSavePostSize(int valueI) { maxSavePostSize = valueI; }
  202. // HTTP
  203. /**
  204. * Maximum size of the HTTP message header.
  205. */
  206. private int maxHttpHeaderSize = 8 * 1024;
  207. public int getMaxHttpHeaderSize() { return maxHttpHeaderSize; }
  208. public void setMaxHttpHeaderSize(int valueI) { maxHttpHeaderSize = valueI; }
  209. // HTTP
  210. /**
  211. * If true, the regular socket timeout will be used for the full duration
  212. * of the connection.
  213. */
  214. private boolean disableUploadTimeout = true;
  215. public boolean getDisableUploadTimeout() { return disableUploadTimeout; }
  216. public void setDisableUploadTimeout(boolean isDisabled) { disableUploadTimeout = isDisabled; }
  217. // HTTP
  218. /**
  219. * Integrated compression support.
  220. */
  221. private String compression = "off";
  222. public String getCompression() { return compression; }
  223. public void setCompression(String valueS) { compression = valueS; }
  224. // HTTP
  225. private String noCompressionUserAgents = null;
  226. public String getNoCompressionUserAgents() { return noCompressionUserAgents; }
  227. public void setNoCompressionUserAgents(String valueS) { noCompressionUserAgents = valueS; }
  228. // HTTP
  229. private String compressableMimeTypes = "text/html,text/xml,text/plain";
  230. public String getCompressableMimeType() { return compressableMimeTypes; }
  231. public void setCompressableMimeType(String valueS) { compressableMimeTypes = valueS; }
  232. public String getCompressableMimeTypes() { return getCompressableMimeType(); }
  233. public void setCompressableMimeTypes(String valueS) { setCompressableMimeType(valueS); }
  234. // HTTP
  235. private int compressionMinSize = 2048;
  236. public int getCompressionMinSize() { return compressionMinSize; }
  237. public void setCompressionMinSize(int valueI) { compressionMinSize = valueI; }
  238. // HTTP
  239. /**
  240. * User agents regular expressions which should be restricted to HTTP/1.0 support.
  241. */
  242. private String restrictedUserAgents = null;
  243. public String getRestrictedUserAgents() { return restrictedUserAgents; }
  244. public void setRestrictedUserAgents(String valueS) { restrictedUserAgents = valueS; }
  245. // HTTP
  246. /**
  247. * Server header.
  248. */
  249. private String server;
  250. public void setServer( String server ) { this.server = server; }
  251. public String getServer() { return server; }
  252. public Executor getExecutor() { return endpoint.getExecutor(); }
  253. public void setExecutor(Executor executor) { endpoint.setExecutor(executor); }
  254. public int getMaxThreads() { return endpoint.getMaxThreads(); }
  255. public void setMaxThreads(int maxThreads) { endpoint.setMaxThreads(maxThreads); }
  256. public int getThreadPriority() { return endpoint.getThreadPriority(); }
  257. public void setThreadPriority(int threadPriority) { endpoint.setThreadPriority(threadPriority); }
  258. public int getPort() { return endpoint.getPort(); }
  259. public void setPort(int port) { endpoint.setPort(port); }
  260. public int getBacklog() { return endpoint.getBacklog(); }
  261. public void setBacklog(int backlog) { endpoint.setBacklog(backlog); }
  262. public boolean getTcpNoDelay() { return endpoint.getTcpNoDelay(); }
  263. public void setTcpNoDelay(boolean tcpNoDelay) { endpoint.setTcpNoDelay(tcpNoDelay); }
  264. public int getSoLinger() { return endpoint.getSoLinger(); }
  265. public void setSoLinger(int soLinger) { endpoint.setSoLinger(soLinger); }
  266. // JSSE SSL attrbutes
  267. public String getAlgorithm() { return endpoint.getAlgorithm();}
  268. public void setAlgorithm(String s ) { endpoint.setAlgorithm(s);}
  269. public String getClientAuth() { return endpoint.getClientAuth();}
  270. public void setClientAuth(String s ) { endpoint.setClientAuth(s);}
  271. public String getKeystoreFile() { return endpoint.getKeystoreFile();}
  272. public void setKeystoreFile(String s ) { endpoint.setKeystoreFile(s);}
  273. public String getKeystorePass() { return endpoint.getKeystorePass();}
  274. public void setKeystorePass(String s ) { endpoint.setKeystorePass(s);}
  275. public String getKeystoreType() { return endpoint.getKeystoreType();}
  276. public void setKeystoreType(String s ) { endpoint.setKeystoreType(s);}
  277. public String getKeystoreProvider() { return endpoint.getKeystoreProvider();}
  278. public void setKeystoreProvider(String s ) { endpoint.setKeystoreProvider(s);}
  279. public String getSslProtocol() { return endpoint.getSslProtocol();}
  280. public void setSslProtocol(String s) { endpoint.setSslProtocol(s);}
  281. public String getCiphers() { return endpoint.getCiphers();}
  282. public void setCiphers(String s) { endpoint.setCiphers(s);}
  283. public String getKeyAlias() { return endpoint.getKeyAlias();}
  284. public void setKeyAlias(String s ) { endpoint.setKeyAlias(s);}
  285. public String getKeyPass() { return endpoint.getKeyPass();}
  286. public void setKeyPass(String s ) { endpoint.setKeyPass(s);}
  287. public void setTruststoreFile(String f){ endpoint.setTruststoreFile(f);}
  288. public String getTruststoreFile(){ return endpoint.getTruststoreFile();}
  289. public void setTruststorePass(String p){ endpoint.setTruststorePass(p);}
  290. public String getTruststorePass(){return endpoint.getTruststorePass();}
  291. public void setTruststoreType(String t){ endpoint.setTruststoreType(t);}
  292. public String getTruststoreType(){ return endpoint.getTruststoreType();}
  293. public void setTruststoreProvider(String t){endpoint.setTruststoreProvider(t);}
  294. public String getTruststoreProvider(){ return endpoint.getTruststoreProvider();}
  295. public void setTruststoreAlgorithm(String a){endpoint.setTruststoreAlgorithm(a);}
  296. public String getTruststoreAlgorithm(){ return endpoint.getTruststoreAlgorithm();}
  297. public void setTrustMaxCertLength(String s){endpoint.setTrustMaxCertLength(s);}
  298. public String getTrustMaxCertLength(){ return endpoint.getTrustMaxCertLength();}
  299. public void setCrlFile(String s){endpoint.setCrlFile(s);}
  300. public String getCrlFile(){ return endpoint.getCrlFile();}
  301. public void setSessionCacheSize(String s){endpoint.setSessionCacheSize(s);}
  302. public String getSessionCacheSize(){ return endpoint.getSessionCacheSize();}
  303. public void setSessionTimeout(String s){endpoint.setSessionTimeout(s);}
  304. public String getSessionTimeout(){ return endpoint.getSessionTimeout();}
  305. public void setAllowUnsafeLegacyRenegotiation(String s) {
  306. endpoint.setAllowUnsafeLegacyRenegotiation(s);
  307. }
  308. public String getAllowUnsafeLegacyRenegotiation() {
  309. return endpoint.getAllowUnsafeLegacyRenegotiation();
  310. }
  311. public abstract void init() throws Exception;
  312. public abstract void start() throws Exception;
  313. // -------------------- JMX related methods --------------------
  314. // *
  315. protected String domain;
  316. protected ObjectName oname;
  317. protected MBeanServer mserver;
  318. public ObjectName getObjectName() {
  319. return oname;
  320. }
  321. public String getDomain() {
  322. return domain;
  323. }
  324. public ObjectName preRegister(MBeanServer server,
  325. ObjectName name) throws Exception {
  326. oname=name;
  327. mserver=server;
  328. domain=name.getDomain();
  329. return name;
  330. }
  331. public void postRegister(Boolean registrationDone) {
  332. }
  333. public void preDeregister() throws Exception {
  334. }
  335. public void postDeregister() {
  336. }
  337. }