Tag Archives: Apache Commons

How to Generate an Authentication Hash for the Oracle Field Service Cloud APIs

Java

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

}

Node.js

Python

Ruby

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 Message from an Apache ActiveMQ Topic with BSF4ooRexx

W/o Durability

connFactory = .bsf~new('org.apache.activemq.ActiveMQConnectionFactory')

conn = connFactory~createConnection()

sess = conn~createSession(.false, bsf.getConstant('javax.jms.Session', 'AUTO_ACKNOWLEDGE'))

dest = sess~createTopic('SampleTopic')

cons = sess~createConsumer(dest)

conn~bsf.invoke('start')

msg = cons~receive()

say msg

conn~close()

::requires 'BSF.CLS'

W/ Durability

connFactory = .bsf~new('org.apache.activemq.ActiveMQConnectionFactory')

conn = connFactory~createConnection()

conn~setClientID('SampleClient')

sess = conn~createSession(.false, bsf.getConstant('javax.jms.Session', 'AUTO_ACKNOWLEDGE'))

dest = sess~createTopic('SampleTopic')

cons = sess~createDurableSubscriber(dest, 'SampleSubscription')

conn~bsf.invoke('start')

msg = cons~receive()

say msg

conn~close()

::requires 'BSF.CLS'

How to Receive a Message from an Apache ActiveMQ Queue with BSF4ooRexx

connFactory = .bsf~new('org.apache.activemq.ActiveMQConnectionFactory')

conn = connFactory~createConnection()

sess = conn~createSession(.false, bsf.getConstant('javax.jms.Session', 'AUTO_ACKNOWLEDGE'))

dest = sess~createQueue('SampleQueue')

cons = sess~createConsumer(dest)

conn~bsf.invoke('start')

msg = cons~receive()

say msg

conn~close()

::requires 'BSF.CLS'

How to Send a Message to an Apache ActiveMQ Topic with BSF4ooRexx

connFactory = .bsf~new('org.apache.activemq.ActiveMQConnectionFactory')

conn = connFactory~createConnection()

sess = conn~createSession(.false, bsf.getConstant('javax.jms.Session', 'AUTO_ACKNOWLEDGE'))

dest = sess~createTopic('SampleTopic')

prod = sess~createProducer(dest)

msg = sess~createTextMessage('Simples Assim')

prod~bsf.invoke('send', msg)

conn~close()

::requires 'BSF.CLS'

How to Send a Message to an Apache ActiveMQ Queue with BSF4ooRexx

connFactory = .bsf~new('org.apache.activemq.ActiveMQConnectionFactory')

conn = connFactory~createConnection()

sess = conn~createSession(.false, bsf.getConstant('javax.jms.Session', 'AUTO_ACKNOWLEDGE'))

dest = sess~createQueue('SampleQueue')

prod = sess~createProducer(dest)

msg = sess~createTextMessage('Simples Assim')

prod~bsf.invoke('send', msg)

conn~close()

::requires 'BSF.CLS'

How to Setup JDBC Persistence in JBoss A-MQ

1) Install the Apache Commons DBCP bundle:

osgi:install mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.commons-dbcp/1.4_3

2) Install a JDBC driver.

3) Define a data source in the <JBoss A-MQ Install Dir>/etc/activemq.xml file:

<bean id="sample-ds" class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <!-- any driver -->
  <property name="url" value="jdbc:mysql://localhost/sample_db"/>
  <property name="username" value="username"/>
  <property name="password" value="password"/>
</bean>

4) Change the persistence adapter in the <JBoss A-MQ Install Dir>/etc/activemq.xml file:

<persistenceAdapter>
  <jdbcPersistenceAdapter dataSource="#sample-ds"/>
</persistenceAdapter>

5) Start JBoss A-MQ.

You can find more details here.