001/* 002 * Copyright (c) 2010 The Regents of the University of California. 003 * All rights reserved. 004 * 005 * '$Author: tao $' 006 * '$Date: 2011-03-31 19:49:31 +0000 (Thu, 31 Mar 2011) $' 007 * '$Revision: 27405 $' 008 * 009 * Permission is hereby granted, without written agreement and without 010 * license or royalty fees, to use, copy, modify, and distribute this 011 * software and its documentation for any purpose, provided that the above 012 * copyright notice and the following two paragraphs appear in all copies 013 * of this software. 014 * 015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 019 * SUCH DAMAGE. 020 * 021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 026 * ENHANCEMENTS, OR MODIFICATIONS. 027 * 028 */ 029 030package org.kepler.gui.kar; 031 032import javax.swing.JOptionPane; 033 034import org.apache.commons.logging.Log; 035import org.apache.commons.logging.LogFactory; 036import org.kepler.authentication.AuthenticationException; 037import org.kepler.authentication.AuthenticationManager; 038import org.kepler.authentication.ProxyEntity; 039import org.kepler.kar.KARFile; 040import org.kepler.kar.UploadToRepository; 041import org.kepler.kar.karxml.KarXmlGenerator; 042import org.kepler.objectmanager.repository.Repository; 043import org.kepler.objectmanager.repository.RepositoryManager; 044 045import ptolemy.actor.gui.TableauFrame; 046 047public class ComponentUploader { 048 private static final Log log = LogFactory.getLog(ComponentUploader.class); 049 private static final boolean isDebugging = log.isDebugEnabled(); 050 051 private TableauFrame _frame; 052 053 private UploadToRepository _uploader; 054 055 private RepositoryManager _manager; 056 057 public ComponentUploader(TableauFrame parent) { 058 _frame = parent; 059 060 try { 061 _manager = RepositoryManager.getInstance(); 062 } catch (Exception e) { 063 System.out.println("Could not get repository manager: " 064 + e.getMessage()); 065 e.printStackTrace(); 066 } 067 } 068 069 /** 070 * Upload a kar file and generated kaxml to the repository. 071 * @param karFile 072 * @return boolean true if the upload succeeded 073 */ 074 public boolean upload(KARFile karFile) { 075 boolean success = false; 076 boolean fileUploadSuccess = uploadDataFile(karFile); 077 if (fileUploadSuccess) { 078 boolean metadataUploadSuccess = false; 079 try { 080 KarXmlGenerator kxg = new KarXmlGenerator(karFile); 081 String metadataObj = kxg.getKarXml(); 082 metadataUploadSuccess = _uploader.uploadMetadata(metadataObj); 083 if (!metadataUploadSuccess) { 084 JOptionPane.showMessageDialog(_frame, 085 "There was a problem uploading the metadata", 086 "Error", JOptionPane.ERROR_MESSAGE); 087 } 088 else { 089 JOptionPane.showMessageDialog(_frame, 090 "Component successfully uploaded.", "Success", 091 JOptionPane.INFORMATION_MESSAGE); 092 success = true; 093 } 094 } catch (Exception e) { 095 e.printStackTrace(); 096 JOptionPane.showMessageDialog(_frame, 097 "There was a problem uploading the metadata: " 098 + e.getMessage(), "Error", 099 JOptionPane.ERROR_MESSAGE); 100 } 101 102 103 } 104 return success; 105 } 106 107 /** 108 * Upload a kar file and associated metadata to a repository 109 * @param karFile the Kar file will be uploaded 110 * @param metadataObj the associated the metadata will be uploaded 111 * @return true if the uploading was successful 112 */ 113 public boolean upload(KARFile karFile, String metadataObj) 114 { 115 boolean success = false; 116 success = uploadDataFile(karFile); 117 if(success) 118 { 119 success = uploadMetadataObj(metadataObj); 120 } 121 if(success) 122 { 123 JOptionPane.showMessageDialog(_frame, 124 "Component successfully uploaded.", "Success", 125 JOptionPane.INFORMATION_MESSAGE); 126 } 127 return success; 128 } 129 130 /* 131 * Upload the kar file to remote repository 132 */ 133 private boolean uploadDataFile(KARFile karFile) { 134 if (isDebugging) 135 log.debug("upload(" + karFile.toString()); 136 137 Repository saveRepository = _manager.getSaveRepository(); 138 if (saveRepository == null) { 139 JOptionPane.showMessageDialog(_frame, 140 "To upload to a remote repository, select one from the " 141 + "component search preferences", "Cannot Save", 142 JOptionPane.ERROR_MESSAGE); 143 return false; 144 } 145 _uploader = new UploadToRepository(karFile.getFileLocation()); 146 _uploader.setRepository(saveRepository); 147 148 if (!authenticateUser()) { 149 return false; 150 } 151 152 queryIfPublic(); 153 boolean fileUploadSuccess = false; 154 try { 155 fileUploadSuccess = _uploader.uploadFile(); 156 if (!fileUploadSuccess) { 157 JOptionPane.showMessageDialog(_frame, 158 "There was a problem uploading the data", "Error", 159 JOptionPane.ERROR_MESSAGE); 160 return false; 161 } 162 } catch (Exception e) { 163 e.printStackTrace(); 164 JOptionPane 165 .showMessageDialog(_frame, 166 "There was a problem uploading the data: " 167 + e.getMessage(), "Error", 168 JOptionPane.ERROR_MESSAGE); 169 return false; 170 } 171 return true; 172 } 173 174 /* 175 * Upload a metacat associated with kar file to the repository. 176 * This method only can be called after calling uploadDataFile 177 */ 178 private boolean uploadMetadataObj(String metadataObj) 179 { 180 return _uploader.uploadMetadata(metadataObj); 181 } 182 183 184 /** 185 * open a login gui to authenticate the user 186 * 187 * @return boolean true if the user was authenticated 188 */ 189 private boolean authenticateUser() { 190 if (isDebugging) 191 log.debug("authenticateUser()"); 192 193 try { 194 AuthenticationManager authMan = AuthenticationManager.getManager(); 195 196 // get the auth domain to authenticate against 197 ProxyEntity pentity = authMan.getProxy(_uploader.getRepository() 198 .getAuthDomain()); 199 _uploader.setSessionId(pentity.getCredential()); 200 if (_uploader.getSessionId() == null) { 201 return false; 202 } 203 204 } catch (AuthenticationException ae) { 205 if (ae.getMessage() != null && ae.getMessage().indexOf("<unauth_login>") != -1) { 206 // bad password... try again 207 JOptionPane.showMessageDialog(_frame, 208 "Incorrect password or username.", 209 "Failed Authentication", 210 JOptionPane.INFORMATION_MESSAGE); 211 212 return false; 213 } else if (ae.getType() == AuthenticationException.USER_CANCEL) { 214 return false; 215 } else { 216 ae.printStackTrace(); 217 JOptionPane.showMessageDialog(_frame, "Error authenticating: " 218 + ae.getMessage(), "Failed Authentication", 219 JOptionPane.INFORMATION_MESSAGE); 220 return false; 221 } 222 } 223 return true; 224 } 225 226 private void queryIfPublic() { 227 if (isDebugging) 228 log.debug("queryIfPublic()"); 229 int ispublic = JOptionPane.showConfirmDialog(_frame, 230 "Would you like your component " 231 + "to be publicly accessible in the library?", 232 "Access Rights", JOptionPane.YES_NO_OPTION); 233 if (JOptionPane.YES_OPTION == ispublic) { 234 _uploader.setPublicFile(true); 235 } 236 } 237}