1 /*
   2  * Copyright (c) 1995, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.applet;
  27 
  28 import java.awt.*;
  29 import java.io.*;
  30 import java.util.Properties;
  31 import sun.net.www.http.HttpClient;
  32 import sun.net.ftp.FtpClient;
  33 import java.security.AccessController;
  34 import java.security.PrivilegedAction;
  35 import java.security.PrivilegedExceptionAction;
  36 import java.security.PrivilegedActionException;
  37 
  38 import sun.security.action.*;
  39 
  40 @SuppressWarnings("serial") // JDK implementation class
  41 class AppletProps extends Frame {
  42 
  43     TextField proxyHost;
  44     TextField proxyPort;
  45     Choice accessMode;
  46 
  47     @SuppressWarnings("deprecation")
  48     AppletProps() {
  49         setTitle(amh.getMessage("title"));
  50         Panel p = new Panel();
  51         p.setLayout(new GridLayout(0, 2));
  52 
  53         p.add(new Label(amh.getMessage("label.http.server", "Http proxy server:")));
  54         p.add(proxyHost = new TextField());
  55 
  56         p.add(new Label(amh.getMessage("label.http.proxy")));
  57         p.add(proxyPort = new TextField());
  58 
  59         p.add(new Label(amh.getMessage("label.class")));
  60         p.add(accessMode = new Choice());
  61         accessMode.addItem(amh.getMessage("choice.class.item.restricted"));
  62         accessMode.addItem(amh.getMessage("choice.class.item.unrestricted"));
  63 
  64         add("Center", p);
  65         p = new Panel();
  66         p.add(new Button(amh.getMessage("button.apply")));
  67         p.add(new Button(amh.getMessage("button.reset")));
  68         p.add(new Button(amh.getMessage("button.cancel")));
  69         add("South", p);
  70         move(200, 150);
  71         pack();
  72         reset();
  73     }
  74 
  75     void reset() {
  76         AppletSecurity security = (AppletSecurity) System.getSecurityManager();
  77         if (security != null)
  78             security.reset();
  79 
  80         String proxyhost = AccessController.doPrivileged(
  81                 new GetPropertyAction("http.proxyHost"));
  82         String proxyport = AccessController.doPrivileged(
  83                 new GetPropertyAction("http.proxyPort"));
  84 
  85         Boolean tmp = AccessController.doPrivileged(
  86                 new GetBooleanAction("package.restrict.access.sun"));
  87 
  88         boolean packageRestrict = tmp.booleanValue();
  89         if (packageRestrict) {
  90            accessMode.select(amh.getMessage("choice.class.item.restricted"));
  91         } else {
  92            accessMode.select(amh.getMessage("choice.class.item.unrestricted"));
  93         }
  94 
  95         if (proxyhost != null) {
  96             proxyHost.setText(proxyhost);
  97             proxyPort.setText(proxyport);
  98         } else {
  99             proxyHost.setText("");
 100             proxyPort.setText("");
 101         }
 102     }
 103 
 104     @SuppressWarnings("deprecation")
 105     void apply() {
 106         String proxyHostValue = proxyHost.getText().trim();
 107         String proxyPortValue = proxyPort.getText().trim();
 108 
 109         // Get properties
 110         final Properties props = AccessController.doPrivileged(
 111              new PrivilegedAction<Properties>() {
 112                  public Properties run() {
 113                      return System.getProperties();
 114                  }
 115         });
 116 
 117         if (proxyHostValue.length() != 0) {
 118             /* 4066402 */
 119             /* Check for parsable value in proxy port number field before */
 120             /* applying. Display warning to user until parsable value is  */
 121             /* entered. */
 122             int proxyPortNumber = 0;
 123             try {
 124                 proxyPortNumber = Integer.parseInt(proxyPortValue);
 125             } catch (NumberFormatException e) {}
 126 
 127             if (proxyPortNumber <= 0) {
 128                 proxyPort.selectAll();
 129                 proxyPort.requestFocus();
 130                 (new AppletPropsErrorDialog(this,
 131                                             amh.getMessage("title.invalidproxy"),
 132                                             amh.getMessage("label.invalidproxy"),
 133                                             amh.getMessage("button.ok"))).show();
 134                 return;
 135             }
 136             /* end 4066402 */
 137 
 138             props.put("http.proxyHost", proxyHostValue);
 139             props.put("http.proxyPort", proxyPortValue);
 140         } else {
 141             props.put("http.proxyHost", "");
 142         }
 143 
 144         if (amh.getMessage("choice.class.item.restricted").equals(accessMode.getSelectedItem())) {
 145             props.put("package.restrict.access.sun", "true");
 146         } else {
 147             props.put("package.restrict.access.sun", "false");
 148         }
 149 
 150         // Save properties
 151         try {
 152             reset();
 153             AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
 154                 public Object run() throws IOException {
 155                     File dotAV = Main.theUserPropertiesFile;
 156                     FileOutputStream out = new FileOutputStream(dotAV);
 157                     Properties avProps = new Properties();
 158                     for (int i = 0; i < Main.avDefaultUserProps.length; i++) {
 159                         String avKey = Main.avDefaultUserProps[i][0];
 160                         avProps.setProperty(avKey, props.getProperty(avKey));
 161                     }
 162                     avProps.store(out, amh.getMessage("prop.store"));
 163                     out.close();
 164                     return null;
 165                 }
 166             });
 167             hide();
 168         } catch (java.security.PrivilegedActionException e) {
 169             System.out.println(amh.getMessage("apply.exception",
 170                                               e.getException()));
 171             // XXX what's the general feeling on stack traces to System.out?
 172             e.printStackTrace();
 173             reset();
 174         }
 175     }
 176 
 177     @SuppressWarnings("deprecation")
 178     public boolean action(Event evt, Object obj) {
 179         if (amh.getMessage("button.apply").equals(obj)) {
 180             apply();
 181             return true;
 182         }
 183         if (amh.getMessage("button.reset").equals(obj)) {
 184             reset();
 185             return true;
 186         }
 187         if (amh.getMessage("button.cancel").equals(obj)) {
 188             reset();
 189             hide();
 190             return true;
 191         }
 192         return false;
 193     }
 194 
 195     private static AppletMessageHandler amh = new AppletMessageHandler("appletprops");
 196 
 197 }
 198 
 199 /* 4066432 */
 200 /* Dialog class to display property-related errors to user */
 201 @SuppressWarnings("serial") // JDK implementation class
 202 class AppletPropsErrorDialog extends Dialog {
 203     @SuppressWarnings("deprecation")
 204     public AppletPropsErrorDialog(Frame parent, String title, String message,
 205                 String buttonText) {
 206         super(parent, title, true);
 207         Panel p = new Panel();
 208         add("Center", new Label(message));
 209         p.add(new Button(buttonText));
 210         add("South", p);
 211         pack();
 212 
 213         Dimension dDim = size();
 214         Rectangle fRect = parent.bounds();
 215         move(fRect.x + ((fRect.width - dDim.width) / 2),
 216              fRect.y + ((fRect.height - dDim.height) / 2));
 217     }
 218 
 219     @SuppressWarnings("deprecation")
 220     public boolean action(Event event, Object object) {
 221         hide();
 222         dispose();
 223         return true;
 224     }
 225 }
 226 
 227 /* end 4066432 */