Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sync database via ftp-protocol / added missed german translations #49

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
<classpathentry kind="lib" path="lib/javacsv.jar"/>
<classpathentry kind="lib" path="lib/AppleJavaExtensions.jar"/>
<classpathentry kind="lib" path="lib/commons-validator-1.4.0.jar"/>
<classpathentry kind="lib" path="lib/ftp4j-1.7.2.jar"/>
<classpathentry kind="output" path="build"/>
</classpath>
Binary file added lib/ftp4j-1.7.2.jar
Binary file not shown.
21 changes: 18 additions & 3 deletions src/com/_17od/upm/gui/DatabaseActions.java
Original file line number Diff line number Diff line change
Expand Up @@ -686,9 +686,12 @@ public void openDatabaseFromURL() throws TransportException, IOException, Proble
String username = openDBDialog.getUsernameTextField().getText();
String password = openDBDialog.getPasswordTextField().getText();

// Ask the user for a location to save the database file to
File saveDatabaseTo = getSaveAsFile(Translator.translate("saveDatabaseAs"));
// find the filename in the url
String file = new URL(remoteLocation).getFile();

// Ask the user for a location to save the database file to
File saveDatabaseTo = getSaveAsFile(Translator.translate("saveDatabaseAs"), new File(file));

if (saveDatabaseTo != null) {

// Download the database
Expand Down Expand Up @@ -807,6 +810,7 @@ public boolean reloadDatabaseFromDisk() throws InvalidPasswordException,
public boolean syncWithRemoteDatabase() throws TransportException, ProblemReadingDatabaseFile, IOException, CryptoException, PasswordDatabaseException {

boolean syncSuccessful = false;
File remoteDatabaseFile = null;

try {
fileMonitor.pause();
Expand All @@ -825,7 +829,7 @@ public boolean syncWithRemoteDatabase() throws TransportException, ProblemReadin

// Download the database that's already at the remote location
Transport transport = Transport.getTransportForURL(new URL(remoteLocation));
File remoteDatabaseFile = transport.getRemoteFile(remoteLocation, database.getDatabaseFile().getName(), httpUsername, httpPassword);
remoteDatabaseFile = transport.getRemoteFile(remoteLocation, database.getDatabaseFile().getName(), httpUsername, httpPassword);

// Attempt to decrypt the database using the password the user entered
PasswordDatabase remoteDatabase = null;
Expand Down Expand Up @@ -909,6 +913,10 @@ public void run() {
}

} finally {
// delete temporary file
if (remoteDatabaseFile != null) {
remoteDatabaseFile.delete();
}
mainWindow.getContentPane().setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
fileMonitor.start();
}
Expand Down Expand Up @@ -1017,12 +1025,19 @@ public void importAccounts() throws TransportException, ProblemReadingDatabaseFi
* @return The file to save to or null
*/
private File getSaveAsFile(String title) {
return getSaveAsFile(title, null);
}

private File getSaveAsFile(String title, File preselectedFile) {
File selectedFile;

boolean gotValidFile = false;
do {
JFileChooser fc = new JFileChooser();
fc.setDialogTitle(title);
if (preselectedFile!=null) {
fc.setSelectedFile(preselectedFile);
}
int returnVal = fc.showSaveDialog(mainWindow);

if (returnVal != JFileChooser.APPROVE_OPTION) {
Expand Down
189 changes: 189 additions & 0 deletions src/com/_17od/upm/transport/FTPtransport.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
package com._17od.upm.transport;

import it.sauronsoftware.ftp4j.FTPClient;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

/**
* Implements the Ftp transport.
*
* This class uses the following classes:
* - FTPclient (http://www.sauronsoftware.it/projects/ftp4j/)
* - URL (Java built-in)
* @file FTPtransport.java
* @author andreas
*
*/
public class FTPtransport extends Transport {

/**
* This is the account name for logging in on the Ftp server if none is
* supplied to the method. This should be an e-mail address.
*/
protected static final String ANONYMOUS = "[email protected]";

// / This is the password sent for an anonymous login.
protected static final String PASSWORD = "secret";

// / This field holds an unique file resource for temporary storing the
// database file.
private File tmpDir;

/**
* Instantiates the object with the given remote location.
*
* @note Currently the constructor does no special job...
* @param remoteLocation
* URL of the remote file
*/
public FTPtransport(String remoteLocation) {
}

/**
* Instantiates the object and supplies a resource on the local host.
*
* @param tmpDir
* The resource where the retrieved database file should be
* stored.
*/
public FTPtransport(File tmpDir) {
this.tmpDir = tmpDir;
}

// ----- put
// -------------------------------------------------------------------------

/**
* puts the source file to the remote location
* @param destination full destination url - with or without filename
* @param source file
* @param username the username accessing the server
* @param username the password accessing the server
* @throws TransportException thrown if there was a problem communicating with the server
*/
public void put(String destination, File source, String username,
String password) throws TransportException {
try {
URL dst = new URL(destination);

if (destination.endsWith("/")) {
throw new TransportException("filename is mandatory");
}

FTPClient ftp = new FTPClient();
ftp.connect(dst.getHost());
ftp.login(username, password);

File dstfile = new File(dst.getPath());
// put the file into the correct place
ftp.changeDirectory(dstfile.getParent().substring(1));
ftp.upload(source);
// rename the remote file if necessary
if (!source.getName().equals(dstfile.getName())) {
ftp.rename(source.getName(), dstfile.getName());
}

ftp.disconnect(false);
} catch (Exception e) {
throw new TransportException(e);
}
}

public void put(String targetLocation, File file) throws TransportException {
put(targetLocation, file, ANONYMOUS, PASSWORD);
}

// ----- get
// -------------------------------------------------------------------------

public byte[] get(String url) throws TransportException {
return get(url, ANONYMOUS, PASSWORD);
}

public byte[] get(String fullRemoteLocation, String username,
String password) throws TransportException {
byte[] retVal = null;

try {
URL url = new URL(fullRemoteLocation);
url = new URL(url.getProtocol() + "://" + username + ":" + password
+ "@" + url.getHost() + url.getPath());
BufferedInputStream src = new BufferedInputStream(url.openStream());
ByteArrayOutputStream buf = new ByteArrayOutputStream();
int ch;
while ((ch = src.read()) != -1) {
buf.write(ch);
}
retVal = buf.toByteArray();
src.close();
buf.close();
} catch (MalformedURLException e) {
throw new TransportException(e);
} catch (IOException e) {
throw new TransportException(e);
}
return retVal;
}

public byte[] get(String url, String fileName) throws TransportException {
return get(url, fileName, ANONYMOUS, PASSWORD);
}

public byte[] get(String url, String fileName, String username,
String password) throws TransportException {
return get(url, fileName, ANONYMOUS, PASSWORD);
}

// ----- getRemoteFile
// ---------------------------------------------------------------

public File getRemoteFile(String remoteLocation) throws TransportException {
return getRemoteFile(remoteLocation, ANONYMOUS, PASSWORD);
}

public File getRemoteFile(String fullRemoteLocation, String fileName,
String username, String password) throws TransportException {
return getRemoteFile(fullRemoteLocation, username, password);
}

public File getRemoteFile(String remoteLocation, String username,
String password) throws TransportException {
try {
URL source = new URL(remoteLocation);
File destination = File.createTempFile("upm", null, tmpDir);
FTPClient ftp = new FTPClient();
ftp.connect(source.getHost());
ftp.login(username, password);
ftp.download(source.getPath(), destination);
ftp.disconnect(false);

return destination;
} catch (Exception e) {
throw new TransportException(e);
}
}

public File getRemoteFile(String remoteLocation, String fileName)
throws TransportException {
return getRemoteFile(remoteLocation, fileName, ANONYMOUS, PASSWORD);
}

// ----- delete
// ----------------------------------------------------------------------

public void delete(String targetLocation, String name, String username,
String password) throws TransportException {
// do nothing
}

public void delete(String targetLocation, String name)
throws TransportException {
// do nothing
}

}
6 changes: 6 additions & 0 deletions src/com/_17od/upm/transport/HTTPTransport.java
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,10 @@ private boolean isNotEmpty(String stringToCheck) {
return retVal;
}


public byte[] get(String url) throws TransportException {
// TODO Auto-generated method stub
return null;
}

}
21 changes: 21 additions & 0 deletions src/com/_17od/upm/transport/Transport.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,29 @@
*/
public abstract class Transport {

/**
* the remote hostname
*/
protected String remoteHost=null;

/**
* the remote path (if any)
*/
protected String remotePath=null;

/**
* the remote filename
*/
protected String remoteFile=null;

public abstract void put(String targetLocation, File file, String username, String password) throws TransportException;

public abstract void put(String targetLocation, File file) throws TransportException;

public abstract byte[] get(String url, String fileName) throws TransportException;

public abstract byte[] get(String url) throws TransportException;

public abstract byte[] get(String url, String fileName, String username, String password) throws TransportException;

public abstract byte[] get(String url, String username, String password) throws TransportException;
Expand All @@ -58,6 +75,8 @@ public static Transport getTransportForURL(URL url) {
retVal = new HTTPTransport();
} else if (url.getProtocol().equals("https")) {
retVal = new HTTPTransport();
} else if (url.getProtocol().equals("ftp")) {
retVal = new FTPtransport(url.toString());
}
return retVal;
}
Expand All @@ -70,6 +89,8 @@ public static boolean isASupportedProtocol(String protocol) {
supported = true;
} else if (protocol.equals("file")) {
supported = true;
} else if (protocol.equals("ftp")) {
supported = true;
}
return supported;
}
Expand Down
26 changes: 25 additions & 1 deletion src/upm_de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ databasePropertiesMenuItem = Eigenschaften von Datenbank
exitMenuItem = Beenden
optionsMenuItem = Optionen
resetSearchMenuItem = Suche zur\u00fccksetzen
exportMenuItem = Export Klartext (csv)
importMenuItem = Import Klartext (csv)

# Account Menu Items
addAccountMenuItem = Konto hinzuf\u00fcgen
Expand All @@ -23,6 +25,10 @@ deleteAccountMenuItem = Konto l\u00f6schen
viewAccountMenuItem = Konto ansehen
copyUsernameMenuItem = Benutzernamen kopieren
copyPasswordMenuItem = Passwort kopieren
launchURLMenuItem = URL \u00f6ffnen
UrlErrorJoptionpaneTitle = Ung\u00fcltige URL
EmptyUrlJoptionpaneMsg = Keine URL eingegeben!
InvalidUrlJoptionpaneMsg = Das Format der URL ist ung\u00fcltig (http://www.url.com)!

# Help Menu Item
aboutMenuItem = Mehr \u00fcber
Expand Down Expand Up @@ -62,6 +68,16 @@ unsynchronised = Nicht synchronisiert
synchronised = Synchronisiert
revision = Revision
localDatabase = Lokale Datenbank
exportFile = Klartext exportieren...
problemExporting = Problem beim Export
import = Klartext importieren
problemImporting = Problem beim Importieren
importExistingTitle = *Import Existing...
importExistingQuestion = Das Konto [{0}] existiert bereits. Was wollen Sie tun?
notCSVFileError = Die Datei [{0}] scheint keine CSV-Klartext Passwortdatei zu sein (problem bei Zeile {1})
reloadDatabase = Datenbank neu laden
askReloadDatabase = Datenbank wurde ver�ndert. Neu vom Datenspeicher laden?
accountDoesntExist = Das Konto [{0}] existiert nicht

# Add/Edit Account dialog messages
account = Konto
Expand All @@ -74,20 +90,28 @@ ok = OK
cancel = Abbrechen
accountAlreadyExistsWithName = Konto namens [{0}] existiert bereits
accountAlreadyExists = Konto existiert bereits...
generate = Generieren

# Options dialog messages
options = Optionen
general = Allgemein
dbToLoadOnStartup = Beim Start zu ladende Datenbank
hideAccountPassword = Konto-Passwort standardm\u00e4\u00dfig verstecken
storeWindowPosition = Erinner fenster position
databaseAutoLock = Automatisch abmelden wenn Fokus fehlt (Minuten)
generatedPasswodLength = L\u00e4nge der generierten Passw\u00f6rter
includePunctuationCharacters = Generierte Passw\u00f6rter enthalten Interpunktionszeichen (#!@$*)
applicationAlwaysonTop = Anwendungsfenster im vVordergrund halten
storeWindowPosition = Fenstergr�sse und -position merken
acceptSelfSignedCerts = Selbst signierte Zertifikate akzeptieren
enableProxy = Proxy benutzen
httpProxy = HTTP Proxy
port = Port
httpProxyUsername = Benutzername f\u00fcr HTTP Proxy
httpProxyPassword = Passwort f\u00fcr HTTP Proxy
dbToOpenOnStartup = Beim Start zu \u00f6ffnende Datenbank......
language = Sprache
invalidValueForDatabaseAutoLockTime = Ung\u00fcltige Eingabe im Feld "Minuten"
invalidValueForAccountPasswordLength = Ung\u00fcltige Eingabe im Feld "Passwortl\u00e4nge"


# About dialog message
Expand Down