001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.providers.urlscraper;
006:
007: import javax.servlet.http.HttpServletRequest;
008: import javax.servlet.http.HttpServletResponse;
009:
010: import java.util.Map;
011: import java.net.URL;
012: import java.net.MalformedURLException;
013: import com.sun.portal.log.common.PortalLogger;
014: import java.util.logging.Level;
015: import java.util.logging.Logger;
016: import java.util.logging.LogRecord;
017:
018: /**
019: * This class is a thread which fetches with in the alloted time or fails.
020: */
021: class FetcherThread extends Thread {
022:
023: /**
024: * The instnace of fetcher that is used
025: */
026: private Fetcher fetcher;
027: /**
028: * if it is terminated
029: */
030: private boolean terminated = false;
031: /**
032: * If it is finished
033: */
034: private boolean finished = false;
035: /**
036: * is it already started
037: */
038: private boolean started = false;
039: /**
040: * The request Object
041: */
042: //private Map data=null;
043: private HttpServletRequest request = null;
044:
045: /**
046: * The response object
047: */
048: private HttpServletResponse response = null;
049:
050: /**
051: * The Url to Fetch
052: */
053: private String urlSt = null;
054: /**
055: * The contentStored stored here after a fetch then it is picked .
056: */
057: private StringBuffer contentBuffer = null;
058: /**
059: * Not to be used
060: */
061: private String contentType = null;
062: /**
063: * A temp place holder , which will be picked to posLoginUrl once it authenticates.
064: */
065: private String lastFetchedUrl = null;
066: /**
067: * private logger
068: *
069: */
070:
071: private static Logger logger = PortalLogger
072: .getLogger(FetcherThread.class);
073:
074: FetcherThread(Fetcher fetcherInst, HttpServletRequest req,
075: HttpServletResponse res, String urlAsSt) {
076: request = req;
077: response = res;
078: this .urlSt = urlAsSt;
079: fetcher = fetcherInst;
080: setDaemon(true);
081: }
082:
083: /**
084: * <P> Check whether reading from the URL is complete
085: *
086: * @return finished (true/false)
087: */
088: public boolean isFinished() {
089: return finished;
090: }
091:
092: /**
093: * <P> Terminate the current thread
094: */
095: public void terminate() {
096: terminated = true;
097: interrupt();
098: }
099:
100: /**
101: *
102: * @return
103: */
104: public boolean isStarted() {
105: return started;
106: }
107:
108: /**
109: * <P> Check whether the current thread is terminated
110: *
111: * @return None
112: */
113: public boolean isTerminated() {
114: return terminated;
115: }
116:
117: /**
118: * The method that does the fetch
119: */
120: public void run() {
121:
122: started = true;
123: finished = false;
124:
125: try {
126: contentBuffer = fetcher.fetch(this , getUrlToFetch());
127:
128: } finally {
129: finished = true;
130: started = false;
131: }
132: }
133:
134: /**
135: * <P> Return the contents of the HTML page
136: *
137: * @return content
138: */
139: public StringBuffer getContentBuffer() {
140: return contentBuffer;
141: }
142:
143: /**
144: *
145: * @return
146: */
147: String getContentType() {
148: return contentType;
149: }
150:
151: /**
152: *
153: * @param contentTypeSt
154: */
155: void setContentType(String contentTypeSt) {
156: this .contentType = contentTypeSt;
157: }
158:
159: /**
160: *
161: * @param req
162: * @throws java.net.MalformedURLException
163: * @return
164: */
165: public static URL getDesktopRequestURL(HttpServletRequest req)
166: throws MalformedURLException {
167:
168: StringBuffer url = new StringBuffer();
169: String host = null;
170:
171: String scheme = req.getScheme();
172: url.append(scheme); // http, https
173: url.append("://");
174:
175: url.append(req.getServerName());
176: int port = req.getServerPort();
177:
178: if ((scheme.equals("http") && port != 80)
179: || (scheme.equals("https") && port != 443)) {
180: url.append(':');
181: url.append(port);
182: }
183: return new URL(url.toString());
184: }
185:
186: /**
187: *
188: * @throws java.net.MalformedURLException
189: * @return
190: */
191: public URL getDesktopRequestURL() throws MalformedURLException {
192: return getDesktopRequestURL(getHttpServletRequest());
193: }
194:
195: /**
196: *
197: * @return
198: */
199: public HttpServletRequest getHttpServletRequest() {
200: return request;
201:
202: }
203:
204: /**
205: *
206: * @return
207: */
208: public HttpServletResponse getHttpServletResponse() {
209: return response;
210: }
211:
212: /**
213: *
214: * @return
215: */
216: private String getUrlToFetch() {
217: return this .urlSt;
218: }
219:
220: /**
221: *
222: * @param url
223: */
224: void setLastFetchedUrl(String url) {
225: lastFetchedUrl = url;
226: }
227:
228: /**
229: *
230: * @return
231: */
232: String getLastFetchedUrl() {
233: return lastFetchedUrl;
234: }
235:
236: }
|