This blog was written by Sai Sandeep Rayanuthala, an Agora Superstar. The Agora Superstar program empowers developers around the world to share their passion and technical expertise, and create innovative real-time communications apps and projects using Agora’s customizable SDKs.
Security for video chat applications is very important. In the Agora platform, one layer of security comes in the form of token authentication. This guide explains how to build a simple microservice using Java and Jersey to generate an Agora RTC token.
Dockerize application and deploy it on a Ubuntu VM.
Project Setup
Archetype: Archetype is a Maven project templating toolkit.
Artifact: An artifact is a file, usually a JAR file, that gets deployed to a Maven repository. A Maven build produces one or more artifacts, such as a compiled JAR file and a source JAR file.
jersey-quickstart-webappprovides a basic project template to work on. It is very useful for beginners and powerful enough.
Open your IntelliJ IDE and click start new project. We are gonna build it using Maven. Click Create from Archetype and add an Archetype with the following configuration.
Group Id: org.glassfish.jersey.archetypes
Artifact Id: jersey-quickstart-webapp
Version: 2.31
The final configuration of the project looks like this:
Once your project is built, we will add the Tomcat server. If you don’t have one installed you can get Tomcat from this link. After completing the installation of Tomcat you can click Add Configuration, which is next to the build icon.
From templates choose Local Tomcat Server. The final configuration looks like this:
Once you’re done apply the configuration and close the configuration window. You are done setting up your Jersey project and configured Tomcat server.
Hello World API
In this section, you create a Hello World API with JSON and XML input and output.
Navigate to pom.xml (which holds all the Maven dependencies), uncomment the JSON dependency, and add the JAVAX dependency. Your dependencies should look like this:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Edit src/main/webapp/index.JSP and add the following code to the file:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Now build the project. After a successful build, IntelliJ opens the browser for you. By default, the index.JSP file is displayed in the browser. The browser should show something like this:
Click jersey resource, which redirects you to a new page:
Notice the URL. By default, ServerletMapping maps everything to /webapi/*. You change that to /*.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
How did “Got it!” appear on the website? Head to the MyResource Java class. It looks something like this:
Here we added a Path annotation (@ PATH). which is used to define a URI matching pattern for incoming HTTP requests for the MyResource class.
We got “myresource” in the URL due to this Path annotation ( i.e., @ PATH(“myresource”) ). We defined @ GET(line 12) for the getIt() method, which specifies that this will use the GET HTTP method. @ Produces annotation(line 13) defines the return response format.
Now you add Agora to the project.
Go on to https://bit.ly/AgoraJAVAGithub, clone the whole repository, and copy and paste the Media and RTM folders. The file structure should look like this:
Note: Go to every file and correct the package name to avoid errors. In my case, it should be com.sandeep.agoratoken instead of io.agora. Usually, IntelliJ does this for you.
Once you are done create 3 new java classes by right-clicking the java folder named Agora, AgoraRepository, and AgoraRTMRepository, and delete the MyResource file.
Paste the following code in your AgoraRepository:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Paste the following code in your AgoraRTMRepository:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Go to AgoraRepository class and enter the command for your operating system:
* Ctrl + N for Linux and Windows users
Alternative for above commands
Your file should look like this:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Repeat this step in AgoraRTMRepository. Your file should now look like this:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
You can see something like this. Click Getter and Setter and select everything. We are almost done with Agora Token Server.
Import stuff and define the Token Server RTC POST Request:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Do some checks before creating the tokens to make sure everything is correct:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Create a timestamp and complete the generation of token:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hurray! You’re done building the Agora Java RTC token server. Now it’s time to get started with Agora RTM token server.
Add the following code to your Agora class:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
First, you need to create a WAR file. Open Maven from the right toggle bar, and click the Maven Icon to execute a command:
Now runmvn clean package
A WAR File is generated in a minute or two. You can see the location of the WAR file.
Create a Dockerfile in your repository, and push your code to GitHub.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters