Kyrexar

[IAP] Conector

Oct 27th, 2019 (edited)
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.35 KB | None | 0 0
  1. package conector;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.net.HttpURLConnection;
  7. import java.net.URL;
  8. import java.util.Scanner;
  9. import java.util.concurrent.TimeoutException;
  10.  
  11. import org.json.JSONException;
  12. import com.rabbitmq.client.BuiltinExchangeType;
  13. import com.rabbitmq.client.Channel;
  14. import com.rabbitmq.client.Connection;
  15. import com.rabbitmq.client.ConnectionFactory;
  16.  
  17. public class Conector {
  18.     private final static String NOMBRE_EXCHANGE = "ajuntament";
  19.  
  20.     public static void main(String[] args) throws IOException, JSONException, TimeoutException {
  21.  
  22.         String userAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36";
  23.         String authorization = "Basic aW5mb3JtYXR1cHY6dHlBMGJxVWU=";
  24.  
  25.         Scanner ent = new Scanner(System.in);
  26.         String url;
  27.         URL objUrl;
  28.         HttpURLConnection connection;
  29.         BufferedReader in;
  30.         StringBuffer response;
  31.         ConnectionFactory factory;
  32.         Connection connectionF;
  33.         Channel channel;
  34.        
  35.         String mensaje = "";
  36.        
  37.  
  38.         while (!mensaje.equals("salir")) {
  39.             System.out.println("�Qu� datos deseas buscar (trafico o bicis)?");
  40.             mensaje = ent.nextLine();
  41.  
  42.             if (mensaje.equals("trafico")) {
  43.                 // GET
  44.                 url = "http://mapas.valencia.es/lanzadera/opendata/Tra-estado-trafico/JSON";
  45.                
  46.                 // Creamos URL
  47.                 objUrl = new URL(url);
  48.                
  49.                 // Creamos conexion HTTP
  50.                 connection = (HttpURLConnection) objUrl.openConnection();
  51.                
  52.                 // Definimos operacion HTTP
  53.                 connection.setRequestMethod("GET");
  54.                
  55.                 // A�adimos encabezados
  56.                 connection.setRequestProperty("authorization", authorization);
  57.                 connection.setRequestProperty("user-agent", userAgent);
  58.                
  59.                 // Procesamos la respuesta
  60.                 in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  61.                 String inputLine;
  62.                 response = new StringBuffer();
  63.                 while ((inputLine = in.readLine()) != null) {
  64.                     response.append(inputLine);
  65.                 }
  66.                 in.close();
  67.                
  68.                
  69.                 String trafico = response.toString();
  70.                
  71.                
  72.                 //JSONArray coleccionT = new JSONArray(trafico);
  73.                
  74.                 // Conectarse con RabbitMQ
  75.                 factory = new ConnectionFactory();
  76.                 factory.setHost("localhost");
  77.                 connectionF = factory.newConnection();
  78.                
  79.                 // Crear un canal de comunicaci�n
  80.                 channel = connectionF.createChannel();
  81.                
  82.                 // Declaramos un Exchange de tipo Topic
  83.                 channel.exchangeDeclare(NOMBRE_EXCHANGE, BuiltinExchangeType.TOPIC);
  84.             /*
  85.                 for (int i = 0; i < coleccionT.length(); i++) {
  86.                     JSONObject traf = coleccionT.getJSONObject(i);
  87.                     channel.basicPublish(NOMBRE_EXCHANGE, trafico, null, traf.toString().getBytes());
  88.                 }*/
  89.                
  90.                 channel.basicPublish(NOMBRE_EXCHANGE, mensaje, null, trafico.getBytes());
  91.  
  92.                 // Cerrar canal y conexion, si no se quiere enviar mas
  93.                 channel.close();
  94.                 connectionF.close();
  95.             } else {
  96.                 // GET
  97.                 url = "http://gobiernoabierto.valencia.es/va/resource/?ds=estacionesvalenbisi&id=cb45daa3-9e97-4873-a2e5-4cd3138f8cd2";
  98.                
  99.                 // Creamos URL
  100.                 objUrl = new URL(url);
  101.                
  102.                 // Creamos conexion HTTP
  103.                 connection = (HttpURLConnection) objUrl.openConnection();
  104.                
  105.                 // Definimos operacion HTTP
  106.                 connection.setRequestMethod("GET");
  107.                
  108.                 // A�adimos encabezados
  109.                 connection.setRequestProperty("authorization", authorization);
  110.                 connection.setRequestProperty("user-agent", userAgent);
  111.                
  112.                 // Procesamos la respuesta
  113.                 in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  114.                 String inputLine;
  115.                 response = new StringBuffer();
  116.                 while ((inputLine = in.readLine()) != null) {
  117.                     response.append(inputLine);
  118.                 }
  119.                 in.close();
  120.                
  121.                 String bicis = response.toString();
  122.                
  123.                 // Conectarse con RabbitMQ
  124.                 factory = new ConnectionFactory();
  125.                 factory.setHost("localhost");
  126.                 connectionF = factory.newConnection();
  127.                
  128.                 // Crear un canal de comunicaci�n
  129.                 channel = connectionF.createChannel();
  130.                
  131.                 // Declaramos un Exchange de tipo Topic
  132.                 channel.exchangeDeclare(NOMBRE_EXCHANGE, BuiltinExchangeType.TOPIC);
  133.                
  134.                 channel.basicPublish(NOMBRE_EXCHANGE, mensaje, null, bicis.getBytes());
  135.                
  136.                 // Cerrar canal y conexion, si no se quiere enviar mas
  137.                 channel.close();
  138.                 connectionF.close();
  139.             }
  140.         }
  141.         ent.close();
  142.  
  143.     }
  144. }
Add Comment
Please, Sign In to add comment