@Override public WebResource getResource() { WebResource webResource = null; ClientConfig config = new DefaultClientConfig(); config.getClasses().add(GsonJerseyProvider.class); Client client = Client.create(config); OAuthParameters oaParams = new OAuthParameters().signatureMethod("HMAC-SHA1").consumerKey(consumerKey) .token(accessToken).version("1.0"); OAuthSecrets oaSecrets = new OAuthSecrets().consumerSecret(consumerSecret).tokenSecret(accessTokenSecret); OAuthClientFilter oAuthFilter = new OAuthClientFilter(client.getProviders(), oaParams, oaSecrets); client.addFilter(oAuthFilter); if (this.sandbox) { webResource = client.resource(Constants.SANDBOX_BASE_API_URL); } else if (null!= this.domain){ webResource = client.resource(this.domain); } else{ webResource = client.resource(Constants.BASE_API_URL); } if (this.trace) { webResource.addFilter(new LoggingFilter()); } return webResource; }
/** * Build the Client used to make HTTP requests with the latest settings, * i.e. objectMapper and debugging. * TODO: better to use the Builder Pattern? */ public ApiClient rebuildHttpClient() { // Add the JSON serialization support to Jersey JacksonJsonProvider jsonProvider = new JacksonJsonProvider(objectMapper); DefaultClientConfig conf = new DefaultClientConfig(); conf.getSingletons().add(jsonProvider); Client client = Client.create(conf); if (debugging) { client.addFilter(new LoggingFilter()); } //to solve the issue of GET:metadata/:guid with accepted encodeing is 'gzip' //in the past, when clients use gzip header, actually it doesn't trigger a gzip encoding... So everything is fine //After the release, the content is return in gzip, while the sdk doesn't handle it correctly client.addFilter(new GZIPContentEncodingFilter(false)); this.httpClient = client; return this; }
/** * Fetches and configures a Web Resource to connect to eWAY * * @return A WebResource */ private WebResource getEwayWebResource() { ClientConfig clientConfig = new DefaultClientConfig(); clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE); Client client = Client.create(clientConfig); client.addFilter(new HTTPBasicAuthFilter(APIKey, password)); if (this.debug) { client.addFilter(new LoggingFilter(System.out)); } // Set additional headers RapidClientFilter rapidFilter = new RapidClientFilter(); rapidFilter.setVersion(apiVersion); client.addFilter(rapidFilter); WebResource resource = client.resource(webUrl); return resource; }
/** * Returns jersey client to make any call to webservice * @return */ private Client getClient(boolean is_multipart) { if(m_client == null) { if(is_multipart) { ClientConfig config = new DefaultClientConfig(); config.getClasses().add(MultiPartWriter.class); m_client = Client.create(config); } else { m_client = new Client(); } if(!isTokenbasedAuth) { final HTTPBasicAuthFilter authFilter = new HTTPBasicAuthFilter(x_username, x_password); m_client.addFilter(authFilter); m_client.addFilter(new LoggingFilter()); } else { //TODO for token based authentication } } return m_client; }
/** * Logs in into the NEst service and captures session information * * @throws ConnectionException if the credentials are incorrect * @throws IOException if the response date cannot be parsed */ @Start public void login() throws ConnectionException, IOException { httpClient.addFilter(new LoggingFilter()); MultivaluedMap<String, String> map = new MultivaluedMapImpl(); map.putSingle("username", getUsername()); map.putSingle("password", getPassword()); WebResource webResource = httpClient.resource("https://home.nest.com/user/login"); ClientResponse response = webResource.type("application/x-www-form-urlencoded") .post(ClientResponse.class, map); if (response.getStatus()> 399) { throw new ConnectionException(ConnectionExceptionCode.INCORRECT_CREDENTIALS, "unauthorized user", response.toString()); } String res = response.getEntity(String.class); System.out.println("Login: " + res); login = mapper.readValue(res, Login.class); baseUri = login.getUrls().getTransportUrl(); getMetaData(); }
public RestClient (URI baseURI) { ObjectMapper mapper = Jackson.newObjectMapper () .registerModule (new SupernodeModule ()); Validator validator = Validation.buildDefaultValidatorFactory ().getValidator (); ClientConfig cc = new DefaultClientConfig (JsonProcessingExceptionMapper.class); cc.getProperties ().put (ClientConfig.PROPERTY_CONNECT_TIMEOUT, 5000); cc.getProperties ().put (ClientConfig.PROPERTY_READ_TIMEOUT, 5000); cc.getSingletons ().add (new JacksonMessageBodyProvider (mapper, validator)); Client client = Client.create (cc); client.addFilter (new LoggingFilter ()); this.client = client; this.baseURI = baseURI; }
@Test public void testGetNewApplication() throws Exception { client().addFilter(new LoggingFilter(System.out)); rm.start(); String mediaTypes[] = { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }; for (String acceptMedia : mediaTypes) { testGetNewApplication(acceptMedia); } rm.stop(); }
@Test public void testGetAppQueue() throws Exception { client().addFilter(new LoggingFilter(System.out)); boolean isCapacityScheduler = rm.getResourceScheduler() instanceof CapacityScheduler; rm.start(); MockNM amNodeManager = rm.registerNode("127.0.0.1:1234", 2048); String[] contentTypes = { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }; for (String contentType : contentTypes) { RMApp app = rm.submitApp(CONTAINER_MB, "", webserviceUserName); amNodeManager.nodeHeartbeat(true); ClientResponse response = this .constructWebResource("apps", app.getApplicationId().toString(), "queue").accept(contentType).get(ClientResponse.class); assertEquals(Status.OK, response.getClientResponseStatus()); String expectedQueue = "default"; if(!isCapacityScheduler) { expectedQueue = "root." + webserviceUserName; } if (contentType.equals(MediaType.APPLICATION_JSON)) { verifyAppQueueJson(response, expectedQueue); } else { verifyAppQueueXML(response, expectedQueue); } } rm.stop(); }
@Test public void testCreateDelegationToken() throws Exception { rm.start(); this.client().addFilter(new LoggingFilter(System.out)); final String renewer = "test-renewer"; String jsonBody = "{ \"renewer\" : \"" + renewer + "\" }"; String xmlBody = "<delegation-token><renewer>" + renewer + "</renewer></delegation-token>"; String[] mediaTypes = { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }; Map<String, String> bodyMap = new HashMap<String, String>(); bodyMap.put(MediaType.APPLICATION_JSON, jsonBody); bodyMap.put(MediaType.APPLICATION_XML, xmlBody); for (final String mediaType : mediaTypes) { final String body = bodyMap.get(mediaType); for (final String contentType : mediaTypes) { if (isKerberosAuth == true) { verifyKerberosAuthCreate(mediaType, contentType, body, renewer); } else { verifySimpleAuthCreate(mediaType, contentType, body); } } } rm.stop(); return; }
public void testNodeHelper(String path, String media) throws JSONException, Exception { WebResource r = resource(); Application app = new MockApp(1); nmContext.getApplications().put(app.getAppId(), app); addAppContainers(app); Application app2 = new MockApp(2); nmContext.getApplications().put(app2.getAppId(), app2); addAppContainers(app2); client().addFilter(new LoggingFilter()); ClientResponse response = r.path("ws").path("v1").path("node").path(path) .accept(media).get(ClientResponse.class); assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); JSONObject json = response.getEntity(JSONObject.class); JSONObject info = json.getJSONObject("containers"); assertEquals("incorrect number of elements", 1, info.length()); JSONArray conInfo = info.getJSONArray("container"); assertEquals("incorrect number of elements", 4, conInfo.length()); for (int i = 0; i < conInfo.length(); i++) { verifyNodeContainerInfo( conInfo.getJSONObject(i), nmContext.getContainers().get( ConverterUtils.toContainerId(conInfo.getJSONObject(i).getString( "id")))); } }
@Test public void testNodeSingleContainerXML() throws JSONException, Exception { WebResource r = resource(); Application app = new MockApp(1); nmContext.getApplications().put(app.getAppId(), app); HashMap<String, String> hash = addAppContainers(app); Application app2 = new MockApp(2); nmContext.getApplications().put(app2.getAppId(), app2); addAppContainers(app2); client().addFilter(new LoggingFilter(System.out)); for (String id : hash.keySet()) { ClientResponse response = r.path("ws").path("v1").path("node") .path("containers").path(id).accept(MediaType.APPLICATION_XML) .get(ClientResponse.class); assertEquals(MediaType.APPLICATION_XML_TYPE, response.getType()); String xml = response.getEntity(String.class); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(); is.setCharacterStream(new StringReader(xml)); Document dom = db.parse(is); NodeList nodes = dom.getElementsByTagName("container"); assertEquals("incorrect number of elements", 1, nodes.getLength()); verifyContainersInfoXML(nodes, nmContext.getContainers().get(ConverterUtils.toContainerId(id))); } }
/** * Build the Client used to make HTTP requests with the latest settings, * i.e. objectMapper and debugging. */ public ApiClient rebuildHttpClient() { // Add the JSON serialization support to Jersey JacksonJsonProvider jsonProvider = new JacksonJsonProvider(objectMapper); DefaultClientConfig conf = new DefaultClientConfig(); conf.getSingletons().add(jsonProvider); Client client = Client.create(conf); if (debugging) { client.addFilter(new LoggingFilter()); } this.httpClient = client; return this; }
/** * Get an existing client or create a new client to handle HTTP request. */ private Client getClient() { if(!hostMap.containsKey(basePath)) { Client client = Client.create(); if (debugging) client.addFilter(new LoggingFilter()); hostMap.put(basePath, client); } return hostMap.get(basePath); }
public static void main(String[] args) throws Exception { TwitterAdsClient.setTrace(true); TwitterAdsClient.setSandbox(false); String domain = "https://ads-api.twitter.com"; TwitterAdsClient.setDomain(domain); WebResource webResource = Resource.getResource(); webResource.addFilter(new LoggingFilter()); String resourcePath = "/0/accounts/" + ACCOUNT_ID + "/promoted_tweets"; MultivaluedMap<String, String> params = new MultivaluedMapImpl(); params.add("with_deleted", "true"); List<PromotedTweet> promotedTweets = webResource .queryParams(params) .path(resourcePath) .accept(MediaType.APPLICATION_JSON_TYPE) .get(new GenericType<List<PromotedTweet>>() {}); for (PromotedTweet promotedTweet : promotedTweets) { //System.out.println(promotedTweet.toString()); Long tweetIdL = Long.parseLong(promotedTweet.getTweet_id()); System.out.println(tweetIdL); Tweet.destroy(tweetIdL); } }
/** * Get an existing client or create a new client to handle HTTP request. */ private Client getClient(String baseUrl) { if (!hostMap.containsKey(baseUrl)) { Client client = Client.create(); if (debugging) client.addFilter(new LoggingFilter()); hostMap.put(baseUrl, client); } return hostMap.get(baseUrl); }
/** * Get an existing client or create a new client to handle HTTP request. */ public Client getClient() { if(!hostMap.containsKey(basePath)) { Client client = Client.create(); client.setConnectTimeout(10000); client.setReadTimeout(20000); if (debugging) client.addFilter(new LoggingFilter()); hostMap.put(basePath, client); } return hostMap.get(basePath); }
private Client getClient(String host, boolean tlsEnabled, boolean hostnameVerificationEnabled) throws NoSuchAlgorithmException { if (!hostMap.containsKey(host)) { ClientConfig clientConfig = getClientConfig(tlsEnabled, hostnameVerificationEnabled); Client client = Client.create(clientConfig); if (isDebug) { client.addFilter(new LoggingFilter()); } hostMap.put(host, client); } return hostMap.get(host); }
private Client getClient(String host, String username, String password, boolean tlsEnabled, boolean hostnameVerificationEnabled) throws NoSuchAlgorithmException { if (!hostMap.containsKey(host)) { ClientConfig clientConfig = getClientConfig(tlsEnabled, hostnameVerificationEnabled); Client client = Client.create(clientConfig); client.addFilter(new HTTPBasicAuthFilter(username, password)); if (isDebug) { client.addFilter(new LoggingFilter()); } hostMap.put(host, client); } return hostMap.get(host); }
private Client getClient(String host) { if (!hostMap.containsKey(host)) { Client client = Client.create(); if (isDebug){ client.addFilter(new LoggingFilter()); } hostMap.put(host, client); } return hostMap.get(host); }
private Client getClient(String host) { if(!hostMap.containsKey(host)) { Client client = Client.create(); client.addFilter(new LoggingFilter()); hostMap.put(host, client); } return hostMap.get(host); }