Advertisement
Guest User

Salesforce pushtopic

a guest
Nov 27th, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.64 KB | None | 0 0
  1.  
  2.     public static void main(String[] args) throws Exception {
  3.         if (args.length < 2) {
  4.             System.err.println("Usage: MakeTopic topicname query");
  5.             System.exit(-1);
  6.         }
  7.  
  8.         String topicname = args[0];
  9.         String query = args[1];
  10.  
  11. //        setNoValidation();
  12.  
  13.         JSONObject authResponse = oauthLogin();
  14.         System.out.println("Login response: " + authResponse.toString(2));
  15.         if (!authResponse.has("access_token")) {
  16.             throw new Exception("OAuth failed: " + authResponse.toString());
  17.         }
  18.  
  19.         String url = authResponse.getString("instance_url")
  20.                 + "/services/data/v23.0/sobjects/PushTopic/";
  21.  
  22.         JSONObject topic = new JSONObject();
  23.  
  24.         topic.put("ApiVersion", API_VERSION);
  25.         topic.put("Name", topicname);
  26.         topic.put("Query", query);
  27.  
  28.         System.out.print("PushTopic data: ");
  29.         System.out.println(topic.toString(2));
  30.  
  31.         HttpClient httpClient = new HttpClient(new HttpClientTransportOverHTTP(), new SslContextFactory(true));
  32.         httpClient.start();
  33.         System.out.println("Push URL : " + url);
  34.         httpClient.POST(url).header(HttpHeader.CONTENT_TYPE, "application/json")
  35.                 .header(HttpHeader.AUTHORIZATION, "OAuth " + authResponse.getString("access_token"))
  36.                 .content(new StringContentProvider(topic.toString())).send(new Response.CompleteListener() {
  37.             @Override
  38.             public void onComplete(Result result) {
  39.                 System.out.println("Result status : " + result.isSucceeded());
  40.                 System.out.printf("Response : " + result.getResponse());
  41. //                System.out.println(result.getResponse().getReason());
  42. //                result.getFailure().printStackTrace();
  43.             }
  44.         });
  45.  
  46.         while (true) {
  47.             TimeUnit.MINUTES.sleep(5);
  48.         }
  49.     }
  50.  
  51.     private static JSONObject oauthLogin() throws Exception {
  52.         String url = LOGIN_SERVER + "/services/oauth2/token";
  53.         String message = "grant_type=password&client_id=" + CLIENT_ID
  54.                 + "&client_secret=" + CLIENT_SECRET + "&username=" + USERNAME
  55.                 + "&password=" + PASSWORD;
  56.  
  57.         HttpClient httpClient = new HttpClient(new HttpClientTransportOverHTTP(), new SslContextFactory(true));
  58.         httpClient.start();
  59.  
  60.         ContentResponse contentResponse = httpClient.POST(url).header(HttpHeader.CONTENT_TYPE, "application/x-www-form-urlencoded")
  61.                 .content(new StringContentProvider(message)).send();
  62.  
  63.         return new JSONObject(new JSONTokener(contentResponse.getContentAsString()));
  64.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement