Loading de.ugoe.cs.swe.T3Q/src/de/ugoe/cs/swe/T3Q/HttpFileClient.java 0 → 100644 +251 −0 Original line number Diff line number Diff line package de.ugoe.cs.swe.T3Q; import java.io.*; import java.net.*; import java.nio.charset.StandardCharsets; import java.nio.file.*; import java.util.*; public class HttpFileClient { private static final String CRLF = "\r\n"; private static final String BOUNDARY = "----WebKitFormBoundary" + System.currentTimeMillis(); /** * Response container holding the JSON data and additional metadata */ public static class ApiResponse { private final String jsonBody; private final int statusCode; private final Map<String, List<String>> headers; public ApiResponse(String jsonBody, int statusCode, Map<String, List<String>> headers) { this.jsonBody = jsonBody; this.statusCode = statusCode; this.headers = headers; } public String getJsonBody() { return jsonBody; } public int getStatusCode() { return statusCode; } public Map<String, List<String>> getHeaders() { return headers; } public String getHeader(String name) { List<String> values = headers.get(name); return values != null && !values.isEmpty() ? values.get(0) : null; } } /** * Upload one or more files via POST to the specified URL * * @param url API endpoint URL * @param files Map of field names to file paths * @param additionalFields Optional text fields to include in the request * @return ApiResponse containing JSON and metadata */ public static ApiResponse uploadFiles(String url, Map<String, Path> files, Map<String, String> additionalFields) throws IOException { System.out.println("CLIENT: Starting upload to " + url); URL apiUrl = new URL(url); HttpURLConnection conn = (HttpURLConnection) apiUrl.openConnection(); try { // Configure connection System.out.println("CLIENT: Configuring connection"); conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.setDoInput(true); conn.setUseCaches(false); conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY); conn.setRequestProperty("Accept", "application/json"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setChunkedStreamingMode(8192); // Use chunked mode to avoid hanging System.out.println("CLIENT: Getting output stream"); // Write multipart request body try (OutputStream out = conn.getOutputStream()) { System.out.println("CLIENT: Got output stream, writing data"); // Add additional text fields if provided if (additionalFields != null) { for (Map.Entry<String, String> field : additionalFields.entrySet()) { System.out.println("CLIENT: Writing field: " + field.getKey()); writeField(out, field.getKey(), field.getValue()); } } // Add files for (Map.Entry<String, Path> file : files.entrySet()) { System.out.println("CLIENT: Writing file: " + file.getValue().getFileName()); writeFile(out, file.getKey(), file.getValue()); } // End multipart request System.out.println("CLIENT: Writing end boundary"); String endBoundary = "--" + BOUNDARY + "--" + CRLF; out.write(endBoundary.getBytes(StandardCharsets.UTF_8)); out.flush(); System.out.println("CLIENT: Finished writing request body"); } // Read response System.out.println("CLIENT: Getting response code"); int statusCode = conn.getResponseCode(); System.out.println("CLIENT: Response code: " + statusCode); InputStream responseStream = statusCode >= 400 ? conn.getErrorStream() : conn.getInputStream(); String jsonResponse = readInputStream(responseStream); System.out.println("CLIENT: Got response: " + jsonResponse); Map<String, List<String>> headers = conn.getHeaderFields(); return new ApiResponse(jsonResponse, statusCode, headers); } finally { conn.disconnect(); } } private static void writeField(OutputStream out, String name, String value) throws IOException { StringBuilder sb = new StringBuilder(); sb.append("--").append(BOUNDARY).append(CRLF); sb.append("Content-Disposition: form-data; name=\"").append(name).append("\"").append(CRLF); sb.append(CRLF); sb.append(value).append(CRLF); out.write(sb.toString().getBytes(StandardCharsets.UTF_8)); } private static void writeFile(OutputStream out, String fieldName, Path filePath) throws IOException { String fileName = filePath.getFileName().toString(); StringBuilder sb = new StringBuilder(); sb.append("--").append(BOUNDARY).append(CRLF); sb.append("Content-Disposition: form-data; name=\"") .append(fieldName).append("\"; filename=\"") .append(fileName).append("\"").append(CRLF); sb.append("Content-Type: ").append(probeContentType(filePath)).append(CRLF); sb.append(CRLF); out.write(sb.toString().getBytes(StandardCharsets.UTF_8)); // Write file bytes Files.copy(filePath, out); out.write(CRLF.getBytes(StandardCharsets.UTF_8)); } /** * Upload a single file */ public static ApiResponse uploadFile(String url, String fieldName, Path filePath) throws IOException { Map<String, Path> files = new HashMap<>(); files.put(fieldName, filePath); return uploadFiles(url, files, null); } /** * Probe content type or return default */ private static String probeContentType(Path path) { try { String type = Files.probeContentType(path); return type != null ? type : "application/octet-stream"; } catch (IOException e) { return "application/octet-stream"; } } /** * Read input stream to string */ private static String readInputStream(InputStream is) throws IOException { if (is == null) return ""; try (BufferedReader reader = new BufferedReader( new InputStreamReader(is, StandardCharsets.UTF_8))) { StringBuilder sb = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { sb.append(line).append("\n"); } return sb.toString().trim(); } } // // Example usage // public static void main(String[] args) { // try { // // Example 1: Upload single file // Path file1 = Paths.get("document.pdf"); // ApiResponse response1 = uploadFile( // "https://api.example.com/upload", // "file", // file1 // ); // // System.out.println("Status: " + response1.getStatusCode()); // System.out.println("JSON Response: " + response1.getJsonBody()); // System.out.println("Content-Type: " + response1.getHeader("Content-Type")); // // // Example 2: Upload multiple files with additional fields // Map<String, Path> files = new HashMap<>(); // files.put("document", Paths.get("file1.pdf")); // files.put("image", Paths.get("photo.jpg")); // // Map<String, String> fields = new HashMap<>(); // fields.put("userId", "12345"); // fields.put("category", "reports"); // // ApiResponse response2 = uploadFiles( // "https://api.example.com/upload", // files, // fields // ); // // System.out.println("\nMultiple files uploaded:"); // System.out.println("Status: " + response2.getStatusCode()); // System.out.println("Response: " + response2.getJsonBody()); // // } catch (IOException e) { // System.err.println("Upload failed: " + e.getMessage()); // e.printStackTrace(); // } // } //} // Example usage public static void main(String[] args) { try { // Example 1: Upload single file // Path file1 = Paths.get("build.properties"); // ApiResponse response1 = uploadFile( // "http://localhost:3005/compile_asn", // "file", // file1 // ); // // System.out.println("Status: " + response1.getStatusCode()); // System.out.println("JSON Response: " + response1.getJsonBody()); // System.out.println("Content-Type: " + response1.getHeader("Content-Type")); // Example 2: Upload multiple files with additional fields Map<String, Path> files = new HashMap<>(); files.put("file1", Paths.get("build.properties")); files.put("file2", Paths.get("pom.xml")); Map<String, String> fields = new HashMap<>(); fields.put("userId", "12345"); fields.put("category", "reports"); ApiResponse response2 = uploadFiles( "http://localhost:3005/compile_asn", files, fields ); System.out.println("\nMultiple files uploaded:"); System.out.println("Status: " + response2.getStatusCode()); System.out.println("Response: " + response2.getJsonBody()); } catch (IOException e) { System.err.println("Upload failed: " + e.getMessage()); e.printStackTrace(); } } } No newline at end of file de.ugoe.cs.swe.T3Q/src/de/ugoe/cs/swe/T3Q/JsonSchemaBridge.java +46 −0 Original line number Diff line number Diff line package de.ugoe.cs.swe.T3Q; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; Loading @@ -16,6 +19,7 @@ import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.google.gson.JsonPrimitive; import de.ugoe.cs.swe.T3Q.HttpFileClient.ApiResponse; import de.ugoe.cs.swe.common.logging.LoggingInterface.LogLevel; public class JsonSchemaBridge { Loading @@ -25,6 +29,48 @@ public class JsonSchemaBridge { } public void convertASN1toJSONSchema(String titanCompilerPath, List<String> asnFilePaths, String targetPath) { if (titanCompilerPath.startsWith("http")) { convertASN1toJSONSchemaRemote(titanCompilerPath, asnFilePaths, targetPath); } else { convertASN1toJSONSchemaLocal(titanCompilerPath, asnFilePaths, targetPath); } } public void convertASN1toJSONSchemaRemote(String titanCompilerPath, List<String> asnFilePaths, String targetPath) { Map<String, Path> files = new HashMap<>(); int i = 1; for (String p : asnFilePaths) { files.put("file"+i, Paths.get(p)); i++; } Map<String, String> fields = new HashMap<>(); fields.put("command", "12345"); fields.put("argument", "reports"); try { ApiResponse response = HttpFileClient.uploadFiles( titanCompilerPath, files, fields ); System.out.println("\nASN.1 files submitted..:"); System.out.println("Status: " + response.getStatusCode()); String body = response.getJsonBody(); // System.out.println("Response: " + body); Files.writeString(Path.of(targetPath), body, StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING); //TODO: handle } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void convertASN1toJSONSchemaLocal(String titanCompilerPath, List<String> asnFilePaths, String targetPath) { Process process = null; try { List<String> command = new ArrayList<>(); Loading Loading
de.ugoe.cs.swe.T3Q/src/de/ugoe/cs/swe/T3Q/HttpFileClient.java 0 → 100644 +251 −0 Original line number Diff line number Diff line package de.ugoe.cs.swe.T3Q; import java.io.*; import java.net.*; import java.nio.charset.StandardCharsets; import java.nio.file.*; import java.util.*; public class HttpFileClient { private static final String CRLF = "\r\n"; private static final String BOUNDARY = "----WebKitFormBoundary" + System.currentTimeMillis(); /** * Response container holding the JSON data and additional metadata */ public static class ApiResponse { private final String jsonBody; private final int statusCode; private final Map<String, List<String>> headers; public ApiResponse(String jsonBody, int statusCode, Map<String, List<String>> headers) { this.jsonBody = jsonBody; this.statusCode = statusCode; this.headers = headers; } public String getJsonBody() { return jsonBody; } public int getStatusCode() { return statusCode; } public Map<String, List<String>> getHeaders() { return headers; } public String getHeader(String name) { List<String> values = headers.get(name); return values != null && !values.isEmpty() ? values.get(0) : null; } } /** * Upload one or more files via POST to the specified URL * * @param url API endpoint URL * @param files Map of field names to file paths * @param additionalFields Optional text fields to include in the request * @return ApiResponse containing JSON and metadata */ public static ApiResponse uploadFiles(String url, Map<String, Path> files, Map<String, String> additionalFields) throws IOException { System.out.println("CLIENT: Starting upload to " + url); URL apiUrl = new URL(url); HttpURLConnection conn = (HttpURLConnection) apiUrl.openConnection(); try { // Configure connection System.out.println("CLIENT: Configuring connection"); conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.setDoInput(true); conn.setUseCaches(false); conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY); conn.setRequestProperty("Accept", "application/json"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setChunkedStreamingMode(8192); // Use chunked mode to avoid hanging System.out.println("CLIENT: Getting output stream"); // Write multipart request body try (OutputStream out = conn.getOutputStream()) { System.out.println("CLIENT: Got output stream, writing data"); // Add additional text fields if provided if (additionalFields != null) { for (Map.Entry<String, String> field : additionalFields.entrySet()) { System.out.println("CLIENT: Writing field: " + field.getKey()); writeField(out, field.getKey(), field.getValue()); } } // Add files for (Map.Entry<String, Path> file : files.entrySet()) { System.out.println("CLIENT: Writing file: " + file.getValue().getFileName()); writeFile(out, file.getKey(), file.getValue()); } // End multipart request System.out.println("CLIENT: Writing end boundary"); String endBoundary = "--" + BOUNDARY + "--" + CRLF; out.write(endBoundary.getBytes(StandardCharsets.UTF_8)); out.flush(); System.out.println("CLIENT: Finished writing request body"); } // Read response System.out.println("CLIENT: Getting response code"); int statusCode = conn.getResponseCode(); System.out.println("CLIENT: Response code: " + statusCode); InputStream responseStream = statusCode >= 400 ? conn.getErrorStream() : conn.getInputStream(); String jsonResponse = readInputStream(responseStream); System.out.println("CLIENT: Got response: " + jsonResponse); Map<String, List<String>> headers = conn.getHeaderFields(); return new ApiResponse(jsonResponse, statusCode, headers); } finally { conn.disconnect(); } } private static void writeField(OutputStream out, String name, String value) throws IOException { StringBuilder sb = new StringBuilder(); sb.append("--").append(BOUNDARY).append(CRLF); sb.append("Content-Disposition: form-data; name=\"").append(name).append("\"").append(CRLF); sb.append(CRLF); sb.append(value).append(CRLF); out.write(sb.toString().getBytes(StandardCharsets.UTF_8)); } private static void writeFile(OutputStream out, String fieldName, Path filePath) throws IOException { String fileName = filePath.getFileName().toString(); StringBuilder sb = new StringBuilder(); sb.append("--").append(BOUNDARY).append(CRLF); sb.append("Content-Disposition: form-data; name=\"") .append(fieldName).append("\"; filename=\"") .append(fileName).append("\"").append(CRLF); sb.append("Content-Type: ").append(probeContentType(filePath)).append(CRLF); sb.append(CRLF); out.write(sb.toString().getBytes(StandardCharsets.UTF_8)); // Write file bytes Files.copy(filePath, out); out.write(CRLF.getBytes(StandardCharsets.UTF_8)); } /** * Upload a single file */ public static ApiResponse uploadFile(String url, String fieldName, Path filePath) throws IOException { Map<String, Path> files = new HashMap<>(); files.put(fieldName, filePath); return uploadFiles(url, files, null); } /** * Probe content type or return default */ private static String probeContentType(Path path) { try { String type = Files.probeContentType(path); return type != null ? type : "application/octet-stream"; } catch (IOException e) { return "application/octet-stream"; } } /** * Read input stream to string */ private static String readInputStream(InputStream is) throws IOException { if (is == null) return ""; try (BufferedReader reader = new BufferedReader( new InputStreamReader(is, StandardCharsets.UTF_8))) { StringBuilder sb = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { sb.append(line).append("\n"); } return sb.toString().trim(); } } // // Example usage // public static void main(String[] args) { // try { // // Example 1: Upload single file // Path file1 = Paths.get("document.pdf"); // ApiResponse response1 = uploadFile( // "https://api.example.com/upload", // "file", // file1 // ); // // System.out.println("Status: " + response1.getStatusCode()); // System.out.println("JSON Response: " + response1.getJsonBody()); // System.out.println("Content-Type: " + response1.getHeader("Content-Type")); // // // Example 2: Upload multiple files with additional fields // Map<String, Path> files = new HashMap<>(); // files.put("document", Paths.get("file1.pdf")); // files.put("image", Paths.get("photo.jpg")); // // Map<String, String> fields = new HashMap<>(); // fields.put("userId", "12345"); // fields.put("category", "reports"); // // ApiResponse response2 = uploadFiles( // "https://api.example.com/upload", // files, // fields // ); // // System.out.println("\nMultiple files uploaded:"); // System.out.println("Status: " + response2.getStatusCode()); // System.out.println("Response: " + response2.getJsonBody()); // // } catch (IOException e) { // System.err.println("Upload failed: " + e.getMessage()); // e.printStackTrace(); // } // } //} // Example usage public static void main(String[] args) { try { // Example 1: Upload single file // Path file1 = Paths.get("build.properties"); // ApiResponse response1 = uploadFile( // "http://localhost:3005/compile_asn", // "file", // file1 // ); // // System.out.println("Status: " + response1.getStatusCode()); // System.out.println("JSON Response: " + response1.getJsonBody()); // System.out.println("Content-Type: " + response1.getHeader("Content-Type")); // Example 2: Upload multiple files with additional fields Map<String, Path> files = new HashMap<>(); files.put("file1", Paths.get("build.properties")); files.put("file2", Paths.get("pom.xml")); Map<String, String> fields = new HashMap<>(); fields.put("userId", "12345"); fields.put("category", "reports"); ApiResponse response2 = uploadFiles( "http://localhost:3005/compile_asn", files, fields ); System.out.println("\nMultiple files uploaded:"); System.out.println("Status: " + response2.getStatusCode()); System.out.println("Response: " + response2.getJsonBody()); } catch (IOException e) { System.err.println("Upload failed: " + e.getMessage()); e.printStackTrace(); } } } No newline at end of file
de.ugoe.cs.swe.T3Q/src/de/ugoe/cs/swe/T3Q/JsonSchemaBridge.java +46 −0 Original line number Diff line number Diff line package de.ugoe.cs.swe.T3Q; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; Loading @@ -16,6 +19,7 @@ import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.google.gson.JsonPrimitive; import de.ugoe.cs.swe.T3Q.HttpFileClient.ApiResponse; import de.ugoe.cs.swe.common.logging.LoggingInterface.LogLevel; public class JsonSchemaBridge { Loading @@ -25,6 +29,48 @@ public class JsonSchemaBridge { } public void convertASN1toJSONSchema(String titanCompilerPath, List<String> asnFilePaths, String targetPath) { if (titanCompilerPath.startsWith("http")) { convertASN1toJSONSchemaRemote(titanCompilerPath, asnFilePaths, targetPath); } else { convertASN1toJSONSchemaLocal(titanCompilerPath, asnFilePaths, targetPath); } } public void convertASN1toJSONSchemaRemote(String titanCompilerPath, List<String> asnFilePaths, String targetPath) { Map<String, Path> files = new HashMap<>(); int i = 1; for (String p : asnFilePaths) { files.put("file"+i, Paths.get(p)); i++; } Map<String, String> fields = new HashMap<>(); fields.put("command", "12345"); fields.put("argument", "reports"); try { ApiResponse response = HttpFileClient.uploadFiles( titanCompilerPath, files, fields ); System.out.println("\nASN.1 files submitted..:"); System.out.println("Status: " + response.getStatusCode()); String body = response.getJsonBody(); // System.out.println("Response: " + body); Files.writeString(Path.of(targetPath), body, StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING); //TODO: handle } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void convertASN1toJSONSchemaLocal(String titanCompilerPath, List<String> asnFilePaths, String targetPath) { Process process = null; try { List<String> command = new ArrayList<>(); Loading