Commit 228f1776 authored by Adrien's avatar Adrien

apply to real endpoint

parent ec9d37f9
......@@ -47,7 +47,6 @@ dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
compile "org.springframework.boot:spring-boot-starter-web:$spring_boot_version"
compile "org.springframework.boot:spring-boot-starter-test:$spring_boot_version"
compile "org.apache.commons:commons-io:1.3.2"
testCompile 'org.jetbrains.spek:spek-api:1.1.5'
testCompile 'org.jetbrains.spek:spek-junit-platform-engine:1.1.5'
testCompile 'org.junit.platform:junit-platform-runner:1.0.0'
......
......@@ -14,7 +14,6 @@
*/
package org.bonitasoft.releng
import org.apache.commons.io.IOUtils
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.bonitasoft.exception.BuildDmgException
......@@ -23,10 +22,8 @@ import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.multipart.MultipartFile
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody
import java.io.File
import java.io.FileInputStream
import java.lang.RuntimeException
import java.nio.file.Files
import javax.servlet.http.HttpServletResponse
......@@ -38,36 +35,15 @@ class CodeSigningRestController {
var windowsCodeSign = WindowsCodeSign()
var macCodeSign = MacCodeSign()
@PostMapping("/test")
fun test(response: HttpServletResponse) {
val file : File = File("/Users/codesign/Downloads/BonitaStudioCommunity-7.8.0-SNAPSHOT-x86.exe")
response.setContentType("application/octet-stream")
response.setHeader("Content-Disposition", "attachment; filename='${file.name}'")
logger.info("returning file " + file.name +"...")
FileInputStream(file.canonicalFile).use { inputStream ->
try {
val bytesCopied: Int = IOUtils.copy(inputStream, response.outputStream)
logger.info(bytesCopied.toString() + " have been copied")
} catch (e: Exception) {
logger.error("An error occured during stream copy", e)
} catch (e: RuntimeException) {
logger.error("An error occured during stream copy", e)
}
}
}
@PostMapping("/sign")
fun signProduct(@RequestParam("exeFile") file: MultipartFile,
response: HttpServletResponse): StreamingResponseBody {
response: HttpServletResponse) {
if (file.isEmpty) {
logger.error("File '%s' is empty".format(file.originalFilename))
response.setStatus(400)
return StreamingResponseBody { outputStream ->
outputStream.write("File '%s' is empty".format(file.originalFilename).toByteArray())
outputStream.close()
}
response.outputStream.write("File '%s' is empty".format(file.originalFilename).toByteArray())
response.outputStream.close()
return
}
try {
......@@ -86,43 +62,38 @@ class CodeSigningRestController {
} catch (e: SignException) {
logger.error(e)
response.setStatus(500)
return StreamingResponseBody { outputStream ->
outputStream.write("An error occured while signing file.".toByteArray())
}
response.outputStream.write("An error occured while signing file.".toByteArray())
response.outputStream.close()
return
}
response.setContentType("application/octet-stream")
response.setHeader("Content-Disposition", "attachment; filename='${fileSigned.name}'")
return StreamingResponseBody { outputStream ->
FileInputStream(fileSigned.canonicalFile).use { inputStream ->
inputStream.copyTo(outputStream)
outputStream.flush()
outputStream.close()
}
if (!fileSigned.delete()) {
logger.error("Failed to delete temp file $fileSigned")
}
FileInputStream(fileSigned.canonicalFile).use { inputStream ->
inputStream.copyTo(response.outputStream)
response.outputStream.close()
}
if (!fileSigned.delete()) {
logger.error("Failed to delete temp file $fileSigned")
}
} catch (e: Throwable) {
logger.error("An error occured", e)
}
return StreamingResponseBody { outputStream ->
outputStream.write("An error occured server side. Check logs.".toByteArray())
}
response.outputStream.write("An error occured server side. Check logs.".toByteArray())
response.outputStream.close()
}
@PostMapping("/buildAndSignMacInstaller")
fun buildAndSignOsxInstaller(@RequestParam("exeFile") file: MultipartFile,
response: HttpServletResponse): StreamingResponseBody {
response: HttpServletResponse) {
if (file.isEmpty) {
logger.error("File '%s' is empty".format(file.originalFilename))
response.setStatus(400)
return StreamingResponseBody { outputStream ->
outputStream.write("File '%s' is empty".format(file.originalFilename).toByteArray())
outputStream.close()
}
response.outputStream.write("File '%s' is empty".format(file.originalFilename).toByteArray())
response.outputStream.close()
return
}
try {
val exeCopy: File = Files.createTempFile(file.originalFilename, "").toFile()
......@@ -135,37 +106,31 @@ class CodeSigningRestController {
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename='${fileSigned.name}'");
return StreamingResponseBody { outputStream ->
FileInputStream(fileSigned).use { inputStream ->
inputStream.copyTo(outputStream)
outputStream.flush()
outputStream.close()
}
if (!fileSigned.delete()) {
logger.error("Failed to delete temp file $fileSigned")
}
FileInputStream(fileSigned.canonicalFile).use { inputStream ->
inputStream.copyTo(response.outputStream)
response.outputStream.close()
}
if (!fileSigned.delete()) {
logger.error("Failed to delete temp file $fileSigned")
}
} catch (e: SignException) {
logger.error(e)
response.setStatus(500);
return StreamingResponseBody { outputStream ->
outputStream.write("An error occured while signing file.".toByteArray())
}
response.outputStream.write("An error occured while signing file.".toByteArray())
response.outputStream.close()
return
} catch (e: BuildDmgException) {
logger.error(e)
response.setStatus(500);
return StreamingResponseBody { outputStream ->
outputStream.write("An error occured while building dmg.".toByteArray())
}
response.outputStream.write("An error occured while building dmg.".toByteArray())
response.outputStream.close()
return
}
} catch (e: Throwable) {
logger.error("An error occured", e)
}
return StreamingResponseBody { outputStream ->
outputStream.write("An error occured server side. Check logs.".toByteArray())
}
response.outputStream.write("An error occured server side. Check logs.".toByteArray())
response.outputStream.close()
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment