@Override public synchronized void close() throws java.io.IOException { if (httpClient != null) httpClient.close(); }
protected void configClient(HttpClientBuilder cb, Map<Prop, Object> settings) throws HTTPException { cb.useSystemProperties(); String agent = (String) settings.get(Prop.USER_AGENT); if(agent != null) cb.setUserAgent(agent); session.setInterceptors(cb); session.setContentDecoderRegistry(cb); session.setClientManager(cb, this); session.setRetryHandler(cb); }
public HTTPMethod setCredentials(Credentials creds) throws HTTPException { this.session.setCredentials(creds); return this; }
static synchronized void processDFlags() { // Pull overrides from command line int seconds = getDPropInt(DCONNTIMEOUT); if(seconds > 0) setGlobalConnectionTimeout(seconds*1000); seconds = getDPropInt(DSOTIMEOUT); if(seconds > 0) setGlobalSoTimeout(seconds*1000); int conns = getDPropInt(DMAXCONNS); if(conns > 0) setGlobalMaxConnections(conns); }
static synchronized void setGlobalKeyStore() { String keypassword = cleanproperty("keystorepassword"); String keypath = cleanproperty("keystore"); String trustpassword = cleanproperty("truststorepassword"); String trustpath = cleanproperty("truststore"); if(keypath != null || trustpath != null) { // define conditionally HTTPSSLProvider sslprovider = new HTTPSSLProvider(keypath, keypassword, trustpath, trustpassword); setGlobalCredentialsProvider( new AuthScope(ANY_HOST, ANY_PORT, ANY_REALM, HTTPAuthPolicy.SSL), sslprovider); } }
/** * Calling close will force the method to close, and will * force any open stream to terminate. If the session is local, * Then that too will be closed. */ public synchronized void close() { if(closed) return; // multiple calls ok closed = true; // mark as closed to prevent recursive calls if(methodstream != null) { try { methodstream.close(); } catch (IOException ioe) {/*failure is ok*/} ; methodstream = null; } if(this.request != null) { this.request.releaseConnection(); this.request = null; } if(session != null) { session.removeMethod(this); if(localsession) { session.close(); session = null; } } }
System.out.println("Test: HTTPSession.setGlobalUserAgent(" + GLOBALAGENT + ")"); HTTPSession.setInterceptors(false); HTTPSession.setGlobalUserAgent(GLOBALAGENT); try (HTTPSession session = HTTPFactory.newSession(TESTURL1)) { List<Header> agents = null; agents = HTTPSession.debugRequestInterceptor().getHeaders(HTTPSession.HEADER_USERAGENT); Assert.assertFalse("User-Agent Header not found", agents.size() == 0); Assert.assertFalse("Multiple User-Agent Headers", agents.size() > 1); HTTPSession.resetInterceptors(); session.setUserAgent(SESSIONAGENT); method = HTTPFactory.Get(session, TESTURL1); method.execute(); agents = HTTPSession.debugRequestInterceptor().getHeaders(HTTPSession.HEADER_USERAGENT); Assert.assertFalse("User-Agent Header not found", agents.size() == 0); Assert.assertFalse("Multiple User-Agent Headers", agents.size() > 1);
session.setSoTimeout(17777); session.setConnectionTimeout(37777); session.setMaxRedirects(111); Credentials bp = new UsernamePasswordCredentials("anyuser", "password"); session.setCredentials(bp);
@Test public void testCache() throws Exception { logger.info("*** Testing: Cache Invalidation"); for(AuthDataBasic data : basictests) { Result result = null; TestProvider provider = null; logger.info("*** URL: " + data.url); // Do each test with a bad password to cause cache invalidation provider = new TestProvider(data.user, BADPASSWORD); HTTPSession session = HTTPFactory.newSession(data.url); session.setCredentialsProvider(provider); result = invoke(session, data.url); report(result,provider.counter); Assert.assertTrue("Incorrect return code: " + result.status, result.status == HttpStatus.SC_UNAUTHORIZED || result.status == HttpStatus.SC_FORBIDDEN); Assert.assertTrue("Credentials provider called: " + provider.counter, provider.counter == 1); // retry with correct password; // AuthCache should automatically clear bad one from cache. provider.setUserPwd(data.user, data.password); session.setCredentialsProvider(provider); result = invoke(session, data.url); report(result,provider.counter); Assert.assertTrue("Incorrect return code: " + result.status, check(result.status)); session.close(); } }
static protected synchronized void kill() { if(sessionList != null) { for(HTTPSession session : sessionList) { session.close(); } sessionList.clear(); // Rebuild the connection manager connmgr.shutdown(); connmgr = new PoolingClientConnectionManager(); setGlobalThreadCount(DFALTTHREADCOUNT); } }
Assert.assertFalse(session.isClosed()); int methodcount = session.getMethodcount(); Assert.assertTrue(methodcount == 1); Assert.assertTrue(method.isClosed()); Assert.assertFalse(method.hasStreamOpen()); methodcount = session.getMethodcount(); Assert.assertTrue(methodcount == 0); methodcount = session.getMethodcount(); Assert.assertTrue(methodcount == 1); status = method.execute(); methodcount = session.getMethodcount(); Assert.assertTrue(methodcount == 1); status = method.execute(); stream = (HTTPMethodStream) method.getResponseBodyAsStream(); session.close(); Assert.assertTrue(stream.isClosed()); Assert.assertTrue(method.isClosed()); methodcount = session.getMethodcount(); Assert.assertTrue(methodcount == 0); Assert.assertTrue(session.isClosed()); Assert.assertTrue(method.isSessionLocal()); session = method.getSession(); methodcount = session.getMethodcount(); Assert.assertTrue(methodcount == 1);
/** * @param provider * @throws HTTPException */ public HTTPSession setCredentialsProvider(CredentialsProvider provider) throws HTTPException { setCredentialsProvider(provider, null); return this; }
@Deprecated static public void setGlobalCredentialsProvider(AuthScope scope, CredentialsProvider provider) throws HTTPException { setGlobalCredentialsProvider(provider, scope); }
HTTPSession.setGlobalCredentials(creds); try (HTTPSession session = HTTPFactory.newSession(data.url)) { result = invoke(session, data.url); HTTPSession.clearGlobalCredentials(); session.setCredentials(creds); result = invoke(session, data.url); report(result);
HTTPSession.setGlobalCredentialsProvider(provider); HTTPSession.clearGlobalCredentials(); session.setCredentialsProvider(provider); result = invoke(session, data.url); report(result,provider.counter);
url = session.getSessionURI(); if(url == null) throw new HTTPException("HTTPMethod: cannot find usable url"); this.methodurl = HTTPUtil.uriExclude(this.methodurl, HTTPUtil.URIPart.USERINFO); this.session.setCredentials( new UsernamePasswordCredentials(this.userinfo)); this.session.addMethod(this); this.methodkind = m;
public HTTPSession(String url) throws HTTPException { try { new URL(url); } catch (MalformedURLException mue) { throw new HTTPException("Malformed URL: " + url, mue); } this.legalurl = url; try { synchronized (HTTPSession.class) { sessionClient = new DefaultHttpClient(connmgr); } if(TESTING) HTTPSession.track(this); setInterceptors(); } catch (Exception e) { throw new HTTPException("url=" + url, e); } this.execcontext = new BasicHttpContext();// do we need to modify? }
private HTTPMethod getHttpResponse(URI uri) throws IOException { HTTPMethod method = null; try { method = HTTPFactory.Get(uri.toString()); method.getSession().setConnectionTimeout(this.connectionTimeout); method.getSession().setSoTimeout(this.socketTimeout); method.setFollowRedirects(this.followRedirects); method.setRequestHeader("Accept-Encoding", this.contentEncoding); method.execute(); int statusCode = method.getStatusCode(); if (statusCode == 200 || statusCode == 201) { return method; } method.execute(); return method; } catch (Throwable t) { if (method != null) method.close(); throw t; } } }
public TestFormBuilder() { setTitle("HTTPFormBuilder test(s)"); setSystemProperties(); // Turn on Session debugging HTTPSession.TESTING = true; HTTPSession.setInterceptors(false); }
/** * It is convenient to be able to directly set the Credentials * (not the provider) when those credentials are fixed. * Scope defaults to ANY * * @param creds * @throws HTTPException */ static public void setGlobalCredentials(Credentials creds) throws HTTPException { setGlobalCredentials(creds, null); }