Commit fac82a43 authored by Adrien's avatar Adrien

* start kotlin implem

parents
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<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"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.jetbrains.kotlin.core.KOTLIN_CONTAINER"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>windows-codesign-service</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.jetbrains.kotlin.ui.kotlinBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.jetbrains.kotlin.core.kotlinNature</nature>
</natures>
<linkedResources>
<link>
<name>kotlin_bin</name>
<type>2</type>
<locationURI>org.jetbrains.kotlin.core.filesystem:/windows-codesign-service/kotlin_bin</locationURI>
</link>
</linkedResources>
</projectDescription>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.bonitasoft.releng</groupId>
<artifactId>windows-codesign-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<kotlin.version>1.2.40</kotlin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
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
@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.bufferedReader().use {
it.readText()
}
// var nRead: Int
// var data = ByteArray(1024)
// while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
// outputStream.write(data, 0, nRead);
// }
// inputStream.close();
}
} catch (e: IOException ) {
} finally {
exeCopy?.toFile()?.delete();
directory?.toFile()?.delete();
}
return StreamingResponseBody { outputStream -> outputStream.write("An error occured server side. Check logs.".toByteArray()) }
}
}
fun main(args : Array<String>) {
SpringApplication.run(CodeSignApp::class.java, *args)
}
\ No newline at end of file
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());
};
}
}
spring:
servlet:
multipart:
max-file-size: -1
max-request-size: -1
\ 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