Skip to content
This repository was archived by the owner on Jul 21, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ The `LoginExample` class is the default class that EMP Connector executes. This
### `DevLoginExample`
The `DevLoginExample` class accepts a custom login URL, such as a sandbox instance (https://test.salesforce.com). Also, `DevLoginExample` logs to the console the Bayeux connection messages received on the `/meta` channels, such as `/meta/handshake` and `/meta/connect`.

### `OMSRedirectExample`
The `OMSRedirectExample` class accepts a custom login URL, such as a sandbox instance (https://test.salesforce.com). Also, `OMSRedirectExample` logs to Azure Log Analytics.

### `BearerTokenExample`
The `BearerTokenExample` class uses the OAuth bearer token authentication and accepts an access token.

Expand All @@ -69,6 +72,10 @@ To run EMP Connector using the `DevLoginExample` class with username and passwor

`$ java -classpath target/emp-connector-0.0.1-SNAPSHOT-phat.jar com.salesforce.emp.connector.example.DevLoginExample <login_URL> <username> <password> <channel> [optional_replay_id]`

To run EMP Connector using the `OMSRedirectExample` class with username and password authentication, use this command.

`$ java -classpath target/emp-connector-0.0.1-SNAPSHOT-phat.jar com.salesforce.emp.connector.example.DevLoginExample <login_URL> <username> <password> <channel> <oms_workspace_id> <oms_workspace_key> <oms_workspace_logtype> [optional_replay_id]`

To run EMP Connector using an OAuth access token, use this command.

`$ java -classpath target/emp-connector-0.0.1-SNAPSHOT-phat.jar com.salesforce.emp.connector.example.BearerTokenExample <instance_URL> <token> <channel> [optional_replay_id]`
Expand Down
17 changes: 16 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,22 @@
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.6.4</version>
</dependency>
</dependencies>

<build>
<plugins>
Expand Down
70 changes: 70 additions & 0 deletions src/main/java/com/salesforce/emp/connector/example/OMSPost.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.salesforce.emp.connector.example;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import java.io.DataOutputStream;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import javax.xml.bind.DatatypeConverter;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.time.ZonedDateTime;
import java.time.LocalDateTime;
import java.time.ZoneOffset;

public class OMSPost {

public static void main(String args[]) {

String customer_id = args[0];
String shared_key = args[1];
String log_type = args[2];
String json = args[3];

String Signature = "";
String encodedHash = "";
String url = "";

ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC);
DateTimeFormatter f = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss O");
String nowStr = f.format(now);

// String for signing the key
String stringToSign = "POST\n" + json.length() + "\napplication/json\nx-ms-date:" + nowStr + "\n/api/logs";

try {
byte[] decodedBytes = Base64.decodeBase64(shared_key);

Mac hasher = Mac.getInstance("HmacSHA256");
hasher.init(new SecretKeySpec(decodedBytes, "HmacSHA256"));
byte[] hash = hasher.doFinal(stringToSign.getBytes());

encodedHash = DatatypeConverter.printBase64Binary(hash);
Signature = "SharedKey " + customer_id + ":" + encodedHash;

url = "https://" + customer_id + ".ods.opinsights.azure.com/api/logs?api-version=2016-04-01";
URL objUrl = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) objUrl.openConnection();
con.setDoOutput(true);
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Log-Type", log_type);
con.setRequestProperty("x-ms-date", nowStr);
con.setRequestProperty("Authorization", Signature);

DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(json);
wr.flush();
wr.close();

int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("\nPosted log message : " + json);
System.out.println("\nResponse Code : " + responseCode);
}
catch (Exception e) {
System.out.println("Catch statement: " + e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright (c) 2016, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.TXT file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
package com.salesforce.emp.connector.example;

import com.salesforce.emp.connector.BayeuxParameters;
import com.salesforce.emp.connector.EmpConnector;
import com.salesforce.emp.connector.LoginHelper;
import com.salesforce.emp.connector.TopicSubscription;

import java.net.URL;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.Consumer;
import org.eclipse.jetty.util.ajax.JSON;

import static org.cometd.bayeux.Channel.*;

/**
* An example of using the EMP connector
*
* @author hal.hildebrand
* @since API v37.0
*/
public class OMSRedirectExample {

public static void main(String[] argv) throws Throwable {
if (argv.length < 7 || argv.length > 8) {
System.err.println("Usage: OMSRedirectExample url username password topic [replayFrom]");
System.exit(1);
}

Consumer<Map<String, Object>> consumer = event -> {
String log_message = "[" + JSON.toString(event) + "]";
String[] inputArguments = {argv[4], argv[5], argv[6], log_message};
OMSPost.main(inputArguments);
};

BearerTokenProvider tokenProvider = new BearerTokenProvider(() -> {
try {
return LoginHelper.login(new URL(argv[0]), argv[1], argv[2]);
} catch (Exception e) {
throw new RuntimeException(e);
}
});

BayeuxParameters params = tokenProvider.login();

EmpConnector connector = new EmpConnector(params);

LoggingListener loggingListener = new LoggingListener(true, true);

connector.addListener(META_HANDSHAKE, loggingListener)
.addListener(META_CONNECT, loggingListener)
.addListener(META_DISCONNECT, loggingListener)
.addListener(META_SUBSCRIBE, loggingListener)
.addListener(META_UNSUBSCRIBE, loggingListener);

connector.setBearerTokenProvider(tokenProvider);

connector.start().get(5, TimeUnit.SECONDS);

long replayFrom = EmpConnector.REPLAY_FROM_EARLIEST;
if (argv.length == 8) {
replayFrom = Long.parseLong(argv[7]);
}
TopicSubscription subscription;
try {
subscription = connector.subscribe(argv[3], replayFrom, consumer).get(5, TimeUnit.SECONDS);
} catch (ExecutionException e) {
System.err.println(e.getCause().toString());
System.exit(1);
throw e.getCause();
} catch (TimeoutException e) {
System.err.println("Timed out subscribing");
System.exit(1);
throw e.getCause();
}

System.out.println(String.format("Subscribed: %s", subscription));
}
}