Skip to content

DiKode

Coding Journal

Menu
  • Home
  • Author
  • Contact Me
  • Category
    • Java
    • Javascript
  • Privacy Policy
Menu

Receiving Http Request using jPOS

Posted on June 2, 2016October 26, 2017 by didikhari

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.

Receiving Http Request using jPOS

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]


org.eclipse.jetty
jetty-server
9.2.11.v20150529


org.eclipse.jetty
jetty-servlet
9.2.11.v20150529


org.jpos
jpos
1.9.2


org.glassfish.jersey.core
jersey-server
2.7


org.glassfish.jersey.containers
jersey-container-servlet-core
2.7


org.glassfish.jersey.containers
jersey-container-jetty-http
2.7


org.glassfish.jersey.media
jersey-media-moxy
2.7



com.fasterxml.jackson.core
jackson-databind
2.7.3



org.apache.commons
commons-lang3
3.3.2


[/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]

7070




[/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.

Receiving Http Request using jPOS
Receiving Http Request using jPOS
Receiving Http Request using jPOS
Receiving Http Request using jPOS
Receiving Http Request using jPOS
Receiving Http Request using jPOS
Receiving Http Request using jPOS
Receiving Http Request using jPOS
Receiving Http Request using jPOS
Receiving Http Request using jPOS
Receiving Http Request using jPOS
Receiving Http Request using jPOS
Receiving Http Request using jPOS

14 thoughts on “Receiving Http Request using jPOS”

  1. Venkat says:
    June 21, 2016 at 10:40 am

    Hi Didik,

    Your blogs help me understand JPOS much better
    Thanks for your examples.
    I have question regarding this Jetty module interface with jPOS.
    get this error:

    org\jpos\aggregator\rest\QRestServer.java:5: error: package org.eclipse.jetty.server does not exist
    import org.eclipse.jetty.server.Server;
    I am new to jPOS.
    I have downloaded Jetty and Jetty-All.jar file.
    I dont know how to include Jetty in jPOS build. You are help could be useful to me

    Regards,
    Venkat

    Reply
    1. Didik H says:
      June 23, 2016 at 2:47 pm

      Hi Venkat,

      i just update this post and add required library to run the project, please configure your project to maven project for automatically download dependency library.
      please let me know if you are still getting this error.

      Regards,
      Didik

      Reply
  2. Francisco says:
    June 29, 2016 at 10:08 pm

    Hi had a problem with jetty performance with a high volume of transactions so I’m trying something to read http from JPOS

    Reply
    1. Didik H says:
      June 30, 2016 at 4:09 pm

      Hi, Have you perform load test for the service? are you sure thats really jetty’s issue?

      Reply
  3. Sihar says:
    October 19, 2016 at 7:44 am

    Mas Didik,

    Isi file QRestClient belum dijelaskan.

    Reply
  4. sihar says:
    October 21, 2016 at 7:08 am

    I get this error when write QRestServer.java
    The method getPort() of type QRestServer must override a superclass method QRestServer.java
    The method setPort(int) of type QRestServer must override a superclass method QRestServer.java

    Reply
  5. cy says:
    January 10, 2017 at 2:28 pm

    Hi Didik,
    May i know how can i set the Q2 servers to accept OPTIONS header? before i switch to use rest json method due to CORS.

    Reply
  6. george says:
    February 14, 2017 at 4:41 pm

    Hi , is it possible to write out the request and response to a physical log file, ex through jpos util?

    Reply
    1. Didik H says:
      February 20, 2017 at 6:53 am

      Yes, sure.. jPOS already did it. deploy the 00_logger.xml config

      Reply
  7. fakhri says:
    August 13, 2018 at 2:08 pm

    how do i run the server which got created

    Reply
    1. didikhari says:
      August 29, 2018 at 11:56 am

      you can run using jetty

      Reply
  8. ed tshuma says:
    August 24, 2018 at 7:47 am

    hi @Didik..I have a REST web service in Jersey on a tomcat container..Can i use your example to pack messages to IS08583..What do i need to change.. My endpoint produces JSON also

    Reply
    1. didikhari says:
      August 29, 2018 at 11:56 am

      hi ed, sorry for late response.. i think you can create a class like XMLPackager that support pack/unpack from xml to iso8583,
      here JSONPackager from jpos repository that convert json to iso8583.

      Reply
  9. Ram says:
    February 25, 2020 at 5:35 pm

    How to connect with QServer from Spring Boot?

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • Java
  • Javascript
  • jPOS

Recent Post

  • jPOS Server in-out
  • How to Configure Basic Auth Spring RestTemplate
  • Create Custom jPOS ISO8583 Packager
  • Binding Restful XML Parameter
  • Create Dynamic Tree Grid using TreeGrid jQuery Plugin

Tags

Asynchronous Request async request Bind Json Param custom channel custom jPOS packager datatables Design Pattern hibernate http http basic auth http request iso 8583 java javascript jPOS jpos channel jPOS Client jPOS database jPOS deploy directory jPOS Hibernate jpos http jpos rest jPOS Server jpos Space jquery js jsf load balancer moxy mux mux pool prettyfaces primefaces rest client Restful SAF space Specifications spring framework TCP Template Method transaction manager transaction participant tree TreeGrid

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
©2021 DiKode | Built using WordPress and Responsive Blogily theme by Superb