public Repository getRepository(String url) { String[] urlParts = url.split("/"); String owner = urlParts[urlParts.length - 2]; String name = urlParts[urlParts.length - 1]; String path = String.format(REPOSITORY_PATH, owner, name); WebResource resource = client.resource(GITHUB_URL).path(path); return resource.type(MediaType.APPLICATION_JSON_TYPE) .accept(MediaType.APPLICATION_JSON_TYPE) .header("Authorization", authorizationHeader) .get(Repository.class); }
@Override public int getNextRunNumber( final Project project ) { WebResource resource = Client.create().resource( coordinatorFig.getEndpoint() ); resource = addQueryParameters( resource, project, me ); Integer result = resource.path( coordinatorFig.getRunNextPath() ) .type( MediaType.APPLICATION_JSON_TYPE ) .get( Integer.class ); LOG.debug( "Got back result from next run number get = {}", result ); return result; } }
private EurekaHttpResponse<InstanceInfo> getInstanceInternal(String urlPath) { ClientResponse response = null; try { Builder requestBuilder = jerseyClient.resource(serviceUrl).path(urlPath).getRequestBuilder(); addExtraHeaders(requestBuilder); response = requestBuilder.accept(MediaType.APPLICATION_JSON_TYPE).get(ClientResponse.class); InstanceInfo infoFromPeer = null; if (response.getStatus() == Status.OK.getStatusCode() && response.hasEntity()) { infoFromPeer = response.getEntity(InstanceInfo.class); } return anEurekaHttpResponse(response.getStatus(), InstanceInfo.class) .headers(headersOf(response)) .entity(infoFromPeer) .build(); } finally { if (logger.isDebugEnabled()) { logger.debug("Jersey HTTP GET {}/{}; statusCode={}", serviceUrl, urlPath, response == null ? "N/A" : response.getStatus()); } if (response != null) { response.close(); } } }
static void testAuthGet(TestParams testParams) { String result = testParams.addQueryParameters(QUERY_PARAMS) .setEndpoint(AuthResource.ENDPOINT_URL) .newWebResource() .accept(MediaType.APPLICATION_JSON) .get(String.class); assertEquals(AuthResource.GET_MESSAGE, result); }
static void testRunnerRegistryList(TestParams testParams) { List<Runner> runnerList = testParams.addQueryParameters(QUERY_PARAMS) .setEndpoint(RunnerRegistryResource.ENDPOINT) .newWebResource() .path("/list") .type(MediaType.APPLICATION_JSON_TYPE) .accept(MediaType.APPLICATION_JSON_TYPE) .get(new GenericType<List<Runner>>() { }); assertNotNull(runnerList); assertEquals(0, runnerList.size()); }
@Override public EurekaHttpResponse<Application> getApplication(String appName) { String urlPath = "apps/" + appName; ClientResponse response = null; try { Builder requestBuilder = jerseyClient.resource(serviceUrl).path(urlPath).getRequestBuilder(); addExtraHeaders(requestBuilder); response = requestBuilder.accept(MediaType.APPLICATION_JSON_TYPE).get(ClientResponse.class); Application application = null; if (response.getStatus() == Status.OK.getStatusCode() && response.hasEntity()) { application = response.getEntity(Application.class); } return anEurekaHttpResponse(response.getStatus(), Application.class) .headers(headersOf(response)) .entity(application) .build(); } finally { if (logger.isDebugEnabled()) { logger.debug("Jersey HTTP GET {}/{}; statusCode={}", serviceUrl, urlPath, response == null ? "N/A" : response.getStatus()); } if (response != null) { response.close(); } } }
@Override public List<Runner> getRunners() { // get a list of all runners associated with this project WebResource resource = Client.create().resource( coordinatorFig.getEndpoint() ); resource = addQueryParameters( resource, project, me ); List<Runner> runners = resource.path( coordinatorFig.getRunnersListPath() ) .type( MediaType.APPLICATION_JSON ) .accept( MediaType.APPLICATION_JSON_TYPE ) .get( new GenericType<List<Runner>>() {} ); LOG.debug( "Got back runners list = {}", runners ); return runners; }
static void testAuthGetWithUnallowedRole(TestParams testParams) { testParams.addQueryParameters(QUERY_PARAMS) .setEndpoint(AuthResource.ENDPOINT_URL + AuthResource.UNALLOWED_ROLE_PATH) .newWebResource() .accept(MediaType.APPLICATION_JSON) .get(String.class); }
static void testAuthGetWithWrongCredentials(TestParams testParams) { testParams.addQueryParameters(WRONG_USER_PARAMS) .setEndpoint(AuthResource.ENDPOINT_URL) .newWebResource() .accept(MediaType.APPLICATION_JSON) .get(String.class); }
static void testGet(TestParams testParams) { String result = testParams .setEndpoint(TestGetResource.ENDPOINT_URL) .newWebResource() .accept(MediaType.TEXT_PLAIN) .get(String.class); assertEquals(TestGetResource.TEST_MESSAGE, result); }
static void testRunManagerNext( TestParams testParams ) { Integer next = testParams.addQueryParameters( QUERY_PARAMS ) .setEndpoint( RunManagerResource.ENDPOINT ) .newWebResource() .path( "/next" ) .type( MediaType.APPLICATION_JSON_TYPE ) .accept( MediaType.APPLICATION_JSON ) .get( Integer.class ); assertEquals( 1, next.intValue() ); }
@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() ) ); }
public JaxRsResponse get( String path ) { return JaxRsResponse.extractFrom( HTTP.sanityCheck( builder( path ).get( ClientResponse.class ) ) ); }
@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 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 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() ) ); }
public JaxRsResponse get( String path, final MediaType acceptType ) { Builder builder = builder( path, acceptType ); return JaxRsResponse.extractFrom( HTTP.sanityCheck( builder.get( ClientResponse.class ) ) ); }
@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() ) ); }