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
26package sun.applet;
27
28import java.awt.*;
29import java.io.*;
30import java.util.Properties;
31import java.security.AccessController;
32import java.security.PrivilegedAction;
33import java.security.PrivilegedExceptionAction;
34import java.security.PrivilegedActionException;
35
36import sun.security.action.*;
37
38@SuppressWarnings("serial") // JDK implementation class
39@Deprecated(since = "9")
40class AppletProps extends Frame {
41
42    TextField proxyHost;
43    TextField proxyPort;
44    Choice accessMode;
45
46    @SuppressWarnings("deprecation")
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    @SuppressWarnings("deprecation")
104    void apply() {
105        String proxyHostValue = proxyHost.getText().trim();
106        String proxyPortValue = proxyPort.getText().trim();
107
108        // Get properties
109        final Properties props = AccessController.doPrivileged(
110             new PrivilegedAction<Properties>() {
111                 public Properties run() {
112                     return System.getProperties();
113                 }
114        });
115
116        if (proxyHostValue.length() != 0) {
117            /* 4066402 */
118            /* Check for parsable value in proxy port number field before */
119            /* applying. Display warning to user until parsable value is  */
120            /* entered. */
121            int proxyPortNumber = 0;
122            try {
123                proxyPortNumber = Integer.parseInt(proxyPortValue);
124            } catch (NumberFormatException e) {}
125
126            if (proxyPortNumber <= 0) {
127                proxyPort.selectAll();
128                proxyPort.requestFocus();
129                (new AppletPropsErrorDialog(this,
130                                            amh.getMessage("title.invalidproxy"),
131                                            amh.getMessage("label.invalidproxy"),
132                                            amh.getMessage("button.ok"))).show();
133                return;
134            }
135            /* end 4066402 */
136
137            props.put("http.proxyHost", proxyHostValue);
138            props.put("http.proxyPort", proxyPortValue);
139        } else {
140            props.put("http.proxyHost", "");
141        }
142
143        if (amh.getMessage("choice.class.item.restricted").equals(accessMode.getSelectedItem())) {
144            props.put("package.restrict.access.sun", "true");
145        } else {
146            props.put("package.restrict.access.sun", "false");
147        }
148
149        // Save properties
150        try {
151            reset();
152            AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
153                public Object run() throws IOException {
154                    File dotAV = Main.theUserPropertiesFile;
155                    FileOutputStream out = new FileOutputStream(dotAV);
156                    Properties avProps = new Properties();
157                    for (int i = 0; i < Main.avDefaultUserProps.length; i++) {
158                        String avKey = Main.avDefaultUserProps[i][0];
159                        avProps.setProperty(avKey, props.getProperty(avKey));
160                    }
161                    avProps.store(out, amh.getMessage("prop.store"));
162                    out.close();
163                    return null;
164                }
165            });
166            hide();
167        } catch (java.security.PrivilegedActionException e) {
168            System.out.println(amh.getMessage("apply.exception",
169                                              e.getException()));
170            // XXX what's the general feeling on stack traces to System.out?
171            e.printStackTrace();
172            reset();
173        }
174    }
175
176    @SuppressWarnings("deprecation")
177    public boolean action(Event evt, Object obj) {
178        if (amh.getMessage("button.apply").equals(obj)) {
179            apply();
180            return true;
181        }
182        if (amh.getMessage("button.reset").equals(obj)) {
183            reset();
184            return true;
185        }
186        if (amh.getMessage("button.cancel").equals(obj)) {
187            reset();
188            hide();
189            return true;
190        }
191        return false;
192    }
193
194    private static AppletMessageHandler amh = new AppletMessageHandler("appletprops");
195
196}
197
198/* 4066432 */
199/* Dialog class to display property-related errors to user */
200@SuppressWarnings("serial") // JDK implementation class
201@Deprecated(since = "9")
202class 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 */
228