Skip to content
Snippets Groups Projects
Commit f4fbadf5 authored by Andres Nebel's avatar Andres Nebel
Browse files

Version inicial en FING

parents
No related branches found
No related tags found
No related merge requests found
Showing
with 679 additions and 0 deletions
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.conespecifico1</groupId>
<artifactId>conespecifico1</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>conespecifico1 Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.bundles</groupId>
<artifactId>jaxrs-ri</artifactId>
<version>2.16</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.16</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.6</version>
</dependency>
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180130</version>
</dependency>
</dependencies>
<build>
<finalName>conespecifico1</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<jersey.version>1.7</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
package conespecifico1;
import static java.lang.System.getenv;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
public class Connector implements ServletContextListener {
private Thread myThread = null;
public void contextInitialized(ServletContextEvent sce) {
if ((myThread == null) || (!myThread.isAlive())) {
myThread = new Thread(new IncomingMsgProcess(), "IdleConnectionKeepAlive");
myThread.start();
}
}
public void contextDestroyed(ServletContextEvent sce){
try {
myThread.interrupt();
} catch (Exception ex) {}
}
public static boolean isEmpty(String str) {
return str == null || str.trim().length() == 0;
}
class IncomingMsgProcess implements Runnable {
@Override
public void run() {
System.out.println("Conector C1: Inicializando Msg Endpoint..");
ConnectionFactory factory = new ConnectionFactory();
String hostRabbit = getenv("OPENSHIFT_RABBITMQ_SERVICE_HOST");
factory.setHost(hostRabbit);
Connection connection;
try {
connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare("conespecifico1", false, false, false, null);
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope,
AMQP.BasicProperties prop, byte[] body)
throws IOException {
String message = new String(body, "UTF-8");
sendMessageToC1(message);
}
};
channel.basicConsume("conespecifico1", true, consumer);
System.out.println("Conector C1: Todo listo. Esperando pedidos...");
} catch (IOException | TimeoutException e) {
e.printStackTrace();
}
}
public void sendMessageToC1(String message) {
HttpPost req = new HttpPost(getC1URL());
HttpClient httpClient = HttpClients.createDefault();
try {
req.setEntity(new StringEntity(message));
req.setHeader("Content-Type", "application/json");
httpClient.execute(req);
} catch (Exception e) {
e.printStackTrace();
}
}
public String getC1URL() {
String resourcePath = "/sistemac1/pipoc/receive";
String baseUrl = "";
if (!isEmpty(getenv("SISTEMAC1_SERVICE_HOST")) && !isEmpty(getenv("SISTEMAC1_SERVICE_PORT")))
baseUrl = "http://" + getenv("SISTEMAC1_SERVICE_HOST") + ":" + System.getenv("SISTEMAC1_SERVICE_PORT");
return baseUrl + resourcePath;
}
}
}
package conespecifico1;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("pipoc")
public class JAXApp extends Application {
}
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<listener>
<listener-class>conespecifico1.Connector</listener-class>
</listener>
</web-app>
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
File added
File added
File added
File added
Manifest-Version: 1.0
Built-By: andres
Build-Jdk: 1.8.0_144
Created-By: Maven Integration for Eclipse
#Generated by Maven Integration for Eclipse
#Sat Mar 24 11:37:12 UYT 2018
version=0.0.1-SNAPSHOT
groupId=org.conespecifico1
m2e.projectName=conespecifico1
m2e.projectLocation=/home/andres/Documents/poc/conespecifico1
artifactId=conespecifico1
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.conespecifico1</groupId>
<artifactId>conespecifico1</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>conespecifico1 Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.bundles</groupId>
<artifactId>jaxrs-ri</artifactId>
<version>2.16</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.16</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.6</version>
</dependency>
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180130</version>
</dependency>
</dependencies>
<build>
<finalName>conespecifico1</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<jersey.version>1.7</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.conespecifico2</groupId>
<artifactId>conespecifico2</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>conespecifo2 Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.bundles</groupId>
<artifactId>jaxrs-ri</artifactId>
<version>2.16</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.16</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.6</version>
</dependency>
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>conespecifico2</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<jersey.version>1.7</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
package conespecifico2;
import static java.lang.System.getenv;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeoutException;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class Connector implements ServletContextListener {
private TimerTask pollTimer = null;
public void contextInitialized(ServletContextEvent sce) {
if (pollTimer == null) {
pollTimer = new PollTimerTask();
Timer timer = new Timer();
int freq = Integer.parseInt(getenv("poll_frequency_ms"));
timer.schedule(pollTimer, 5000, freq);
}
}
public void contextDestroyed(ServletContextEvent sce){
try {
System.out.println("Sistema2 poller has been shutdown");
} catch (Exception ex) {}
}
public static boolean isEmpty(String str) {
return str == null || str.trim().length() == 0;
}
class PollTimerTask extends TimerTask {
public String urlSistema2 = "";
public String hostRabbit = "";
public String nextStep = "";
@Override
public void run() {
if (urlSistema2.equals("")) {
urlSistema2 = getSistema2URL();
}
HttpGet req = new HttpGet(urlSistema2);
HttpClient httpClient = HttpClients.createDefault();
HttpResponse internalResponse;
String responseStr = "";
try {
internalResponse = httpClient.execute(req);
responseStr = EntityUtils.toString(internalResponse.getEntity());
int statusCode = internalResponse.getStatusLine().getStatusCode();
if (statusCode == 200)
sendAsyncMessage2NextStep(responseStr);
else {
urlSistema2 = getSistema2URL(); //Refresco el host+port porque no anduvo bien
System.out.println("Respuesta de error desde S2: status" +statusCode);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void sendAsyncMessage2NextStep(String message) {
ConnectionFactory factory = new ConnectionFactory();
if (hostRabbit.equals("")) {
hostRabbit = getenv("OPENSHIFT_RABBITMQ_SERVICE_HOST");
}
if (nextStep.equals("")) {
nextStep = getNextStep();
}
factory.setHost(hostRabbit);
Connection connection;
try {
connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(nextStep, false, false, false, null);
//System.out.println("Conector Especifico 2: Invocando al proximo paso de la SI: " + getNextStep());
channel.basicPublish("", nextStep, null, message.getBytes("UTF-8"));
//System.out.println("Conector Especifico 2: Mensaje enviado. Tamaño: "+ message.length());
} catch (IOException | TimeoutException e) {
hostRabbit = getenv("OPENSHIFT_RABBITMQ_SERVICE_HOST"); //Refresco porque el envio no anduvo bien
nextStep = getNextStep();
sendAsyncMessage2NextStep(message);
e.printStackTrace();
}
}
public String getNextStep(){
return getenv("nextstep");
}
public String getSistema2URL(){
String resourcePath = getenv("sistemaorigen_syncpath");
String baseUrl = "";
String originSystemName = getenv("sistemaorigen_nombre").toUpperCase();
if (!isEmpty(getenv(originSystemName+"_SERVICE_HOST")) && !isEmpty(getenv(originSystemName+"_SERVICE_PORT")))
baseUrl = "http://" + getenv(originSystemName+"_SERVICE_HOST") + ":" + System.getenv(originSystemName+"_SERVICE_PORT");
return baseUrl + resourcePath;
}
}
}
\ No newline at end of file
package conespecifico2;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("pipoc")
public class JAXApp extends Application {
}
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<listener>
<listener-class>conespecifico2.Connector</listener-class>
</listener>
</web-app>
Manifest-Version: 1.0
Built-By: andres
Build-Jdk: 1.8.0_144
Created-By: Maven Integration for Eclipse
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.filtro</groupId>
<artifactId>filtro</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>filtro Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.bundles</groupId>
<artifactId>jaxrs-ri</artifactId>
<version>2.16</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.16</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180130</version>
</dependency>
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.6</version>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.3.0</version>
</dependency>
</dependencies>
<build>
<finalName>filtro</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<jersey.version>1.7</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
package filter;
import static java.lang.System.getenv;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
public class AsyncEndpoint implements ServletContextListener {
private Thread myThread = null;
public void contextInitialized(ServletContextEvent sce) {
if ((myThread == null) || (!myThread.isAlive())) {
myThread = new Thread(new IncomingMsgProcess(), "IdleConnectionKeepAlive");
myThread.start();
}
}
public void contextDestroyed(ServletContextEvent sce){
try {
myThread.interrupt();
} catch (Exception ex) {}
}
class IncomingMsgProcess implements Runnable {
@Override
public void run() {
System.out.println("Filtro: Inicializando Msg Endpoint..");
ConnectionFactory factory = new ConnectionFactory();
String hostRabbit = getenv("OPENSHIFT_RABBITMQ_SERVICE_HOST");
factory.setHost(hostRabbit);
Connection connection;
try {
connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare("filtro", false, false, false, null);
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope,
AMQP.BasicProperties prop, byte[] body)
throws IOException {
String message = new String(body, "UTF-8");
String nextStep = getNextStep();
try {
String processedData = Filter.filter(message);
if (!getNextStep().equals("Fin"))
sendAsyncMessage2NextStep(processedData, nextStep);
}
catch(Exception e) {
System.out.println("Catch: "+ e.toString());
}
}
};
channel.basicConsume("filtro", true, consumer);
System.out.println("Filtro: Todo listo. Esperando pedidos...");
} catch (IOException | TimeoutException e) {
e.printStackTrace();
}
}
public void sendAsyncMessage2NextStep(String message, String nextStep) {
ConnectionFactory factory = new ConnectionFactory();
String hostRabbit = getenv("OPENSHIFT_RABBITMQ_SERVICE_HOST");
factory.setHost(hostRabbit);
Connection connection;
try {
connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(nextStep, false, false, false, null);
channel.basicPublish("", nextStep, null, message.getBytes("UTF-8"));
} catch (IOException | TimeoutException e) {
e.printStackTrace();
}
}
public String getNextStep(){
return getenv("nextstep");
}
}
}
package filter;
import static java.lang.System.getenv;
import java.util.List;
import com.jayway.jsonpath.JsonPath;
public class Filter {
public static String filter(String json) {
String filter = getenv("FILTER_QUERY");
System.out.println("FILTERING:"+filter);
System.out.println("JSON:"+json);
List<String> list = JsonPath.parse(json).read(filter);
return list.toString();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment