How to Generate an Authentication Token in the Oracle Field Service Cloud

import static java.lang.System.out;
import static java.time.format.DateTimeFormatter.ISO_INSTANT;
import static org.apache.commons.codec.digest.DigestUtils.sha256Hex;

import java.time.Instant;
import java.time.temporal.ChronoUnit.SECONDS;

public final class OfscSample {

  public static void main(final String[] args) {
    out.println(sha256Hex(ISO_INSTANT.format(Instant.now().truncatedTo(ChronoUnit.SECONDS)) + sha256Hex("password" + sha256Hex("login"))));
  }

}
Advertisements

How to Send a Push Notification to AWS SNS

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;

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

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

}

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

}

JavaScript

AWS.config.update({ credentials: new AWS.CognitoIdentityCredentials({ IdentityPoolId: 'identityPoolId', accessKeyId: 'accessKeyId', secretAccessKey: 'secretAccessKey' }), region: 'regionName' });

const sns = new AWS.SNS();

sns.publish({ Message: 'Simples Assim', TopicArn: 'topicArn' }, (err, res) => {});

Node.js

const AWS = require('aws-sdk');

AWS.config.update({ credentials: new AWS.CognitoIdentityCredentials({ IdentityPoolId: 'identityPoolId', accessKeyId: 'accessKeyId', secretAccessKey: 'secretAccessKey' }), region: 'regionName' });

const sns = new AWS.SNS();

sns.publish({ Message: 'Simples Assim', TopicArn: 'topicArn' }, (err, res) => {});

Python

import boto3

sess = boto3.session.Session('accessKeyId', 'secretAccessKey', region_name='regionName')

sns = sess.resource('sns')

topic = sqs.create_topic(Name='SampleTopic')

topic.publish(Message='Simples Assim')

How to Receive a Message from AWS SQS

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;

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

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

}

Java

import static java.lang.System.out;

import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.sqs.AmazonSQSClient;
import com.amazonaws.services.sqs.model.Message;
import com.amazonaws.services.sqs.model.ReceiveMessageResult;

public final class SqsSample {

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

    final ReceiveMessageResult res = client.receiveMessage("queueUrl");

    for (final Message msg : res.getMessages()) {
      out.println(msg);

      client.deleteMessage("queueUrl", msg.getReceiptHandle());
    }

    client.shutdown();
  }

}

JavaScript

AWS.config.update({ credentials: new AWS.CognitoIdentityCredentials({ IdentityPoolId: 'identityPoolId', accessKeyId: 'accessKeyId', secretAccessKey: 'secretAccessKey' }), region: 'regionName' });

const sqs = new AWS.SQS();

sqs.receiveMessage({ QueueUrl: 'queueUrl' }, (err, res) => {
  console.log(res.Messages[0].Body);

  sqs.deleteMessage({ QueueUrl: 'queueUrl', ReceiptHandle: res.Messages[0].ReceiptHandle }, (err, res) => {});
});

Node.js

const AWS = require('aws-sdk');

AWS.config.update({ credentials: new AWS.CognitoIdentityCredentials({ IdentityPoolId: 'identityPoolId', accessKeyId: 'accessKeyId', secretAccessKey: 'secretAccessKey' }), region: 'regionName' });

const sqs = new AWS.SQS();

sqs.receiveMessage({ QueueUrl: 'queueUrl' }, (err, res) => {
  console.log(res.Messages[0].Body);

  sqs.deleteMessage({ QueueUrl: 'queueUrl', ReceiptHandle: res.Messages[0].ReceiptHandle }, (err, res) => {});
});

Python

import boto3

sess = boto3.session.Session('accessKeyId', 'secretAccessKey', region_name='regionName')

sqs = sess.resource('sqs')

queue = sqs.create_queue(QueueName='SampleQueue')

for msg in queue.receive_messages():
  print(msg)

  msg.delete()

How to Send a Message to AWS SQS

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;

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

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

}

Java

import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.sqs.AmazonSQSClient;

public final class SqsSample {

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

    client.sendMessage("queueUrl", "Simples Assim"");

    client.shutdown();
  }

}

JavaScript

AWS.config.update({ credentials: new AWS.CognitoIdentityCredentials({ IdentityPoolId: 'identityPoolId', accessKeyId: 'accessKeyId', secretAccessKey: 'secretAccessKey' }), region: 'regionName' });

const sqs = new AWS.SQS();

sqs.sendMessage({ MessageBody: 'Simples Assim', QueueUrl: 'queueUrl' }, (err, res) => {});

Node.js

const AWS = require('aws-sdk');

AWS.config.update({ credentials: new AWS.CognitoIdentityCredentials({ IdentityPoolId: 'identityPoolId', accessKeyId: 'accessKeyId', secretAccessKey: 'secretAccessKey' }), region: 'regionName' });

const sqs = new AWS.SQS();

sqs.sendMessage({ MessageBody: 'Simples Assim', QueueUrl: 'queueUrl' }, (err, res) => {});

Python

import boto3

sess = boto3.session.Session('accessKeyId', 'secretAccessKey', region_name='regionName')

sqs = sess.resource('sqs')

queue = sqs.create_queue(QueueName='SampleQueue')

queue.send_message(MessageBody='Simples Assim')

How to Encode/Decode to Base64 with Apache Commons Codec

import static java.lang.System.out;
import static org.apache.commons.codec.binary.Base64.encodeBase64;

public final class CommonsCodecSample {

  public static void main(final String[] args) {
    out.println(encodeBase64("Simples Assim".getBytes()); // decodeBase64 for decoding
  }

}

How to Encode/Decode to Hexadecimal with Apache Commons Codec

import static java.lang.System.out;
import static org.apache.commons.codec.binary.Hex.encodeHex;

public final class CommonsCodecSample {

  public static void main(final String[] args) {
    out.println(encodeHex("Simples Assim".getBytes())); // decodeHex for decoding
  }

}