Revision: 20226 http://sourceforge.net/p/gate/code/20226 Author: domrout Date: 2017-05-26 19:37:58 +0000 (Fri, 26 May 2017) Log Message: ----------- Add handler string responses from Mimir, and code to keep connections opne
Modified Paths: -------------- mimir/trunk/mimir-client/src/gate/mimir/tool/WebUtils.java Modified: mimir/trunk/mimir-client/src/gate/mimir/tool/WebUtils.java =================================================================== --- mimir/trunk/mimir-client/src/gate/mimir/tool/WebUtils.java 2017-05-26 17:39:55 UTC (rev 20225) +++ mimir/trunk/mimir-client/src/gate/mimir/tool/WebUtils.java 2017-05-26 19:37:58 UTC (rev 20226) @@ -15,6 +15,7 @@ */ package gate.mimir.tool; +import org.apache.http.ConnectionReuseStrategy; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.CookieStore; @@ -27,13 +28,16 @@ import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.client.utils.URIBuilder; +import org.apache.http.config.SocketConfig; import org.apache.http.entity.ByteArrayEntity; import org.apache.http.entity.SerializableEntity; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.protocol.HttpContext; +import org.jdom.JDOMException; import sun.net.www.protocol.http.HttpURLConnection; import java.io.*; @@ -75,12 +79,19 @@ public WebUtils(CookieStore cookieJar, String userName, String password, int maxConnections) { - connectionManager = new PoolingHttpClientConnectionManager(30, TimeUnit.SECONDS); - // Increase max total connection to 200 + connectionManager = new PoolingHttpClientConnectionManager(60, TimeUnit.SECONDS); connectionManager.setMaxTotal(maxConnections); - // Increase default max connection per route to 20 connectionManager.setDefaultMaxPerRoute(maxConnections); + SocketConfig.Builder socketConfigBuilder = connectionManager.getDefaultSocketConfig().custom(); + + SocketConfig config = socketConfigBuilder. + setSoReuseAddress(true). + setSoKeepAlive(true). + setSoLinger(0). + build(); + connectionManager.setDefaultSocketConfig(config); + this.cookieJar = cookieJar; @@ -93,15 +104,21 @@ creds = null; } - RequestConfig globalConfig = RequestConfig.custom() - .setCookieSpec(CookieSpecs.DEFAULT) - .build(); + HttpClientBuilder builder = HttpClients.custom() + .setConnectionManager(connectionManager) + .setDefaultSocketConfig(config); - client = HttpClients.custom() - .setConnectionManager(connectionManager) - .setDefaultRequestConfig(globalConfig) + if (cookieJar != null) { + RequestConfig globalConfig = RequestConfig.custom() + .setCookieSpec(CookieSpecs.DEFAULT) + .build(); + + builder .setDefaultCookieStore(cookieJar) - .build(); + .setDefaultRequestConfig(globalConfig); + } + + client = builder.build(); } /** @@ -209,6 +226,25 @@ } /** + * Calls a web service action (i.e. it connects to a URL). If the connection + * fails, for whatever reason, or the response code is different from + * {@link HttpURLConnection#HTTP_OK}, then an IOException is raised. + * This method will write all content available from the + * input stream of the resulting connection to a String and return it. + * + * @param baseUrl the constant part of the URL to be accessed. + * @param params an array of String values, that contain an alternation of + * parameter name, and parameter values. + * @throws IOException if the connection fails. + */ + public String getString(String baseUrl, String... params) throws IOException { + StringBuffer resultBuffer = new StringBuffer(); + getText(resultBuffer, baseUrl, params); + return resultBuffer.toString(); + } + + + /** * Calls a web service action (i.e. it connects to a URL), and reads a * serialised int value from the resulting connection. If the connection * fails, for whatever reason, or the response code is different from This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot _______________________________________________ GATE-cvs mailing list GATE-cvs@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/gate-cvs