Skip to content

Commit

Permalink
Signed-off-by: Juan Saad <[email protected]>
Browse files Browse the repository at this point in the history
  • Loading branch information
juansaad committed Aug 19, 2017
1 parent 1b199f9 commit cabff08
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 0 deletions.
3 changes: 3 additions & 0 deletions dportenis-rest-broker/WebContent/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Class-Path:

1 change: 1 addition & 0 deletions dportenis-rest-broker/WebContent/WEB-INF/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/classes/
15 changes: 15 additions & 0 deletions dportenis-rest-broker/WebContent/WEB-INF/ibm-web-ext.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-ext
xmlns="http://websphere.ibm.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-web-ext_1_1.xsd"
version="1.1">

<reload-interval value="3"/>
<context-root uri="dportenis-rest-broker" />
<enable-directory-browsing value="false"/>
<enable-file-serving value="true"/>
<enable-reloading value="true"/>
<enable-serving-servlets-by-class-name value="false" />

</web-ext>
22 changes: 22 additions & 0 deletions dportenis-rest-broker/WebContent/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<display-name>dportenis-rest-broker</display-name>
<servlet>
<description>
JAX-RS Tools Generated - Do not modify</description>
<servlet-name>JAX-RS Servlet</servlet-name>
<servlet-class>com.ibm.websphere.jaxrs.server.IBMRestServlet</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.dportenis.rest.broker.BrokerApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<enabled>true</enabled>
<async-supported>false</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>JAX-RS Servlet</servlet-name>
<url-pattern>
/jaxrs/*</url-pattern>
</servlet-mapping>
</web-app>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.dportenis.rest.broker;

import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.core.Application;

import com.dportenis.rest.module.OrdenCompra;

public class BrokerApplication extends Application {

@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> classes = new HashSet<Class<?>>();
classes.add(OrdenCompra.class);
return classes;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.dportenis.rest.module;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.ibm.json.java.JSONObject;

@Path(value="/po")
public class OrdenCompra {

public OrdenCompra() {
}

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path(value="/datos")
public JSONObject doExec(JSONObject requestObj)
{
JSONObject responseObj = new JSONObject();
int code = 0;
String message = null;

try
{
message = "mensaje recibido: " + requestObj.toString();
}
catch (Exception e)
{
code = 1;
message = e.getMessage();
e.printStackTrace();
}
finally
{
responseObj.put("codigo", code);
responseObj.put("mensaje", message);
}

return responseObj;
}
}
72 changes: 72 additions & 0 deletions dportenis-rest-broker/test/com/deportenis/rest/test/TestREST.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.deportenis.rest.test;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.ibm.json.java.JSONObject;

public class TestREST {

private String url = "http://localhost:9080/dportenis-rest-broker/jaxrs/po/datos";
private int timeout = 10000; // default to 10 secs

@Before
public void setUp() throws Exception {
}

@After
public void tearDown() throws Exception {
}

@Test
public void testSendData() {
JSONObject request = new JSONObject();
request.put("batchId", "XXXXXX");
try {
setTimeout(5000);
JSONObject response = doPost(request);
System.out.println(response.toString());
} catch (Exception e) {
e.printStackTrace();
}
}

private void setTimeout(int timeout) {
this.timeout = timeout;
}

private JSONObject doPost(JSONObject data) throws Exception {
// Create HTTP Client
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(timeout)
.setSocketTimeout(timeout).build();
HttpClient httpClient = HttpClientBuilder.create().setDefaultRequestConfig(config).build();

// Create HTTP Request
HttpPost request = new HttpPost(url);

// Add additional headers
request.addHeader("Content-Type", "application/json");

// Set Body
StringEntity entity = new StringEntity(data.serialize());
request.setEntity(entity);

// Execute your request and catch response
HttpResponse response = httpClient.execute(request);

// Get string response
HttpEntity resEntity = response.getEntity();
return JSONObject.parse(IOUtils.toString(resEntity.getContent()));
}

}

0 comments on commit cabff08

Please sign in to comment.