| 1 |
|
package yarfraw.io; |
| 2 |
|
|
| 3 |
|
import java.io.File; |
| 4 |
|
import java.io.FileInputStream; |
| 5 |
|
import java.io.IOException; |
| 6 |
|
import java.io.InputStream; |
| 7 |
|
import java.net.URI; |
| 8 |
|
|
| 9 |
|
import org.apache.commons.httpclient.HttpClient; |
| 10 |
|
import org.apache.commons.httpclient.HttpURL; |
| 11 |
|
import org.apache.commons.httpclient.methods.GetMethod; |
| 12 |
|
import org.apache.commons.httpclient.params.HttpClientParams; |
| 13 |
|
|
| 14 |
|
import yarfraw.core.datamodel.FeedFormat; |
| 15 |
|
import yarfraw.core.datamodel.YarfrawException; |
| 16 |
|
import yarfraw.utils.FeedFormatDetector; |
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
|
| 22 |
|
abstract class AbstractBaseFeedParser extends AbstractBaseIO{ |
| 23 |
|
|
| 24 |
273 |
protected HttpURL _httpUrl = null; |
| 25 |
273 |
protected HttpClientParams _httpClientParams = null; |
| 26 |
273 |
protected GetMethod _getMethod = null; |
| 27 |
|
|
| 28 |
|
public AbstractBaseFeedParser(File file, FeedFormat format){ |
| 29 |
21 |
super(file, format); |
| 30 |
21 |
} |
| 31 |
|
|
| 32 |
|
public AbstractBaseFeedParser(String pathName, FeedFormat format){ |
| 33 |
0 |
super(new File(pathName), format); |
| 34 |
0 |
} |
| 35 |
|
|
| 36 |
|
public AbstractBaseFeedParser(URI uri, FeedFormat format){ |
| 37 |
0 |
super(new File(uri), format); |
| 38 |
0 |
} |
| 39 |
|
public AbstractBaseFeedParser(File file){ |
| 40 |
87 |
super(file); |
| 41 |
87 |
} |
| 42 |
|
|
| 43 |
|
public AbstractBaseFeedParser(String pathName){ |
| 44 |
0 |
this(new File(pathName)); |
| 45 |
0 |
} |
| 46 |
|
|
| 47 |
|
public AbstractBaseFeedParser(URI uri){ |
| 48 |
0 |
this(new File(uri)); |
| 49 |
0 |
} |
| 50 |
|
|
| 51 |
|
public AbstractBaseFeedParser(HttpURL httpUrl) throws YarfrawException, IOException{ |
| 52 |
0 |
this(httpUrl, null); |
| 53 |
0 |
} |
| 54 |
|
|
| 55 |
162 |
public AbstractBaseFeedParser(HttpURL httpUrl, HttpClientParams params) throws YarfrawException, IOException{ |
| 56 |
162 |
_httpUrl = httpUrl; |
| 57 |
162 |
_httpClientParams = params; |
| 58 |
|
|
| 59 |
162 |
_format = FeedFormatDetector.getFormat(getStream()); |
| 60 |
159 |
} |
| 61 |
|
|
| 62 |
3 |
public AbstractBaseFeedParser(GetMethod getMethod) throws YarfrawException, IOException{ |
| 63 |
3 |
_getMethod = getMethod; |
| 64 |
3 |
_format = FeedFormatDetector.getFormat(getStream()); |
| 65 |
3 |
} |
| 66 |
|
|
| 67 |
|
public HttpClientParams getHttpClientParams() { |
| 68 |
0 |
return _httpClientParams; |
| 69 |
|
} |
| 70 |
|
|
| 71 |
|
public void setHttpClientParams(HttpClientParams httpClientParams) { |
| 72 |
6 |
_httpClientParams = httpClientParams; |
| 73 |
6 |
} |
| 74 |
|
|
| 75 |
|
|
| 76 |
|
|
| 77 |
|
|
| 78 |
|
|
| 79 |
|
public GetMethod getGetMethod() { |
| 80 |
318 |
return _getMethod; |
| 81 |
|
} |
| 82 |
|
|
| 83 |
|
|
| 84 |
|
|
| 85 |
|
|
| 86 |
|
|
| 87 |
|
|
| 88 |
|
public boolean isRemoteRead(){ |
| 89 |
495 |
return _httpUrl != null || _getMethod != null; |
| 90 |
|
} |
| 91 |
|
|
| 92 |
|
|
| 93 |
|
protected InputStream getStream() throws IOException{ |
| 94 |
|
InputStream stream; |
| 95 |
480 |
if(isRemoteRead()){ |
| 96 |
318 |
GetMethod get = getGetMethod(); |
| 97 |
318 |
if(get == null){ |
| 98 |
312 |
get = new GetMethod(_httpUrl.toString()); |
| 99 |
311 |
get.setFollowRedirects(true); |
| 100 |
|
} |
| 101 |
317 |
HttpClient client = new HttpClient(); |
| 102 |
318 |
if(_httpClientParams != null){ |
| 103 |
6 |
client.setParams(_httpClientParams); |
| 104 |
|
} |
| 105 |
317 |
client.executeMethod(get); |
| 106 |
315 |
stream = get.getResponseBodyAsStream(); |
| 107 |
315 |
}else{ |
| 108 |
162 |
return new FileInputStream(_file); |
| 109 |
|
} |
| 110 |
|
|
| 111 |
315 |
return stream; |
| 112 |
|
} |
| 113 |
|
|
| 114 |
|
|
| 115 |
|
} |