001: /*
002: * $Id: URLUrl.java,v 1.4 2002/04/16 12:40:16 ptalbot Exp $
003: *
004: * ==========================================================================
005: *
006: * The JGenerator Software License, Version 1.0
007: *
008: * Copyright (c) 2000 Dmitry Skavish (skavish@usa.net). All rights reserved.
009: *
010: * Redistribution and use in source and binary forms, with or without
011: * modification, are permitted provided that the following conditions are met:
012: *
013: * 1. Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * 2. Redistributions in binary form must reproduce the above copyright
017: * notice, this list of conditions and the following disclaimer in
018: * the documentation and/or other materials provided with the
019: * distribution.
020: *
021: * 3. The end-user documentation included with the redistribution, if
022: * any, must include the following acknowlegement:
023: * "This product includes software developed by Dmitry Skavish
024: * (skavish@usa.net, http://www.flashgap.com/)."
025: * Alternately, this acknowlegement may appear in the software itself,
026: * if and wherever such third-party acknowlegements normally appear.
027: *
028: * 4. The name "The JGenerator" must not be used to endorse or promote
029: * products derived from this software without prior written permission.
030: * For written permission, please contact skavish@usa.net.
031: *
032: * 5. Products derived from this software may not be called "The JGenerator"
033: * nor may "The JGenerator" appear in their names without prior written
034: * permission of Dmitry Skavish.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
040: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: *
049: */
050:
051: package org.openlaszlo.iv.flash.url;
052:
053: import java.io.*;
054: import java.net.*;
055: import java.util.*;
056: import org.openlaszlo.iv.flash.util.*;
057: import org.openlaszlo.iv.flash.api.*;
058:
059: /**
060: * Implementation of regular url
061: *
062: * @author Dmitry Skavish
063: */
064: public class URLUrl extends IVUrl {
065:
066: private URL url;
067: private URLConnection conn;
068: private long lastModified = 0;
069: private long lastConnect = 0;
070:
071: /**
072: * Creates URLUrl from URL
073: *
074: * @param url specified URL
075: * @exception IVException
076: */
077: public URLUrl(URL url) throws IVException {
078: this .url = url;
079: }
080:
081: public String getParameter(String name) {
082: if (parms == null) {
083: try {
084: parse(getName());
085: } catch (IVException e) {
086: Log.logRB(e);
087: }
088: if (parms == null) {
089: parms = new Hashtable();
090: }
091: }
092: return super .getParameter(name);
093: }
094:
095: public String getName() {
096: return url.toExternalForm();
097: }
098:
099: public String getRef() {
100: return url.getRef();
101: }
102:
103: public long lastModified() {
104: return lastModified;
105: }
106:
107: public InputStream getInputStream() throws IOException {
108: connect();
109: Log.logRB(Resource.RETRIEVINGCONTENT,
110: new Object[] { getName() });
111: return conn.getInputStream();
112: }
113:
114: public void refresh() {
115: try {
116: connect();
117: } catch (IOException e) {
118: Log.logRB(e);
119: }
120: }
121:
122: private synchronized void connect() throws IOException {
123: long now = System.currentTimeMillis();
124: if (lastConnect == 0 || lastConnect + 500 < now) {
125: Log
126: .logRB(Resource.CONNECTINGTO,
127: new Object[] { getName() });
128: String auth = setProxy();
129: conn = url.openConnection();
130: if (auth != null)
131: conn.setRequestProperty("Proxy-Authorization", auth);
132: conn.connect();
133: lastModified = conn.getLastModified();
134: lastConnect = System.currentTimeMillis();
135: }
136: }
137:
138: private String setProxy() {
139: String auth = null;
140: String useProxy = PropertyManager
141: .getProperty("org.openlaszlo.iv.flash.http.proxy.enable");
142: if ("true".equalsIgnoreCase(useProxy)) {
143: String proxy = PropertyManager
144: .getProperty("org.openlaszlo.iv.flash.http.proxy.host");
145: String proxyport = PropertyManager
146: .getProperty("org.openlaszlo.iv.flash.http.proxy.port");
147: String myUserName = PropertyManager
148: .getProperty("org.openlaszlo.iv.flash.http.proxy.username");
149: String myPassword = PropertyManager
150: .getProperty("org.openlaszlo.iv.flash.http.proxy.password");
151: if (proxy != null) {
152: Properties props = System.getProperties();
153: props.put("proxySet", "true");
154: props.put("proxyHost", proxy);
155: if (proxyport != null)
156: props.put("proxyPort", proxyport);
157: else
158: props.put("proxyPort", "80");
159: if (myUserName != null
160: && myUserName.trim().length() > 0) {
161: String authString = myUserName
162: + ":"
163: + ((myPassword != null && myPassword.trim()
164: .length() > 0) ? myPassword : "");
165: auth = "Basic "
166: + new sun.misc.BASE64Encoder()
167: .encode(authString.getBytes());
168: }
169: }
170: }
171: return auth;
172: }
173: }
|