List of usage examples for org.apache.http.client.methods HttpUriRequest getURI
URI getURI();
From source file:com.comcast.cim.rest.client.xhtml.TestRequestBuilder.java
@Test public void testCanBuildRequestForRelativeLinkHref() throws Exception { Element a = new Element("a", XhtmlParser.XHTML_NS_URI); a.setAttribute("href", "/foo"); buildDocument(a);// w w w . ja va 2 s.c o m URL context = new URL("http://www.example.com/bar"); HttpUriRequest result = impl.followLink(a, context); Assert.assertNotNull(result); Assert.assertEquals("GET", result.getMethod()); Assert.assertEquals("http://www.example.com/foo", result.getURI().toString()); }
From source file:org.apache.abdera2.common.protocol.ClientResponseImpl.java
public String getUri() { HttpUriRequest currentReq = (HttpUriRequest) localContext.getAttribute(ExecutionContext.HTTP_REQUEST); HttpHost currentHost = (HttpHost) localContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST); String currentUrl = currentHost.toURI() + currentReq.getURI(); return currentUrl; }
From source file:com.evrythng.java.wrapper.core.api.ApiCommand.java
private HttpResponse performRequest(final HttpClient client, final MethodBuilder<?> method, final Status expectedStatus) throws EvrythngException { HttpResponse response;/* ww w. ja v a 2 s . com*/ HttpUriRequest request = buildRequest(method); try { logger.debug(">> Executing request: [method={}, url={}]", request.getMethod(), request.getURI().toString()); response = client.execute(request); logger.debug("<< Response received: [statusLine={}]", response.getStatusLine().toString()); } catch (Exception e) { // Convert to custom exception: throw new EvrythngClientException(String.format("Unable to execute request: [uri=%s, cause=%s]", request.getURI(), e.getMessage()), e); } // Assert response status: Utils.assertStatus(response, expectedStatus); return response; }
From source file:org.everit.osgi.authentication.cas.tests.SampleApp.java
public void sessionLogout(final SecureHttpClient secureHttpClient) throws Exception { CloseableHttpClient httpClient = secureHttpClient.getHttpClient(); HttpClientContext httpClientContext = secureHttpClient.getHttpClientContext(); HttpGet httpGet = new HttpGet(sessionLogoutUrl); HttpResponse httpResponse = httpClient.execute(httpGet, httpClientContext); Assert.assertEquals("URL should not be accessed [" + sessionLogoutUrl + "]", HttpServletResponse.SC_NOT_FOUND, httpResponse.getStatusLine().getStatusCode()); HttpUriRequest currentReq = (HttpUriRequest) httpClientContext.getRequest(); HttpHost currentHost = httpClientContext.getTargetHost(); String currentUrl = (currentReq.getURI().isAbsolute()) ? currentReq.getURI().toString() : (currentHost.toURI() + currentReq.getURI()); Assert.assertEquals(loggedOutUrl, currentUrl); EntityUtils.consume(httpResponse.getEntity()); }
From source file:org.apache.hadoop.gateway.ha.dispatch.DefaultHaDispatch.java
@Override protected void executeRequest(HttpUriRequest outboundRequest, HttpServletRequest inboundRequest, HttpServletResponse outboundResponse) throws IOException { HttpResponse inboundResponse = null; try {//from w w w. j a v a 2 s . c o m inboundResponse = executeOutboundRequest(outboundRequest); writeOutboundResponse(outboundRequest, inboundRequest, outboundResponse, inboundResponse); } catch (IOException e) { LOG.errorConnectingToServer(outboundRequest.getURI().toString(), e); failoverRequest(outboundRequest, inboundRequest, outboundResponse, inboundResponse, e); } }
From source file:jetbrains.buildServer.commitPublisher.github.api.impl.GitHubApiImpl.java
private void logRequest(@NotNull HttpUriRequest request, @Nullable String requestEntity) throws IOException { if (!LOG.isDebugEnabled()) return;//from w ww .j av a2 s. c o m if (requestEntity == null) { requestEntity = "<none>"; } LOG.debug("Calling GitHub with:\n" + " requestURL: " + request.getURI().toString() + "\n" + " requestMethod: " + request.getMethod() + "\n" + " requestEntity: " + requestEntity); }
From source file:com.shenit.commons.utils.HttpUtils.java
/** * ?//from www. j a v a2 s. c o m * * @param req * @return */ public static String dumpRequest(HttpUriRequest req) { if (req == null) return null; char column = ':', rtn = '\n', space = ' '; StringBuilder builder = new StringBuilder(req.getMethod()); builder.append(space).append(req.getURI()).append(space).append(req.getProtocolVersion()).append(rtn) .append(rtn); builder.append("HEADERS:\n"); Header[] headers = req.getAllHeaders(); int length = headers.length; for (int i = 0; i < length; i++) { builder.append(headers[i].getName()).append(column).append(headers[i].getValue()).append(rtn); } if (req instanceof HttpPost || req instanceof HttpPut) { builder.append(rtn); builder.append("BODY:\n"); if (null != ((HttpPost) req).getEntity()) { BufferedReader reader = null; try { InputStreamReader isReader = new InputStreamReader(((HttpPost) req).getEntity().getContent()); reader = new BufferedReader(isReader); String line; while ((line = reader.readLine()) != null) { builder.append(line); } } catch (IllegalStateException | IOException e) { if (LOG.isWarnEnabled()) LOG.warn("[dumpRequest] Could not read request due to exception", e); } finally { if (reader != null) try { reader.close(); } catch (IOException e) { if (LOG.isWarnEnabled()) LOG.warn("[dumpRequest] could not close reader due to exception", e); } } } } return builder.toString(); }
From source file:com.subgraph.vega.impl.scanner.handlers.PutChecks.java
@Override public void runModule(HttpUriRequest request, IHttpResponse response, IInjectionModuleContext ctx) { final IPathState ps = ctx.getPathState(); if (response.isFetchFail()) { ctx.error(request, response, "during PUT checks"); } else {//from w ww .j av a 2s .com final int rc = response.getResponseCode(); final IPageFingerprint fp = response.getPageFingerprint(); if (rc >= 200 && rc < 300 && !ps.matchesPathFingerprint(fp) && !ps.has404FingerprintMatching(fp)) { final String resource = request.getURI().toString(); final String key = "vinfo-http-put:" + resource; ctx.publishAlert("vinfo-http-put", key, "HTTP PUT succeeded", request, response, "resource", resource); } } injectionChecks.runPageVariabilityCheck(ctx.getPathState()); }
From source file:com.adrup.saldo.bank.lf.LfBankManager.java
@Override public Map<AccountHashKey, RemoteAccount> getAccounts(Map<AccountHashKey, RemoteAccount> accounts) throws BankException { Log.d(TAG, "getAccounts()"); HttpClient httpClient = new SaldoHttpClient(mContext); try {/* w w w . j av a 2s . c o m*/ // get login page Log.d(TAG, "getting login page"); HttpContext httpContext = new BasicHttpContext(); String res = HttpHelper.get(httpClient, LOGIN_URL, httpContext); HttpUriRequest currentReq = (HttpUriRequest) httpContext.getAttribute(ExecutionContext.HTTP_REQUEST); HttpHost currentHost = (HttpHost) httpContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST); String action = currentHost.toURI() + currentReq.getURI(); Log.e(TAG, "action=" + action); Matcher matcher = Pattern.compile(VIEWSTATE_REGEX).matcher(res); if (!matcher.find()) { Log.e(TAG, "No viewstate match."); Log.d(TAG, res); throw new LfBankException("No viewState match."); } String viewState = matcher.group(1); Log.d(TAG, "viewState= " + viewState); matcher = Pattern.compile(EVENTVALIDATION_REGEX).matcher(res); if (!matcher.find()) { Log.e(TAG, "No eventvalidation match."); Log.d(TAG, res); throw new LfBankException("No eventValidation match."); } String eventValidation = matcher.group(1); Log.d(TAG, "eventValidation= " + eventValidation); // do login post List<NameValuePair> parameters = new ArrayList<NameValuePair>(3); parameters.add(new BasicNameValuePair("__LASTFOCUS", "")); parameters.add(new BasicNameValuePair("__EVENTTARGET", "")); parameters.add(new BasicNameValuePair("__EVENTARGUMENT", "")); parameters.add(new BasicNameValuePair(VIEWSTATE_PARAM, viewState)); parameters.add(new BasicNameValuePair("selMechanism", "PIN-kod")); parameters.add(new BasicNameValuePair(USER_PARAM, mBankLogin.getUsername())); parameters.add(new BasicNameValuePair(PASS_PARAM, mBankLogin.getPassword())); parameters.add(new BasicNameValuePair("btnLogIn.x", "39")); parameters.add(new BasicNameValuePair("btnLogIn.y", "11")); parameters.add(new BasicNameValuePair(EVENTVALIDATION_PARAM, eventValidation)); Log.d(TAG, "logging in..."); res = HttpHelper.post(httpClient, action, parameters); if (res.contains("Felaktig inloggning")) { Log.d(TAG, "auth fail"); throw new AuthenticationException("auth fail"); } Log.d(TAG, "getting accountsUrl"); // token matcher = Pattern.compile(TOKEN_REGEX).matcher(res); if (!matcher.find()) { Log.e(TAG, "No token match."); Log.d(TAG, res); throw new LfBankException("No token match."); } String token = matcher.group(1); Log.d(TAG, "token= " + token); // accountsUrl matcher = Pattern.compile(ACCOUNTS_URL_REGEX).matcher(res); if (!matcher.find()) { Log.e(TAG, "No accountsUrl match."); Log.d(TAG, res); throw new LfBankException("No accountsUrl match."); } String accountsUrl = Html.fromHtml(matcher.group(1)).toString(); accountsUrl += "&_token=" + token; Log.d(TAG, "tokenized accountsUrl= " + accountsUrl); // get accounts page Log.d(TAG, "fetching accounts"); res = HttpHelper.get(httpClient, accountsUrl); matcher = Pattern.compile(ACCOUNTS_REGEX).matcher(res); int remoteId = 1; int count = 0; while (matcher.find()) { count++; int groupCount = matcher.groupCount(); for (int i = 1; i <= groupCount; i++) { Log.d(TAG, i + ":" + matcher.group(i)); } if (groupCount < 2) { throw new BankException("Pattern match issue: groupCount < 2"); } int ordinal = remoteId; String name = Html.fromHtml(matcher.group(1)).toString(); long balance = Long.parseLong(matcher.group(2).replaceAll("\\,|\\.| ", "")) / 100; accounts.put(new AccountHashKey(String.valueOf(remoteId), mBankLogin.getId()), new Account(String.valueOf(remoteId), mBankLogin.getId(), ordinal, name, balance)); remoteId++; } if (count == 0) { Log.d(TAG, "no accounts added"); Log.d(TAG, res); } } catch (IOException e) { Log.e(TAG, e.getMessage(), e); throw new LfBankException(e.getMessage(), e); } catch (HttpException e) { Log.e(TAG, e.getMessage(), e); throw new LfBankException(e.getMessage(), e); } finally { httpClient.getConnectionManager().shutdown(); } return accounts; }
From source file:com.subgraph.vega.internal.crawler.HttpResponseProcessor.java
private void runLoop() throws InterruptedException { while (!stop) { CrawlerTask task = crawlerResponseQueue.take(); if (task.isExitTask()) { crawlerRequestQueue.add(CrawlerTask.createExitTask()); crawlerResponseQueue.add(task); return; }/* w w w .ja va 2 s. c o m*/ HttpUriRequest req = task.getRequest(); activeRequest = req; try { if (task.getResponse() != null) { task.getResponseProcessor().processResponse(crawler, req, task.getResponse(), task.getArgument()); } } catch (Exception e) { logger.log(Level.WARNING, "Unexpected exception processing crawler request: " + req.getURI(), e); } finally { synchronized (requestLock) { activeRequest = null; } final HttpEntity entity = (task.getResponse() == null) ? (null) : task.getResponse().getRawResponse().getEntity(); if (entity != null) try { EntityUtils.consume(entity); } catch (IOException e) { logger.log(Level.WARNING, "I/O exception consuming request entity content for " + req.getURI() + " : " + e.getMessage()); } } synchronized (counter) { counter.addCompletedTask(); crawler.updateProgress(); } if (task.causedException()) { crawler.notifyException(req, task.getException()); } if (outstandingTasks.decrementAndGet() <= 0) { crawlerRequestQueue.add(CrawlerTask.createExitTask()); crawlerResponseQueue.add(CrawlerTask.createExitTask()); return; } } }