Tag Archives: REST

How to Receive a Notification from the Microsoft Azure Service Bus with Java

import static java.lang.System.out;

import com.microsoft.windowsazure.services.servicebus.ServiceBusConfiguration;
import com.microsoft.windowsazure.services.servicebus.ServiceBusContract;
import com.microsoft.windowsazure.services.servicebus.ServiceBusService;
import com.microsoft.windowsazure.services.servicebus.models.BrokeredMessage;
import com.microsoft.windowsazure.services.servicebus.models.ReceiveSubscriptionMessageResult;

public final class AzureSample {

  public static void main(final String[] args) throws Exception {
    final ServiceBusContract service = ServiceBusService.create(ServiceBusConfiguration.configureWithSASAuthentication("accountname", "sharedaccesskeyname", "sharedaccesskey", ".servicebus.windows.net"));

    final ReceiveSubscriptionMessageResult res = service.receiveSubscriptionMessage("SampleTopic", "SampleSubscription");

    final BrokeredMessage msg = res.getValue();

    out.println(msg);
  }

}
Advertisements

How to Send a Notification to the Microsoft Azure Service Bus with Java

import com.microsoft.windowsazure.services.servicebus.ServiceBusConfiguration;
import com.microsoft.windowsazure.services.servicebus.ServiceBusContract;
import com.microsoft.windowsazure.services.servicebus.ServiceBusService;
import com.microsoft.windowsazure.services.servicebus.models.BrokeredMessage;

public final class AzureSample {

  public static void main(final String[] args) throws Exception {
    final ServiceBusContract service = ServiceBusService.create(ServiceBusConfiguration.configureWithSASAuthentication("accountname", "sharedaccesskeyname", "sharedaccesskey", ".servicebus.windows.net"));

    service.sendMessage("SampleTopic", new BrokeredMessage("Simples Assim"));
  }

}

How to Receive a Message from the Microsoft Azure Service Bus with Java

import static java.lang.System.out;

import com.microsoft.windowsazure.services.servicebus.ServiceBusConfiguration;
import com.microsoft.windowsazure.services.servicebus.ServiceBusContract;
import com.microsoft.windowsazure.services.servicebus.ServiceBusService;
import com.microsoft.windowsazure.services.servicebus.models.BrokeredMessage;
import com.microsoft.windowsazure.services.servicebus.models.ReceiveMessageResult;

public final class AzureSample {

  public static void main(final String[] args) throws Exception {
    final ServiceBusContract service = ServiceBusService.create(ServiceBusConfiguration.configureWithSASAuthentication("accountname", "sharedaccesskeyname", "sharedaccesskey", ".servicebus.windows.net"));

    final ReceiveMessageResult res = service.receiveMessage("SampleQueue");

    final BrokeredMessage msg = res.getValue();

    out.println(msg);
  }

}

How to Send a Message to the Microsoft Azure Service Bus with Java

import com.microsoft.windowsazure.services.servicebus.ServiceBusConfiguration;
import com.microsoft.windowsazure.services.servicebus.ServiceBusContract;
import com.microsoft.windowsazure.services.servicebus.ServiceBusService;
import com.microsoft.windowsazure.services.servicebus.models.BrokeredMessage;

public final class AzureSample {

  public static void main(final String[] args) throws Exception {
    final ServiceBusContract service = ServiceBusService.create(ServiceBusConfiguration.configureWithSASAuthentication("accountname", "sharedaccesskeyname", "sharedaccesskey", ".servicebus.windows.net"));

    service.sendMessage("SampleQueue", new BrokeredMessage("Simples Assim"));
  }

}

How to Receive a Message from the Microsoft Azure Queue Service with Java

import static java.lang.System.out;

import com.microsoft.azure.storage.CloudStorageAccount;
import com.microsoft.azure.storage.queue.CloudQueue;
import com.microsoft.azure.storage.queue.CloudQueueClient;
import com.microsoft.azure.storage.queue.CloudQueueMessage;

public final class AzureSample {

  public static void main(final String[] args) throws Exception {
    final CloudStorageAccount account = CloudStorageAccount.parse("DefaultEndpointsProtocol=https;AccountName=accountname;AccountKey=accountkey");

    final CloudQueueClient client = account.createCloudQueueClient();

    final CloudQueue queue = client.getQueueReference("samplequeue");

    final CloudQueueMessage msg = queue.retrieveMessage();

    out.println(msg.getMessageContentAsString());

    queue.deleteMessage(msg);
  }

}

How to Send a Message to Microsoft Azure Queue Service with Java

import com.microsoft.azure.storage.CloudStorageAccount;
import com.microsoft.azure.storage.queue.CloudQueue;
import com.microsoft.azure.storage.queue.CloudQueueClient;
import com.microsoft.azure.storage.queue.CloudQueueMessage;

public final class AzureSample {

  public static void main(final String[] args) throws Exception {
    final CloudStorageAccount account = CloudStorageAccount.parse("DefaultEndpointsProtocol=https;AccountName=accountname;AccountKey=accountKey");

    final CloudQueueClient client = account.createCloudQueueClient();

    final CloudQueue queue = client.getQueueReference("sample-queue");

    queue.addMessage(new CloudQueueMessage("Simples Assim"));
  }

}

How to Receive a Notification from Google Cloud Pub/Sub with Java

import static java.lang.System.out;

import java.io.FileInputStream;
import java.util.List;

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.util.Utils;
import com.google.api.services.pubsub.Pubsub;
import com.google.api.services.pubsub.Pubsub.Projects.Subscriptions;
import com.google.api.services.pubsub.PubsubScopes;
import com.google.api.services.pubsub.model.AcknowledgeRequest;
import com.google.api.services.pubsub.model.PullRequest;
import com.google.api.services.pubsub.model.ReceivedMessage;
import com.google.common.collect.ImmutableList;

public final class PubsubSample {

  public static void main(final String[] args) throws Exception {
    final Subscriptions subscriptions = new Pubsub.Builder(Utils.getDefaultTransport(), Utils.getDefaultJsonFactory(), GoogleCredential.fromStream(new FileInputStream("projectid.json")).createScoped(PubsubScopes.all())).build().projects().subscriptions();

    final List<ReceivedMessage> msgLst = subscriptions.pull("projects/projectid/subscriptions/SampleSubscription", new PullRequest().setMaxMessages(1)).execute().getReceivedMessages().iterator();

    for (final ReceivedMessage msg : msgLst) {
      out.println(msg);

      subscriptions.acknowledge("projects/projectid/subscriptions/SampleSubscription", new AcknowledgeRequest().setAckIds(ImmutableList.of(msg.getAckId()))).execute();
    }

  }

}

How to Send a Notification to Google Cloud Pub/Sub with Java

import java.io.FileInputStream;

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.util.Utils;
import com.google.api.services.pubsub.Pubsub;
import com.google.api.services.pubsub.PubsubScopes;
import com.google.api.services.pubsub.model.PublishRequest;
import com.google.api.services.pubsub.model.PubsubMessage;
import com.google.common.collect.ImmutableList;

public final class PubsubSample {

  public static void main(final String[] args) throws Exception {
    new Pubsub.Builder(Utils.getDefaultTransport(), Utils.getDefaultJsonFactory(), GoogleCredential.fromStream(new FileInputStream("projectid.json")).createScoped(PubsubScopes.all())).build().projects().topics().publish("projects/projectid/topics/SampleTopic", new PublishRequest().setMessages(ImmutableList.of(new PubsubMessage().encodeData("Simples Assim".getBytes())))).execute();
  }

}

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

}