Send message To windows ServiceBus - Java Azure

Java examples for Azure:Service Bus

Description

Send message To windows ServiceBus

Demo Code



import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import java.util.Date;

import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;

import org.apache.commons.codec.binary.Base64;
import org.codehaus.jettison.json.JSONObject;

public class SendmessageToServiceBus {

    public static void main(String[] args) throws Exception {

        String tokenString = generateSasToken(
                "https://jamborsb.servicebus.windows.net/mytest/", "test",
                "key");
        String authString = "Authorization:" + tokenString;

        System.out.println(authString);

    }/* w  ww  .  jav  a  2  s .  com*/

    private static String generateSasToken(String uri, String keyName,
            String key) {
        String ret = "";

        Date now = new Date();
        Date previousDate = new Date(1970);
        long tokenExpirationTime = ((now.getTime() - previousDate.getTime()) / 1000) + 3600;

        try {
            String stringToSign = URLEncoder.encode(
                    new URL(uri).toString(),
                    java.nio.charset.StandardCharsets.UTF_8.toString())
                    + "\n" + tokenExpirationTime;

            System.out.println(stringToSign);
            SecretKey secretKey = null;

            byte[] keyBytes = key.getBytes("UTF-8");

            Mac mac = Mac.getInstance("HMACSHA256");

            secretKey = new SecretKeySpec(keyBytes, mac.getAlgorithm());

            mac.init(secretKey);

            byte[] digest = mac.doFinal(stringToSign.getBytes());
            String signature = Base64.encodeBase64String(digest);
            System.out.println(URLEncoder.encode(signature,
                    java.nio.charset.StandardCharsets.UTF_8.toString()));
            ret = String.format(
                    "SharedAccessSignature sr=%s&sig=%s&se=%s&skn=%s",
                    URLEncoder.encode(uri,
                            java.nio.charset.StandardCharsets.UTF_8
                                    .toString()), URLEncoder.encode(
                            signature,
                            java.nio.charset.StandardCharsets.UTF_8
                                    .toString()), String
                            .valueOf(tokenExpirationTime), keyName);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return ret;
    }

}

Related Tutorials