Tag Archives: Java Servlet API

How to Send a Push Notification to AWS SNS with Java

import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.sns.AmazonSNSClient;

public final class SnsSample {

  public static void main(final String[] args) throws Exception {
    final AmazonSNSClient client = new AmazonSNSClient(new BasicAWSCredentials("accessKeyId", "secretAccessKey"));

    client.publish("topicArn", "Simples Assim"");

    client.shutdown();
  }

}
Advertisements

How to Send a Push Notification to AWS SNS with JAX-RS

import static org.apache.commons.codec.digest.DigestUtils.sha256Hex;
import static org.apache.commons.codec.digest.HmacUtils.hmacSha256;
import static org.apache.commons.codec.digest.HmacUtils.hmacSha256Hex;

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.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;

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

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

    final String stringToSign = "AWS4-HMAC-SHA256\nlongDate\nshortDate/regionName/sns/aws4_request\n" + sha256Hex("POST\n/accountId/SampleTopic\n\nhost:host\nx-amz-date:longDate\n\nhost;x-amz-date\n" + sha256Hex("Action=Publish&Message=Simples+Assim&TopicArn=topicArn"));

    final String signature = hmacSha256Hex(hmacSha256(hmacSha256(hmacSha256(hmacSha256("AWS4secretKey", shortDate), "regionName".getBytes()), "sns".getBytes()), "aws4_request".getBytes()), stringToSign.getBytes());

    final MultivaluedMap<String, String> params = new MultivaluedHashMap<>();

    params.putSingle("Action", "Publish");

    params.putSingle("Message", "Simples Assim");

    params.putSingle("TopicArn", "topicArn");

    client.target("queueUrl").request().header("X-Amz-Date", longDate).header("Authorization", "AWS4-HMAC-SHA256 Credential=accessKeyId/shortDate/regionName/sns/aws4_request, SignedHeaders=host;x-amz-date, Signature=" + signature).post(Entity.form(params));

    client.close();
  }

}

How to Receive a Message from AWS SQS with JAX-RS

import static java.lang.System.out;
import static org.apache.commons.codec.digest.DigestUtils.sha256Hex;
import static org.apache.commons.codec.digest.HmacUtils.hmacSha256;
import static org.apache.commons.codec.digest.HmacUtils.hmacSha256Hex;

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.Form;
import javax.ws.rs.core.Response;

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

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

    final String stringToSign = "AWS4-HMAC-SHA256\nlongDate\nshortDate/regionName/sqs/aws4_request\n" + sha256Hex("POST\n/accountId/SampleQueue\n\nhost:host\nx-amz-date:longDate\n\nhost;x-amz-date\n" + sha256Hex("Action=ReceiveMessage"));

    final String signature = hmacSha256Hex(hmacSha256(hmacSha256(hmacSha256(hmacSha256("AWS4secretKey", shortDate), "regionName".getBytes()), "sqs".getBytes()), "aws4_request".getBytes()), stringToSign.getBytes());

    final Response res = client.target("queueUrl").request().header("X-Amz-Date", longDate).header("Authorization", "AWS4-HMAC-SHA256 Credential=accessKeyId/shortDate/regionName/sqs/aws4_request, SignedHeaders=host;x-amz-date, Signature=" + signature).post(Entity.form(new Form().param("Action", "ReceiveMessage")));

    out.println(res.readEntity(String.class));

    client.close();
  }

}

How to Send a Message to AWS SQS with JAX-RS

import static org.apache.commons.codec.digest.DigestUtils.sha256Hex;
import static org.apache.commons.codec.digest.HmacUtils.hmacSha256;
import static org.apache.commons.codec.digest.HmacUtils.hmacSha256Hex;

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.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;

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

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

    final String stringToSign = "AWS4-HMAC-SHA256\nlongDate\nshortDate/regionName/sqs/aws4_request\n" + sha256Hex("POST\n/accountId/SampleQueue\n\nhost:host\nx-amz-date:longDate\n\nhost;x-amz-date\n" + sha256Hex("Action=SendMessage&MessageBody=Simples+Assim"));

    final String signature = hmacSha256Hex(hmacSha256(hmacSha256(hmacSha256(hmacSha256("AWS4secretKey", shortDate), "regionName".getBytes()), "sqs".getBytes()), "aws4_request".getBytes()), stringToSign.getBytes());

    final MultivaluedMap<String, String> params = new MultivaluedHashMap<>();

    params.putSingle("Action", "SendMessage");

    params.putSingle("MessageBody", "Simples Assim");

    client.target("queueUrl").request().header("X-Amz-Date", longDate).header("Authorization", "AWS4-HMAC-SHA256 Credential=accessKeyId/shortDate/regionName/sqs/aws4_request, SignedHeaders=host;x-amz-date, Signature=" + signature).post(Entity.form(params));

    client.close();
  }

}

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

W/o Durability

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", "/topics/SampleTopic").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();
  }

}

W/ Durability

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").queryParam("clientId", "SampleClient").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", "/topics/SampleTopic").queryParam("session", "SampleSession").queryParam("subscriptionName", "SampleSubscription").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();
  }

}

How to Send a Pull/Push Notification to the Oracle Messaging Cloud Service with JAX-RS

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) {
    final Client client = ClientBuilder.newClient();

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

    final 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/producers/SampleProducer").queryParam("destination", "/topics/SampleTopic").queryParam("session", "SampleSession").request().cookie(jsessionid).put(Entity.json(""));

    client.target("https://host/service/api/v1/producers/SampleProducer/messages").request().cookie(jsessionid).post(Entity.json("Simples Assim"));

    client.close();
  }

}

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();
  }

}

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

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) {
    final Client client = ClientBuilder.newClient();

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

    final 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/producers/SampleProducer").queryParam("destination", "/queues/SampleQueue").queryParam("session", "SampleSession").request().cookie(jsessionid).put(Entity.json(""));

    client.target("https://host/service/api/v1/producers/SampleProducer/messages").request().cookie(jsessionid).post(Entity.json("Simples Assim"));

    client.close();
  }

}

How to Receive a Pull Notification from the Oracle Messaging Cloud Service with Java

W/o Durability

import static java.lang.System.out;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;

import oracle.cloud.messaging.client.MessagingService;
import oracle.cloud.messaging.client.MessagingServiceCredentials;
import oracle.cloud.messaging.client.MessagingServiceFactory;
import oracle.cloud.messaging.client.MessagingServiceNamespace;

public final class OmcsSample {

  public static void main(final String[] args) throws Exception {
    final MessagingServiceFactory serviceFactory = MessagingServiceFactory.getInstance();

    final MessagingService service = serviceFactory.getMessagingService(new MessagingServiceNamespace("uri"), new MessagingServiceCredentials("username", "password"));

    final ConnectionFactory connFactory = service.getConnectionFactory();

    final Connection conn = connFactory.createConnection();

    final Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

    final Destination dest = sess.createTopic("SampleTopic");

    final MessageConsumer cons = sess.createConsumer(dest);

    conn.start();

    final Message msg = cons.receive();

    out.println(msg);

    conn.close();
  }

}

W/ Durability

import static java.lang.System.out;
 
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.Topic;

import oracle.cloud.messaging.client.MessagingService;
import oracle.cloud.messaging.client.MessagingServiceCredentials;
import oracle.cloud.messaging.client.MessagingServiceFactory;
import oracle.cloud.messaging.client.MessagingServiceNamespace;

public final class OmcsSample {

  public static void main(final String[] args) throws Exception {
    final MessagingServiceFactory serviceFactory = MessagingServiceFactory.getInstance();

    final MessagingService service = serviceFactory.getMessagingService(new MessagingServiceNamespace("uri"), new MessagingServiceCredentials("username", "password"));

    final ConnectionFactory connFactory = service.getConnectionFactory("SampleClient");

    final Connection conn = connFactory.createConnection();

    final Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

    final Topic dest = sess.createTopic("SampleTopic");

    final MessageConsumer cons = sess.createDurableSubscriber(dest, "SampleSubscription");

    conn.start();

    final Message msg = cons.receive();

    out.println(msg);

    conn.close();
  }

}

How to Send a Pull/Push Notification to the Oracle Messaging Cloud Service with Java

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;

import oracle.cloud.messaging.client.MessagingService;
import oracle.cloud.messaging.client.MessagingServiceCredentials;
import oracle.cloud.messaging.client.MessagingServiceFactory;
import oracle.cloud.messaging.client.MessagingServiceNamespace;

public final class OmcsSample {

  public static void main(final String[] args) throws Exception {
    final MessagingServiceFactory serviceFactory = MessagingServiceFactory.getInstance();

    final MessagingService service = serviceFactory.getMessagingService(new MessagingServiceNamespace("uri"), new MessagingServiceCredentials("username", "password"));

    final ConnectionFactory connFactory = service.getConnectionFactory();

    final Connection conn = connFactory.createConnection();

    final Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

    final Destination dest = sess.createTopic("SampleTopic");

    final MessageProducer prod = sess.createProducer(dest);

    final Message msg = sess.createTextMessage("Simples Assim");

    prod.send(msg);

    conn.close();
  }

}