Sunteți pe pagina 1din 5

package com.lim.dw.vendors.

rtewtrre;
import java.io.IOException;
import java.net.URL;
import java.util.Calendar;
import java.util.List;
import java.util.regex.Pattern;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import com.lim.dw.jobs.ReturnStatusCode;
import com.lim.dw.loader.HttpDownload;
import com.lim.dw.util.html.Analyzer;
import com.lim.dw.util.html.Link;
import com.lim.dw.util.html.Row;
import com.lim.dw.util.html.Table;
import com.lim.util.DateTime;
public class Download extends HttpDownload{
@Override
public String getHttpHost() {
return "http://clients.rte-france.com";
}
@Override
public String getVendorName() {
return "rtewtrre";
}
@Override
public void run() {
int success = ReturnStatusCode.SUCCESS.intValue();
try
{
String date = getArg("date");
String frame = getArg("frame");
String argHist = getArg("history");
if("yes".equalsIgnoreCase(argHist) || frame.contains("hi
story")){
success = downloadHistoryFile(date);
}
else if (frame.contains("day") || frame.contains("corr")
)
{
success = downloadDailyFile(date,"daily");
}
else
{
success = ReturnStatusCode.WARNING_CREATE_TICKET
.intValue();
printWarning(" Frame is not as expected " + fram
e);
}
}
catch (Exception ex)
{
success = ReturnStatusCode.FATAL.intValue();
printError(" Exception occured in download ", new Throwa
ble(ex));
}
setStatusCode(success);
}
/**
* This function download the Daily and history file from the url Depend
ing on the file type argument.
* Daily downloads the current year and last year file.
* History will download all data.
*
*
* @return success
*/
private int downloadDailyFile(String date, String fileType)throws Except
ion {
int success = ReturnStatusCode.SUCCESS.intValue();
String url="";
int year = 0;
int rowLength=0;
Calendar cal = DateTime.string2Calendar(date, DateTime.YYYYMMDD)
;
String absoluteHref ="";
String localFileName="";
try {
url = getHttpHost()+"/lang/an/visiteurs/vie/prod/stock_
hydraulique.jsp";
String pageContent = getPageContentFromHttp(url);
Analyzer fileAnalyzer = new Analyzer(pageContent);
fileAnalyzer.analyzeTables(true);
List<Table> tables = fileAnalyzer.searchTables("Years,Do
wnload");
Table tbldataTable = tables.get(0);
if (tbldataTable != null && tbldataTable.rows().size() >
0) {
List<Row> dataRow = tbldataTable.rows();
rowLength = dataRow.size();
if(fileType.equalsIgnoreCase("daily")){
for(int i=0;i<2;i++){
try {
year = cal.get(Calendar.
YEAR);
absoluteHref = getHttpHo
st()+"/servlets/SHServlet?annee="+year;
printReport("Getting " +
fileType + " file for year "+year+" on date " + date);
localFileName = "Stock_h
ydraulique_"+year+".zip";
success |= getHttpFile(a
bsoluteHref.substring(getHttpHost().length(), absoluteHref.length()), localFileN
ame);
} catch (Exception ex) {
printWarning(" Download
fail for the file for year " + year + "on date"+ date);
success |= ReturnStatusC
ode.WARNING.intValue();
}
cal.add(Calendar.YEAR, -1);
}
}
else{
for(int i=0;i<rowLength-1;i++){
try {
year = cal.get(Calendar.
YEAR);
absoluteHref = getHttpHo
st()+"/servlets/SHServlet?annee="+year;
printReport("Getting " +
fileType + " file for year "+year+" on date " + date);
localFileName = "Stock_h
ydraulique_"+year+".zip";
success |= getHttpFile(a
bsoluteHref.substring(getHttpHost().length(), absoluteHref.length()), localFileN
ame);
} catch (Exception ex) {
printWarning(" Download
fail for the file for year " + year + "on date"+ date);
success |= ReturnStatusC
ode.WARNING.intValue();
}
cal.add(Calendar.YEAR, -1);
}
}
}
}catch (Exception e) {
printError(" Exception is occured in opening the page "
+ url);
throw e;
}
return success;
}
/**
* This function download the History file from the url .
*
* @param argDate
*
* @return success
*/
public int downloadHistoryFile(String argDate){
int success = ReturnStatusCode.SUCCESS.intValue();
try
{
String absoluteHref = getHttpHost()+"/htm/fr/vie/prod/te
lecharge/Stock_hydraulique_historique.zip";
printReport("Getting history file on date " + argDate);
success |= getHttpFile(absoluteHref.substring(getHttpHos
t().length(), absoluteHref.length()));
success |= downloadDailyFile(argDate,"history");
}
catch(Exception ex){
printError(" Download fail for the file for date " + arg
Date, ex);
success |= ReturnStatusCode.FATAL.intValue();
}
return success;
}

public String getPageContentFromHttp(String url) throws HttpException, I


OException
{
String pageContent = null;
HttpClient client = null;
GetMethod get = null;
try
{
client = new HttpClient();
get = new GetMethod(url);
get.addRequestHeader("User-Agent", "Mozilla/4.0 (compati
ble; MSIE 7.0; Windows NT 5.1; GTB5; .NET CLR 2.0.50727)");
client.getParams().setSoTimeout(20000);
int status = client.executeMethod(get);
if (status < 200 || status > 299)
{
throw new HttpException ("Unexpected HTTP status: " + status);
}
pageContent = get.getResponseBodyAsString();
}
finally
{
if (get != null)
{
get.releaseConnection();
}
}
return pageContent;
}
private String getAbsoluteURL(String url, Pattern pattern) throws Except
ion
{
String absoluteDownloadHref = null;
try
{
String pageContent = getPageContentFromHttp(url);
Analyzer analyzer = new Analyzer(pageContent);
List<Link> hrefLinks = analyzer.linksByText(pattern);
if (hrefLinks.size() == 0)
{
throw new Exception (" size of link found on " +
url + " for pattern " + pattern.toString() + " is zero");
}
Link hrefLink = hrefLinks.get(0); // only one link is g
oing to be there.
String hrefHomeStr = hrefLink.getHRef();
absoluteDownloadHref = new URL(new URL(url), hrefHomeStr
).toString();
}
catch (Exception ex)
{
printError(" Exception is occured in opening the page "
+ url);
throw ex;
}
return absoluteDownloadHref;
}
public static void main(String [] arg){
main(new Download(),arg);
}

}
ant ven -DUSER=lim -DDIR=rtegrf

S-ar putea să vă placă și