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 java.security.AccessController;
  32 import java.security.PrivilegedAction;
  33 import java.security.PrivilegedExceptionAction;
  34 import java.security.PrivilegedActionException;
  35 
  36 import sun.security.action.*;
  37 
  38 @SuppressWarnings("serial") // JDK implementation class
  39 class AppletProps extends Frame {
  40 
  41     TextField proxyHost;
  42     TextField proxyPort;
  43     Choice accessMode;
  44 
  45     @SuppressWarnings("deprecation")
  46     AppletProps() {
  47         setTitle(amh.getMessage("title"));
  48         Panel p = new Panel();
  49         p.setLayout(new GridLayout(0, 2));
  50 
  51         p.add(new Label(amh.getMessage("label.http.server", "Http proxy server:")));
  52         p.add(proxyHost = new TextField());
  53 
  54         p.add(new Label(amh.getMessage("label.http.proxy")));
  55         p.add(proxyPort = new TextField());
  56 
  57         p.add(new Label(amh.getMessage("label.class")));
  58         p.add(accessMode = new Choice());
  59         accessMode.addItem(amh.getMessage("choice.class.item.restricted"));
  60         accessMode.addItem(amh.getMessage("choice.class.item.unrestricted"));
  61 
  62         add("Center", p);
  63         p = new Panel();
  64         p.add(new Button(amh.getMessage("button.apply")));
  65         p.add(new Button(amh.getMessage("button.reset")));
  66         p.add(new Button(amh.getMessage("button.cancel")));
  67         add("South", p);
  68         move(200, 150);
  69         pack();
  70         reset();
  71     }
  72 
  73     void reset() {
  74         AppletSecurity security = (AppletSecurity) System.getSecurityManager();
  75         if (security != null)
  76             security.reset();
  77 
  78         String proxyhost = AccessController.doPrivileged(
  79                 new GetPropertyAction("http.proxyHost"));
  80         String proxyport = AccessController.doPrivileged(
  81                 new GetPropertyAction("http.proxyPort"));
  82 
  83         Boolean tmp = AccessController.doPrivileged(
  84                 new GetBooleanAction("package.restrict.access.sun"));
  85 
  86         boolean packageRestrict = tmp.booleanValue();
  87         if (packageRestrict) {
  88            accessMode.select(amh.getMessage("choice.class.item.restricted"));
  89         } else {
  90            accessMode.select(amh.getMessage("choice.class.item.unrestricted"));
  91         }
  92 
  93         if (proxyhost != null) {
  94             proxyHost.setText(proxyhost);
  95             proxyPort.setText(proxyport);
  96         } else {
  97             proxyHost.setText("");
  98             proxyPort.setText("");
  99         }
 100     }
 101 
 102     @SuppressWarnings("deprecation")
 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     @SuppressWarnings("deprecation")
 176     public boolean action(Event evt, Object obj) {
 177         if (amh.getMessage("button.apply").equals(obj)) {
 178             apply();
 179             return true;
 180         }
 181         if (amh.getMessage("button.reset").equals(obj)) {
 182             reset();
 183             return true;
 184         }
 185         if (amh.getMessage("button.cancel").equals(obj)) {
 186             reset();
 187             hide();
 188             return true;
 189         }
 190         return false;
 191     }
 192 
 193     private static AppletMessageHandler amh = new AppletMessageHandler("appletprops");
 194 
 195 }
 196 
 197 /* 4066432 */
 198 /* Dialog class to display property-related errors to user */
 199 @SuppressWarnings("serial") // JDK implementation class
 200 class AppletPropsErrorDialog extends Dialog {
 201     @SuppressWarnings("deprecation")
 202     public AppletPropsErrorDialog(Frame parent, String title, String message,
 203                 String buttonText) {
 204         super(parent, title, true);
 205         Panel p = new Panel();
 206         add("Center", new Label(message));
 207         p.add(new Button(buttonText));
 208         add("South", p);
 209         pack();
 210 
 211         Dimension dDim = size();
 212         Rectangle fRect = parent.bounds();
 213         move(fRect.x + ((fRect.width - dDim.width) / 2),
 214              fRect.y + ((fRect.height - dDim.height) / 2));
 215     }
 216 
 217     @SuppressWarnings("deprecation")
 218     public boolean action(Event event, Object object) {
 219         hide();
 220         dispose();
 221         return true;
 222     }
 223 }
 224 
 225 /* end 4066432 */