Merge pull request #5 from workinghard/newBuild

New GTX client due to server rework
This commit is contained in:
2024-07-06 17:22:22 +02:00
committed by GitHub
14 changed files with 344 additions and 548 deletions

View File

@@ -9,6 +9,7 @@
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
@@ -18,7 +19,7 @@
<attribute name="test" 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">
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
@@ -36,6 +37,13 @@
<attribute name="m2e-apt" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
<attributes>
<attribute name="optional" value="true"/>

View File

@@ -22,12 +22,12 @@
</natures>
<filteredResources>
<filter>
<id>1641408443802</id>
<id>1720116449156</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>

85
pom.xml
View File

@@ -6,23 +6,31 @@
<groupId>guitartex2.GuitarTeX2</groupId>
<artifactId>GuitarTeX2</artifactId>
<version>3.5.0</version>
<version>3.6.0</version>
<name>GuitarTeX2</name>
<url></url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<!--<maven.compiler.source>1.9</maven.compiler.source>
<maven.compiler.target>1.9</maven.compiler.target> -->
</properties>
<dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20240303</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
@@ -47,77 +55,13 @@
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>guitartex2.GuitarTeX2Convert</mainClass>
</manifest>
</archive>
<finalName>${project.artifactId}Convert-${project.version}</finalName>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- bind to the packaging phase -->
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>${argLine}</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.3</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>target/jacoco.exec</dataFile>
<outputDirectory>target/jacoco-ut</outputDirectory>
</configuration>
</execution>
</executions>
<configuration>
<systemPropertyVariables>
<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
@@ -134,6 +78,9 @@
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release> <!--or <release>10</release>-->
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>

View File

@@ -402,7 +402,17 @@ final class Configurations {
}
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) {

View File

@@ -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);
}
}
}

View File

@@ -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 StatusBox _myStatusBox;
private String _texFileName;
private String _pdfFileName;
private String _showPdf;
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 boolean _hostExist = false;
private int id;
private StatusBox myStatusBox;
private String fileName;
private String showPdf;
private boolean hostExist = false;
private ResourceBundle _resbundle;
private ResourceBundle resbundle;
protected Socket serverConn;
DataOutputStream dout;
DataInputStream din;
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 @@ public class GTXClient extends Thread{
}
}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 class GTXClient extends Thread{
}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) {
}*/
}

View File

@@ -142,15 +142,11 @@ class GTXConsole extends JFrame implements ActionListener{
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 @@ class GTXConsole extends JFrame implements ActionListener{
// 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();

View File

@@ -284,15 +284,11 @@ public class GuitarTeX2 extends JFrame {
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 class GuitarTeX2 extends JFrame {
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) {

View File

@@ -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;
}
}

View File

@@ -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 @@ public class PreferencesBox extends JFrame implements ActionListener {
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 class PreferencesBox extends JFrame implements ActionListener {
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);

View File

@@ -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();
}
}

View File

@@ -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

View File

@@ -128,5 +128,5 @@ warnTitle=Warning!
statusTitle=Status
frameConstructor=GuitarTeX2
appVersion=Version - 3.5.0
appVersion=Version - 3.6.0
copyright=Nikolai Rinas

View File

@@ -127,5 +127,5 @@ warnTitle=Warnung!
statusTitle=Status
frameConstructor=GuitarTeX2
appVersion=Version - 3.5.0
appVersion=Version - 3.6.0
copyright=Nikolai Rinas