Commit 35dbc5e8 authored by Adrien's avatar Adrien

* fix kotlin issues

* remove java class
parent 7d81b843
......@@ -17,11 +17,6 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
......
......@@ -29,6 +29,10 @@
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
</dependency>
</dependencies>
<build>
......
package org.bonitasoft.releng
import org.springframework.stereotype.Controller
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import kotlin.jvm.javaClass
import org.springframework.boot.SpringApplication
import org.springframework.web.multipart.MultipartFile
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestParam
import javax.servlet.http.HttpServletResponse
import java.io.IOException
import java.nio.file.Path
import java.io.File
import java.nio.file.Files
import java.io.FileInputStream
import org.springframework.boot.autoconfigure.SpringBootApplication
@Controller
@EnableAutoConfiguration
class CodeSignApp {
val logger:Log = LogFactory.getLog(javaClass)
@PostMapping("/sign")
fun singleFileUpload(@RequestParam("exeFile") file: MultipartFile, response: HttpServletResponse): StreamingResponseBody {
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()) }
}
var directory: Path? = null
var exeCopy: Path? = null
try {
val bytes = file.bytes
directory = Files.createTempDirectory("codeSign")
exeCopy = directory.resolve(file.originalFilename)
logger.info("Copy of input file into %s...".format(directory.toString()))
Files.write(exeCopy,bytes)
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition","attachment; filename=\"%s\"".format(file.originalFilename));
val processBuilder = ProcessBuilder("signtool", "sign", "/tr", "http://timestamp.digicert.com",
"/td", "sha256", "/fd","sha256", "/a", exeCopy.toFile().canonicalFile.absolutePath)
logger.info("Signing file...")
val signResult = processBuilder.inheritIO().start().waitFor()
if (signResult != 0) {
logger.error("An error occured while signing file.")
response.setStatus(500);
return StreamingResponseBody { outputStream -> outputStream.write("An error occured while signing file.".toByteArray()) }
}
val inputStream = FileInputStream(exeCopy.toFile())
return StreamingResponseBody { outputStream ->
inputStream.copyTo(outputStream)
inputStream.close()
}
} catch (e: IOException ) {
logger.error("An error occured", e);
} finally {
exeCopy?.toFile()?.delete();
directory?.toFile()?.delete();
}
return StreamingResponseBody { outputStream -> outputStream.write("An error occured server side. Check logs.".toByteArray()) }
}
}
@SpringBootApplication
open class CodeSignApp
fun main(args : Array<String>) {
SpringApplication.run(CodeSignApp::class.java, *args)
......
package org.bonitasoft.releng;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
@Controller
@EnableAutoConfiguration
public class CodeSignApplication {
private static final Log logger = LogFactory.getLog(CodeSignApplication.class);
public static void main(String[] args) {
SpringApplication.run(CodeSignApplication.class, args);
}
@PostMapping("/sign")
public StreamingResponseBody singleFileUpload(@RequestParam("exeFile") MultipartFile file,
HttpServletResponse response) {
if (file.isEmpty()) {
logger.error(String.format("File '%s' is empty", file.getOriginalFilename()));
response.setStatus(400);
return outputStream -> {
outputStream.write(String.format("File '%s' is empty", file.getOriginalFilename()).getBytes());
};
}
Path directory = null;
Path exeCopy = null;
try {
// Get the file and save it somewhere
byte[] bytes = file.getBytes();
directory = Files.createTempDirectory("codeSign");
exeCopy = directory.resolve(file.getOriginalFilename());
logger.info(String.format("Copy of input file into %s...", directory.toString()));
Files.write(exeCopy, bytes);
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition",
String.format("attachment; filename=\"%s\"", file.getOriginalFilename()));
ProcessBuilder processBuilder = new ProcessBuilder("signtool", "sign", "/tr", "http://timestamp.digicert.com",
"/td", "sha256", "/fd",
"sha256", "/a", exeCopy.toFile().getCanonicalFile().getAbsolutePath());
logger.info("Signing file...");
logger.debug(String.format("Sign command: %s", processBuilder.command()));
int signResult = processBuilder.inheritIO().start().waitFor();
if (signResult != 0) {
logger.error("An error occured while signing file.");
response.setStatus(500);
return outputStream -> {
outputStream.write("An error occured while signing file.".getBytes());
};
}
InputStream inputStream = new FileInputStream(exeCopy.toFile());
return outputStream -> {
int nRead;
byte[] data = new byte[1024];
while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
outputStream.write(data, 0, nRead);
}
inputStream.close();
};
} catch (IOException | InterruptedException e) {
logger.error("An error occured", e);
} finally {
if (exeCopy != null) {
exeCopy.toFile().delete();
}
if (directory != null) {
directory.toFile().delete();
}
}
response.setStatus(500);
return outputStream -> {
outputStream.write("An error occured server side. Check logs.".getBytes());
};
}
}
package org.bonitasoft.releng
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
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.FileInputStream
import java.io.IOException
import java.nio.file.Files
import java.nio.file.Path
import javax.servlet.http.HttpServletResponse
@RestController
class CodeSigneRestController {
val logger:Log = LogFactory.getLog(javaClass)
@PostMapping("/sign")
fun singleFileUpload(@RequestParam("exeFile") file: MultipartFile, response: HttpServletResponse): StreamingResponseBody {
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()) }
}
var directory: Path? = null
var exeCopy: Path? = null
try {
val bytes = file.bytes
directory = Files.createTempDirectory("codeSign")
exeCopy = directory.resolve(file.originalFilename)
logger.info("Copy of input file into %s...".format(directory.toString()))
Files.write(exeCopy,bytes)
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition","attachment; filename=\"%s\"".format(file.originalFilename));
val processBuilder = ProcessBuilder("signtool", "sign", "/tr", "http://timestamp.digicert.com",
"/td", "sha256", "/fd","sha256", "/a", exeCopy.toFile().canonicalFile.absolutePath)
logger.info("Signing file...")
val signResult = processBuilder.inheritIO().start().waitFor()
if (signResult != 0) {
logger.error("An error occured while signing file.")
response.setStatus(500);
return StreamingResponseBody { outputStream -> outputStream.write("An error occured while signing file.".toByteArray()) }
}
val inputStream = FileInputStream(exeCopy.toFile())
return StreamingResponseBody { outputStream ->
inputStream.copyTo(outputStream)
inputStream.close()
}
} catch (e: IOException ) {
logger.error("An error occured", e);
} finally {
exeCopy?.toFile()?.delete();
directory?.toFile()?.delete();
}
return StreamingResponseBody { outputStream -> outputStream.write("An error occured server side. Check logs.".toByteArray()) }
}
}
\ 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