List of usage examples for org.apache.http.impl.client AbstractHttpClient execute
public CloseableHttpResponse execute(final HttpUriRequest request, final HttpContext context) throws IOException, ClientProtocolException
From source file:jp.ambrosoli.quickrestclient.apache.service.ApacheHttpService.java
public HttpResponse execute(final HttpRequest request) { HttpParams httpParams = this.createHttpParams(); this.setProtocolVersion(httpParams, request.getProtocol()); this.setTimeout(httpParams, request.getTimeout()); this.setProxy(httpParams, request.getProxyInfo()); this.setCharset(httpParams, request.getCharset()); URI uri = request.getUri();//w w w. j a va2 s . c o m HttpUriRequest httpUriRequest = this.createHttpUriRequest(uri, request.getMethod(), request.getParams(), request.getCharset()); this.setHeaders(httpUriRequest, request.getHeaders()); SchemeRegistry schreg = this.createSchemeRegistry(uri); ClientConnectionManager conman = this.createClientConnectionManager(httpParams, schreg); AbstractHttpClient client = this.createHttpClient(conman, httpParams); this.setCredentialsAuthenticate(uri, request.getAuthInfo(), client.getCredentialsProvider()); try { return client.execute(httpUriRequest, new ApacheResponseHandler()); } catch (SocketTimeoutException e) { throw new SocketTimeoutRuntimeException(e); } catch (IOException e) { throw new IORuntimeException(e); } finally { conman.shutdown(); } }
From source file:org.jasig.portlet.proxy.service.web.HttpContentServiceImpl.java
public GenericContentResponseImpl getContent(HttpContentRequestImpl proxyRequest, PortletRequest request, boolean runWrapperMethods) { try {/*from ww w .ja va 2 s. c o m*/ // get an HttpClient appropriate for this user and portlet instance // and set any basic auth credentials, if applicable final AbstractHttpClient httpclient = httpClientService.getHttpClient(request); // create the request final HttpUriRequest httpRequest = getHttpRequest(proxyRequest, request); if (log.isTraceEnabled()) { log.trace("Proxying " + httpRequest.getURI() + " via " + httpRequest.getMethod()); } // execute the request final HttpContext context = new BasicHttpContext(); final HttpResponse response = httpclient.execute(httpRequest, context); final HttpEntity entity = response.getEntity(); // create the response object and set the content stream final HttpContentResponseImpl proxyResponse = new HttpContentResponseImpl(entity); proxyResponse.setContent(entity.getContent()); // add each response header to our response object for (Header header : response.getAllHeaders()) { proxyResponse.getHeaders().put(header.getName(), header.getValue()); } // set the final URL of the response in case it was redirected String finalUrl = (String) context.getAttribute(RedirectTrackingResponseInterceptor.FINAL_URL_KEY); if (finalUrl == null) { finalUrl = proxyRequest.getProxiedLocation(); } proxyResponse.setProxiedLocation(finalUrl); return proxyResponse; } catch (ClientProtocolException e) { log.error("Exception retrieving remote content", e); } catch (IOException e) { log.error("Exception retrieving remote content", e); } return null; }
From source file:net.sourceforge.jwbf.mediawiki.live.LoginTest.java
/** * Login on last MW with SSL and htaccess. * //from w ww. j a v a 2 s . co m * @throws Exception * a */ @Test public final void loginWikiMWLastSSLAndHtaccess() throws Exception { AbstractHttpClient httpClient = getSSLFakeHttpClient(); Version latest = Version.getLatest(); URL u = new URL(getValue("wiki_url_latest").replace("http", "https")); assertEquals("https", u.getProtocol()); int port = 443; { // test if authentication required HttpHost targetHost = new HttpHost(u.getHost(), port, u.getProtocol()); HttpGet httpget = new HttpGet(u.getPath()); HttpResponse resp = httpClient.execute(targetHost, httpget); assertEquals(401, resp.getStatusLine().getStatusCode()); resp.getEntity().consumeContent(); } httpClient.getCredentialsProvider().setCredentials(new AuthScope(u.getHost(), port), new UsernamePasswordCredentials(BotFactory.getWikiUser(latest), BotFactory.getWikiPass(latest))); HttpActionClient sslFakeClient = new HttpActionClient(httpClient, u); bot = new MediaWikiBot(sslFakeClient); bot.login(BotFactory.getWikiUser(latest), BotFactory.getWikiPass(latest)); assertTrue(bot.isLoggedIn()); }
From source file:net.sourceforge.jwbf.mediawiki.live.LoginIT.java
/** * Login on last MW with SSL and htaccess. *///from ww w .j ava 2s . c o m @Test public final void loginWikiMWLastSSLAndHtaccess() throws Exception { AbstractHttpClient httpClient = getSSLFakeHttpClient(); Version latest = Version.getLatest(); String url = getValueOrSkip("wiki_url_latest").replace("http", "https"); assumeReachable(url); URL u = new URL(url); assertEquals("https", u.getProtocol()); int port = 443; TestHelper.assumeReachable(u); { // test if authentication required HttpHost targetHost = new HttpHost(u.getHost(), port, u.getProtocol()); HttpGet httpget = new HttpGet(u.getPath()); HttpResponse resp = httpClient.execute(targetHost, httpget); assertEquals(401, resp.getStatusLine().getStatusCode()); resp.getEntity().consumeContent(); } httpClient.getCredentialsProvider().setCredentials(new AuthScope(u.getHost(), port), new UsernamePasswordCredentials(BotFactory.getWikiUser(latest), BotFactory.getWikiPass(latest))); HttpClientBuilder clientBuilder = HttpClientBuilder.create(); HttpActionClient sslFakeClient = new HttpActionClient(clientBuilder, u); bot = new MediaWikiBot(sslFakeClient); bot.login(BotFactory.getWikiUser(latest), BotFactory.getWikiPass(latest)); assertTrue(bot.isLoggedIn()); }
From source file:org.apache.jena.atlas.web.auth.FormsAuthenticator.java
@Override public void apply(AbstractHttpClient client, HttpContext httpContext, URI target) { if (client == null) return;//ww w .ja va 2 s .c om // Do we have a login available for the target server? FormLogin login = this.findCredentials(target); if (login == null) return; // We need to synchronize on the login because making a login attempt // may take a while and there is no point making multiple login attempts // against the same server synchronized (login) { // Have we already logged into this server? if (login.hasCookies()) { // Use existing cookies LOG.info("Using existing cookies to authenticate access to " + target.toString()); CookieStore store = login.getCookies(); if (store != null) client.setCookieStore(store); // Check if any of the cookies have expired if (!store.clearExpired(Calendar.getInstance().getTime())) { // No cookies were cleared so our cookies are still fresh // and we don't need to login again return; } // If we reach here then some of our cookies have expired and we // may no longer be logged in and should login again instead of // proceeding with the existing cookies } try { // Use a fresh Cookie Store for new login attempts CookieStore store = new BasicCookieStore(); client.setCookieStore(store); // Try to login LOG.info("Making login attempt against " + login.getLoginFormURL() + " to obtain authentication for access to " + target.toString()); HttpPost post = new HttpPost(login.getLoginFormURL()); post.setEntity(login.getLoginEntity()); HttpResponse response = client.execute(post, httpContext); // Always read the payload to ensure reusable connections final String payload = HttpOp.readPayload(response.getEntity()); // Check for successful login if (response.getStatusLine().getStatusCode() >= 400) { LOG.warn("Failed to login against " + login.getLoginFormURL() + " to obtain authentication for access to " + target.toString()); throw new HttpException(response.getStatusLine().getStatusCode(), "Login attempt failed - " + response.getStatusLine().getReasonPhrase(), payload); } // Otherwise assume a successful login LOG.info("Successfully logged in against " + login.getLoginFormURL() + " and obtained authentication for access to " + target.toString()); login.setCookies(client.getCookieStore()); } catch (UnsupportedEncodingException e) { throw new HttpException("UTF-8 encoding not supported on your platform", e); } catch (IOException e) { throw new HttpException("Error making login request", e); } } }