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     AppletProps() {
  48         setTitle(amh.getMessage("title"));
  49         Panel p = new Panel();
  50         p.setLayout(new GridLayout(0, 2));
  51 
  52         p.add(new Label(amh.getMessage("label.http.server", "Http proxy server:")));
  53         p.add(proxyHost = new TextField());
  54 
  55         p.add(new Label(amh.getMessage("label.http.proxy")));
  56         p.add(proxyPort = new TextField());
  57 
  58         p.add(new Label(amh.getMessage("label.class")));
  59         p.add(accessMode = new Choice());
  60         accessMode.addItem(amh.getMessage("choice.class.item.restricted"));
  61         accessMode.addItem(amh.getMessage("choice.class.item.unrestricted"));
  62 
  63         add("Center", p);
  64         p = new Panel();
  65         p.add(new Button(amh.getMessage("button.apply")));
  66         p.add(new Button(amh.getMessage("button.reset")));
  67         p.add(new Button(amh.getMessage("button.cancel")));
  68         add("South", p);
  69         move(200, 150);
  70         pack();
  71         reset();
  72     }
  73 
  74     void reset() {
  75         AppletSecurity security = (AppletSecurity) System.getSecurityManager();
  76         if (security != null)
  77             security.reset();
  78 
  79         String proxyhost = AccessController.doPrivileged(
  80                 new GetPropertyAction("http.proxyHost"));
  81         String proxyport = AccessController.doPrivileged(
  82                 new GetPropertyAction("http.proxyPort"));
  83 
  84         Boolean tmp = AccessController.doPrivileged(
  85                 new GetBooleanAction("package.restrict.access.sun"));
  86 
  87         boolean packageRestrict = tmp.booleanValue();
  88         if (packageRestrict) {
  89            accessMode.select(amh.getMessage("choice.class.item.restricted"));
  90         } else {
  91            accessMode.select(amh.getMessage("choice.class.item.unrestricted"));
  92         }
  93 
  94         if (proxyhost != null) {
  95             proxyHost.setText(proxyhost);
  96             proxyPort.setText(proxyport);
  97         } else {
  98             proxyHost.setText("");
  99             proxyPort.setText("");
 100         }
 101     }
 102 
 103     void apply() {
 104         String proxyHostValue = proxyHost.getText().trim();
 105         String proxyPortValue = proxyPort.getText().trim();
 106 
 107         // Get properties
 108         final Properties props = AccessController.doPrivileged(
 109              new PrivilegedAction<Properties>() {
 110                  public Properties run() {
 111                      return System.getProperties();
 112                  }
 113         });
 114 
 115         if (proxyHostValue.length() != 0) {
 116             /* 4066402 */
 117             /* Check for parsable value in proxy port number field before */
 118             /* applying. Display warning to user until parsable value is  */
 119             /* entered. */
 120             int proxyPortNumber = 0;
 121             try {
 122                 proxyPortNumber = Integer.parseInt(proxyPortValue);
 123             } catch (NumberFormatException e) {}
 124 
 125             if (proxyPortNumber <= 0) {
 126                 proxyPort.selectAll();
 127                 proxyPort.requestFocus();
 128                 (new AppletPropsErrorDialog(this,
 129                                             amh.getMessage("title.invalidproxy"),
 130                                             amh.getMessage("label.invalidproxy"),
 131                                             amh.getMessage("button.ok"))).show();
 132                 return;
 133             }
 134             /* end 4066402 */
 135 
 136             props.put("http.proxyHost", proxyHostValue);
 137             props.put("http.proxyPort", proxyPortValue);
 138         } else {
 139             props.put("http.proxyHost", "");
 140         }
 141 
 142         if (amh.getMessage("choice.class.item.restricted").equals(accessMode.getSelectedItem())) {
 143             props.put("package.restrict.access.sun", "true");
 144         } else {
 145             props.put("package.restrict.access.sun", "false");
 146         }
 147 
 148         // Save properties
 149         try {
 150             reset();
 151             AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
 152                 public Object run() throws IOException {
 153                     File dotAV = Main.theUserPropertiesFile;
 154                     FileOutputStream out = new FileOutputStream(dotAV);
 155                     Properties avProps = new Properties();
 156                     for (int i = 0; i < Main.avDefaultUserProps.length; i++) {
 157                         String avKey = Main.avDefaultUserProps[i][0];
 158                         avProps.setProperty(avKey, props.getProperty(avKey));
 159                     }
 160                     avProps.store(out, amh.getMessage("prop.store"));
 161                     out.close();
 162                     return null;
 163                 }
 164             });
 165             hide();
 166         } catch (java.security.PrivilegedActionException e) {
 167             System.out.println(amh.getMessage("apply.exception",
 168                                               e.getException()));
 169             // XXX what's the general feeling on stack traces to System.out?
 170             e.printStackTrace();
 171             reset();
 172         }
 173     }
 174 
 175     public boolean action(Event evt, Object obj) {
 176         if (amh.getMessage("button.apply").equals(obj)) {
 177             apply();
 178             return true;
 179         }
 180         if (amh.getMessage("button.reset").equals(obj)) {
 181             reset();
 182             return true;
 183         }
 184         if (amh.getMessage("button.cancel").equals(obj)) {
 185             reset();
 186             hide();
 187             return true;
 188         }
 189         return false;
 190     }
 191 
 192     private static AppletMessageHandler amh = new AppletMessageHandler("appletprops");
 193 
 194 }
 195 
 196 /* 4066432 */
 197 /* Dialog class to display property-related errors to user */
 198 @SuppressWarnings("serial") // JDK implementation class
 199 class AppletPropsErrorDialog extends Dialog {
 200     public AppletPropsErrorDialog(Frame parent, String title, String message,
 201                 String buttonText) {
 202         super(parent, title, true);
 203         Panel p = new Panel();
 204         add("Center", new Label(message));
 205         p.add(new Button(buttonText));
 206         add("South", p);
 207         pack();
 208 
 209         Dimension dDim = size();
 210         Rectangle fRect = parent.bounds();
 211         move(fRect.x + ((fRect.width - dDim.width) / 2),
 212              fRect.y + ((fRect.height - dDim.height) / 2));
 213     }
 214 
 215     public boolean action(Event event, Object object) {
 216         hide();
 217         dispose();
 218         return true;
 219     }
 220 }
 221 
 222 /* end 4066432 */