PageRenderTime 6254ms CodeModel.GetById 22ms RepoModel.GetById 2ms app.codeStats 1ms

/projects/netbeans-7.3/j2ee.sun.appsrv/src/org/netbeans/modules/j2ee/sun/ide/editors/AdminAuthenticator.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 300 lines | 189 code | 38 blank | 73 comment | 26 complexity | 2a1456bb4d0259446db291fb640ed78b MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * Contributor(s):
  28. *
  29. * The Original Software is NetBeans. The Initial Developer of the Original
  30. * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
  31. * Microsystems, Inc. All Rights Reserved.
  32. *
  33. * If you wish your version of this file to be governed by only the CDDL
  34. * or only the GPL Version 2, indicate your decision by adding
  35. * "[Contributor] elects to include this software in this distribution
  36. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  37. * single choice of license, a recipient has the option to distribute
  38. * your version of this file under either the CDDL, the GPL Version 2 or
  39. * to extend the choice of license to its licensees as provided above.
  40. * However, if you add GPL Version 2 code and therefore, elected the GPL
  41. * Version 2 license, then the option applies only if the new code is
  42. * made subject to such option by the copyright holder.
  43. */
  44. package org.netbeans.modules.j2ee.sun.ide.editors;
  45. import java.util.Arrays;
  46. import java.util.ResourceBundle;
  47. import java.util.logging.Level;
  48. import java.util.logging.Logger;
  49. import java.util.prefs.Preferences;
  50. import org.netbeans.api.keyring.Keyring;
  51. import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties;
  52. import org.netbeans.modules.j2ee.sun.api.SunDeploymentManagerInterface;
  53. import org.netbeans.modules.j2ee.sun.api.SunURIManager;
  54. import org.openide.DialogDescriptor;
  55. import org.openide.DialogDisplayer;
  56. import org.openide.NotifyDescriptor;
  57. import org.openide.util.NbBundle;
  58. import org.openide.util.NbPreferences;
  59. import org.openide.util.NetworkSettings;
  60. /** Global password protected sites Authenticator for IDE
  61. *
  62. * @author Ludo, Petr Hrebejk
  63. */
  64. public class AdminAuthenticator extends java.net.Authenticator {
  65. private SunDeploymentManagerInterface preferredSunDeploymentManagerInterface;
  66. private boolean displayed = false;
  67. private static final long TIMEOUT = 3000;
  68. private static long lastTry = 0;
  69. public AdminAuthenticator() {
  70. preferredSunDeploymentManagerInterface = null;
  71. Preferences proxySettingsNode = NbPreferences.root().node("/org/netbeans/core"); //NOI18N
  72. assert proxySettingsNode != null;
  73. }
  74. public AdminAuthenticator(SunDeploymentManagerInterface dm) {
  75. this();
  76. preferredSunDeploymentManagerInterface = dm;
  77. }
  78. @Override
  79. protected java.net.PasswordAuthentication getPasswordAuthentication() {
  80. Logger.getLogger(AdminAuthenticator.class.getName()).log(Level.FINER, "AdminAuthenticator.getPasswordAuthentication() with prompt " + this.getRequestingPrompt()); //NOI18N
  81. String title = getRequestingPrompt();
  82. // if the request is coming from a proxy and the IDE has proxy auth values
  83. // send them straight back...
  84. if (RequestorType.PROXY == getRequestorType() && ProxySettings.useAuthentication()) {
  85. Logger.getLogger(AdminAuthenticator.class.getName()).log(Level.FINER, "Username set to " + ProxySettings.getAuthenticationUsername() + " while request " + this.getRequestingURL()); //NOI18N
  86. return new java.net.PasswordAuthentication(ProxySettings.getAuthenticationUsername(), ProxySettings.getAuthenticationPassword());
  87. } else {
  88. // if we haven't been called in the last three seconds...
  89. //
  90. if (System.currentTimeMillis() - lastTry > TIMEOUT) {
  91. // if anything other than the GF AS is asking for authentication
  92. // make it possible to see the dialog
  93. if (!"admin-realm".equals(title)) { // NOI18N
  94. displayed = false;
  95. }
  96. if (!displayed && !NetworkSettings.isAuthenticationDialogSuppressed()) {
  97. // try to prevent the dialog from popping up too often... since the
  98. // plugin sends a bunch of requests to the AS one after the other to try to
  99. // populate its node tree.
  100. //
  101. displayed = true;
  102. java.net.InetAddress site = getRequestingSite();
  103. ResourceBundle bundle = NbBundle.getBundle(AdminAuthenticator.class);
  104. String host = site == null ? bundle.getString("CTL_PasswordProtected") : site.getHostName(); // NOI18N
  105. InstanceProperties ip = null;
  106. String keyURI;
  107. String name = ""; // NOI18N
  108. if ("admin-realm".equals(title)) { // NOI18N so this is a request from the sun server
  109. if (preferredSunDeploymentManagerInterface != null) {
  110. //Make sure this is really the admin port and not the app port (see bug 85605
  111. if (preferredSunDeploymentManagerInterface.getPort() == getRequestingPort()) {
  112. ip = SunURIManager.getInstanceProperties(
  113. preferredSunDeploymentManagerInterface.getPlatformRoot(),
  114. preferredSunDeploymentManagerInterface.getHost(),
  115. preferredSunDeploymentManagerInterface.getPort());
  116. }
  117. }
  118. }
  119. // fill in values if we have them
  120. //
  121. if (ip != null) {
  122. title = ip.getProperty(InstanceProperties.DISPLAY_NAME_ATTR);
  123. name = ip.getProperty(InstanceProperties.USERNAME_ATTR);
  124. }
  125. PasswordPanel passwordPanel = new PasswordPanel(name);
  126. DialogDescriptor dd = new DialogDescriptor(passwordPanel, host);
  127. passwordPanel.setPrompt(title);
  128. java.awt.Dialog dialog = DialogDisplayer.getDefault().createDialog(dd);
  129. dialog.setVisible(true);
  130. if (dd.getValue().equals(NotifyDescriptor.OK_OPTION)) {
  131. // try to update info for the node...
  132. if (ip != null) {
  133. String oldpass = Arrays.toString(Keyring.read(ip.getProperty(InstanceProperties.URL_ATTR)));
  134. String oldname = ip.getProperty(InstanceProperties.USERNAME_ATTR);
  135. ip.setProperty(InstanceProperties.USERNAME_ATTR, passwordPanel.getUsername());
  136. ip.setProperty(InstanceProperties.PASSWORD_ATTR, passwordPanel.getTPassword());
  137. if (preferredSunDeploymentManagerInterface != null) {
  138. preferredSunDeploymentManagerInterface.setUserName(passwordPanel.getUsername());
  139. preferredSunDeploymentManagerInterface.setPassword(passwordPanel.getTPassword());
  140. }
  141. ip.refreshServerInstance();
  142. // this data should not be allowed to trickle back to the filesystem.
  143. ip.setProperty(InstanceProperties.PASSWORD_ATTR, oldpass);
  144. ip.setProperty(InstanceProperties.USERNAME_ATTR, oldname);
  145. }
  146. lastTry = System.currentTimeMillis();
  147. return new java.net.PasswordAuthentication(passwordPanel.getUsername(), passwordPanel.getPassword());
  148. } else {
  149. // cancelled... so just update the delay stamp.
  150. lastTry = System.currentTimeMillis();
  151. }
  152. } // fi !displayed
  153. } // fi time check...
  154. }
  155. Logger.getLogger(AdminAuthenticator.class.getName()).log(Level.WARNING, "No authentication set while requesting " + this.getRequestingURL()); //NOI18N
  156. return null;
  157. }
  158. /** Inner class for JPanel with Username & Password fields */
  159. static class PasswordPanel extends javax.swing.JPanel {
  160. private static final int DEFAULT_WIDTH = 200;
  161. private static final int DEFAULT_HEIGHT = 0;
  162. /** Generated serialVersionUID */
  163. static final long serialVersionUID = 1555749205340031767L;
  164. ResourceBundle bundle = org.openide.util.NbBundle.getBundle(AdminAuthenticator.class);
  165. /** Creates new form PasswordPanel */
  166. public PasswordPanel(String userName) {
  167. initComponents();
  168. usernameField.setText(userName);
  169. usernameField.setSelectionStart(0);
  170. usernameField.setSelectionEnd(userName.length());
  171. usernameField.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_UserNameField"));
  172. passwordField.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_PasswordField"));
  173. }
  174. @Override
  175. public java.awt.Dimension getPreferredSize() {
  176. java.awt.Dimension sup = super.getPreferredSize();
  177. return new java.awt.Dimension(Math.max(sup.width, DEFAULT_WIDTH), Math.max(sup.height, DEFAULT_HEIGHT));
  178. }
  179. /** This method is called from within the constructor to
  180. * initialize the form.
  181. * WARNING: Do NOT modify this code. The content of this method is
  182. * always regenerated by the FormEditor.
  183. */
  184. private void initComponents() {
  185. setLayout(new java.awt.BorderLayout());
  186. mainPanel = new javax.swing.JPanel();
  187. mainPanel.setLayout(new java.awt.GridBagLayout());
  188. java.awt.GridBagConstraints gridBagConstraints1;
  189. mainPanel.setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(12, 12, 0, 11)));
  190. promptLabel = new javax.swing.JLabel();
  191. promptLabel.setHorizontalAlignment(0);
  192. gridBagConstraints1 = new java.awt.GridBagConstraints();
  193. gridBagConstraints1.gridwidth = 0;
  194. gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
  195. gridBagConstraints1.insets = new java.awt.Insets(0, 0, 6, 0);
  196. mainPanel.add(promptLabel, gridBagConstraints1);
  197. jLabel1 = new javax.swing.JLabel();
  198. org.openide.awt.Mnemonics.setLocalizedText(jLabel1,
  199. bundle.getString("LAB_AUTH_User_Name")); // NOI18N
  200. gridBagConstraints1 = new java.awt.GridBagConstraints();
  201. gridBagConstraints1.insets = new java.awt.Insets(0, 0, 5, 12);
  202. gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
  203. mainPanel.add(jLabel1, gridBagConstraints1);
  204. usernameField = new javax.swing.JTextField();
  205. usernameField.setMinimumSize(new java.awt.Dimension(70, 20));
  206. usernameField.setPreferredSize(new java.awt.Dimension(70, 20));
  207. jLabel1.setLabelFor(usernameField);
  208. gridBagConstraints1 = new java.awt.GridBagConstraints();
  209. gridBagConstraints1.gridwidth = 0;
  210. gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
  211. gridBagConstraints1.insets = new java.awt.Insets(0, 0, 5, 0);
  212. gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
  213. gridBagConstraints1.weightx = 1.0;
  214. mainPanel.add(usernameField, gridBagConstraints1);
  215. jLabel2 = new javax.swing.JLabel();
  216. org.openide.awt.Mnemonics.setLocalizedText(jLabel2,
  217. bundle.getString("LAB_AUTH_Password")); // NOI18N
  218. gridBagConstraints1 = new java.awt.GridBagConstraints();
  219. gridBagConstraints1.insets = new java.awt.Insets(0, 0, 0, 12);
  220. gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
  221. mainPanel.add(jLabel2, gridBagConstraints1);
  222. passwordField = new javax.swing.JPasswordField();
  223. passwordField.setMinimumSize(new java.awt.Dimension(70, 20));
  224. passwordField.setPreferredSize(new java.awt.Dimension(70, 20));
  225. jLabel2.setLabelFor(passwordField);
  226. gridBagConstraints1 = new java.awt.GridBagConstraints();
  227. gridBagConstraints1.gridwidth = 0;
  228. gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
  229. gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
  230. gridBagConstraints1.weightx = 1.0;
  231. mainPanel.add(passwordField, gridBagConstraints1);
  232. add(mainPanel, "Center"); // NOI18N
  233. }
  234. // Variables declaration - do not modify//GEN-BEGIN:variables
  235. private javax.swing.JPanel mainPanel;
  236. private javax.swing.JLabel promptLabel;
  237. private javax.swing.JLabel jLabel1;
  238. private javax.swing.JTextField usernameField;
  239. private javax.swing.JLabel jLabel2;
  240. private javax.swing.JPasswordField passwordField;
  241. // End of variables declaration//GEN-END:variables
  242. String getUsername() {
  243. return usernameField.getText();
  244. }
  245. char[] getPassword() {
  246. return passwordField.getPassword();
  247. }
  248. String getTPassword() {
  249. return passwordField.getText();
  250. }
  251. void setPrompt(String prompt) {
  252. if (prompt == null) {
  253. promptLabel.setVisible(false);
  254. getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_NbAuthenticatorPasswordPanel"));
  255. } else {
  256. promptLabel.setVisible(true);
  257. promptLabel.setText(prompt);
  258. getAccessibleContext().setAccessibleDescription(prompt);
  259. }
  260. }
  261. }
  262. }