Skip to content

Instantly share code, notes, and snippets.

@nicmarti
Created May 3, 2018 22:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicmarti/6dadcb35d8fa1640efe3106fa3c6a320 to your computer and use it in GitHub Desktop.
Save nicmarti/6dadcb35d8fa1640efe3106fa3c6a320 to your computer and use it in GitHub Desktop.
Devoxx CFP Web client to replay votes from a Gluon log file
import org.apache.http.client.ClientProtocolException
import org.apache.http.client.ResponseHandler
import org.apache.http.client.methods.HttpPost
import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.HttpClients
import org.apache.http.util.EntityUtils
import java.io.File
/**
* This is a basic Kotlin sample that was used to replay all votes emitted by Mobile application
* during the Devoxx France 2018 conference.
* The Authentication is fake here and this version works on localhost:9000.
* @author Nicolas Martignole
* May, 3rd, 2018
*/
// Main function
// 1) Load a Gluon log file
// 2) Extract the JSON vote object
// 3) Clean-up a little bit lines
// 4) Send it to the replayVote function
fun main(args: Array<String>) {
println("--- KVotes 1.0 ---")
println("Load a log file and replay against CFP Devoxx API the votes")
val file = File("devoxxfr-2018-all-votes.log")
var stateInLoop = false
var accumul = ArrayList<String>()
val allVotes = ArrayList<String>()
for (line in file.readLines()) {
if (line.contains("{\"details\":[")) {
stateInLoop = true
}
if (stateInLoop) {
val subLine = line.substring(line.indexOf(" "))
accumul.add(subLine)
}
if (line.contains(" ]]")) {
allVotes.add(accumul.joinToString(separator = ""))
stateInLoop = false
accumul = ArrayList()
}
}
for(vote in allVotes){
replayVote(vote.replace("]]",""))
}
println("Replayed ${allVotes.size} votes")
}
/**
* Send the specified vote JSON object to the CFP voting API
*/
fun replayVote(vote: String) {
val httpclient = HttpClients.createDefault()
httpclient.use { client ->
val httpPost = HttpPost("http://localhost:9000/api/voting/v1/vote")
httpPost.addHeader("Authorization", "Basic toto")
httpPost.addHeader("Content-Type", "application/json")
httpPost.addHeader("User-Agent", "GluonCloudLink")
val requestEntity = StringEntity(vote,"application/json", "UTF-8")
httpPost.entity = requestEntity
// Create a custom response handler
val responseHandler = ResponseHandler<String> { response ->
val status = response.statusLine.statusCode
if (status in 200..299) {
val entity = response.entity
if (entity != null) EntityUtils.toString(entity) else null
} else {
val entity = response.entity
val updatedEntity = if (entity != null) EntityUtils.toString(entity) else "??"
print("Error got "+updatedEntity)
throw ClientProtocolException("Unexpected response status: " + status)
}
}
println(".")
//val responseBody = client.execute<String>(httpPost, responseHandler)
//println(responseBody)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment