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 class AppletProps extends Frame {
  41 
  42     TextField proxyHost;
  43     TextField proxyPort;
  44     Choice accessMode;
  45 
  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     void apply() {
 103         String proxyHostValue = proxyHost.getText().trim();
 104         String proxyPortValue = proxyPort.getText().trim();
 105 
 106         // Get properties
 107         final Properties props = (Properties) AccessController.doPrivileged(
 108              new PrivilegedAction() {
 109                  public Object run() {
 110                      return System.getProperties();
 111                  }
 112         });
 113 
 114         if (proxyHostValue.length() != 0) {
 115             /* 4066402 */
 116             /* Check for parsable value in proxy port number field before */
 117             /* applying. Display warning to user until parsable value is  */
 118             /* entered. */
 119             int proxyPortNumber = 0;
 120             try {
 121                 proxyPortNumber = Integer.parseInt(proxyPortValue);
 122             } catch (NumberFormatException e) {}
 123 
 124             if (proxyPortNumber <= 0) {
 125                 proxyPort.selectAll();
 126                 proxyPort.requestFocus();
 127                 (new AppletPropsErrorDialog(this,
 128                                             amh.getMessage("title.invalidproxy"),
 129                                             amh.getMessage("label.invalidproxy"),
 130                                             amh.getMessage("button.ok"))).show();
 131                 return;
 132             }
 133             /* end 4066402 */
 134 
 135             props.put("http.proxyHost", proxyHostValue);
 136             props.put("http.proxyPort", proxyPortValue);
 137         } else {
 138             props.put("http.proxyHost", "");
 139         }
 140 
 141         if (amh.getMessage("choice.class.item.restricted").equals(accessMode.getSelectedItem())) {
 142             props.put("package.restrict.access.sun", "true");
 143         } else {
 144             props.put("package.restrict.access.sun", "false");
 145         }
 146 
 147         // Save properties
 148         try {
 149             reset();
 150             AccessController.doPrivileged(new PrivilegedExceptionAction() {
 151                 public Object run() throws IOException {
 152                     File dotAV = Main.theUserPropertiesFile;
 153                     FileOutputStream out = new FileOutputStream(dotAV);
 154                     Properties avProps = new Properties();
 155                     for (int i = 0; i < Main.avDefaultUserProps.length; i++) {
 156                         String avKey = Main.avDefaultUserProps[i][0];
 157                         avProps.setProperty(avKey, props.getProperty(avKey));
 158                     }
 159                     avProps.store(out, amh.getMessage("prop.store"));
 160                     out.close();
 161                     return null;
 162                 }
 163             });
 164             hide();
 165         } catch (java.security.PrivilegedActionException e) {
 166             System.out.println(amh.getMessage("apply.exception",
 167                                               e.getException()));
 168             // XXX what's the general feeling on stack traces to System.out?
 169             e.printStackTrace();
 170             reset();
 171         }
 172     }
 173 
 174     public boolean action(Event evt, Object obj) {
 175         if (amh.getMessage("button.apply").equals(obj)) {
 176             apply();
 177             return true;
 178         }
 179         if (amh.getMessage("button.reset").equals(obj)) {
 180             reset();
 181             return true;
 182         }
 183         if (amh.getMessage("button.cancel").equals(obj)) {
 184             reset();
 185             hide();
 186             return true;
 187         }
 188         return false;
 189     }
 190 
 191     private static AppletMessageHandler amh = new AppletMessageHandler("appletprops");
 192 
 193 }
 194 
 195 /* 4066432 */
 196 /* Dialog class to display property-related errors to user */
 197 
 198 class AppletPropsErrorDialog extends Dialog {
 199     public AppletPropsErrorDialog(Frame parent, String title, String message,
 200                 String buttonText) {
 201         super(parent, title, true);
 202         Panel p = new Panel();
 203         add("Center", new Label(message));
 204         p.add(new Button(buttonText));
 205         add("South", p);
 206         pack();
 207 
 208         Dimension dDim = size();
 209         Rectangle fRect = parent.bounds();
 210         move(fRect.x + ((fRect.width - dDim.width) / 2),
 211              fRect.y + ((fRect.height - dDim.height) / 2));
 212     }
 213 
 214     public boolean action(Event event, Object object) {
 215         hide();
 216         dispose();
 217         return true;
 218     }
 219 }
 220 
 221 /* end 4066432 */