Today i wanna share about Receiving Http Request using jPOS. when we talk about http request, its mean that we can use restful or soap Service. In this post, we will use Restful service as example. Mostly when we use restful service, we use JSON as data format.
First, jPOS is TCP based framework. its not possible to receive http request, but it’s possible if we can integrate jPOS with Servlet container like jetty. Jetty will open connection to receive http/s request, and then we can manipulate the data, wether the request is forwarded to another server using another protocols or just processed in internal system.
Like my another tutorials, we will create custom jPOS MBean interface that extends QBeanSupportMBean interface so we can deploy this class by putting xml configuration into Q2 deploy folder.
Before we can Receiving Http Request using jPOS, this is list of dependency library for run this project. Please create maven project or configure your java project to maven project. on eclipse you can configure your java project by right click on your project –> Configure –> convert to maven project.
[xml]
[/xml]
[java]
package com.didikh.rest;
import org.jpos.q2.QBeanSupportMBean;
public interface QRestServerMBean extends QBeanSupportMBean {
void setPort(int port);
int getPort();
}
[/java]
After we create QRestServerMBean interface, we need to create the implementation class. this class will start jetty server with specific port base on xml parameter that we will create later. This class also get the controller and include it to jetty servlet container. let’s call this class QRestServer.
Please share this post to unlock the QRestServer code below. š
[java]
package com.didikh.rest;
import java.lang.reflect.Constructor;
import java.util.Iterator;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.servlet.ServletContainer;
import org.jdom.Element;
import org.jpos.iso.ISOPackager;
import org.jpos.q2.QBeanSupport;
import org.jpos.q2.QFactory;
import org.jpos.util.NameRegistrar;
/**
* Starting the jetty server
* @author Didik Hari
* @since 26 May 2016
*/
public class QRestServer extends QBeanSupport implements QRestServerMBean {
private Server server;
private int port;
public QRestServer() {
super();
}
@Override
protected void startService() throws Exception {
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath(“/”);
context.addServlet(new ServletHolder(new ServletContainer(resourcesConfig())), “/*”);
server = new Server(port);
server.setHandler(context);
server.start();
NameRegistrar.register(getName(), this);
}
@SuppressWarnings(“rawtypes”)
private ResourceConfig resourcesConfig() throws Exception {
ResourceConfig resourceConfig = new ResourceConfig();
QFactory factory = getFactory();
Iterator iterator = getPersist().getChildren(“rest-listener”).iterator();
while (iterator.hasNext()) {
Element l = (Element) iterator.next();
Class> cl = Class.forName(l.getAttributeValue (“class”));
Constructor> cons = cl.getConstructor(ISOPackager.class);
String packagerName = l.getAttributeValue (“packager”);
ISOPackager packager = null;
if (packagerName != null) {
packager = (ISOPackager) factory.newInstance (packagerName);
}
RestListener listener = (RestListener) cons.newInstance(packager);
factory.setLogger (listener, l);
factory.setConfiguration (listener, l);
resourceConfig.register(listener);
}
return resourceConfig;
}
@Override
protected void stopService() throws Exception {
server.stop();
server.destroy();
NameRegistrar.unregister(getName());
}
@Override
public void setPort(int port) {
this.port = port;
}
@Override
public int getPort() {
return this.port;
}
}
[/java]
The QRestServer class automatically register the object to NameRegistrar once the jetty server start and unregister while the Q2 server is shutdown. So, you can get the object by calling NameRegistrar.get(key) method.
In my case, i need to inject jPOS packager to controller class, so i can use it for further actions. Create class called RestListener that will hold the packager, and any controller that implement RestListener class can access it.
[java]
package com.didikh.rest;
import org.jpos.iso.ISOPackager;
import org.jpos.util.LogSource;
import org.jpos.util.Logger;
/**
* This class is used for manual packager injection (future usage)
* limit: one Listener only have one packager
* @author Didik Hari
*
*/
public class RestListener implements LogSource {
protected ISOPackager packager;
protected Logger logger;
protected String realm;
public RestListener(ISOPackager packager) {
this.packager = packager;
}
@Override
public void setLogger(Logger logger, String realm) {
this.logger = logger;
this.realm = realm;
}
@Override
public String getRealm() {
return this.realm;
}
@Override
public Logger getLogger() {
return this.logger;
}
}
[/java]
And this is the example of XML configuration to deploy this modul. call it 70_example-rest-server.xml and place it to your jPOS Q2 deploy directory.
[xml]
[/xml]
the class attribute in rest-listener tag is a class that extends RestListener, this will receive any http/s request. Below is the example of the Controller class.
[java]
package com.didikh.rest.controller;
import java.sql.Timestamp;
import java.util.Date;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jetty.util.StringUtil;
import org.hibernate.Session;
import org.jpos.core.Configurable;
import org.jpos.core.Configuration;
import org.jpos.core.ConfigurationException;
import org.jpos.iso.ISOPackager;
import org.jpos.util.LogEvent;
import org.jpos.util.LogSource;
import org.jpos.util.Logger;
import org.jpos.util.NameRegistrar;
import org.jpos.util.NameRegistrar.NotFoundException;
import com.didikh.rest.QRestClient;
import com.didikh.rest.RestListener;
@Path(“/jpos-rest/v1/”)
public class TestController extends RestListener implements LogSource, Configurable {
public TestController(ISOPackager packager) {
super(packager);
}
@POST
@Path(value = “account-status”)
@Produces(MediaType.APPLICATION_JSON)
public Response accountStatus(final String input) {
try {
String responseJson = “[{‘msg’:’this is response’}]”;
return Response.status(200).entity(responseJson).build();
} catch (Exception e) {
e.printStackTrace();
}
return Response.status(204).build();
}
@Override
public void setConfiguration(Configuration cfg)
throws ConfigurationException {
// TODO getting the config
}
}
[/java]
This example of code maybe error on import section, because i modify this class on the dashboard blog, not on the eclipse IDE. I think its all of Receiving Http Request using jPOS Tutorials, thank you for visiting my blog. if you have any question, concern or correction we can discuss it on comment section.
Leave a Reply