Author: bpoussin Date: 2013-01-28 20:05:28 +0100 (Mon, 28 Jan 2013) New Revision: 2479 Url: http://nuiton.org/projects/nuiton-utils/repository/revisions/2479 Log: ajout du support de l'authentification Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/ApplicationUpdater.java Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/ApplicationUpdater.java =================================================================== --- trunk/nuiton-utils/src/main/java/org/nuiton/util/ApplicationUpdater.java 2013-01-28 13:57:54 UTC (rev 2478) +++ trunk/nuiton-utils/src/main/java/org/nuiton/util/ApplicationUpdater.java 2013-01-28 19:05:28 UTC (rev 2479) @@ -74,10 +74,14 @@ * <li>os.arch: l'architecture du systeme d'exploitation sur lequel l'application fonctionne (ex: amd64) * * <h3>format du fichier de properties</h3> - * [osName.][osArch.]appName.version=version de l'application - * [osName.][osArch.]appName.url=url du fichier compresse de la nouvelle version - * (format <a href="http://commons.apache.org/vfs/filesystems.html">commons-vfs2</a>) * + * <li>[osName.][osArch.]appName.version=version de l'application</li> + * <li>[osName.][osArch.]appName.auth=true ou false selon que l'acces a l'url + * demande une authentification a fournir par le callback + * (voir {@link ApplicationUpdaterCallback#updateToDo})</li> + * <li>[osName.][osArch.]appName.url=url du fichier compresse de la nouvelle version + * (format <a href="http://commons.apache.org/vfs/filesystems.html">commons-vfs2</a>)</li> + * * appName est a remplacer par le nom de l'application. Il est possible * d'avoir plusieurs application dans le meme fichier ou plusieurs version * en fonction de l'os et de l'architecture. @@ -134,6 +138,7 @@ final static public String HTTP_PROXY = "http_proxy"; final static public String URL_KEY = "url"; + final static public String AUTHENTICATION_KEY = "auth"; final static public String VERSION_KEY = "version"; final static public String VERSION_FILE = "version.appup"; @@ -197,8 +202,18 @@ * Permet de modifier le repertoire destination ou l'url du zip de * l'application pour une application/version * particuliere ou d'annuler la mise a jour en le supprimant de la map - * qui sera retourne + * qui sera retourne. * + * Si {@link ApplicationInfo#needAuthentication} est vrai, il faut que + * les valeurs {@link ApplicationInfo#login} et {@link ApplicationInfo#password} + * soient renseignees. Si elle ne le sont pas la recuperation de la + * ressource echouera. Pour des raisons de securite vous pouvez souhaiter + * mettre le mot de passe sous une forme encrypte. Dans ce cas il doit + * etre encadrer par '{' et '}'. Pour encrypter le mot de passe + * vous devez utiliser: + * <pre> + * java -cp commons-vfs-2.0.jar org.apache.commons.vfs2.util.EncryptUtil encrypt mypassword + * </pre> * @param appToUpdate liste des applications a mettre a jour * @return null or empty map if we don't want update, otherwize list of * app to update @@ -232,16 +247,25 @@ public String oldVersion; public String newVersion; public String url; + public boolean needAuthentication; + public String login; + public char[] password; public File destDir; - public ApplicationInfo(String name, String oldVersion, String newVersion, String url, File destDir) { + public ApplicationInfo(String name, String oldVersion, String newVersion, String url, File destDir, boolean needAuthentication) { this.name = name; this.oldVersion = oldVersion; this.newVersion = newVersion; this.url = url; + this.needAuthentication = needAuthentication; this.destDir = destDir; } + public void setAuthentication(String login, char[] password) { + this.login = login; + this.password = password; + } + @Override public String toString() { String result = String.format( @@ -301,9 +325,11 @@ if (greater) { String urlString = releaseConfig.getOption( app + SEPARATOR_KEY + URL_KEY); + boolean needAuthentication = releaseConfig.getOptionAsBoolean( + app + SEPARATOR_KEY + AUTHENTICATION_KEY); appToUpdate.put(app, new ApplicationInfo( - app, currentVersion, newVersion, urlString, destDir)); + app, currentVersion, newVersion, urlString, destDir, needAuthentication)); } } @@ -367,7 +393,12 @@ protected void doUpdate(FileSystemOptions vfsConfig, ApplicationInfo info) throws Exception { if (info.destDir != null) { File dest = new File(info.destDir, info.name); - deepCopy(vfsConfig, info.url, dest.getAbsolutePath()); + String url = toVfsURL(info.url); + if (info.needAuthentication) { + StringUtils.replaceOnce(url, "://", + String.format("://%s:%s@", info.login, new String(info.password))); + } + deepCopy(vfsConfig, url, dest.getAbsolutePath()); // ajout du fichier de version File versionFile = new File(dest, VERSION_FILE); @@ -393,7 +424,7 @@ protected void deepCopy(FileSystemOptions vfsConfig, String srcPath, String targetPath) throws FileSystemException { FileSystemManager fsManager = VFS.getManager(); - FileObject archive = fsManager.resolveFile(toVfsURL(srcPath), vfsConfig); + FileObject archive = fsManager.resolveFile(srcPath, vfsConfig); FileObject[] children = archive.getChildren(); if (children.length == 1) {