Example usage for com.amazonaws.auth BasicAWSCredentials BasicAWSCredentials

List of usage examples for com.amazonaws.auth BasicAWSCredentials BasicAWSCredentials

Introduction

In this page you can find the example usage for com.amazonaws.auth BasicAWSCredentials BasicAWSCredentials.

Prototype

public BasicAWSCredentials(String accessKey, String secretKey) 

Source Link

Document

Constructs a new BasicAWSCredentials object, with the specified AWS access key and AWS secret key.

Usage

From source file:com.comcast.cns.io.SQSEndpointPublisher.java

License:Apache License

@Override
public void send() throws Exception {

    if ((message == null) || (endpoint == null)) {
        throw new Exception("Message and Endpoint must both be set");
    }// w  w w  .  ja  va2s  .c  o m

    awsCredentials = new BasicAWSCredentials(CMBProperties.getInstance().getAwsAccessKey(),
            CMBProperties.getInstance().getAwsAccessSecret());
    sqs = new AmazonSQSClient(awsCredentials);

    String url;

    if (com.comcast.cqs.util.Util.isValidQueueUrl(endpoint)) {
        url = endpoint;
    } else {
        url = com.comcast.cqs.util.Util.getAbsoluteAWSQueueUrlForArn(endpoint);
    }

    try {

        String msg = null;

        if (message.getMessageStructure() == CNSMessageStructure.json) {
            msg = message.getProtocolSpecificMessage(CnsSubscriptionProtocol.cqs);
        } else {
            msg = message.getMessage();
        }

        if (!rawMessageDelivery && message.getMessageType() == CNSMessageType.Notification) {
            msg = com.comcast.cns.util.Util.generateMessageJson(message, CnsSubscriptionProtocol.cqs);
        }

        logger.debug("event=send_sqs_message endpoint=" + endpoint + "\" message=\"" + msg);

        sqs.sendMessage(new SendMessageRequest(url, msg));

    } catch (Exception ex) {
        logger.warn("event=send_sqs_message endpoint=" + endpoint + "\" message=\"" + message, ex);
        throw new CMBException(CNSErrorCodes.InternalError, "internal service error");
    }

    logger.debug("event=send_sqs_message endpoint=" + endpoint + " message=\"" + message + "\"");
}

From source file:com.comcast.cns.tools.CQSHandler.java

License:Apache License

private static synchronized void initialize() throws PersistenceException {

    if (initialized) {
        return;//from   ww w .j av a  2 s.  c o m
    }

    IUserPersistence userHandler = PersistenceFactory.getUserPersistence();

    if (CMBProperties.getInstance().getCNSUserAccessKey() != null
            && CMBProperties.getInstance().getCNSUserAccessSecret() != null) {

        //TODO: should add user id to cmb properties file

        awsCredentials = new BasicAWSCredentials(CMBProperties.getInstance().getCNSUserAccessKey(),
                CMBProperties.getInstance().getCNSUserAccessSecret());
        cnsInternal = userHandler.getUserByAccessKey(awsCredentials.getAWSAccessKeyId());

    } else {

        cnsInternal = userHandler.getUserByName(CMBProperties.getInstance().getCNSUserName());

        if (cnsInternal == null) {
            cnsInternal = userHandler.createDefaultUser();
        }

        awsCredentials = new BasicAWSCredentials(cnsInternal.getAccessKey(), cnsInternal.getAccessSecret());
    }

    sqs = new AmazonSQSClient(awsCredentials);
    sqs.setEndpoint(CMBProperties.getInstance().getCQSServiceUrl());

    useInlineApiCalls = CMBProperties.getInstance().useInlineApiCalls()
            && CMBProperties.getInstance().getCQSServiceEnabled();

    initialized = true;
}

From source file:com.comcast.cqs.controller.CQSQueueMessagesPageServlet.java

License:Apache License

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    if (redirectUnauthenticatedUser(request, response)) {
        return;/*from   w  ww . j av a 2 s  . com*/
    }

    CMBControllerServlet.valueAccumulator.initializeAllCounters();
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    String userId = request.getParameter("userId");
    String queueName = request.getParameter("queueName");
    String msgStr = request.getParameter("message");
    String prevHandle = request.getParameter("prevHandle");
    String nextHandle = request.getParameter("nextHandle");
    String receiptHandle = request.getParameter("receiptHandle");
    Map<?, ?> parameters = request.getParameterMap();
    Message receivedMessage = null;
    int shard = 0;

    if (request.getParameter("shard") != null) {
        shard = Integer.parseInt(request.getParameter("shard"));
    }

    String queueUrl = Util.getAbsoluteQueueUrlForName(queueName, userId);

    connect(request);

    if (parameters.containsKey("Send")) {

        try {

            SendMessageRequest sendMessageRequest = new SendMessageRequest(queueUrl, msgStr);
            sqs.sendMessage(sendMessageRequest);
            logger.debug("event=send_message queue_url= " + queueUrl + " user_id=" + userId);

        } catch (Exception ex) {
            logger.error("event=send_message queue_url= " + queueUrl + " user_id=" + userId, ex);
            throw new ServletException(ex);
        }

    } else if (parameters.containsKey("Delete")) {

        try {

            DeleteMessageRequest deleteMessageRequest = new DeleteMessageRequest(queueUrl, receiptHandle);
            sqs.deleteMessage(deleteMessageRequest);
            logger.debug("event=delete_message queue_url= " + queueUrl + " receipt_handle=" + receiptHandle);

        } catch (Exception ex) {
            logger.error("event=delete_message queue_url= " + queueUrl + " receipt_handle=" + receiptHandle,
                    ex);
            throw new ServletException(ex);
        }
    } else if (parameters.containsKey("Receive")) {

        try {

            ReceiveMessageResult result = sqs.receiveMessage(new ReceiveMessageRequest(queueUrl));
            logger.debug("event=receive_message queue_url= " + queueUrl + " user_id=" + userId);
            List<Message> receivedMessages = null;

            if (result != null) {
                receivedMessages = result.getMessages();
                if (receivedMessages != null && receivedMessages.size() > 0) {
                    receivedMessage = receivedMessages.get(0);
                }
            }
        } catch (Exception ex) {
            logger.error("event=receive_message queue_url= " + queueUrl + " user_id=" + userId, ex);
            throw new ServletException(ex);
        }

    }

    int numberOfShards = 1;

    try {

        GetQueueAttributesRequest getQueueAttributesRequest = new GetQueueAttributesRequest(queueUrl);
        getQueueAttributesRequest.setAttributeNames(Arrays.asList("NumberOfShards"));
        GetQueueAttributesResult getQueueAttributesResult = sqs.getQueueAttributes(getQueueAttributesRequest);
        Map<String, String> attributes = getQueueAttributesResult.getAttributes();
        numberOfShards = Integer.parseInt(attributes.get("NumberOfShards"));

    } catch (Exception ex) {
        logger.error("event=get_queue_attributes url=" + queueUrl);
    }

    out.println("<html>");

    out.println("<script type='text/javascript' language='javascript'>");
    out.println("function setVisibility(id, buttonid) {");
    out.println("if(document.getElementById(buttonid).value=='Less'){");
    out.println("document.getElementById(buttonid).value = 'More';");
    out.println("document.getElementById(id).style.display = 'none';");
    out.println("}else{");
    out.println("document.getElementById(buttonid).value = 'Less';");
    out.println("document.getElementById(id).style.display = 'inline';");
    out.println("}");
    out.println("}");
    out.println("</script>");

    header(request, out, "Peek Messages for Queue " + queueName);

    out.println("<body>");
    out.println("<h2>Peek Messages for Queue " + queueName + "</h2>");

    if (user != null) {
        out.println("<table><tr><td><b>User Name:</b></td><td>" + user.getUserName() + "</td></tr>");
        out.println("<tr><td><b>User ID:</b></td><td>" + user.getUserId() + "</td></tr>");
        out.println("<tr><td><b>Access Key:</b></td><td>" + user.getAccessKey() + "</td></tr>");
        out.println("<tr><td><b>Access Secret:</b></td><td>" + user.getAccessSecret() + "</td></tr>");
        out.println("<tr><td><b>Queue Name:</b></td><td>" + queueName + "</td></tr></table>");
    }

    out.println("<p><table><tr><td><b>Send message:</b></td><td></td></tr>");
    out.println("<form id='frm1' action=\"/webui/cqsuser/message/?userId=" + userId + "&queueName=" + queueName
            + "\" method=POST>");
    out.println(
            "<tr><td><textarea rows='3' cols='50' name='message'></textarea><input type='hidden' name='userId' value='"
                    + userId
                    + "'></td><td valign='bottom'><input type='submit' value='Send' name='Send' /></td></tr></form>");
    out.println("<tr><td>&nbsp;</td></tr>");

    if (numberOfShards > 1) {
        out.println("<form id='frm2' action=\"/webui/cqsuser/message/?userId=" + userId + "&queueName="
                + queueName + "\" method=POST>");
        out.println(
                "<tr><td>Shard: <select name='shard' onChange='document.getElementById(\"frm2\").submit();'>");
        for (int i = 0; i < numberOfShards; i++) {
            out.print("<option value='" + i + "'");
            if (shard == i) {
                out.print(" selected");
            }
            out.println(">" + i + "</option>");
        }
        out.println("</form></select></td></tr></table></p>");
    } else {
        out.println("</table></p>");
    }

    // for receive message

    out.println("<form id='formsendmessage' action=\"/webui/cqsuser/message/?userId=" + userId + "&queueName="
            + queueName + "\" method=POST>");
    out.println("<table><tr><td><b>Receive Message:</b></td>");
    out.println("<td><input type='hidden' name='userId' value='" + userId
            + "'></td><td valign='bottom'><input type='submit' value='Receive Message' name='Receive' /></td>");
    out.println("</form></tr></table>");

    // showing received message

    out.println("<p><hr width='100%' align='left' /><p>");
    out.println("<h3>Received Messages</h3>");

    if (receivedMessage != null) {

        Map<String, String> attributes = receivedMessage.getAttributes();

        String timeSent = "";

        if (attributes.get("SentTimestamp") != null) {
            try {
                timeSent = new Date(Long.parseLong(attributes.get("SentTimestamp"))).toString();
            } catch (Exception ex) {
            }
        }

        String timeReceived = "";

        if (attributes.get("ApproximateFirstReceiveTimestamp") != null) {
            try {
                timeReceived = new Date(Long.parseLong(attributes.get("ApproximateFirstReceiveTimestamp")))
                        .toString();
            } catch (Exception ex) {
            }
        }

        out.println("<table class = 'alternatecolortable'>");
        out.println(
                "<tr><th></th><th>Receipt Handle</th><th>MD5</th><th>Body</th><th>Time Sent</th><th>Time First Received (Appr.)</th><th>Receive Count (Appr.)</th><th>Sender</th><th>&nbsp;</th></tr>");

        out.println("<tr>");
        out.println("<td>0</td>");
        out.println("<td>" + receivedMessage.getReceiptHandle() + "</td>");
        out.println("<td>" + receivedMessage.getMD5OfBody() + "</td>");
        String messageBody = receivedMessage.getBody();
        String messageBodyPart1 = null;
        String messageBodyPart2 = null;

        if ((messageBody != null) && (messageBody.length() > 300)) {

            messageBodyPart1 = messageBody.substring(0, 299);
            messageBodyPart2 = messageBody.substring(299);

            out.println("<td>");
            out.println(messageBodyPart1);
            out.println("<div id='rdetail' style=\"display: none;\">" + messageBodyPart2 + "</div>");
            out.println(
                    "<input type=button name=type id='rbt' value='More' onclick=\"setVisibility('rdetail', 'rbt');\";> ");
            out.println("</td>");
        } else {
            out.println("<td>" + receivedMessage.getBody() + "</td>");
        }

        out.println("<td>" + timeSent + "</td>");
        out.println("<td>" + timeReceived + "</td>");
        out.println("<td>" + attributes.get("ApproximateReceiveCount") + "</td>");
        out.println("<td>" + attributes.get("SenderId") + "</td>");
        out.println("<td><form action=\"/webui/cqsuser/message/?userId=" + user.getUserId() + "&queueName="
                + queueName + "&receiptHandle=" + receivedMessage.getReceiptHandle()
                + "\" method=POST><input type='submit' value='Delete' name='Delete'/><input type='hidden' name='queueUrl' value='"
                + queueUrl + "' /></form></td></tr>");
        out.println("</table>");

    } else {
        out.println("<p><i>no messages</i></p>");
    }

    List<CQSMessage> availableMessages = null;
    String queueFirstMessageHandle = null;

    try {

        if (queueUrl != null) {

            String peekRequestUrl = cqsServiceBaseUrl + user.getUserId() + "/" + queueName
                    + "?Action=PeekMessage&AWSAccessKeyId=" + user.getAccessKey()
                    + "&MaxNumberOfMessages=10&Shard=" + shard;

            if (prevHandle != null) {
                peekRequestUrl += "&PreviousReceiptHandle=" + prevHandle;
            } else if (nextHandle != null) {
                peekRequestUrl += "&NextReceiptHandle=" + nextHandle;
            }

            AWSCredentials awsCredentials = new BasicAWSCredentials(user.getAccessKey(),
                    user.getAccessSecret());
            String peekXml = httpPOST(cqsServiceBaseUrl, peekRequestUrl, awsCredentials);
            Element root = XmlUtil.buildDoc(peekXml);

            List<Element> messageElements = XmlUtil.getCurrentLevelChildNodes(
                    XmlUtil.getCurrentLevelChildNodes(root, "ReceiveMessageResult").get(0), "Message");

            for (Element messageElement : messageElements) {

                if (availableMessages == null) {
                    availableMessages = new ArrayList<CQSMessage>();
                }

                String body = XmlUtil.getCurrentLevelTextValue(messageElement, "Body");
                String id = XmlUtil.getCurrentLevelTextValue(messageElement, "MessageId").trim();
                String handle = XmlUtil.getCurrentLevelTextValue(messageElement, "ReceiptHandle").trim();

                CQSMessage msg = new CQSMessage(id, body);
                msg.setReceiptHandle(handle);

                List<Element> attributeElements = XmlUtil.getCurrentLevelChildNodes(messageElement,
                        "Attribute");
                Map<String, String> attributes = new HashMap<String, String>();

                for (Element attribute : attributeElements) {

                    String name = XmlUtil.getCurrentLevelTextValue(attribute, "Name");
                    String value = XmlUtil.getCurrentLevelTextValue(attribute, "Value");

                    if (name != null && value != null) {
                        attributes.put(name, value);
                    }
                }

                msg.setAttributes(attributes);

                availableMessages.add(msg);
            }

            //retrieve first messageId from queue. this is for pagination
            String peekRequestFirstMessageUrl = cqsServiceBaseUrl + user.getUserId() + "/" + queueName
                    + "?Action=PeekMessage&AWSAccessKeyId=" + user.getAccessKey()
                    + "&MaxNumberOfMessages=1&Shard=" + shard;
            String peekFirstMessageXml = httpPOST(cqsServiceBaseUrl, peekRequestFirstMessageUrl,
                    awsCredentials);
            Element rootFirstMessage = XmlUtil.buildDoc(peekFirstMessageXml);

            List<Element> messageElementsForFirstMessage = XmlUtil.getCurrentLevelChildNodes(
                    XmlUtil.getCurrentLevelChildNodes(rootFirstMessage, "ReceiveMessageResult").get(0),
                    "Message");
            if (messageElementsForFirstMessage.size() == 1) {
                queueFirstMessageHandle = XmlUtil
                        .getCurrentLevelTextValue(messageElementsForFirstMessage.get(0), "ReceiptHandle")
                        .trim();
            }
        }

    } catch (Exception ex) {
        logger.error("event=peek_message queue_url=" + queueUrl, ex);
        throw new ServletException(ex);
    }

    String previousHandle = null;
    nextHandle = null;

    out.println("<p><hr width='100%' align='left' /><p>");
    out.println("<h3>Available Messages</h3>");

    if ((availableMessages == null) || (availableMessages.size() == 0)) {
        out.println("<p><i>no messages</i></p>");
    }

    for (int i = 0; availableMessages != null && i < availableMessages.size(); i++) {

        CQSMessage message = availableMessages.get(i);

        Map<String, String> attributes = message.getAttributes();

        String timeSent = "";

        if (attributes.get("SentTimestamp") != null) {
            try {
                timeSent = new Date(Long.parseLong(attributes.get("SentTimestamp"))).toString();
            } catch (Exception ex) {
            }
        }

        String timeReceived = "";

        if (attributes.get("ApproximateFirstReceiveTimestamp") != null) {
            try {
                timeReceived = new Date(Long.parseLong(attributes.get("ApproximateFirstReceiveTimestamp")))
                        .toString();
            } catch (Exception ex) {
            }
        }

        if (i == 0) {
            out.println("<table class = 'alternatecolortable'>");
            out.println(
                    "<tr><th></th><th>Receipt Handle</th><th>MD5</th><th>Body</th><th>Time Sent</th><th>Time First Received (Appr.)</th><th>Receive Count (Appr.)</th><th>Sender</th><th>&nbsp;</th></tr>");
            previousHandle = message.getReceiptHandle();
        }

        out.println("<tr>");
        out.println("<td>" + i + "</td>");
        out.println("<td>" + message.getReceiptHandle() + "</td>");
        out.println("<td>" + message.getMD5OfBody() + "</td>");
        String messageBody = message.getBody();
        String messageBodyPart1 = null;
        String messageBodyPart2 = null;
        if ((messageBody != null) && (messageBody.length() > 300)) {
            messageBodyPart1 = messageBody.substring(0, 299);
            messageBodyPart2 = messageBody.substring(299);

            out.println("<td>");
            out.println(messageBodyPart1);
            out.println("<div id='detail" + i + "' style=\"display: none;\">" + messageBodyPart2 + "</div>");
            out.println("<input type=button name=type id='bt" + i
                    + "' value='More' onclick=\"setVisibility('detail" + i + "', 'bt" + i + "');\";> ");
            out.println("</td>");
        } else {
            out.println("<td>" + message.getBody() + "</td>");
        }
        out.println("<td>" + timeSent + "</td>");
        out.println("<td>" + timeReceived + "</td>");
        out.println("<td>" + attributes.get("ApproximateReceiveCount") + "</td>");
        out.println("<td>" + attributes.get("SenderId") + "</td>");
        out.println("<td></td></tr>");

        if (i == availableMessages.size() - 1) {
            nextHandle = message.getReceiptHandle();
        }
    }

    out.println("</table>");

    //This case is for click on "Next" button of the previous page
    if (prevHandle != null) {

        if ((previousHandle) != null) {
            out.println("<a style='float:left;' href='/webui/cqsuser/message/?userId=" + user.getUserId()
                    + "&queueName=" + queueName + "&nextHandle=" + previousHandle + "'>Prev</a>");
        } else {
            out.println("<a style='float:left;' href='javascript:history.back()'>Prev</a>");
        }
    } else if ((previousHandle != null) && (!previousHandle.equals(queueFirstMessageHandle))) { //this is for all other cases
        out.println("<a style='float:left;' href='/webui/cqsuser/message/?userId=" + user.getUserId()
                + "&queueName=" + queueName + "&nextHandle=" + previousHandle + "'>Prev</a>");
    }

    if (availableMessages != null && availableMessages.size() > 0) {
        out.println("<a style='float:right;' href='/webui/cqsuser/message/?userId=" + user.getUserId()
                + "&queueName=" + queueName + "&prevHandle=" + nextHandle + "'>Next</a>");
    }

    out.println("<h5 style='text-align:center;'><a href='/webui'>ADMIN HOME</a>");
    out.println("<a href='/webui/cqsuser?userId=" + userId + "'>BACK TO QUEUE</a></h5>");

    out.println("</body></html>");

    CMBControllerServlet.valueAccumulator.deleteAllCounters();
}

From source file:com.comcast.cqs.controller.CQSUserPageServlet.java

License:Apache License

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    if (redirectUnauthenticatedUser(request, response)) {
        return;//from w  ww.  j  av  a  2s.c om
    }

    boolean showQueueAttributes = false;
    boolean showQueuesWithMessagesOnly = false;

    CMBControllerServlet.valueAccumulator.initializeAllCounters();
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    Map<?, ?> parameters = request.getParameterMap();
    String userId = request.getParameter("userId");
    String queueName = request.getParameter("queueName");
    String queueNamePrefix = request.getParameter("queueNamePrefix");
    String queueUrl = request.getParameter("qUrl");

    if (request.getParameter("ShowMessagesOnly") != null) {
        showQueuesWithMessagesOnly = true;
    }

    if (request.getParameter("ShowAttributes") != null) {
        showQueueAttributes = true;
    }

    List<String> queueUrls = new ArrayList<String>();

    connect(request);

    if (parameters.containsKey("Search")) {
    }

    try {

        String url = cqsServiceBaseUrl + "?Action=ListQueues&AWSAccessKeyId=" + user.getAccessKey();

        if (queueNamePrefix != null && !queueNamePrefix.equals("")) {
            url += "&QueueNamePrefix=" + queueNamePrefix;
        }

        if (showQueuesWithMessagesOnly) {
            url += "&ContainingMessagesOnly=true";
        }

        AWSCredentials awsCredentials = new BasicAWSCredentials(user.getAccessKey(), user.getAccessSecret());
        String apiStateXml = httpPOST(cqsServiceBaseUrl, url, awsCredentials);
        Element root = XmlUtil.buildDoc(apiStateXml);
        List<Element> resultList = XmlUtil.getCurrentLevelChildNodes(root, "ListQueuesResult");

        for (Element resultElement : resultList) {
            List<Element> urls = XmlUtil.getCurrentLevelChildNodes(resultElement, "QueueUrl");
            for (Element urlElement : urls) {
                queueUrls.add(urlElement.getTextContent().trim());
            }
        }

    } catch (Exception ex) {
        logger.error("event=list_queues user_id= " + userId, ex);
        throw new ServletException(ex);
    }

    if (parameters.containsKey("Create")) {

        try {
            CreateQueueRequest createQueueRequest = new CreateQueueRequest(queueName);
            CreateQueueResult createQueueResult = sqs.createQueue(createQueueRequest);
            queueUrl = createQueueResult.getQueueUrl();
            queueUrls.add(queueUrl);
            logger.debug("event=create_queue queue_url=" + queueUrl + " user_id= " + userId);
        } catch (Exception ex) {
            logger.error("event=create_queue queue_url=" + queueUrl + " user_id= " + userId, ex);
            throw new ServletException(ex);
        }

    } else if (parameters.containsKey("Delete")) {

        try {
            DeleteQueueRequest deleteQueueRequest = new DeleteQueueRequest(queueUrl);
            sqs.deleteQueue(deleteQueueRequest);
            queueUrls.remove(queueUrl);
            logger.debug("event=delete_queue queue_url=" + queueUrl + " user_id= " + userId);
        } catch (Exception ex) {
            logger.error("event=delete_queue queue_url=" + queueUrl + " user_id= " + userId, ex);
            throw new ServletException(ex);
        }

    } else if (parameters.containsKey("DeleteAll")) {

        for (int i = 0; queueUrls != null && i < queueUrls.size(); i++) {

            try {
                DeleteQueueRequest deleteQueueRequest = new DeleteQueueRequest(queueUrls.get(i));
                sqs.deleteQueue(deleteQueueRequest);
                logger.debug("event=delete_queue queue_url=" + queueUrls.get(i) + " user_id= " + userId);
            } catch (Exception ex) {
                logger.error("event=delete_queue queue_url=" + queueUrls.get(i) + " user_id= " + userId, ex);
            }
        }

        queueUrls = new ArrayList<String>();

    } else if (parameters.containsKey("ClearQueue")) {

        String qName = Util.getNameForAbsoluteQueueUrl(queueUrl);
        String qUserId = Util.getUserIdForAbsoluteQueueUrl(queueUrl);

        try {
            String url = cqsServiceBaseUrl;
            if (!url.endsWith("/")) {
                url += "/";
            }
            url += qUserId + "/" + qName + "?Action=ClearQueue&AWSAccessKeyId=" + user.getAccessKey();
            httpGet(url);
            logger.debug("event=clear_queue url=" + url + " user_id= " + userId);
        } catch (Exception ex) {
            logger.error("event=clear_queue queue_name=" + qName + " user_id= " + userId, ex);
            throw new ServletException(ex);
        }

    } else if (parameters.containsKey("ClearAllQueues")) {

        for (int i = 0; queueUrls != null && i < queueUrls.size(); i++) {

            String qName = Util.getNameForAbsoluteQueueUrl(queueUrls.get(i));
            String qUserId = Util.getUserIdForAbsoluteQueueUrl(queueUrls.get(i));

            try {
                String url = cqsServiceBaseUrl;
                if (!url.endsWith("/")) {
                    url += "/";
                }
                url += qUserId + "/" + qName + "?Action=ClearQueue&AWSAccessKeyId=" + user.getAccessKey();
                httpGet(url);
                logger.debug("event=clear_queue url=" + url + " user_id= " + userId);
            } catch (Exception ex) {
                logger.error("event=clear_queue queue_name=" + qName + " user_id= " + userId, ex);
            }
        }
    }

    out.println("<html>");

    header(request, out, "Queues");

    out.println("<body>");

    out.println("<h2>Queues</h2>");

    long numQueues = 0;

    try {
        numQueues = PersistenceFactory.getUserPersistence().getNumUserQueues(userId);
    } catch (PersistenceException ex) {
        logger.warn("event=queue_count_failure", ex);
    }

    if (user != null) {
        out.println("<table><tr><td><b>User Name:</b></td><td>" + user.getUserName() + "</td></tr>");
        out.println("<tr><td><b>User ID:</b></td><td>" + user.getUserId() + "</td></tr>");
        out.println("<tr><td><b>Access Key:</b></td><td>" + user.getAccessKey() + "</td></tr>");
        out.println("<tr><td><b>Access Secret:</b></td><td>" + user.getAccessSecret() + "</td>");
        out.println("<tr><td><b>Queue Count</b></td><td>" + numQueues + "</td></tr></table>");
    }

    out.println("<p><table>");

    out.println("<tr><td>Search queues with name prefix:</td><td></td></tr>");
    out.println("<tr><form action=\"/webui/cqsuser?userId=" + user.getUserId() + "\" method=POST>");
    out.println("<td><input type='text' name='queueNamePrefix' value='"
            + (queueNamePrefix != null ? queueNamePrefix : "") + "'/><input type='hidden' name='userId' value='"
            + userId + "'/>");
    out.println("<input type='checkbox' " + (showQueueAttributes ? "checked='true' " : "")
            + "name='ShowAttributes' value='ShowAttributes'>Show Attributes</input>");
    out.println("<input type='checkbox' " + (showQueuesWithMessagesOnly ? "checked='true' " : "")
            + " name='ShowMessagesOnly' value='ShowMessagesOnly'>Only Queues With Messages</input></td>");
    out.println("<td><input type='submit' value='Search' name='Search' /></td></form></tr>");

    out.println("<tr><td>Create queue with name:</td><td></td></tr>");
    out.println("<tr><form action=\"/webui/cqsuser?userId=" + user.getUserId() + "\" method=POST>");
    out.println("<td><input type='text' name='queueName' /><input type='hidden' name='userId' value='" + userId
            + "'></td>");
    out.println("<input type='hidden' name='queueNamePrefix' value='"
            + (queueNamePrefix != null ? queueNamePrefix : "") + "'/>");
    if (showQueuesWithMessagesOnly) {
        out.println("<input type='hidden' name='ShowMessagesOnly' value='true'/>");
    }
    if (showQueueAttributes) {
        out.println("<input type='hidden' name='ShowAttributes' value='true'/>");
    }
    out.println("<td><input type='submit' value='Create' name='Create' /></td></form></tr>");

    out.println("<tr><td>Delete all queues:</td><td></td></tr>");
    out.println("<tr><form action=\"/webui/cqsuser?userId=" + user.getUserId() + "\" "
            + "method=POST><td><input type='hidden' name='userId' value='" + userId + "'/>");
    out.println("<input type='hidden' name='queueNamePrefix' value='"
            + (queueNamePrefix != null ? queueNamePrefix : "") + "'/></td>");
    if (showQueuesWithMessagesOnly) {
        out.println("<input type='hidden' name='ShowMessagesOnly' value='true'/>");
    }
    if (showQueueAttributes) {
        out.println("<input type='hidden' name='ShowAttributes' value='true'/>");
    }
    out.println(
            "<td><input type='submit' value='Delete All' name='DeleteAll' onclick=\"return confirm('Are you sure you want to delete all?')\" /></td></form></tr>");

    out.println("<tr><td>Clear all queues:</td><td></td></tr>");
    out.println("<tr><form action=\"/webui/cqsuser?userId=" + user.getUserId() + "\" "
            + "method=POST><td><input type='hidden' name='userId' value='" + userId + "'/>");
    out.println("<input type='hidden' name='queueNamePrefix' value='"
            + (queueNamePrefix != null ? queueNamePrefix : "") + "'/></td>");
    if (showQueuesWithMessagesOnly) {
        out.println("<input type='hidden' name='ShowMessagesOnly' value='true'/>");
    }
    if (showQueueAttributes) {
        out.println("<input type='hidden' name='ShowAttributes' value='true'/>");
    }
    out.println("<td><input type='submit' value='Clear All' name='ClearAllQueues'/></td></form></tr>");

    out.println("</table></p>");

    out.println("<p><hr width='100%' align='left' /></p>");

    if ((queueUrls != null) && (queueUrls.size() >= 1000)) {
        out.println("Warning: Only first 1000 queues listed.  Use the search prefix method to find queues");
    }
    out.println("<p><table class = 'alternatecolortable' border='1'>");
    out.println("<tr><th>&nbsp;</th>");
    out.println("<th>Queue Url</th>");
    out.println("<th>Queue Arn</th>");
    out.println("<th>Queue Name</th>");
    out.println("<th>User Id</th>");
    out.println("<th>Region</th>");
    out.println("<th>Visibility TO</th>");
    out.println("<th>Max Msg Size</th>");
    out.println("<th>Msg Rention Period</th>");
    out.println("<th>Delay Seconds</th>");
    out.println("<th>Wait Time Seconds</th>");
    out.println("<th>Num Partitions</th>");
    out.println("<th>Num Shards</th>");
    out.println("<th>Compressed</th>");
    out.println("<th>Approx Num Msg</th>");
    out.println("<th>Approx Num Msg Not Visible</th>");
    out.println("<th>Approx Num Msg Delayed</th>");
    out.println("<th>&nbsp;</th><th>&nbsp;</th><th>&nbsp;</th><th>&nbsp;</th></tr>");

    for (int i = 0; queueUrls != null && i < queueUrls.size(); i++) {

        Map<String, String> attributes = new HashMap<String, String>();

        if (showQueueAttributes) {
            try {
                GetQueueAttributesRequest getQueueAttributesRequest = new GetQueueAttributesRequest(
                        queueUrls.get(i));
                getQueueAttributesRequest.setAttributeNames(Arrays.asList("VisibilityTimeout",
                        "MaximumMessageSize", "MessageRetentionPeriod", "DelaySeconds",
                        "ApproximateNumberOfMessages", "ApproximateNumberOfMessagesNotVisible",
                        "ApproximateNumberOfMessagesDelayed", "ReceiveMessageWaitTimeSeconds",
                        "NumberOfPartitions", "NumberOfShards", "IsCompressed"));
                GetQueueAttributesResult getQueueAttributesResult = sqs
                        .getQueueAttributes(getQueueAttributesRequest);
                attributes = getQueueAttributesResult.getAttributes();
            } catch (Exception ex) {
                logger.error("event=get_queue_attributes url=" + queueUrls.get(i));
            }
        }

        out.println("<tr>");
        out.println("<form action=\"/webui/cqsuser?userId=" + user.getUserId() + "\" method=POST>");
        out.println("<td>" + i + "</td>");
        out.println("<td>" + queueUrls.get(i) + "<input type='hidden' name='qUrl' value=" + queueUrls.get(i)
                + "><input type='hidden' name='qName' value="
                + Util.getNameForAbsoluteQueueUrl(queueUrls.get(i)) + "></td>");
        out.println("<td>" + Util.getArnForAbsoluteQueueUrl(queueUrls.get(i))
                + "<input type='hidden' name='arn' value=" + Util.getArnForAbsoluteQueueUrl(queueUrls.get(i))
                + "></td>");
        out.println("<td>" + Util.getNameForAbsoluteQueueUrl(queueUrls.get(i)) + "</td>");
        out.println("<td>" + user.getUserId() + "<input type='hidden' name='userId' value=" + user.getUserId()
                + "></td>");
        out.println("<td>" + CMBProperties.getInstance().getRegion() + "</td>");

        out.println("<td>"
                + (attributes.get("VisibilityTimeout") != null ? attributes.get("VisibilityTimeout") : "")
                + "</td>");
        out.println("<td>"
                + (attributes.get("MaximumMessageSize") != null ? attributes.get("MaximumMessageSize") : "")
                + "</td>");
        out.println("<td>"
                + (attributes.get("MessageRetentionPeriod") != null ? attributes.get("MessageRetentionPeriod")
                        : "")
                + "</td>");
        out.println("<td>" + (attributes.get("DelaySeconds") != null ? attributes.get("DelaySeconds") : "")
                + "</td>");
        out.println("<td>" + (attributes.get("ReceiveMessageWaitTimeSeconds") != null
                ? attributes.get("ReceiveMessageWaitTimeSeconds")
                : "") + "</td>");
        out.println("<td>"
                + (attributes.get("NumberOfPartitions") != null ? attributes.get("NumberOfPartitions") : "")
                + "</td>");
        out.println("<td>" + (attributes.get("NumberOfShards") != null ? attributes.get("NumberOfShards") : "")
                + "</td>");
        out.println("<td>" + (attributes.get("IsCompressed") != null ? attributes.get("IsCompressed") : "")
                + "</td>");
        out.println("<td>" + (attributes.get("ApproximateNumberOfMessages") != null
                ? attributes.get("ApproximateNumberOfMessages")
                : "") + "</td>");
        out.println("<td>" + (attributes.get("ApproximateNumberOfMessagesNotVisible") != null
                ? attributes.get("ApproximateNumberOfMessagesNotVisible")
                : "") + "</td>");
        out.println("<td>" + (attributes.get("ApproximateNumberOfMessagesDelayed") != null
                ? attributes.get("ApproximateNumberOfMessagesDelayed")
                : "") + "</td>");

        out.println("<td><a href='/webui/cqsuser/message?userId=" + user.getUserId() + "&queueName="
                + Util.getNameForAbsoluteQueueUrl(queueUrls.get(i)) + "'>Messages</a></td>");
        out.println("<td><a href='/webui/cqsuser/permissions?userId=" + user.getUserId() + "&queueName="
                + Util.getNameForAbsoluteQueueUrl(queueUrls.get(i)) + "'>Permissions</a></td>");
        out.println("<td><a href='' onclick=\"window.open('/webui/cqsuser/editqueueattributes?queueName="
                + Util.getNameForAbsoluteQueueUrl(queueUrls.get(i)) + "&userId=" + userId
                + "', 'EditQueueAttributes', 'height=630,width=580,toolbar=no')\">Attributes</a></td>");

        out.println("<input type='hidden' name='queueNamePrefix' value='"
                + (queueNamePrefix != null ? queueNamePrefix : "") + "'/>");
        if (showQueuesWithMessagesOnly) {
            out.println("<input type='hidden' name='ShowMessagesOnly' value='true'/>");
        }
        if (showQueueAttributes) {
            out.println("<input type='hidden' name='ShowAttributes' value='true'/>");
        }

        out.println(
                "<td><input type='submit' value='Clear' name='ClearQueue'/> <br/><input type='submit' value='Delete' name='Delete' onclick=\"return confirm('Are you sure you want to delete queue "
                        + Util.getNameForAbsoluteQueueUrl(queueUrls.get(i)) + "?')\" /></td></form></tr>");

    }

    out.println("</table></p>");
    out.println("<h5 style='text-align:center;'><a href='/webui'>ADMIN HOME</a></h5>");
    out.println("</body></html>");

    CMBControllerServlet.valueAccumulator.deleteAllCounters();
}

From source file:com.conductor.s3.S3HadoopUtils.java

License:Apache License

/**
 * Extracts the AWS key and secret from the conf, and returns a new S3 client.
 * //from  w ww  .jav a2s  .  c  o m
 * @param conf
 *            job conf
 * @return an S3 client.
 * @throws java.lang.IllegalArgumentException
 *             if it cannot find key/id in the conf.
 */
public static AmazonS3 getS3Client(final Configuration conf) {
    final String accessKey = conf.get("fs.s3n.awsAccessKeyId", conf.get("fs.s3.awsAccessKeyId"));
    checkArgument(!Strings.isNullOrEmpty(accessKey), "Missing fs.s3/n.awsAccessKeyId conf.");
    final String secretKey = conf.get("fs.s3n.awsSecretAccessKey", conf.get("fs.s3.awsSecretAccessKey"));
    checkArgument(!Strings.isNullOrEmpty(secretKey), "Missing fs.s3/n.awsSecretAccessKey conf.");
    return new AmazonS3Client(new BasicAWSCredentials(accessKey, secretKey));
}

From source file:com.connexience.server.model.archive.glacier.SetupUtils.java

License:Open Source License

public static SQSInfo setupSQS(String accessKey, String secretKey, String domainName, String vaultName) {
    SQSInfo sqsInfo = null;//  w w  w.  j  av a 2 s  .c  om
    try {
        AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);

        AmazonSQSClient amazonSQSClient = new AmazonSQSClient(awsCredentials);
        amazonSQSClient.setEndpoint("https://sqs." + domainName + ".amazonaws.com/");

        String queueName = vaultName + "-inkspot_glacier-queue";
        CreateQueueRequest createQueueRequest = new CreateQueueRequest();
        createQueueRequest.withQueueName(queueName);

        CreateQueueResult createQueueResult = amazonSQSClient.createQueue(createQueueRequest);
        if (createQueueResult != null) {
            String queueURL = createQueueResult.getQueueUrl();

            GetQueueAttributesRequest getQueueAttributesRequest = new GetQueueAttributesRequest();
            getQueueAttributesRequest.withQueueUrl(queueURL);
            getQueueAttributesRequest.withAttributeNames("QueueArn");

            GetQueueAttributesResult getQueueAttributesResult = amazonSQSClient
                    .getQueueAttributes(getQueueAttributesRequest);

            if (getQueueAttributesResult != null) {
                String queueARN = getQueueAttributesResult.getAttributes().get("QueueArn");

                Statement sqsStatement = new Statement(Effect.Allow);
                sqsStatement.withPrincipals(Principal.AllUsers);
                sqsStatement.withActions(SQSActions.SendMessage);
                sqsStatement.withResources(new Resource(queueARN));

                Policy sqsPolicy = new Policy();
                sqsPolicy.withStatements(sqsStatement);

                Map<String, String> sqsAttributes = new HashMap<>();
                sqsAttributes.put("Policy", sqsPolicy.toJson());

                SetQueueAttributesRequest setQueueAttributesRequest = new SetQueueAttributesRequest();
                setQueueAttributesRequest.withQueueUrl(queueURL);
                setQueueAttributesRequest.withAttributes(sqsAttributes);

                amazonSQSClient.setQueueAttributes(setQueueAttributesRequest);

                sqsInfo = new SQSInfo(queueARN, queueURL);
            } else
                logger.warn("Unable to get queue attributes: \"" + queueName + "\"");
        } else
            logger.warn("Unable to create queue: \"" + queueName + "\"");

        amazonSQSClient.shutdown();
    } catch (AmazonServiceException amazonServiceException) {
        logger.warn("AmazonServiceException: " + amazonServiceException);
        logger.debug(amazonServiceException);
    } catch (IllegalArgumentException illegalArgumentException) {
        logger.warn("IllegalArgumentException: " + illegalArgumentException);
        logger.debug(illegalArgumentException);
    } catch (AmazonClientException amazonClientException) {
        logger.warn("AmazonClientException: " + amazonClientException);
        logger.debug(amazonClientException);
    } catch (Throwable throwable) {
        logger.warn("Throwable: " + throwable);
        logger.debug(throwable);
    }

    return sqsInfo;
}

From source file:com.connexience.server.model.archive.glacier.SetupUtils.java

License:Open Source License

public static String setupSNS(String accessKey, String secretKey, String domainName, String vaultName,
        String queueARN) {//  w  ww  .java 2 s.c o m
    String topicARN = null;
    try {
        AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);

        AmazonSNSClient amazonSNSClient = new AmazonSNSClient(awsCredentials);
        amazonSNSClient.setEndpoint("https://sns." + domainName + ".amazonaws.com/");

        String topicName = vaultName + "-inkspot_glacier-topic";
        CreateTopicRequest createTopicRequest = new CreateTopicRequest();
        createTopicRequest.withName(topicName);

        CreateTopicResult createTopicResult = amazonSNSClient.createTopic(createTopicRequest);
        if (createTopicResult != null) {
            topicARN = createTopicResult.getTopicArn();

            SubscribeRequest subscribeRequest = new SubscribeRequest();
            subscribeRequest.withTopicArn(topicARN);
            subscribeRequest.withProtocol("sqs");
            subscribeRequest.withEndpoint(queueARN);

            SubscribeResult subscribeResult = amazonSNSClient.subscribe(subscribeRequest);
            if (subscribeResult == null)
                logger.warn("Unable to subscribe topic: \"" + topicName + "\" to queue: \"" + queueARN + "\"");
        } else
            logger.warn("Unable to create topic: \"" + topicName + "\"");

        amazonSNSClient.shutdown();
    } catch (AmazonServiceException amazonServiceException) {
        logger.warn("AmazonServiceException: " + amazonServiceException);
        logger.debug(amazonServiceException);
    } catch (IllegalArgumentException illegalArgumentException) {
        logger.warn("IllegalArgumentException: " + illegalArgumentException);
        logger.debug(illegalArgumentException);
    } catch (AmazonClientException amazonClientException) {
        logger.warn("AmazonClientException: " + amazonClientException);
        logger.debug(amazonClientException);
    } catch (Throwable throwable) {
        logger.warn("Throwable: " + throwable);
        logger.debug(throwable);
    }

    return topicARN;
}

From source file:com.connexience.server.model.archive.glacier.SetupUtils.java

License:Open Source License

public static void setupVault(String accessKey, String secretKey, String domainName, String vaultName,
        String topicARN) {/*from  ww w. j a v  a 2 s  .c o m*/
    try {
        AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);

        AmazonGlacierClient amazonGlacierClient = new AmazonGlacierClient(awsCredentials);
        amazonGlacierClient.setEndpoint("https://glacier." + domainName + ".amazonaws.com/");

        CreateVaultRequest createVaultRequest = new CreateVaultRequest();
        createVaultRequest.withVaultName(vaultName);

        CreateVaultResult createVaultResult = amazonGlacierClient.createVault(createVaultRequest);
        if (createVaultResult != null) {
            VaultNotificationConfig vaultNotificationConfig = new VaultNotificationConfig();
            vaultNotificationConfig.withSNSTopic(topicARN);
            vaultNotificationConfig.withEvents("ArchiveRetrievalCompleted", "InventoryRetrievalCompleted");

            SetVaultNotificationsRequest setVaultNotificationsRequest = new SetVaultNotificationsRequest();
            setVaultNotificationsRequest.withVaultName(vaultName);
            setVaultNotificationsRequest.withVaultNotificationConfig(vaultNotificationConfig);

            amazonGlacierClient.setVaultNotifications(setVaultNotificationsRequest);
        } else
            logger.warn("Unable to create vault: \"" + vaultName + "\"");

        amazonGlacierClient.shutdown();
    } catch (AmazonServiceException amazonServiceException) {
        logger.warn("AmazonServiceException: " + amazonServiceException);
        logger.debug(amazonServiceException);
    } catch (IllegalArgumentException illegalArgumentException) {
        logger.warn("IllegalArgumentException: " + illegalArgumentException);
        logger.debug(illegalArgumentException);
    } catch (AmazonClientException amazonClientException) {
        logger.warn("AmazonClientException: " + amazonClientException);
        logger.debug(amazonClientException);
    } catch (Throwable throwable) {
        logger.warn("Throwable: " + throwable);
        logger.debug(throwable);
    }
}

From source file:com.crickdata.upload.s3.UploadLiveData.java

License:Open Source License

public Map<String, Date> uploadToS3(String fileName, boolean type) throws IOException {

    Statistics statistics = new Statistics();
    Map<String, Date> perfMap = new HashMap<String, Date>();
    AWSCredentials credentials = null;/*from   w  ww .  ja  v  a  2 s  . c  o m*/
    try {
        credentials = new BasicAWSCredentials("AKIAI6QKTRAQE7MXQOIQ",
                "wIG6u1yI5ZaseeJbvYSUmD98qelIJNSCVBzt5k2q");
    } catch (Exception e) {
        throw new AmazonClientException("Cannot load the credentials from the credential profiles file. "
                + "Please make sure that your credentials file is at the correct "
                + "location (C:\\Users\\bssan_000\\.aws\\credentials), and is in valid format.", e);
    }

    AmazonS3 s3 = new AmazonS3Client(credentials);
    Region usWest2 = Region.getRegion(Regions.US_WEST_2);
    s3.setRegion(usWest2);
    String bucketName;
    if (!type)
        bucketName = "cricmatchinfo";
    else
        bucketName = "cricmatchinfoseries";
    String key = fileName.replace(".json", "").trim();
    try {
        perfMap.put("S3INSERTREQ", new Date());
        statistics.setS3Req(new Date());
        File f = readMatchFile(fileName);

        double bytes = f.length();
        double kilobytes = (bytes / 1024);
        System.out.println("Details :" + kilobytes);
        s3.putObject(new PutObjectRequest(bucketName, key, f));
        statistics.setSize(String.valueOf(kilobytes));

        S3Object object = s3.getObject(new GetObjectRequest(bucketName, key));
        perfMap.put("S3SAVERES", object.getObjectMetadata().getLastModified());
        statistics.setKey(key);
        statistics.setS3Res(object.getObjectMetadata().getLastModified());
        MyUI.stats.add(statistics);

        displayTextInputStream(object.getObjectContent());

        ObjectListing objectListing = s3
                .listObjects(new ListObjectsRequest().withBucketName(bucketName).withPrefix("My"));
        for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
            System.out.println(
                    " - " + objectSummary.getKey() + "  " + "(size = " + objectSummary.getSize() + ")");
        }
    } catch (AmazonServiceException ase) {
        System.out.println("Caught an AmazonServiceException, which means your request made it "
                + "to Amazon S3, but was rejected with an error response for some reason.");
        System.out.println("Error Message:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("AWS Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.getRequestId());
    } catch (AmazonClientException ace) {
        System.out.println("Caught an AmazonClientException, which means the client encountered "
                + "a serious internal problem while trying to communicate with S3, "
                + "such as not being able to access the network.");
        System.out.println("Error Message: " + ace.getMessage());
    }
    return perfMap;
}

From source file:com.datatorrent.contrib.kinesis.KinesisUtil.java

License:Open Source License

/**
 * Create the AmazonKinesisClient with the given credentials
 * @param accessKey AWS accessKeyId/*from ww  w .  j ava 2s  .co m*/
 * @param secretKey AWS secretAccessKey
 * @throws Exception
 */
public void createKinesisClient(String accessKey, String secretKey, String endPoint) throws Exception {
    if (client == null) {
        try {
            client = new AmazonKinesisClient(new BasicAWSCredentials(accessKey, secretKey));
            if (endPoint != null) {
                client.setEndpoint(endPoint);
            }
        } catch (Exception e) {
            throw new AmazonClientException("Unable to load credentials", e);
        }
    }
}