From 2a627919c1e4020c9f160f4aa5e159b059900947 Mon Sep 17 00:00:00 2001 From: workinghard Date: Sat, 6 Jul 2024 17:17:52 +0200 Subject: [PATCH] New GTX client due to server rework --- .classpath | 10 +- .project | 4 +- pom.xml | 85 +---- src/main/java/guitartex2/Configurations.java | 12 +- src/main/java/guitartex2/FileTransfer.java | 303 ------------------ src/main/java/guitartex2/GTXClient.java | 255 ++++++++------- src/main/java/guitartex2/GTXConsole.java | 17 +- src/main/java/guitartex2/GuitarTeX2.java | 23 +- .../java/guitartex2/MultipartFormSender.java | 98 ++++++ src/main/java/guitartex2/PreferencesBox.java | 25 +- src/main/java/guitartex2/ServerResponse.java | 50 +++ src/main/resources/GuitarTeX2.properties | 4 +- .../resources/GuitarTeX2strings.properties | 2 +- .../resources/GuitarTeX2strings_de.properties | 2 +- 14 files changed, 343 insertions(+), 547 deletions(-) delete mode 100644 src/main/java/guitartex2/FileTransfer.java create mode 100644 src/main/java/guitartex2/MultipartFormSender.java create mode 100644 src/main/java/guitartex2/ServerResponse.java diff --git a/.classpath b/.classpath index 39abf1c..c6f2285 100644 --- a/.classpath +++ b/.classpath @@ -9,6 +9,7 @@ + @@ -18,7 +19,7 @@ - + @@ -36,6 +37,13 @@ + + + + + + + diff --git a/.project b/.project index 6aa5e77..ed6cbba 100644 --- a/.project +++ b/.project @@ -22,12 +22,12 @@ - 1641408443802 + 1720116449156 30 org.eclipse.core.resources.regexFilterMatcher - node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ diff --git a/pom.xml b/pom.xml index 1b77e48..3ad33e8 100644 --- a/pom.xml +++ b/pom.xml @@ -6,23 +6,31 @@ guitartex2.GuitarTeX2 GuitarTeX2 - 3.5.0 + 3.6.0 GuitarTeX2 UTF-8 - 1.8 - 1.8 + + + + + org.json + json + 20240303 + + + org.apache.maven.plugins maven-assembly-plugin - 3.1.1 jar-with-dependencies @@ -47,77 +55,13 @@ - - org.apache.maven.plugins - maven-assembly-plugin - 3.1.1 - - - jar-with-dependencies - - - - guitartex2.GuitarTeX2Convert - - - ${project.artifactId}Convert-${project.version} - false - - - - make-assembly - - package - - single - - - - - - maven-surefire-plugin ${argLine} - - org.jacoco - jacoco-maven-plugin - 0.8.3 - - - prepare-agent - - prepare-agent - - - - report - prepare-package - - report - - - - post-unit-test - test - - report - - - target/jacoco.exec - target/jacoco-ut - - - - - - target/jacoco.exec - - - + @@ -134,6 +78,9 @@ maven-compiler-plugin 3.8.0 + + 11 + maven-surefire-plugin diff --git a/src/main/java/guitartex2/Configurations.java b/src/main/java/guitartex2/Configurations.java index 33cc1d9..06e907c 100644 --- a/src/main/java/guitartex2/Configurations.java +++ b/src/main/java/guitartex2/Configurations.java @@ -402,7 +402,17 @@ public void setGtxServer(String mValue) { } public int getGtxServerPort() { - return Integer.parseInt(mProperties.getProperty("gtxServerPort")); + int serverPort = -1; + try { + serverPort = Integer.parseInt(mProperties.getProperty("gtxServerPort")); + }catch (Exception e) { + // It's fine to have empty field + } + if ( serverPort > 0 ) { + return serverPort; + }else{ + return -1; + } } public void setGtxServerPort(String mValue) { diff --git a/src/main/java/guitartex2/FileTransfer.java b/src/main/java/guitartex2/FileTransfer.java deleted file mode 100644 index a49c56b..0000000 --- a/src/main/java/guitartex2/FileTransfer.java +++ /dev/null @@ -1,303 +0,0 @@ -/* - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - */ -package guitartex2; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.RandomAccessFile; -import java.util.zip.GZIPInputStream; -import java.util.zip.GZIPOutputStream; - -class FileTransfer { - - // Debug 0/1 - int debug = 0; - - // Define commands - //private static String quit = "CMD:123_QUIT_123"; - //private static String transfer = "CMD:123_TRANSFER_123"; - private static String ok = "CMD:123_OK_123"; - private static String failed = "CMD:123_FAILED_123"; - - // Father ID - int fId; - - // Streams - DataInputStream inStream; - DataOutputStream outStream; - - // Main - Constructor - public FileTransfer(int id, DataInputStream inputStream, DataOutputStream outputStream) { - fId = id; - inStream = inputStream; - outStream = outputStream; - } - - public FileTransfer(int id, int mDebug, DataInputStream inputStream, DataOutputStream outputStream) { - debug = mDebug; - fId = id; - inStream = inputStream; - outStream = outputStream; - } - - @SuppressWarnings("UnusedAssignment") - public int sendFile(String myFile) { - RandomAccessFile in; - - long length; - String fileLengthResult; - - int zipResult = gzipFile(myFile, false); - if (zipResult == 0) { - myFile = myFile + ".gz"; - } else { - sendMsg("Filetransfer[" + fId + "]: can't gzip file"); - try { - outStream.writeInt(0); - fileLengthResult = inStream.readUTF(); - } catch (Exception e) { - sendMsg("Filetransfer[" + fId + "]: stream error" + e); - cleanTmp(myFile); - return 1; - } - cleanTmp(myFile); - return 1; - } - - try { - in = new RandomAccessFile(myFile, "r"); - length = in.length(); - } catch (Exception g) { - sendMsg("Filetransfer[" + fId + "]: can't read file" + g); - try { - outStream.writeInt(0); - fileLengthResult = inStream.readUTF(); - } catch (Exception h) { - sendMsg("Filetransfer[" + fId + "]: stream error" + h); - cleanTmp(myFile); - return 1; - } - cleanTmp(myFile); - return 1; - } - if (length > Integer.MAX_VALUE) { - // File is too large - sendMsg("Filetransfer[" + fId + "]: File is too large"); - cleanTmp(myFile, in); - return 1; - } else { - int myFileLength = (int) length; - sendMsg("Filetransfer[" + fId + "]: sending fille length: " + myFileLength); - try { - outStream.writeInt(myFileLength); - fileLengthResult = inStream.readUTF(); - } catch (Exception i) { - sendMsg("Filetransfer[" + fId + "]: stream error" + i); - cleanTmp(myFile, in); - return 1; - } - if (fileLengthResult.equals(ok)) { - try { - sendMsg("Filetransfer[" + fId + "]: send a file..."); - byte b; - for (int i = 0; i < myFileLength; i++) { - b = in.readByte(); - outStream.writeByte(b); - } - in.close(); - File f = new File(myFile); - f.delete(); - String fileTransferResult = inStream.readUTF(); - if (fileTransferResult.equals(failed)) { - sendMsg("Filetransfer[" + fId + "]: sending file failed: " + fileTransferResult); - cleanTmp(myFile); - return 1; - } - } catch (Exception j) { - sendMsg("Filetransfer[" + fId + "]: stream error" + j); - cleanTmp(myFile); - return 1; - } - } else { - sendMsg("Filetransfer[" + fId + "]: sending file length failed: " + fileLengthResult); - cleanTmp(myFile, in); - return 1; - } - } - return 0; - } - - public int receiveFile(String myFile) { - int fileLength; - RandomAccessFile out; - - sendMsg("Filetransfer[" + fId + "]: awaiting filelength ..."); - try { - fileLength = inStream.readInt(); - } catch (IOException e) { - sendMsg("Filetransfer[" + fId + "]: stream error " + e); - cleanTmp(myFile); - return 1; - } - sendMsg("Filetransfer[" + fId + "]: file length = " + fileLength); - if (fileLength > 0) { - try { - out = new RandomAccessFile(myFile + ".gz", "rw"); - } catch (IOException g) { - sendMsg("Filetransfer[" + fId + "]: can't write to a file " + g); - try { - outStream.writeUTF(failed); - } catch (Exception h) { - sendMsg("Filetransfer[" + fId + "]: stream error " + h); - } - cleanTmp(myFile); - return 1; - } - try { - outStream.writeUTF(ok); - } catch (Exception i) { - sendMsg("Filetransfer[" + fId + "]: stream error " + i); - cleanTmp(myFile, out); - return 1; - } - sendMsg("Filetransfer[" + fId + "]: awaiting file..."); - try { - byte b; - for (int i = 0; i < fileLength; i++) { - b = inStream.readByte(); - out.write(b); - } - out.close(); - } catch (Exception j) { - sendMsg("Filetransfer[" + fId + "]: file transfer failed " + j); - try { - outStream.writeUTF(failed); - } catch (Exception k) { - sendMsg("Filetransfer[" + fId + "]: stream error " + k); - } - cleanTmp(myFile); - return 1; - } - try { - outStream.writeUTF(ok); - int gunzipResult = gunzipFile(myFile + ".gz", true); - if (gunzipResult != 0) { - sendMsg("Filetransfer[" + fId + "]: gunzip failed"); - cleanTmp(myFile); - return 1; - } - } catch (Exception l) { - sendMsg("Filetransfer[" + fId + "]: stream error " + l); - cleanTmp(myFile); - return 1; - } - sendMsg("Filetransfer[" + fId + "]: file transfer complete."); - } else { - try { - outStream.writeUTF(failed); - } catch (Exception f) { - sendMsg("Filetransfer[" + fId + "]: file length invalid"); - cleanTmp(myFile); - return 1; - } - } - return 0; - } - - private int gzipFile(String myFile, boolean delSource) { - int read; - byte[] data = new byte[1024]; - sendMsg("Filetransfer[" + fId + "]: gzip file"); - try { - File f = new File(myFile); - GZIPOutputStream out; - try (FileInputStream in = new FileInputStream(f)) { - out = new GZIPOutputStream(new FileOutputStream(myFile + ".gz")); - while ((read = in.read(data, 0, 1024)) != -1) { - out.write(data, 0, read); - } - } - out.close(); - if (delSource == true) { - f.delete(); - } - } catch (Exception e) { - sendMsg("Filetransfer[" + fId + "]: gzipping file failed " + e); - return 1; - } - - sendMsg("Filetransfer[" + fId + "]: gzip file"); - return 0; - } - - private int gunzipFile(String myGZFile, boolean delSource) { - int read; - byte[] data = new byte[1024]; - - sendMsg("Filetransfer[" + fId + "]: gunzip file"); - - try { - File f = new File(myGZFile); - FileOutputStream out; - try (GZIPInputStream in = new GZIPInputStream(new FileInputStream(f))) { - String myFile; - if (myGZFile.endsWith(".gz")) { - myFile = myGZFile.substring(0, myGZFile.length() - 3); - } else { - myFile = myGZFile; - } out = new FileOutputStream(myFile); - while ((read = in.read(data, 0, 1024)) != -1) { - out.write(data, 0, read); - } - } - out.close(); - if (delSource == true) { - f.delete(); - } - } catch (Exception e) { - sendMsg("Filetransfer[" + fId + "]: gunzipping file failed " + e); - return 1; - } - return 0; - } - - private void sendMsg(String msg) { - if (debug == 1) { - System.out.println(msg); - } - } - - private void cleanTmp(String fileName, RandomAccessFile file) { - try { - new File(fileName).delete(); - file.close(); - } catch (Exception e) { - sendMsg("can't clean tmp file: " + e); - } - } - - private void cleanTmp(String fileName) { - try { - new File(fileName).delete(); - } catch (Exception e) { - sendMsg("can't clean tmp file: " + e); - } - } -} diff --git a/src/main/java/guitartex2/GTXClient.java b/src/main/java/guitartex2/GTXClient.java index 7e9d743..6f32fce 100644 --- a/src/main/java/guitartex2/GTXClient.java +++ b/src/main/java/guitartex2/GTXClient.java @@ -16,77 +16,67 @@ package guitartex2; -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.net.Socket; -import java.net.UnknownHostException; import java.util.Locale; import java.util.ResourceBundle; +import java.net.URL; +import java.io.*; +import javax.net.ssl.HttpsURLConnection; public class GTXClient extends Thread{ - - private static String quit = "CMD:123_QUIT_123"; - private static String transfer = "CMD:123_TRANSFER_123"; - //private static String ok = "CMD:123_OK_123"; - private static String failed = "CMD:123_FAILED_123"; - private static String ping = "CMD:123_PING_123"; - private static String pong = "CMD:123_PONG_123"; - private int id; - private StatusBox myStatusBox; - private String fileName; - private String showPdf; + private StatusBox _myStatusBox; + private String _texFileName; + private String _pdfFileName; + private String _showPdf; - private boolean hostExist = false; + private boolean _hostExist = false; - private ResourceBundle resbundle; - protected Socket serverConn; - DataOutputStream dout; - DataInputStream din; + private ResourceBundle _resbundle; - private GTXConsole myConsole; + private GTXConsole _myConsole; private String logCache = ""; - + private String _urlPingEndpoint = "/ping"; + private String _urlLatexEndpoint = "/latex"; + private String _httpsURLPing = ""; + private String _httpsURLLatex = ""; + // Konstruktor public GTXClient(String host, int port) { - resbundle = ResourceBundle.getBundle ("GuitarTeX2strings", Locale.getDefault()); - try { - logToConsole("Trying to connect to " + host + " " + port); - serverConn = new Socket(host, port); - }catch (UnknownHostException e) { - logToConsole("Bad host name given."); - }catch (IOException e) { - logToConsole("GtxClient: " + e); + _resbundle = ResourceBundle.getBundle ("GuitarTeX2strings", Locale.getDefault()); + if ( port > 0) { + _httpsURLPing = host + ":" + port + _urlPingEndpoint; + _httpsURLLatex = host + ":" + port + _urlLatexEndpoint; + }else{ + _httpsURLPing = host + _urlPingEndpoint; + _httpsURLLatex = host + _urlLatexEndpoint; + } + logToConsole("PingURL: " + _httpsURLPing); + logToConsole("LatexURL: " + _httpsURLLatex); + int available = this.checkServerConnection(); + if ( available == 0 ) { + _hostExist = true; + logToConsole("Made server connection"); + }else{ + _hostExist = false; + logToConsole("No server connection"); } - hostExist = true; - logToConsole("Made server connection"); } - public GTXClient(String host, int port, StatusBox sBox, String file, int myId, String sPdf) { - resbundle = ResourceBundle.getBundle ("GuitarTeX2strings", Locale.getDefault()); - try { - logToConsole("Trying to connect to " + host + " " + port); - serverConn = new Socket(host, port); - }catch (UnknownHostException e) { - logToConsole("Bad host name given."); - }catch (IOException e) { - logToConsole("GtxClient: " + e); - } + public GTXClient(String host, int port, StatusBox sBox, String texFile, String pdfFile, String sPdf) { + this(host, port); - hostExist = true; - showPdf = sPdf; - id = myId; - myStatusBox = sBox; - fileName = file; + _showPdf = sPdf; + _myStatusBox = sBox; + _texFileName = texFile; + _pdfFileName = pdfFile; logToConsole("Made server connection"); } private void logToConsole(String text) { - if ( myConsole == null ) { + if ( _myConsole == null ) { if ( logCache.equals("") ) { logCache = text; }else{ @@ -94,32 +84,86 @@ private void logToConsole(String text) { } }else{ if ( ! logCache.equals("") ) { - myConsole.addText(logCache); + _myConsole.addText(logCache); logCache = ""; - myConsole.addText(text); + _myConsole.addText(text); }else{ - myConsole.addText(text); + _myConsole.addText(text); } } } void setGTXConsole(GTXConsole mConsole) { - myConsole = mConsole; + _myConsole = mConsole; if ( ! logCache.equals("") ) { - myConsole.addText(logCache); + _myConsole.addText(logCache); logCache = ""; } } void forceLogCache() { - if ( myConsole != null ) { - myConsole.addText(logCache); + if ( _myConsole != null ) { + _myConsole.addText(logCache); logCache = null; } } @Override public void run () { + // Do something only if the connection exist + if ( _hostExist == true ) { + if ( _myStatusBox != null ) { + _myStatusBox.setStatus(_resbundle.getString("sendTexFile")); + } + logToConsole("Sending tex file..."); + try { + ServerResponse myResponse = MultipartFormSender.sendMultipartForm(_httpsURLLatex, _texFileName, + "file", "text", "{\"version\":\"1.0\"}"); + logToConsole(myResponse.toString()); + if ( myResponse.isInitial() == false ) { + if ( myResponse.getCmdRC() == 0 ) { + if ( myResponse.getDownloadURL() != "" ) { + _myStatusBox.setStatus(_resbundle.getString("receivePdfFile")); + // Download file + BufferedInputStream in = new BufferedInputStream(new URL(myResponse.getDownloadURL()).openStream()); + FileOutputStream fileOutputStream = new FileOutputStream(_pdfFileName); + byte dataBuffer[] = new byte[1024]; + int bytesRead; + while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) { + fileOutputStream.write(dataBuffer, 0, bytesRead); + } + fileOutputStream.close(); + in.close(); + + logToConsole("pdf file received."); + // Show PDF + _myStatusBox.setStatus("try to show PDF file ..."); + logToConsole("try to show pdf file"); + try{ + Runtime.getRuntime().exec(_showPdf); + }catch (Exception h) { + logToConsole("ERR: " + h); + } + }else{ + logToConsole("Download URL is missing"); + } + }else{ + logToConsole("Tex command failed"); + new InfoBox(_resbundle.getString("texFailed")); + } + }else{ + logToConsole("Got no response from server"); + } + } catch (IOException e) { + logToConsole("tex file send failed: " + e.getMessage()); + } + }else{ + logToConsole("We're offline. Please check internet connection"); + } + + _myStatusBox.setVisible(false); + + /* try { if ( myStatusBox != null ) { myStatusBox.setStatus(resbundle.getString("sendTexFile")); @@ -171,87 +215,38 @@ public void run () { }catch (Exception e) { logToConsole("failed texin file: " + e); } - } - - public int openConnection() { - if ( hostExist) { - try { - dout = new DataOutputStream(serverConn.getOutputStream()); - din = new DataInputStream(serverConn.getInputStream()); - logToConsole("connection open"); - return 0; - }catch (Exception e) { - logToConsole("open connection failed: " + e); - return 1; - } - }else{ - logToConsole("Host not exists!"); - return 1; - } - } - - public int closeConnection() { - if ( hostExist ) { - try { - dout.writeUTF(quit); - din.close(); - dout.close(); - logToConsole("connection closed"); - return 0; - }catch (Exception e) { - logToConsole("close connection failed: " + e); - return 1; - } - }else{ - logToConsole("Host not exists!"); - return 1; - } + */ } public int checkServerConnection() { - if ( hostExist ) { - try { - logToConsole("sending ping ..."); - dout.writeUTF(ping); - logToConsole("awaiting pong ..."); - String pingResult = din.readUTF(); - logToConsole(pingResult); - if ( pingResult.equals(pong)) { - logToConsole("pong received."); - return 0; - }else { - logToConsole("server doesn't working."); - return 1; - } - }catch (Exception e) { - logToConsole("ping failed " + e); - return 1; - } - }else{ - logToConsole("Host not exists!"); - return 1; - } + try{ + URL myUrl = new URL(_httpsURLPing); + HttpsURLConnection conn = (HttpsURLConnection)myUrl.openConnection(); + if ( conn.getResponseCode() == 200 ) { + return 0; + }else{ + logToConsole("responseCode: " + conn.getResponseCode()); + InputStream is = conn.getInputStream(); + InputStreamReader isr = new InputStreamReader(is); + BufferedReader br = new BufferedReader(isr); + String inputLine; + String bodeString = ""; + while ((inputLine = br.readLine()) != null) { + bodeString = bodeString + inputLine; + } + br.close(); + logToConsole("Error: " + bodeString); + return 1; + } + }catch (Exception e) { + logToConsole("Error: " + e.getMessage()); + return 1; + } } public String sendText(String text) { - String result = "unknown command!"; - if ( text.equals("ping") ){ - try { - dout.writeUTF(ping); - result = din.readUTF(); - if ( result.equals(pong) ) { - result = "pong"; - } - dout.writeUTF(quit); - }catch (Exception e) { - logToConsole("Sending failed!"); - return "Sending failed!"; - } - } + String result = "not implemented yet!"; return result; } -/* public int tex2pdf(String fileName, int id) { - - }*/ } \ No newline at end of file diff --git a/src/main/java/guitartex2/GTXConsole.java b/src/main/java/guitartex2/GTXConsole.java index deb66ad..640451e 100644 --- a/src/main/java/guitartex2/GTXConsole.java +++ b/src/main/java/guitartex2/GTXConsole.java @@ -142,15 +142,11 @@ public GTXConsole() { public void setGTXClient(Configurations mConf) { myConf = mConf; gtxClient = new GTXClient(myConf.getGtxServer(), myConf.getGtxServerPort()); - int openResult = gtxClient.openConnection(); - if ( openResult == 0 ) { - int connResult = gtxClient.checkServerConnection(); - if ( connResult == 0) { - mInputButton.setEnabled(true); - mInputAction.setEnabled(true); - } + int connResult = gtxClient.checkServerConnection(); + if ( connResult == 0) { + mInputButton.setEnabled(true); + mInputAction.setEnabled(true); } - gtxClient.closeConnection(); } public class buttonActionClass extends AbstractAction { @@ -170,12 +166,11 @@ public void actionPerformed(ActionEvent e) { // Nachricht senden gtxClient = new GTXClient(myConf.getGtxServer(), myConf.getGtxServerPort()); - int openResult = gtxClient.openConnection(); - if ( openResult == 0 ) { + int checkResult = gtxClient.checkServerConnection(); + if ( checkResult == 0 ) { String receiveText = gtxClient.sendText(mInputTextField.getText()); addText("Server: " + receiveText); } - gtxClient.closeConnection(); mInputTextField.setText(""); mInputTextField.requestFocus(); diff --git a/src/main/java/guitartex2/GuitarTeX2.java b/src/main/java/guitartex2/GuitarTeX2.java index 2775c9e..130938e 100644 --- a/src/main/java/guitartex2/GuitarTeX2.java +++ b/src/main/java/guitartex2/GuitarTeX2.java @@ -284,15 +284,11 @@ public GuitarTeX2() { GTXClient gtxClient = new GTXClient(myConf.getGtxServer(), myConf.getGtxServerPort()); gtxClient.setGTXConsole(consoleBox); - int openResult = gtxClient.openConnection(); - if (openResult == 0) { - int connResult = gtxClient.checkServerConnection(); - if (connResult == 0) { - mTeX2PdfButton.setEnabled(true); - mTeX2PdfAction.setEnabled(true); - } + int connResult = gtxClient.checkServerConnection(); + if (connResult == 0) { + mTeX2PdfButton.setEnabled(true); + mTeX2PdfAction.setEnabled(true); } - gtxClient.closeConnection(); // Create StatusBox myStatusBox = new StatusBox(); @@ -1175,13 +1171,14 @@ public void actionPerformed(ActionEvent e) { try (OutputStreamWriter out = new OutputStreamWriter(fos, Charset.forName("UTF-8"))) { out.write(mShowTeXArea.getText()); //if ( mShowTeXArea.isEnabled() ) { - String showPdf = myConf.quoteString(myConf.getPdfViewer()) + " " + myConf.quoteString(tmpDir + tmpDirPrefix + pdfFileName); - GTXClient gtxClient = new GTXClient(myConf.getGtxServer(), myConf.getGtxServerPort(), myStatusBox, tmpDir + tmpDirPrefix + texFileName, id, showPdf); + String pdfFileNamePath = tmpDir + tmpDirPrefix + pdfFileName; + String showPdf = myConf.quoteString(myConf.getPdfViewer()) + " " + myConf.quoteString(pdfFileNamePath); + GTXClient gtxClient = new GTXClient(myConf.getGtxServer(), myConf.getGtxServerPort(), myStatusBox, tmpDir + tmpDirPrefix + texFileName, pdfFileNamePath, showPdf); gtxClient.setGTXConsole(consoleBox); - int openResult = gtxClient.openConnection(); - if (openResult == 0) { + //int checkResult = gtxClient.checkServerConnection(); + //if (checkResult == 0) { gtxClient.start(); - } + //} //} } } catch (Exception g) { diff --git a/src/main/java/guitartex2/MultipartFormSender.java b/src/main/java/guitartex2/MultipartFormSender.java new file mode 100644 index 0000000..58e46bf --- /dev/null +++ b/src/main/java/guitartex2/MultipartFormSender.java @@ -0,0 +1,98 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package guitartex2; + +import java.io.*; +import java.net.HttpURLConnection; +import java.net.URL; + +import org.json.JSONObject; + +public class MultipartFormSender { + + public static ServerResponse sendMultipartForm(String requestURL, String filePath, String fileFormFieldName, String fieldName, String fieldValue) throws IOException { + String boundary = "*****"+Long.toString(System.currentTimeMillis())+"*****"; + String lineEnd = "\r\n"; + String twoHyphens = "--"; + ServerResponse myResponse = new ServerResponse(); + + URL url = new URL(requestURL); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setUseCaches(false); + connection.setDoOutput(true); // Indicates POST method + connection.setDoInput(true); + connection.setRequestMethod("POST"); + connection.setRequestProperty("Connection", "Keep-Alive"); + connection.setRequestProperty("User-Agent", "GuitarTex2 v3.5"); + connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); + + try (DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream())) { + // Write form fields + outputStream.writeBytes(twoHyphens + boundary + lineEnd); + outputStream.writeBytes("Content-Disposition: form-data; name=\"" + fieldName + "\"" + lineEnd + lineEnd); + outputStream.writeBytes(fieldValue + lineEnd); + + + // Write file + outputStream.writeBytes(twoHyphens + boundary + lineEnd); + outputStream.writeBytes("Content-Disposition: form-data; name=\"" + fileFormFieldName + "\"; filename=\"" + new File(filePath).getName() + "\"\r\n"); + // Ensure the file part has an appropriate Content-Type, if necessary + outputStream.writeBytes("Content-Type: application/octet-stream\r\n\r\n"); + try (BufferedInputStream fileStream = new BufferedInputStream(new FileInputStream(filePath))) { + byte[] buffer = new byte[8192]; // Consider adjusting the buffer size + int bytesRead; + while ((bytesRead = fileStream.read(buffer)) != -1) { + outputStream.write(buffer, 0, bytesRead); + } + outputStream.writeBytes("\r\n"); + } + + // End of multipart/form-data. + outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); + } + + // Handle the response from the server + int responseCode = connection.getResponseCode(); + myResponse.setUrlRC(responseCode); + //System.out.println("Response Code: " + responseCode); + + try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { + String line; + StringBuilder response = new StringBuilder(); + + while ((line = reader.readLine()) != null) { + response.append(line); + response.append(System.lineSeparator()); + } + // Try to decode the response + //JsonObject jsonObject = Json.createReader(new InputStream()).readObject(); + + JSONObject myJsonObject = new JSONObject(response.toString()); + myResponse.setCmdRC(myJsonObject.getInt("cmd_rc")); + myResponse.setMsg(myJsonObject.getString("msg")); + myResponse.setDownloadURL(myJsonObject.getString("download_url")); + + //myResponse.setMsg(); + //System.out.println("Response: " + response.toString()); + } catch (Exception e) { + System.out.println("Json failed: " + e.getMessage()); + } finally { + connection.disconnect(); + } + return myResponse; + } +} diff --git a/src/main/java/guitartex2/PreferencesBox.java b/src/main/java/guitartex2/PreferencesBox.java index 4edaa16..eb539ae 100644 --- a/src/main/java/guitartex2/PreferencesBox.java +++ b/src/main/java/guitartex2/PreferencesBox.java @@ -165,7 +165,10 @@ public class PreferencesBox extends JFrame implements ActionListener { JLabel mGtxServerPortLabel = new JLabel(resbundle.getString("mPrefGtxServerPort"), JLabel.TRAILING); p.add(mGtxServerPortLabel); mGtxServerPortField = new JTextField(fieldLength); - mGtxServerPortField.setText(Integer.toString(myConfiguration.getGtxServerPort())); + int portNr = myConfiguration.getGtxServerPort(); + if ( portNr > 0 ) { + mGtxServerPortField.setText(Integer.toString(portNr)); + } mGtxServerPortLabel.setLabelFor(mGtxServerPortField); p.add(mGtxServerPortField); p.add(new JLabel()); @@ -283,7 +286,10 @@ private void resetFields() { mPdfViewerField.setText(myConfiguration.getPdfViewer()); mTmpPathField.setText(myConfiguration.getTmpDir()); mGtxServerField.setText(myConfiguration.getGtxServer()); - mGtxServerPortField.setText(myConfiguration.getGtxServerPort() + ""); + int portNr = myConfiguration.getGtxServerPort(); + if ( portNr > 0 ) { + mGtxServerPortField.setText(portNr + ""); + } } class SymWindow extends java.awt.event.WindowAdapter { @@ -306,17 +312,10 @@ public void actionPerformed(ActionEvent newEvent) { myConfiguration.saveSettings(); GTXClient gtxClient = new GTXClient(myConfiguration.getGtxServer(), myConfiguration.getGtxServerPort()); gtxClient.setGTXConsole(myConfiguration.getConsole()); - int openResult = gtxClient.openConnection(); - if (openResult == 0) { - int connResult = gtxClient.checkServerConnection(); - if (connResult == 0) { - tex2pdfButton.setEnabled(true); - tex2pdfAction.setEnabled(true); - } else { - tex2pdfButton.setEnabled(false); - tex2pdfAction.setEnabled(false); - } - gtxClient.closeConnection(); + int connResult = gtxClient.checkServerConnection(); + if (connResult == 0) { + tex2pdfButton.setEnabled(true); + tex2pdfAction.setEnabled(true); } else { tex2pdfButton.setEnabled(false); tex2pdfAction.setEnabled(false); diff --git a/src/main/java/guitartex2/ServerResponse.java b/src/main/java/guitartex2/ServerResponse.java new file mode 100644 index 0000000..f83743b --- /dev/null +++ b/src/main/java/guitartex2/ServerResponse.java @@ -0,0 +1,50 @@ +package guitartex2; + +public class ServerResponse { + private int _urlRC = -1; + private int _cmdRC = -1; + private String _msg = ""; + private String _downloadURL = ""; + + public int getUrlRC() { + return _urlRC; + } + public void setUrlRC(int urlRC) { + _urlRC = urlRC; + } + public int getCmdRC() { + return _cmdRC; + } + public void setCmdRC(int cmdRC) { + _cmdRC = cmdRC; + } + public String getMsg(){ + return _msg; + } + public void setMsg(String msg){ + _msg = msg; + } + public String getDownloadURL(){ + return _downloadURL; + } + public void setDownloadURL(String downloadString ){ + _downloadURL = downloadString; + } + + public boolean isInitial(){ + if ( _urlRC == -1 && _cmdRC == -1 && _msg == "" && _downloadURL == "" ) { + return true; + }else{ + return false; + } + } + + public String toString() { + StringBuilder myresponse = new StringBuilder(); + myresponse.append("urlRC: " + _urlRC + "\n"); + myresponse.append("cmdRC: " + _cmdRC + "\n"); + myresponse.append("msg: " + _msg + "\n"); + myresponse.append("DownloadURL: " + _downloadURL + "\n"); + return myresponse.toString(); + } +} diff --git a/src/main/resources/GuitarTeX2.properties b/src/main/resources/GuitarTeX2.properties index a90474c..5134197 100644 --- a/src/main/resources/GuitarTeX2.properties +++ b/src/main/resources/GuitarTeX2.properties @@ -7,5 +7,5 @@ windowsPdfViewer=/Programme/Adobe/Reader/AcroRd32.exe linuxPdfViewer=/usr/bin/xdg-open exSongFile=griechischer_wein.gtx exBookFile=my_first_book.gtb -gtxServer=guitartex2.nikolai-rinas.de -gtxServerPort=3121 +gtxServer=https://latex2pdf.nikolai-rinas.de +gtxServerPort=443 diff --git a/src/main/resources/GuitarTeX2strings.properties b/src/main/resources/GuitarTeX2strings.properties index 64b186a..1f34d1c 100644 --- a/src/main/resources/GuitarTeX2strings.properties +++ b/src/main/resources/GuitarTeX2strings.properties @@ -128,5 +128,5 @@ warnTitle=Warning! statusTitle=Status frameConstructor=GuitarTeX2 -appVersion=Version - 3.5.0 +appVersion=Version - 3.6.0 copyright=Nikolai Rinas diff --git a/src/main/resources/GuitarTeX2strings_de.properties b/src/main/resources/GuitarTeX2strings_de.properties index 230fa79..4666722 100644 --- a/src/main/resources/GuitarTeX2strings_de.properties +++ b/src/main/resources/GuitarTeX2strings_de.properties @@ -127,5 +127,5 @@ warnTitle=Warnung! statusTitle=Status frameConstructor=GuitarTeX2 -appVersion=Version - 3.5.0 +appVersion=Version - 3.6.0 copyright=Nikolai Rinas \ No newline at end of file