Commit 0213c9e1 authored by Adrien's avatar Adrien Committed by GitHub

Merge pull request #10 from bonitasoft/test

Do not use streamingResponseBody anymore
parents 4af2fc11 228f1776
......@@ -22,7 +22,6 @@ 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.nio.file.Files
......@@ -38,14 +37,13 @@ class CodeSigningRestController {
@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 {
......@@ -64,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()
......@@ -113,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