|
| 1 | +--- |
| 2 | +title: include file |
| 3 | +description: include file |
| 4 | +services: azure-communication-services |
| 5 | +author: rahulva |
| 6 | +manager: shahen |
| 7 | +ms.service: azure-communication-services |
| 8 | +ms.subservice: azure-communication-services |
| 9 | +ms.date: 09/20/2021 |
| 10 | +ms.topic: include |
| 11 | +ms.custom: include file |
| 12 | +ms.author: rahulva |
| 13 | +--- |
| 14 | + |
| 15 | +> [!NOTE] |
| 16 | +> Find the finalized code for this quickstart on [GitHub](https://github.com/Azure-Samples/communication-services-java-quickstarts/tree/main/relay-token-quickstart) |
| 17 | +
|
| 18 | +## Prerequisites for Java |
| 19 | + |
| 20 | +- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F). |
| 21 | +- [Java Development Kit (JDK)](/azure/developer/java/fundamentals/java-jdk-install) version 8 or above. |
| 22 | +- [Apache Maven](https://maven.apache.org/download.cgi). |
| 23 | +- A deployed Communication Services resource and connection string. [Create a Communication Services resource](../create-communication-resource.md). |
| 24 | + |
| 25 | +## Setting Up |
| 26 | + |
| 27 | +### Create a new Java application |
| 28 | + |
| 29 | +Open your terminal or command window. Navigate to the directory where you'd like to create your Java application. Run the command below to generate the Java project from the maven-archetype-quickstart template. |
| 30 | + |
| 31 | +```console |
| 32 | +mvn archetype:generate -DgroupId=com.communication.quickstart -DartifactId=communication-quickstart -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false |
| 33 | +``` |
| 34 | + |
| 35 | +You'll notice that the 'generate' task created a directory with the same name as the `artifactId`. Under this directory, the src/main/java directory contains the project source code, the `src/test/java directory` contains the test source, and the `pom.xml` file is the project's Project Object Model, or POM. |
| 36 | + |
| 37 | +### Install the package |
| 38 | + |
| 39 | +Open the **pom.xml** file in your text editor. Add the following dependency element to the group of dependencies. |
| 40 | + |
| 41 | +```xml |
| 42 | +<dependency> |
| 43 | + <groupId>com.azure</groupId> |
| 44 | + <artifactId>azure-communication-identity</artifactId> |
| 45 | + <version>1.0.0</version> |
| 46 | +</dependency> |
| 47 | +``` |
| 48 | + |
| 49 | +### Set up the app framework |
| 50 | + |
| 51 | +From the project directory: |
| 52 | + |
| 53 | +1. Navigate to the `/src/main/java/com/communication/quickstart` directory |
| 54 | +2. Open the `App.java` file in your editor |
| 55 | +3. Replace the `System.out.println("Hello world!");` statement |
| 56 | +4. Add `import` directives |
| 57 | + |
| 58 | +Use the following code to begin: |
| 59 | + |
| 60 | +```java |
| 61 | +package com.communication.quickstart; |
| 62 | + |
| 63 | +import com.azure.communication.common.*; |
| 64 | +import com.azure.communication.identity.*; |
| 65 | +import com.azure.communication.identity.models.*; |
| 66 | +import com.azure.core.credential.*; |
| 67 | +import com.communication.network.traversal.*; |
| 68 | + |
| 69 | +import java.io.IOException; |
| 70 | +import java.time.*; |
| 71 | +import java.util.*; |
| 72 | + |
| 73 | +public class App |
| 74 | +{ |
| 75 | + public static void main( String[] args ) throws IOException |
| 76 | + { |
| 77 | + System.out.println("Azure Communication Services - Network Credentials Quickstart"); |
| 78 | + // Quickstart code goes here |
| 79 | + } |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +## Authenticate the client |
| 84 | + |
| 85 | +Instantiate a `CommunicationIdentityClient` with your resource's access key and endpoint. Learn how to [manage your resource's connection string](../create-communication-resource.md#store-your-connection-string). In addition, you can initialize the client with any custom HTTP client the implements the `com.azure.core.http.HttpClient` interface. |
| 86 | + |
| 87 | +Add the following code to the `main` method: |
| 88 | + |
| 89 | +```java |
| 90 | +// Your can find your endpoint and access key from your resource in the Azure portal |
| 91 | +String endpoint = "https://<RESOURCE_NAME>.communication.azure.com"; |
| 92 | +String accessKey = "SECRET"; |
| 93 | + |
| 94 | +CommunicationRelayClient communicationRelayClient = new CommunicationRelayClientBuilder() |
| 95 | + .endpoint(endpoint) |
| 96 | + .credential(new DefaultAzureCredentialBuilder().build()) |
| 97 | + .buildClient(); |
| 98 | + |
| 99 | +``` |
| 100 | + |
| 101 | +You can also provide the entire connection string using the `connectionString()` function instead of providing the endpoint and access key. |
| 102 | +```java |
| 103 | +// Your can find your connection string from your resource in the Azure portal |
| 104 | +String connectionString = "<connection_string>"; |
| 105 | + |
| 106 | +CommunicationRelayClient communicationRelayClient = new CommunicationRelayClientBuilder() |
| 107 | + .connectionString(connectionString) |
| 108 | + .buildClient(); |
| 109 | +``` |
| 110 | + |
| 111 | +## Create an identity |
| 112 | + |
| 113 | +Azure Communication Services maintains a lightweight identity directory. Use the `createUser` method to create a new entry in the directory with a unique `Id`. Store received identity with mapping to your application's users. For example, by storing them in your application server's database. The identity is required later to issue access tokens. |
| 114 | + |
| 115 | +```java |
| 116 | +CommunicationUserIdentifier user = communicationIdentityClient.createUser(); |
| 117 | +System.out.println("\nCreated an identity with ID: " + user.getId()); |
| 118 | +``` |
| 119 | + |
| 120 | +## Getting the relay configuration |
| 121 | +Call the Azure Communication token service to exchange the user access token for a TURN service token |
| 122 | + |
| 123 | +```java |
| 124 | +CommunicationIdentityClient communicationIdentityClient = new CommunicationIdentityClientBuilder() |
| 125 | + .connectionString(connectionString) |
| 126 | + .buildClient(); |
| 127 | + |
| 128 | +CommunicationUserIdentifier user = communicationIdentityClient.createUser(); |
| 129 | +System.out.println("User id: " + user.getId()); |
| 130 | + |
| 131 | +CommunicationRelayConfiguration config = communicationRelayClient.getRelayConfiguration(user); |
| 132 | + |
| 133 | + System.out.println("Expires on:" + config.getExpiresOn()); |
| 134 | + List<CommunicationIceServer> iceServers = config.getIceServers(); |
| 135 | + |
| 136 | + for (CommunicationIceServer iceS : iceServers) { |
| 137 | + System.out.println("URLS: " + iceS.getUrls()); |
| 138 | + System.out.println("Username: " + iceS.getUsername()); |
| 139 | + System.out.println("Credential: " + iceS.getCredential()); |
| 140 | + } |
| 141 | +``` |
| 142 | + |
| 143 | +## Run the code |
| 144 | + |
| 145 | +Navigate to the directory containing the *pom.xml* file and compile the project by using the following `mvn` command. |
| 146 | + |
| 147 | +```console |
| 148 | +mvn compile |
| 149 | +``` |
| 150 | + |
| 151 | +Then, build the package. |
| 152 | + |
| 153 | +```console |
| 154 | +mvn package |
| 155 | +``` |
| 156 | + |
| 157 | +Run the following `mvn` command to execute the app. |
| 158 | + |
| 159 | +Navigate to the directory containing the *pom.xml* file and compile the project by using the following `mvn` command. |
| 160 | + |
| 161 | +```console |
| 162 | +mvn compile |
| 163 | +``` |
| 164 | + |
| 165 | +Then, build the package. |
| 166 | + |
| 167 | +```console |
| 168 | +mvn package |
| 169 | +``` |
| 170 | + |
| 171 | +Run the following `mvn` command to execute the app. |
| 172 | + |
| 173 | +```console |
| 174 | +mvn exec:java -Dexec.mainClass="com.communication.quickstart.App" -Dexec.cleanupDaemonThreads=false |
| 175 | +``` |
0 commit comments