HttpSchemeStrategy implements SchemeStrategy { @Override public InputStream getInputStream(final String sessionId, final URI uri, final RequestMethod method, final long timeout, final Collection<KeyValuePair> parameters) throws BadFetchError { final HttpClientBuilder builder = SESSION_STORAGE .getSessionIdentifier(sessionId); final RequestConfig config = setTimeout(timeout); try (CloseableHttpClient client = builder .setDefaultRequestConfig(config).build()) { final String fragmentLessUriString = StringUtils.substringBeforeLast(uri.toString(), "#"); final URI fragmentLessUri = new URI(fragmentLessUriString); final URI requestUri = addParameters(parameters, fragmentLessUri); if (LOGGER.isDebugEnabled()) { LOGGER.debug("connecting to '" + requestUri + "'..."); } final HttpUriRequest request; if (method == RequestMethod.GET) { request = new HttpGet(requestUri); } else { request = new HttpPost(requestUri); } attachFiles(request, parameters); final HttpResponse response = client.execute(request); final StatusLine statusLine = response.getStatusLine(); final int status = statusLine.getStatusCode(); if (status != HttpStatus.SC_OK) { final String reasonPhrase = statusLine.getReasonPhrase(); LOGGER.error("error accessing '" + uri + "': " + reasonPhrase + " (HTTP error code " + status + ")"); return null; } final HttpEntity entity = response.getEntity(); final InputStream input = entity.getContent(); final ReadBuffer buffer = new ReadBuffer(); buffer.read(input); return buffer.getInputStream(); } catch (IOException | URISyntaxException | ParseException | SemanticError e) { throw new BadFetchError(e.getMessage(), e); } } HttpSchemeStrategy(); void setScheme(final String value); @Override String getScheme(); void setFetchTimeout(final int timeout); @Override InputStream getInputStream(final String sessionId, final URI uri,
            final RequestMethod method, final long timeout,
            final Collection<KeyValuePair> parameters); @Override void sessionClosed(final String sessionId); static final String HTTP_SCHEME_NAME; }
@Test(expected = BadFetchError.class) public void testBadFetch() throws Exception, BadFetchError { when(httpClient.execute(any())).thenThrow(new IOException("Simulated HTTP IOException")); final URI uri = new URI(BASE_URL + "?session=id"); final Collection<KeyValuePair> parameters = Arrays.asList( new KeyValuePair("firstName", "Horst"), new KeyValuePair("lastName", "Buchholz") ); strategy.getInputStream(SESSION_ID, uri, RequestMethod.GET, TIMEOUT, parameters); }
@Test public void testGetWithParameterCollection() throws Exception, BadFetchError { final URI uri = new URI(BASE_URL); final Collection<KeyValuePair> parameters = Arrays.asList( new KeyValuePair("firstName", "Horst"), new KeyValuePair("lastName", "Buchholz") ); strategy.getInputStream(SESSION_ID, uri, RequestMethod.GET, TIMEOUT, parameters); verify(httpClient).execute(httpRequestCaptor.capture()); HttpUriRequest httpRequest = httpRequestCaptor.getValue(); assertEquals("GET", httpRequest.getMethod()); assertEquals("http: }
@Test public void testGetWithUriParameters() throws Exception, BadFetchError { final String uriString = BASE_URL + "?firstName=Horst&lastName=Buchholz"; final URI uri = new URI(uriString); final Collection<KeyValuePair> parameters = Collections.emptyList(); strategy.getInputStream(SESSION_ID, uri, RequestMethod.GET, TIMEOUT, parameters); String httpRequestString = captureHttpRequestString(); assertEquals(uriString, httpRequestString); }
@Test public void testUrlParameterWithSpace() throws Exception, BadFetchError { final String uriString = BASE_URL + "?param1=blah%20blah"; final URI uri = new URI(uriString); final Collection<KeyValuePair> parameters = Collections.emptyList(); strategy.getInputStream(SESSION_ID, uri, RequestMethod.GET, TIMEOUT, parameters); String httpRequestString = captureHttpRequestString(); assertEquals(uriString, httpRequestString); }
@Test public void testUrlParameterWithPlus() throws Exception, BadFetchError { final String uriString = BASE_URL + "?param1=blah%2Bblah"; final URI uri = new URI(uriString); final Collection<KeyValuePair> parameters = Collections.emptyList(); strategy.getInputStream(SESSION_ID, uri, RequestMethod.GET, TIMEOUT, parameters); String httpRequestString = captureHttpRequestString(); assertEquals(uriString, httpRequestString); }
@Test public void testUrlFragmentIsRemoved() throws Exception, BadFetchError { final String uriString = BASE_URL + "#thisIsTheFragment"; final URI uri = new URI(uriString); final Collection<KeyValuePair> parameters = Collections.emptyList(); strategy.getInputStream(SESSION_ID, uri, RequestMethod.GET, TIMEOUT, parameters); String httpRequestString = captureHttpRequestString(); assertEquals(BASE_URL, httpRequestString); }