001: /*
002: * Copyright 2006-2007 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt.
007: *
008: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
009: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
010: * the license for the specific language governing your rights and limitations.
011: *
012: * Additional Contributor(s): Martin Schmid gridvision engineering GmbH
013: */
014: package org.pentaho.reportdesigner.crm.report.settings;
015:
016: import org.gjt.xpp.XmlPullNode;
017: import org.jetbrains.annotations.NotNull;
018: import org.jetbrains.annotations.Nullable;
019: import org.pentaho.reportdesigner.crm.report.PropertyKeys;
020: import org.pentaho.reportdesigner.lib.common.xml.XMLContext;
021: import org.pentaho.reportdesigner.lib.common.xml.XMLExternalizable;
022: import org.pentaho.reportdesigner.lib.common.xml.XMLWriter;
023:
024: import java.io.IOException;
025:
026: /**
027: * User: Martin
028: * Date: 03.03.2006
029: * Time: 14:12:13
030: */
031: public class ProxySettings implements XMLExternalizable {
032: @NotNull
033: private static final String LOCALHOST = "localhost";
034:
035: @NotNull
036: private ProxyType proxyType;
037: @NotNull
038: private String httpProxyHost;
039: @NotNull
040: private String httpProxyPort;
041:
042: private boolean useSocksProxy;
043: @NotNull
044: private String socksProxyPort;
045: @NotNull
046: private String socksProxyHost;
047:
048: @NotNull
049: private String proxyUser;
050: @NotNull
051: private String proxyPassword;
052:
053: public ProxySettings() {
054: proxyType = ProxyType.AUTO_DETECT_PROXY;
055: String httpProxyHost = System
056: .getProperty(PropertyKeys.HTTP_DOT_PROXY_HOST);
057: String httpProxyPort = System
058: .getProperty(PropertyKeys.HTTP_DOT_PROXY_PORT);
059:
060: if (httpProxyHost == null) {
061: this .httpProxyHost = "";
062: } else {
063: this .httpProxyHost = httpProxyHost;
064: }
065:
066: if (httpProxyPort == null) {
067: this .httpProxyPort = "";
068: } else {
069: this .httpProxyPort = httpProxyPort;
070: }
071:
072: socksProxyHost = "";
073: socksProxyPort = "";
074:
075: proxyUser = "";
076: proxyPassword = "";
077: }
078:
079: @NotNull
080: public ProxyType getProxyType() {
081: return proxyType;
082: }
083:
084: public void setProxyType(@NotNull
085: ProxyType proxyType) {
086: //noinspection ConstantConditions
087: if (proxyType == null) {
088: throw new IllegalArgumentException(
089: "proxyType must not be null");
090: }
091:
092: this .proxyType = proxyType;
093: }
094:
095: @NotNull
096: public String getHTTPProxyHost() {
097: return httpProxyHost;
098: }
099:
100: public void setHTTPProxyHost(@NotNull
101: String httpProxyHost) {
102: //noinspection ConstantConditions
103: if (httpProxyHost == null) {
104: throw new IllegalArgumentException(
105: "httpProxyHost must not be null");
106: }
107:
108: this .httpProxyHost = httpProxyHost;
109: }
110:
111: @NotNull
112: public String getHTTPProxyPort() {
113: return httpProxyPort;
114: }
115:
116: public void setHTTPProxyPort(@NotNull
117: String httpProxyPort) {
118: //noinspection ConstantConditions
119: if (httpProxyPort == null) {
120: throw new IllegalArgumentException(
121: "httpProxyPort must not be null");
122: }
123:
124: this .httpProxyPort = httpProxyPort;
125: }
126:
127: public void setProxyUser(@NotNull
128: String proxyUser) {
129: //noinspection ConstantConditions
130: if (proxyUser == null) {
131: throw new IllegalArgumentException(
132: "proxyUser must not be null");
133: }
134: this .proxyUser = proxyUser;
135: }
136:
137: public void setProxyPassword(@NotNull
138: String proxyPassword) {
139: //noinspection ConstantConditions
140: if (proxyPassword == null) {
141: throw new IllegalArgumentException(
142: "proxyPassword must not be null");
143: }
144: this .proxyPassword = proxyPassword;
145: }
146:
147: public boolean isUseSocksProxy() {
148: return useSocksProxy;
149: }
150:
151: public void setUseSocksProxy(boolean useSocksProxy) {
152: this .useSocksProxy = useSocksProxy;
153: }
154:
155: @NotNull
156: public String getSocksProxyPort() {
157: return socksProxyPort;
158: }
159:
160: public void setSocksProxyPort(@NotNull
161: String socksProxyPort) {
162: //noinspection ConstantConditions
163: if (socksProxyPort == null) {
164: throw new IllegalArgumentException(
165: "socksProxyPort must not be null");
166: }
167:
168: this .socksProxyPort = socksProxyPort;
169: }
170:
171: @NotNull
172: public String getSocksProxyHost() {
173: return socksProxyHost;
174: }
175:
176: public void setSocksProxyHost(@NotNull
177: String socksProxyHost) {
178: //noinspection ConstantConditions
179: if (socksProxyHost == null) {
180: throw new IllegalArgumentException(
181: "socksProxyHost must not be null");
182: }
183:
184: this .socksProxyHost = socksProxyHost;
185: }
186:
187: @NotNull
188: public String getHTTPNonProxyHosts() {
189: return LOCALHOST;
190: }
191:
192: @NotNull
193: public String getSOCKSNonProxyHosts() {
194: return LOCALHOST;
195: }
196:
197: @NotNull
198: public String getProxyUser() {
199: if (proxyType == ProxyType.NO_PROXY) {
200: return "";
201: }
202: return proxyUser;
203: }
204:
205: @NotNull
206: public String getProxyPassword() {
207: if (proxyType == ProxyType.NO_PROXY) {
208: return "";
209: }
210: return proxyPassword;
211: }
212:
213: public void applySettings() {
214: switch (proxyType) {
215: case AUTO_DETECT_PROXY: {
216: System.setProperty(PropertyKeys.HTTP_DOT_PROXY_HOST, "");
217: System.setProperty(PropertyKeys.HTTP_DOT_PROXY_PORT, "");
218: System.setProperty(PropertyKeys.SOCKS_PROXY_HOST, "");
219: System.setProperty(PropertyKeys.SOCKS_PROXY_PORT, "");
220:
221: String host = getWebstartHTTPProxyHost();
222: boolean httpProxySet = false;
223: if (host != null && host.trim().length() > 0) {
224: System.setProperty(PropertyKeys.HTTP_DOT_PROXY_HOST,
225: host);
226: httpProxySet = true;
227: }
228: String port = getWebstartHTTPProxyPort();
229: if (port != null) {
230: System.setProperty(PropertyKeys.HTTP_DOT_PROXY_PORT,
231: port);
232: }
233:
234: if (!httpProxySet) {
235: String socksHost = getWebstartSOCKSProxyHost();
236: if (socksHost != null && socksHost.trim().length() > 0) {
237: System.setProperty(PropertyKeys.SOCKS_PROXY_HOST,
238: socksHost);
239: }
240:
241: String socksPort = getWebstartSOCKSProxyPort();
242: if (socksPort != null && socksPort.trim().length() > 0) {
243: System.setProperty(PropertyKeys.SOCKS_PROXY_PORT,
244: socksPort);
245: }
246: }
247: }
248: break;
249: case NO_PROXY: {
250: System.setProperty(PropertyKeys.HTTP_DOT_PROXY_HOST, "");
251: System.setProperty(PropertyKeys.HTTP_DOT_PROXY_PORT, "");
252: System.setProperty(PropertyKeys.SOCKS_PROXY_HOST, "");
253: System.setProperty(PropertyKeys.SOCKS_PROXY_PORT, "");
254: }
255: break;
256: case USER_PROXY: {
257: if (useSocksProxy) {
258: System
259: .setProperty(PropertyKeys.HTTP_DOT_PROXY_HOST,
260: "");
261: System
262: .setProperty(PropertyKeys.HTTP_DOT_PROXY_PORT,
263: "");
264: System.setProperty(PropertyKeys.SOCKS_PROXY_HOST,
265: socksProxyHost);
266: System.setProperty(PropertyKeys.SOCKS_PROXY_PORT,
267: socksProxyPort);
268: } else {
269: System.setProperty(PropertyKeys.HTTP_DOT_PROXY_HOST,
270: httpProxyHost);
271: System.setProperty(PropertyKeys.HTTP_DOT_PROXY_PORT,
272: httpProxyPort);
273: System.setProperty(PropertyKeys.SOCKS_PROXY_HOST, "");
274: System.setProperty(PropertyKeys.SOCKS_PROXY_PORT, "");
275: }
276: }
277: break;
278: }
279: }
280:
281: public void externalizeObject(@NotNull
282: XMLWriter xmlWriter, @NotNull
283: XMLContext xmlContext) throws IOException {
284: xmlWriter.writeAttribute("v", "1");
285:
286: xmlWriter.writeAttribute(PropertyKeys.HTTP_PROXY_HOST,
287: httpProxyHost);
288: xmlWriter.writeAttribute(PropertyKeys.HTTP_PROXY_PORT,
289: httpProxyPort);
290:
291: String proxyTypeName = "AUTO_DETECT_PROXY";//NON-NLS
292: if (proxyType == ProxyType.NO_PROXY) {
293: proxyTypeName = "NO_PROXY";//NON-NLS
294: } else if (proxyType == ProxyType.USER_PROXY) {
295: proxyTypeName = "USER_PROXY";//NON-NLS
296: }
297: xmlWriter.writeAttribute(PropertyKeys.PROXY_TYPE_NAME,
298: proxyTypeName);
299:
300: xmlWriter.writeAttribute(PropertyKeys.USE_SOCKS_PROXY, String
301: .valueOf(useSocksProxy));
302: xmlWriter.writeAttribute(PropertyKeys.SOCKS_PROXY_HOST,
303: socksProxyHost);
304: xmlWriter.writeAttribute(PropertyKeys.SOCKS_PROXY_PORT,
305: socksProxyPort);
306:
307: xmlWriter.writeAttribute(PropertyKeys.PROXY_USER, proxyUser);
308: xmlWriter.writeAttribute(PropertyKeys.PROXY_PASSWORD,
309: proxyPassword);
310: }
311:
312: public void readObject(@NotNull
313: XmlPullNode node, @NotNull
314: XMLContext xmlContext) {
315: String httpProxyHost = node
316: .getAttributeValueFromRawName(PropertyKeys.PROXY_HOST);//backward compatibility
317: if (httpProxyHost == null) {
318: httpProxyHost = node
319: .getAttributeValueFromRawName(PropertyKeys.HTTP_PROXY_HOST);
320: }
321: if (httpProxyHost == null) {
322: this .httpProxyHost = "";
323: } else {
324: this .httpProxyHost = httpProxyHost;
325: }
326:
327: String httpProxyPort = node
328: .getAttributeValueFromRawName(PropertyKeys.PROXY_PORT);//backward compatibility
329: if (httpProxyPort == null) {
330: httpProxyPort = node
331: .getAttributeValueFromRawName(PropertyKeys.HTTP_PROXY_PORT);
332: }
333:
334: if (httpProxyPort == null) {
335: this .httpProxyPort = "";
336: } else {
337: this .httpProxyPort = httpProxyPort;
338: }
339:
340: String proxyTypeName = node
341: .getAttributeValueFromRawName(PropertyKeys.PROXY_TYPE_NAME);
342:
343: if ("NO_PROXY".equals(proxyTypeName))//NON-NLS
344: {
345: proxyType = ProxyType.NO_PROXY;
346: } else if ("USER_PROXY".equals(proxyTypeName))//NON-NLS
347: {
348: proxyType = ProxyType.USER_PROXY;
349: } else {
350: proxyType = ProxyType.AUTO_DETECT_PROXY;
351: }
352:
353: //SOCKS
354: useSocksProxy = Boolean
355: .parseBoolean(node
356: .getAttributeValueFromRawName(PropertyKeys.USE_SOCKS_PROXY));
357: String socksProxyHost = node
358: .getAttributeValueFromRawName(PropertyKeys.SOCKS_PROXY_HOST);
359: String socksProxyPort = node
360: .getAttributeValueFromRawName(PropertyKeys.SOCKS_PROXY_PORT);
361:
362: if (socksProxyHost == null) {
363: this .socksProxyHost = "";
364: } else {
365: this .socksProxyHost = socksProxyHost;
366: }
367:
368: if (socksProxyPort == null) {
369: this .socksProxyPort = "";
370: } else {
371: this .socksProxyPort = socksProxyPort;
372: }
373:
374: String proxyUser = node
375: .getAttributeValueFromRawName(PropertyKeys.PROXY_USER);
376: String proxyPassword = node
377: .getAttributeValueFromRawName(PropertyKeys.PROXY_PASSWORD);
378:
379: if (proxyUser == null) {
380: this .proxyUser = "";
381: } else {
382: this .proxyUser = proxyUser;
383: }
384:
385: if (proxyPassword == null) {
386: this .proxyPassword = "";
387: } else {
388: this .proxyPassword = proxyPassword;
389: }
390: applySettings();
391: }
392:
393: @Nullable
394: public static String getWebstartHTTPProxyHost() {
395: String host = System
396: .getProperty(PropertyKeys.DEPLOYMENT_PROXY_HTTP_HOST);
397: if (host != null) {
398: return host;
399: }
400:
401: host = System.getProperty(PropertyKeys.PROXY_HOST);
402: return host;
403: }
404:
405: @Nullable
406: public static String getWebstartHTTPProxyPort() {
407: String port = System
408: .getProperty(PropertyKeys.DEPLOYMENT_PROXY_HTTP_PORT);
409: if (port != null) {
410: return port;
411: }
412:
413: port = System.getProperty(PropertyKeys.PROXY_PORT);
414: return port;
415: }
416:
417: @Nullable
418: public static String getWebstartSOCKSProxyHost() {
419: return System
420: .getProperty(PropertyKeys.DEPLOYMENT_PROXY_SOCKS_HOST);
421: }
422:
423: @Nullable
424: public static String getWebstartSOCKSProxyPort() {
425: return System
426: .getProperty(PropertyKeys.DEPLOYMENT_PROXY_SOCKS_PORT);
427: }
428:
429: }
|