@SuppressWarnings("unchecked") public List<FiddleSyndEntry> getFeeds(URL url, CategoryDetail catDetail) { List<FiddleSyndEntry> feeds = new ArrayList<FiddleSyndEntry>(); XmlReader reader = null; SyndFeed feed = null; try { System.setProperty("http.proxyHost", "proxy-tw.bcbsmn.com"); System.setProperty("http.proxyPort", "9119"); System.setProperty("http.proxyUser", "a0c5zz"); System.setProperty("http.proxyPassword", "test0810"); reader = new XmlReader(url); feed = new SyndFeedInput().build(reader); feeds = feed.getEntries(); } catch (IOException e) { log.error("Exception reading url"); } catch (IllegalArgumentException e) { log.error("Exception reading url"); } catch (FeedException e) { log.error("Exception reading feed"); } finally { if (reader != null) try { reader.close(); } catch (IOException e) { log.error("Unable to close Feed XmlReader"); } } return feeds; }
public static void main(String[] args) throws Exception { URL url = new URL("http://feeds.reuters.com/reuters/businessNews"); XmlReader reader = null; try { reader = new XmlReader(url); SyndFeed feed = new SyndFeedInput().build(reader); System.out.println("Feed Title: " + feed.getAuthor()); for (@SuppressWarnings("unchecked") Iterator<SyndEntry> i = feed.getEntries().iterator(); i.hasNext(); ) { SyndEntry entry = i.next(); System.out.println(entry.getTitle()); } } finally { if (reader != null) reader.close(); } }
/** @see com.elia.rssexample.data.NewsDao */ @SuppressWarnings("unchecked") public List<NewsItem> getNewsList() { // TODO: exception handling log.trace("Enter getNewsList()."); List<NewsItem> newsList = new ArrayList<NewsItem>(); XmlReader reader = null; try { for (String rssUrl : rssUrlList) { reader = new XmlReader(new URL(rssUrl)); SyndFeed feed = new SyndFeedInput().build(reader); for (SyndEntry entry : (List<SyndEntry>) feed.getEntries()) { NewsItem item = new NewsItem(); item.setTitle(entry.getTitle()); item.setDescription(entry.getDescription().getValue()); item.setLink(entry.getLink()); item.setPublished(entry.getPublishedDate()); newsList.add(item); } } } catch (Exception e) { log.error("Error reading feed.", e); } finally { try { reader.close(); } catch (IOException e) { log.warn("Unable to close xml reader.", e); } } return newsList; }
public static synchronized void init() { if (_initialized) { return; } StopWatch stopWatch = new StopWatch(); stopWatch.start(); // Set the default locale used by Liferay. This locale is no longer set // at the VM level. See LEP-2584. String userLanguage = SystemProperties.get("user.language"); String userCountry = SystemProperties.get("user.country"); String userVariant = SystemProperties.get("user.variant"); LocaleUtil.setDefault(userLanguage, userCountry, userVariant); // Set the default time zone used by Liferay. This time zone is no // longer set at the VM level. See LEP-2584. String userTimeZone = SystemProperties.get("user.timezone"); TimeZoneUtil.setDefault(userTimeZone); // Shared class loader try { PortalClassLoaderUtil.setClassLoader(ClassLoaderUtil.getContextClassLoader()); } catch (Exception e) { e.printStackTrace(); } // Properties com.liferay.portal.kernel.util.PropsUtil.setProps(new PropsImpl()); // Log4J if (GetterUtil.getBoolean(SystemProperties.get("log4j.configure.on.startup"), true)) { ClassLoader classLoader = InitUtil.class.getClassLoader(); Log4JUtil.configureLog4J(classLoader); } // Shared log try { LogFactoryUtil.setLogFactory(new Log4jLogFactoryImpl()); } catch (Exception e) { e.printStackTrace(); } // Log sanitizer SanitizerLogWrapper.init(); // Java properties JavaDetector.isJDK5(); // Security manager SecurityManagerUtil.init(); if (SecurityManagerUtil.ENABLED) { com.liferay.portal.kernel.util.PropsUtil.setProps( DoPrivilegedUtil.wrap(com.liferay.portal.kernel.util.PropsUtil.getProps())); LogFactoryUtil.setLogFactory(DoPrivilegedUtil.wrap(LogFactoryUtil.getLogFactory())); } // Cache registry CacheRegistryUtil.setCacheRegistry(DoPrivilegedUtil.wrap(new CacheRegistryImpl())); // Configuration factory ConfigurationFactoryUtil.setConfigurationFactory( DoPrivilegedUtil.wrap(new ConfigurationFactoryImpl())); // Data source factory DataSourceFactoryUtil.setDataSourceFactory(DoPrivilegedUtil.wrap(new DataSourceFactoryImpl())); // DB factory DBFactoryUtil.setDBFactory(DoPrivilegedUtil.wrap(new DBFactoryImpl())); // ROME XmlReader.setDefaultEncoding(StringPool.UTF8); if (_PRINT_TIME) { System.out.println("InitAction takes " + stopWatch.getTime() + " ms"); } _initialized = true; }
public Vector<SyndEntry> crawl() { if (feedid == 1301) { int z = 0; z++; } SyndFeedInput input = new SyndFeedInput(); XmlReader reader = null; Vector<SyndEntry> ret = new Vector<SyndEntry>(); Document doc; try { /* * Document doc = Jsoup.parse(feedurl,10000); * getLogger().info(doc.toString()); */ SyndFeed feed = null; try { feed = input.build(reader = new XmlReader(feedurl)); } catch (Exception fe) { try { System.err.println("from url: " + feedurl); fe.printStackTrace(); doc = Jsoup.parse(feedurl, 10000); feed = input.build(new StringReader(doc.toString())); getLogger().info("Could fix it woith jsoup"); } catch (ExceptionInInitializerError ed) { System.err.println("from url: " + feedurl); ed.printStackTrace(); try { URL url = feedurl; String feedpage = FeedCrawler.readPage(url); feedpage = feedpage .replaceAll("\\&ldquo;", "\"") .replaceAll("“", "\"") .replaceAll("\\&rdquo;", "\"") .replaceAll("”", "\""); System.out.println(feedpage); // feedpage=feedpage.replaceAll("\\&ldquo;", "\""); // reader=new XmlReader(new InputSource(new // StringReader(feedpage)).getCharacterStream()); feed = input.build(new InputSource(new StringReader(feedpage))); getLogger().info("Could fix it with complicatedreader"); } catch (URISyntaxException e) { // TODO Auto-generated catch block System.err.println("from url: " + feedurl); e.printStackTrace(); System.out.println("URL does not work: " + feedurl); } catch (FeedException e) { // TODO Auto-generated catch block System.err.println("from url: " + feedurl); e.printStackTrace(); System.out.println("URL does not work: " + feedurl); } catch (Exception ex) { System.err.println("from url: " + feedurl); ex.printStackTrace(); System.out.println("URL does not work: " + feedurl); } } } for (Iterator<SyndEntry> i = feed.getEntries().iterator(); i.hasNext(); ) { SyndEntry entry = (SyndEntry) i.next(); System.out.println("\t INFOR: Entry\t" + entry.getUri()); if (!entry.getUri().startsWith("http")) { System.out.println("\t ERROR: \t" + entry.getLink()); System.out.println("\t DEBUG: \t" + feedurl); System.out.println("\t FIX: \t"); if (domain.length() > 0) { entry.setLink(domain + entry.getLink()); entry.setUri(domain + entry.getUri()); ret.add(entry); } } else ret.add(entry); getLogger().info(entry.getTitle()); } } catch (IllegalArgumentException e) { // TODO Auto-generated catch block getLogger().info("Feed error: " + feedurl); getLogger().info("try with jsoup"); System.err.println("from url: " + feedurl); e.printStackTrace(); try { doc = Jsoup.parse(feedurl, 10000); } catch (IOException e1) { // TODO Auto-generated catch block System.err.println("from url: " + feedurl); e1.printStackTrace(); } } catch (FeedException fe) { getLogger().info("Even jsoup did not work: " + feedurl); System.err.println("from url: " + feedurl); fe.printStackTrace(); } catch (IOException e) { getLogger().info("Feed error: " + feedurl); getLogger().info("try with jsoup"); // TODO Auto-generated catch block System.err.println("from url: " + feedurl); e.printStackTrace(); try { doc = Jsoup.parse(feedurl, 10000); } catch (IOException e2) { // TODO Auto-generated catch block System.err.println("from url: " + feedurl); e2.printStackTrace(); try { getLogger().info("try with jsoup"); doc = Jsoup.parse(feedurl, 10000); getLogger().info(doc.toString()); } catch (IOException e1) { // TODO Auto-generated catch block getLogger().info("once again, jsoup cant do it"); System.err.println("from url: " + feedurl); e1.printStackTrace(); } } } finally { if (reader != null) try { reader.close(); } catch (IOException eg) { getLogger().info("Feed error: " + feedurl); // TODO Auto-generated catch block System.err.println("from url: " + feedurl); eg.printStackTrace(); } } return ret; }
/** * Exports a list of patients at the clinic and their records. Compare a list of all patients in * the rss file last published by the server with a list of patients in the system. Publish any * changed records if they are not equal If site is the master data center (such as ZEPRS), loops * through each site's patient list and creates RSS file for each site. Note that a patient's xml * record could be listed in more than one site - The patient listing for each site selects from * patient_status.site_id. Download the remote site listing and see if there are adidtional * patients who may have been transferred to another site - UTH for instance. This enables sites * to see records of their regular patients who were referred to another site. Also creates an * index which is used on remote sites for finding patient records within the imported archive. * * @param conn * @param siteAbbrev - if siteabbrev = ZEP,, export all sites w/ their own rss feed. * @param siteId * @param checkXml - true if you want to check if xml files have been generated. * @return * @throws SQLException * @throws ServletException * @throws ObjectNotFoundException * @throws IOException * @throws ClassNotFoundException * @throws IllegalAccessException * @throws PersistenceException * @throws InvocationTargetException * @throws FeedException */ public static String createSiteRssFiles( Connection conn, String siteAbbrev, Long siteId, Boolean checkXml) throws SQLException, ServletException, ObjectNotFoundException, IOException, ClassNotFoundException, IllegalAccessException, PersistenceException, InvocationTargetException, FeedException { String message = null; StringBuffer sbuf = new StringBuffer(); Timestamp beginDate = null; Timestamp endDate = new Timestamp(System.currentTimeMillis()); Map statusMap = DynaSiteObjects.getStatusMap(); Publisher publisher = null; String publisherFile = Constants.ARCHIVE_PATH + "publisher.xml"; String ipAddress = null; Boolean renderAllSites = false; try { publisher = (Publisher) XmlUtils.getOne(publisherFile); ipAddress = publisher.getUrl(); } catch (FileNotFoundException e) { log.debug("publisher.xml not available - cannot render site rss files."); } if (publisher != null) { // check the db for the date of the last archive. // Fetch list of updated patients. This list is used to generate each patient's updated xml // file ResultSet updatedPatients = null; try { ArchiveLog mostRecentArchive = (ArchiveLog) ArchiveDAO.getOne(conn); beginDate = mostRecentArchive.getArchived(); updatedPatients = PatientDAO.getAllDate(conn, beginDate, endDate); } catch (SQLException e) { e.printStackTrace(); } catch (ServletException e) { e.printStackTrace(); } catch (ObjectNotFoundException e) { // This is the first time this has been run - archive all. updatedPatients = PatientDAO.getAllRS(conn); renderAllSites = true; } // update date of last archive before any more records get updated. Long archiveId = (Long) ArchiveDAO.save(conn, endDate, (long) 1); HashMap updatedMap = new HashMap(); while (updatedPatients.next()) { Long updatedId = updatedPatients.getLong("id"); updatedMap.put(updatedId, updatedId); } List<Site> clinics = null; if (siteAbbrev.equals("ZEP")) { // loop thorough all of the sites and render an rss file for each one. clinics = DynaSiteObjects.getClinics(); // todo: reset to the big list - preset to KAL and AIR /*clinics = new ArrayList<Site>(); Site site = (Site) DynaSiteObjects.getClinicMap().get(Long.valueOf(16)); clinics.add(site); site = (Site) DynaSiteObjects.getClinicMap().get(Long.valueOf(1)); clinics.add(site);*/ } else { clinics = new ArrayList<Site>(); Site site = (Site) DynaSiteObjects.getClinicMap().get(siteId); clinics.add(site); } // create the archive listing // StringBuffer indexBuffer = new StringBuffer(); HashMap<String, String> indexMap = new HashMap<String, String>(); int currentSiteNum = 0; int archivedPatientsAllSites = 0; for (Site site : clinics) { currentSiteNum++; int archivedPatientsThisSite = 0; siteAbbrev = site.getAbbreviation(); siteId = site.getId(); String rssFileName = org.cidrz.webapp.dynasite.Constants.ARCHIVE_PATH + siteAbbrev + Constants.pathSep + "rss.xml"; Timestamp lastBuildDate = new Timestamp(System.currentTimeMillis()); Channel feed = new Channel(); String feedType = "rss_2.0"; feed.setFeedType(feedType); feed.setDescription(siteAbbrev + " Patients"); String port = Constants.APP_PORT; feed.setLink( "http://" + ipAddress + ":" + port + "/" + "archive/" + siteAbbrev + "/rss.xml"); feed.setTitle(siteAbbrev); // Title is used to identify which site the feed is from. feed.setLastBuildDate(lastBuildDate); List entries = new ArrayList(); // Loop through the list of all patients and see if their id exists in the updatedMap ResultSet patientListResultSet = PatientDAO.getAllSiteRS(conn, siteId); ArrayList<Patient> patientRssList = new ArrayList<Patient>(); while (patientListResultSet.next()) { Long patientId = patientListResultSet.getLong("id"); String zeprsId = patientListResultSet.getString("district_patient_id"); String surname = patientListResultSet.getString("surname"); Timestamp lastModified = patientListResultSet.getTimestamp("last_modified"); Long clinicId = patientListResultSet.getLong( "site_id"); // clinic the patient belongs to - should be the same as the site // feed. Patient rssPatient = new Patient(); rssPatient.setId(patientId); rssPatient.setDistrictPatientid(zeprsId); rssPatient.setSurname(surname); rssPatient.setLastModified(lastModified); rssPatient.setSiteId(clinicId); patientRssList.add(rssPatient); } HashMap<String, String> patientRssMap = new HashMap<String, String>(); // loop through the patientRssList to weed out duplicates for (Patient patient : patientRssList) { String zeprsId = patient.getDistrictPatientid(); patientRssMap.put(zeprsId, zeprsId); } String rssMessage = "Generating RSS and Patient Xml for " + siteAbbrev + " :" + currentSiteNum + " out of " + clinics.size() + " sites. " + updatedMap.size() + " patient records to be generated."; statusMap.put("RSS-message", rssMessage); if (renderAllSites == true) { log.debug(rssMessage); } // see if there is already a subscription to this site. String importFilePath = org.cidrz.webapp.dynasite.Constants.ARCHIVE_PATH + siteAbbrev + Constants.pathSep + "import" + Constants.pathSep; String importedRssPath = importFilePath + "rss.xml"; // Reader reader = new BufferedReader((new FileReader(importedRssPath))); File importedRssFile = new File(importedRssPath); if (importedRssFile.exists() && importedRssFile.length() > 0) { final WireFeedInput input = new WireFeedInput(); WireFeed rssFeed = null; XmlReader reader = null; Channel channel = null; reader = new XmlReader(importedRssFile); try { rssFeed = input.build(reader); rssFeed.setFeedType("rss_2.0"); channel = (Channel) rssFeed; List items = channel.getItems(); // siteAbbrev = channel.getTitle(); // Create a list of patients the remote site is "interested" in. // ArrayList<String> subscribedPatientList = new ArrayList<String>(); ArrayList<Patient> subscribedPatientList = new ArrayList<Patient>(); for (int i = 0; i < items.size(); i++) { Item item = (Item) items.get(i); // String link = item.getLink(); String fileName = item.getTitle(); String zeprsId = fileName.replace(".xml", ""); try { Patient rssPatient = PatientDAO.getOneFromZeprsId(conn, zeprsId); // check if the patient is not already in the patientRssList if (patientRssMap.get(zeprsId) == null) { subscribedPatientList.add(rssPatient); } } catch (ObjectNotFoundException e) { // Patient may not yet be in system. It should be OK. } } patientRssList.addAll(subscribedPatientList); } catch (ParsingFeedException e1) { log.debug("Unable to parse RSS feed at " + importedRssPath); } reader.close(); } // while (patientListResultSet.next()) { for (Patient rssPatient : patientRssList) { Long patientId = rssPatient.getId(); String zeprsId = rssPatient.getDistrictPatientid(); String surname = rssPatient.getSurname(); Timestamp lastModified = rssPatient.getLastModified(); Long clinicId = rssPatient .getSiteId(); // clinic the patient belongs to - should be the same as the site // feed. // get the siteAbbrev from the parent's record only // no longer need this part. /*try { site = (Site) DynaSiteObjects.getClinicMap().get(clinicId); siteAbbrev = site.getAbbreviation(); } catch (Exception e) { log.error("Incorrect site data for patient: " + zeprsId + " clinicId: " + clinicId); }*/ String filePath = org.cidrz.webapp.dynasite.Constants.ARCHIVE_PATH + siteAbbrev + Constants.pathSep + "local" + Constants.pathSep; String fileName = getPatientFilename(zeprsId, surname, patientId); // write to the index // String indexValue = zeprsId + ":" + siteAbbrev + Constants.pathSep + "local" + // Constants.pathSep + fileName + "\n"; // indexBuffer.append(indexValue); indexMap.put( zeprsId, siteAbbrev + Constants.pathSep + "local" + Constants.pathSep + fileName); // check if the xml file exists. This is useful when testing - you may not have all of the // xml files generated yet. boolean forceXml = false; if (checkXml != null) { File filecheck = new File(filePath + fileName); if (filecheck.length() == 0) { log.debug("Re-rendering file for : " + fileName + " in " + siteAbbrev + "/local."); forceXml = true; } } if (updatedMap.get(patientId) != null || forceXml) { PatientRecordUtils.archivePatient(conn, patientId, siteAbbrev, filePath, fileName); archivedPatientsThisSite++; archivedPatientsAllSites++; rssMessage = "Generating RSS and Patient Xml for " + siteAbbrev + ": " + currentSiteNum + " out of " + clinics.size() + " sites. " + archivedPatientsAllSites + " out of " + updatedMap.size() + " patient records have been generated. " + "Current file: " + filePath + fileName; statusMap.put("RSS-message", rssMessage); // don't remove from the list because this patient may also be at another site // (referral). // updatedMap.remove(patientId); } if (lastModified == null) { lastModified = DateUtils.generateTimestamp(); log.debug("Null lastModified for patient: " + zeprsId); } java.util.Date pubDate = new Date(lastModified.getTime()); Item item; Description description; String url = "http://" + ipAddress + ":" + port + "/archive/" + siteAbbrev + "/local/" + fileName; item = new Item(); item.setTitle(fileName); item.setLink(url); item.setPubDate(pubDate); item.setAuthor(clinicId.toString()); Guid gd = new Guid(); gd.setPermaLink(true); gd.setValue(url); item.setGuid(gd); description = new Description(); description.setType("text/plain"); description.setValue(clinicId.toString()); item.setDescription(description); entries.add(item); } // Open the remote site RSS file and add patients who may have been transferred // ArrayList<String> remotePatientIdList = populateRemotePatientIdList(urlString); // log.debug("RSS feed being generated for " + siteAbbrev + " has " + entries.size() + " // patients. URL: " + rssFileName); sbuf.append(entries.size() + " patients added to " + siteAbbrev + "<br/>"); feed.setItems(entries); Writer writer = new FileWriter(rssFileName); WireFeedOutput output = new WireFeedOutput(); output.output(feed, writer); writer.close(); entries.clear(); } // create the archive listing /*Writer indexWriter = new FileWriter(Constants.MASTER_ARCHIVE_INDEX); indexWriter.write(indexBuffer.toString()); indexWriter.flush(); indexWriter.close();*/ XmlUtils.save(indexMap, Constants.MASTER_ARCHIVE_INDEX_XML); // log.debug("writing index file to " + Constants.MASTER_ARCHIVE_INDEX_JS); // XmlUtils.saveJson(indexMap, Constants.MASTER_ARCHIVE_INDEX_JS); updatedMap.clear(); indexMap.clear(); String rssMessage = "Completed RSS and patient XML generation task"; statusMap.put("RSS-message", rssMessage); } // Create the archive // Compress.zipDirectory(Constants.ARCHIVE_PATH, Constants.MASTER_ARCHIVE_ZIP); // Zip.zip(Constants.ARCHIVE_PATH, Constants.MASTER_ARCHIVE_ZIP, // Constants.MASTER_ARCHIVE_CHECKSUM); // Zip.CreateZipFile(new File(Constants.MASTER_ARCHIVE_ZIP), new File[]{new // File(Constants.ARCHIVE_PATH)}, false); message = sbuf.toString(); return message; }