Refine search
HttpClient apacheClient = HttpClientBuilder.create().build(); Client client = new Client(new ApacheHttpClient4Handler(apacheClient, new BasicCookieStore(), true)); WebResource webResource = client.resource("http://localhost:8080/path"); ClientResponse response = webResource.accept("application/json") .get(ClientResponse.class);
private Builder builder( String path, final MediaType accept ) { WebResource resource = client.resource( uri( pathOrAbsolute( path ) ) ); Builder builder = resource.accept( accept ); if ( !headers.isEmpty() ) { for ( Map.Entry<String,String> header : headers.entrySet() ) { builder = builder.header( header.getKey(), header.getValue() ); } } return builder; }
private void initManaged( PurRepositoryMeta repositoryMeta, IUser userInfo ) throws JSONException { String baseUrl = repositoryMeta.getRepositoryLocation().getUrl(); String webService = baseUrl + ( baseUrl.endsWith( "/" ) ? "" : "/" ) + "api/system/authentication-provider"; HTTPBasicAuthFilter authFilter = new HTTPBasicAuthFilter( userInfo.getLogin(), userInfo.getPassword() ); Client client = new Client(); client.addFilter( authFilter ); WebResource.Builder resource = client.resource( webService ).accept( MediaType.APPLICATION_JSON_TYPE ); /** * if set, _trust_user_ needs to be considered. See other places in pur-plugin's: * * @link https://github.com/pentaho/pentaho-kettle/blob/8.0.0.0-R/plugins/pur/core/src/main/java/org/pentaho/di/repository/pur/PurRepositoryConnector.java#L97-L101 * @link https://github.com/pentaho/pentaho-kettle/blob/8.0.0.0-R/plugins/pur/core/src/main/java/org/pentaho/di/repository/pur/WebServiceManager.java#L130-L133 */ if ( StringUtils.isNotBlank( System.getProperty( "pentaho.repository.client.attemptTrust" ) ) ) { resource = resource.header( TRUST_USER, userInfo.getLogin() ); } String response = resource.get( String.class ); String provider = new JSONObject( response ).getString( "authenticationType" ); managed = "jackrabbit".equals( provider ); }
private <T> T getForEntity(String url, Object[] queryParams, Function<ClientResponse, T> entityProvider, Object... uriVariables) { URI uri = null; ClientResponse clientResponse; try { uri = getURIBuilder(root + url, queryParams).build(uriVariables); clientResponse = client.resource(uri) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN) .get(ClientResponse.class); if (clientResponse.getStatus() < 300) { return entityProvider.apply(clientResponse); } else { throw new UniformInterfaceException(clientResponse); } } catch (UniformInterfaceException e) { handleUniformInterfaceException(e, uri); } catch (RuntimeException e) { handleRuntimeException(e, uri); } return null; }
@Test public void shouldUseXForwardedHostHeaderWhenPresent() { // when ClientResponse response = client.resource( getManageUri() ) .accept( APPLICATION_JSON ) .header( X_FORWARDED_HOST, "jimwebber.org" ) .get( ClientResponse.class ); // then String entity = response.getEntity( String.class ); assertTrue( entity.contains( "http://jimwebber.org" ) ); assertFalse( entity.contains( "http://localhost" ) ); }
@Test public void shouldUseXForwardedProtoHeaderWhenPresent() { // when ClientResponse response = client.resource( getManageUri() ) .accept( APPLICATION_JSON ) .header( X_FORWARDED_PROTO, "https" ) .get( ClientResponse.class ); // then String entity = response.getEntity( String.class ); assertTrue( entity.contains( "https://localhost" ) ); assertFalse( entity.contains( "http://localhost" ) ); }
@Test public void shouldPickFirstXForwardedHostHeaderValueFromCommaOrCommaAndSpaceSeparatedList() { // when ClientResponse response = client.resource( getManageUri() ) .accept( APPLICATION_JSON ) .header( X_FORWARDED_HOST, "jimwebber.org, kathwebber.com,neo4j.org" ) .get( ClientResponse.class ); // then String entity = response.getEntity( String.class ); assertTrue( entity.contains( "http://jimwebber.org" ) ); assertFalse( entity.contains( "http://localhost" ) ); }
@Test public void shouldUseBaseUriIfFirstAddressInXForwardedHostHeaderIsBad() { // when ClientResponse response = client.resource( getManageUri() ) .accept( APPLICATION_JSON ) .header( X_FORWARDED_HOST, ":bad_URI,good-host" ) .get( ClientResponse.class ); // then String entity = response.getEntity( String.class ); assertTrue( entity.contains( getServerUri() ) ); }
@Test public void shouldUseBaseUriOnBadXForwardedHostHeader() { // when ClientResponse response = client.resource( getManageUri() ) .accept( APPLICATION_JSON ) .header( X_FORWARDED_HOST, ":bad_URI" ) .get( ClientResponse.class ); // then String entity = response.getEntity( String.class ); assertTrue( entity.contains( getServerUri() ) ); }
@Test public void shouldUseBaseUriOnBadXForwardedProtoHeader() { // when ClientResponse response = client.resource( getManageUri() ) .accept( APPLICATION_JSON ) .header( X_FORWARDED_PROTO, "%%%DEFINITELY-NOT-A-PROTO!" ) .get( ClientResponse.class ); // then String entity = response.getEntity( String.class ); assertTrue( entity.contains( getServerUri() ) ); }
@Test public void shouldUseXForwardedHostAndXForwardedProtoHeadersWhenPresent() { // when ClientResponse response = client.resource( getManageUri() ) .accept( APPLICATION_JSON ) .header( X_FORWARDED_HOST, "jimwebber.org" ) .header( X_FORWARDED_PROTO, "https" ) .get( ClientResponse.class ); // then String entity = response.getEntity( String.class ); assertTrue( entity.contains( "https://jimwebber.org" ) ); assertFalse( entity.contains( getServerUri() ) ); }
@Test public void shouldUseXForwardedHostAndXForwardedProtoHeadersInCypherResponseRepresentations() { // when String jsonString = "{\"statements\" : [{ \"statement\": \"MATCH (n) RETURN n\", " + "\"resultDataContents\":[\"REST\"] }] }"; ClientResponse response = client.resource( getServerUri() + "db/data/transaction" ) .accept( APPLICATION_JSON ) .header( X_FORWARDED_HOST, "jimwebber.org:2354" ) .header( X_FORWARDED_PROTO, "https" ) .entity( jsonString, MediaType.APPLICATION_JSON_TYPE ) .post( ClientResponse.class ); // then String entity = response.getEntity( String.class ); assertTrue( entity.contains( "https://jimwebber.org:2354" ) ); assertFalse( entity.contains( getServerUri() ) ); }
ClientConfig config = new DefaultClientConfig(); Client client = Client.create(config); WebResource webResource = client.resource(UriBuilder.fromUri(restprotocol + elastiCenterAddress + restpath).build()); System.out.println("Command Sent " + command + " : " + queryParams); ClientResponse response = webResource.queryParams(queryParams).accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
public class TestClient { public static void main(String[] args) { Client client = Client.create(); WebResource web_resource = client.resource("https://graph.facebook.com/me?fields=id,name&access_token=deleted"); MyBean response=web_resource.accept(MediaType.APPLICATION_JSON).get(MyBean.class); System.out.println(response.getId()); } }