How to Receive a Message from the Oracle Messaging Cloud Service with JAX-RS

import java.io.IOException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Cookie;
import javax.ws.rs.core.Response;

import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;

@SuppressWarnings("serial")
@WebServlet(name = "SampleOmcsServlet", value = "/")
public final class SampleOmcsServlet extends HttpServlet {

  @Override
  protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws IOException {
    final Client client = ClientBuilder.newClient();

    client.register(HttpAuthenticationFeature.basic("username", "password"));

    Response res = client.target("https://host/service/api/v1/connections/SampleConnection").request().header("X-OC-ID-TOKEN-STATUS", "disabled").put(Entity.json(""));

    final Cookie jsessionid = res.getCookies().get("JSESSIONID");

    client.target("https://host/service/api/v1/sessions/SampleSession").queryParam("connection", "SampleConnection").request().cookie(jsessionid).put(Entity.json(""));

    client.target("https://host/service/api/v1/consumers/SampleConsumer").queryParam("destination", "/queues/SampleQueue").queryParam("session", "SampleSession").request().cookie(str).put(Entity.json(""));

    client.target("https://host/service/api/v1/connections/SampleConnection").queryParam("action", "start").request().cookie(jsessionid).post(Entity.json(""));

    res = client.target("https://host/service/api/v1/consumers/SampleConsumer/messages").queryParam("timeout", 1000).request().cookie(jsessionid).post(Entity.json(""));

    resp.getWriter().println(res.readEntity(String.class));

    client.close();
  }

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s