001: //** Copyright Statement ***************************************************
002: //The Salmon Open Framework for Internet Applications (SOFIA)
003: // Copyright (C) 1999 - 2002, Salmon LLC
004: //
005: // This program is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU General Public License version 2
007: // as published by the Free Software Foundation;
008: //
009: // This program is distributed in the hope that it will be useful,
010: // but WITHOUT ANY WARRANTY; without even the implied warranty of
011: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: // GNU General Public License for more details.
013: //
014: // You should have received a copy of the GNU General Public License
015: // along with this program; if not, write to the Free Software
016: // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: //
018: // For more information please visit http://www.salmonllc.com
019: //** End Copyright Statement ***************************************************
020: package com.salmonllc.personalization;
021:
022: import com.salmonllc.sql.Base64Encoder;
023:
024: import java.util.Vector;
025: import java.util.Properties;
026: import java.io.*;
027: import java.net.URLConnection;
028: import java.net.URL;
029:
030: /**
031: * This class loads skins using the PersonalizationServer servlet to get the data. It can be used to load server side skins to applets and Java applications.
032: */
033:
034: public class ProxySkinManager extends SkinManager {
035:
036: String _serverURL, _sessionID, _proxyUserID, _proxyPw;
037:
038: void init(String st) {
039: }
040:
041: /**
042: * Creates a new ProxySkinManager
043: * @param serverURL the URL of the personalization server servlet
044: * @param sessionID the users session id
045: */
046: public ProxySkinManager(String serverURL, String sessionID) {
047: _serverURL = serverURL;
048: _sessionID = sessionID;
049: }
050:
051: /**
052: * Creates a new ProxySkinManager that can go through a proxy server
053: */
054: public ProxySkinManager(String serverURL, String sessionID,
055: String proxyHost, String proxyPort, String proxyUserID,
056: String proxyPw) {
057: _proxyPw = proxyPw;
058: _proxyUserID = proxyUserID;
059: _serverURL = serverURL;
060: _sessionID = sessionID;
061: if (proxyHost != null) {
062: Properties p = System.getProperties();
063: p.put("proxySet", "true");
064: p.put("proxyHost", proxyHost);
065: p.put("proxyPort", proxyPort);
066: }
067: }
068:
069: /*Loads the data for a skin with the specified name and append its attributes to the current skin*/
070: public void load(String name, Skin skin) {
071: try {
072: BufferedReader in = openConnection(_serverURL, _sessionID,
073: "LOAD", name);
074: String line = in.readLine();
075: int pos = 0;
076: while (line != null) {
077: if (line.length() > 0 && line.charAt(0) != '#') {
078: pos = line.indexOf('=');
079: if (pos > -1) {
080: String att = line.substring(0, pos).trim();
081: String val = line.substring(pos + 1).trim();
082: boolean classAtt = false;
083: boolean instAtt = false;
084: if (att.startsWith("class."))
085: classAtt = true;
086: else if (att.startsWith("instance."))
087: instAtt = true;
088:
089: if (classAtt || instAtt) {
090: int ndx = att.lastIndexOf(".");
091: int ndx2 = att.indexOf(".");
092: if (ndx == ndx2)
093: continue;
094: String attName = att.substring(ndx + 1);
095: String attClass = att.substring(ndx2 + 1,
096: ndx);
097: if (classAtt)
098: skin.setClassAttribute(attClass,
099: attName, val);
100: else
101: skin.setInstanceAttribute(attClass,
102: attName, val);
103: } else {
104: skin.setProperty(att, val);
105: }
106:
107: }
108: }
109: line = in.readLine();
110: }
111: in.close();
112: return;
113: } catch (Exception e) {
114: e.printStackTrace();
115: }
116: }
117:
118: /*Saves a skin under the specified name, if it exists it is overwritten*/
119: public void save(String name, Skin skin) {
120: //for security purposes we don't update skins remotely
121: }
122:
123: /*Gets an array of available skin names*/
124: public String[] getSkinNames() {
125: Vector v = new Vector();
126: BufferedReader in = null;
127: try {
128: in = openConnection(_serverURL, _sessionID, "LIST", null);
129: String line = in.readLine();
130: while (line != null) {
131: v.add(line);
132: line = in.readLine();
133: }
134: in.close();
135: } catch (Exception e) {
136: e.printStackTrace();
137: }
138: String ret[] = new String[v.size()];
139: v.copyInto(ret);
140: return ret;
141: }
142:
143: private BufferedReader openConnection(String url, String sessionId,
144: String operation, String skinName) throws Exception {
145: if (operation != null)
146: url += "?OP=" + operation;
147: if (skinName != null)
148: url += (operation == null ? "?" : "&") + "SKINNAME="
149: + skinName;
150:
151: URL u = new URL(url);
152: URLConnection conn = u.openConnection();
153: conn.setDoInput(true);
154: conn.setDoOutput(true);
155: conn.setUseCaches(false);
156:
157: String session = sessionId;
158: if (session != null)
159: conn.setRequestProperty("Cookie", "sesessionid=" + session
160: + ";session=" + session + ";sessionid=" + session
161: + ";JSESSIONID=" + session + ";jsessionid="
162: + session);
163:
164: if (_proxyUserID != null) {
165: String authString = _proxyUserID + ":" + _proxyPw;
166: String auth = "Basic "
167: + new Base64Encoder().encode(authString);
168: conn.setRequestProperty("Proxy-Authorization", auth);
169: }
170:
171: return new BufferedReader(new InputStreamReader(conn
172: .getInputStream()));
173: }
174:
175: }
|