Skip to content
This repository was archived by the owner on Jan 23, 2025. It is now read-only.

Plat1350 add kafka to dev maven #570

Open
wants to merge 3 commits into
base: dev-maven
Choose a base branch
from
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
6 changes: 6 additions & 0 deletions app/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1251,6 +1251,12 @@
<version>1.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.topcoder.direct.services.kafka;

public class CreateChallengeMessage {
public String legacyId;
public String topic;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.topcoder.direct.services.kafka;

import java.util.Hashtable;
import com.google.gson.*;
import java.net.*;
import java.io.*;

public class DirectKafkaProducer {

private static DirectKafkaProducer myProducer = null;
public static synchronized DirectKafkaProducer getInstance() {
if (myProducer == null) { myProducer = new DirectKafkaProducer(); }
return myProducer;
}

public final static int CHALLENGE_CREATE = 0;

private Hashtable<Integer, String> topics = null;

protected DirectKafkaProducer() {
topics = new Hashtable<Integer, String>();
topics.put(CHALLENGE_CREATE, "challenge.notification.create");
}

public void postChallengeCreate(long challengeId)
throws IOException/*, MalformedUrlException*/ {
CreateChallengeMessage ccm = new CreateChallengeMessage();
ccm.legacyId = ""+challengeId;
ccm.topic = topics.get(CHALLENGE_CREATE);
Gson gson = new Gson();
String requestBody = gson.toJson(ccm);
postEvent(requestBody);
}

private String getUrl() {
// get bus api Url from config
return System.getenv("bus.api.url");
}

private void doPost(String url, String json)
throws IOException {
// make HTTPS POST call to api
OutputStream os = null;
InputStream is = null;
HttpURLConnection con = null;
try {
URL _url = new URL(url);
con = (HttpURLConnection) _url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Accept", "application/json");
os = con.getOutputStream();
byte[] input = json.getBytes("utf-8");
os.write(input, 0, input.length);
os.flush();
// read just to be sure it sent
is = con.getInputStream();
} finally {
if (os != null) { try { os.close(); } catch (Throwable t) {} }
if (is != null) { try { is.close(); } catch (Throwable t) {} }
}
}

private void postEvent(final String json)
throws IOException {
new Thread(new Runnable() {
public void run() {
final String url = getUrl();
try {
doPost(url, json);
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
}
}).start();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
package com.topcoder.direct.services.view.action.contest.launch;

import com.topcoder.direct.services.kafka.DirectKafkaProducer;
import com.topcoder.catalog.entity.Category;
import com.topcoder.catalog.entity.CompDocumentation;
import com.topcoder.catalog.entity.CompUploadedFile;
Expand Down Expand Up @@ -882,6 +883,12 @@ protected void executeAction() throws Exception {
// to make the action robust.
addFieldError("competitionType", "The competition type is unknown");
}
try {
DirectKafkaProducer.getInstance().postChallengeCreate(softwareCompetition.getId());
} catch (Exception e) {
e.printStackTrace();
// log failure to submit to API
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import javax.crypto.spec.SecretKeySpec;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
//import javax.rmi.PortableRemoteObject;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
Expand Down
7 changes: 6 additions & 1 deletion token.properties.docker
Original file line number Diff line number Diff line change
Expand Up @@ -349,4 +349,9 @@ defaultGroupIdForTrial =

# The Topcoder Connect Url to which the projects creation will redirect
TopcoderConnectUrl=https://connect.topcoder-dev.com
TopcoderNewAuthUrl=https://accounts-auth0.topcoder-dev.com
TopcoderNewAuthUrl=https://accounts-auth0.topcoder-dev.com

#####################################
# Bus API #
#####################################
bus.api.url=https://api.topcoder-dev.com/v5/bus/events