PageRenderTime 817ms CodeModel.GetById 31ms RepoModel.GetById 2ms app.codeStats 1ms

/branches/ZCrawl/ZCrawlSources/src/org/zonales/crawlConfig/plugins/unpublishers/StandardUnpublisher.java

https://gitlab.com/BGCX261/zonales-svn-to-git
Java | 73 lines | 57 code | 7 blank | 9 comment | 7 complexity | 4dd34f63e121674cf2e934feaf77ed92 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package org.zonales.crawlConfig.plugins.unpublishers;
  6. import com.google.gson.Gson;
  7. import com.mongodb.MongoException;
  8. import java.net.HttpURLConnection;
  9. import java.util.Properties;
  10. import java.util.logging.Level;
  11. import java.util.logging.Logger;
  12. import org.zonales.ZGram.ZGram;
  13. import org.zonales.ZGram.daos.ZGramDao;
  14. import org.zonales.crawlConfig.objets.State;
  15. import org.zonales.errors.ZMessage;
  16. import org.zonales.errors.ZMessages;
  17. import org.zonales.helpers.ConnHelper;
  18. /**
  19. *
  20. * @author juanma
  21. */
  22. public class StandardUnpublisher implements Unpublisher {
  23. @Override
  24. public String unpublish(ZGram zgram, String id, Properties props) {
  25. try {
  26. //Saco del Scheduler la extracción
  27. HttpURLConnection connection = ConnHelper.getURLConnection(props.getProperty("ZCrawlSchedulerURL") + "unschedulingJob?id=" + zgram.getId().get$oid(), Integer.valueOf(props.getProperty("timeout")));
  28. String zMessageJson;
  29. Gson zMessageGson = new Gson();
  30. ZMessage zmessage = new ZMessage();
  31. int code = connection.getResponseCode();
  32. Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Unscheduling Job - Código de respuesta: {0}", code);
  33. if (code == 200) {
  34. zMessageJson = ConnHelper.getStringFromInpurStream(connection.getInputStream());
  35. Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Unscheduling Job - Respuesta: {0}", zMessageJson);
  36. zmessage = zMessageGson.fromJson(zMessageJson, ZMessage.class);
  37. connection.disconnect();
  38. } else {
  39. Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Error eliminando Job en el Scheduler");
  40. return ZMessages.ZSCHEDULER_CONN_ERROR.toString();
  41. }
  42. if (zmessage.getCod() == 100) {
  43. zgram.setEstado(State.UNPUBLISHED);
  44. ZGramDao zGramDao = new ZGramDao(props.getProperty("db_host"), Integer.valueOf(props.getProperty("db_port")), props.getProperty("db_name"));
  45. zGramDao.update(id, zgram);
  46. Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Extracción actualizada {0}", new Object[]{zgram});
  47. return ZMessages.SUCCESS.toString();
  48. } else {
  49. Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Error en Scheduler");
  50. return zmessage.toString();
  51. }
  52. } catch (MongoException ex) {
  53. Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Error actualizando extracción {0}: {1}", new Object[]{zgram, ex.getMessage()});
  54. return ZMessages.MONGODB_ERROR.toString();
  55. } catch (Exception ex) {
  56. StringBuilder stacktrace = new StringBuilder();
  57. for (StackTraceElement line : ex.getStackTrace()) {
  58. stacktrace.append(line.toString());
  59. stacktrace.append("\n");
  60. }
  61. Logger.getLogger(this.getClass().getName()).log(Level.SEVERE,
  62. "EXCEPCION: {0}\nTRACE: {1}", new Object[]{ex, stacktrace.toString()});
  63. return ZMessages.UNKNOWN_ERROR.toString();
  64. }
  65. }
  66. }