summaryrefslogtreecommitdiff
path: root/java/broker/src/main/java/org/apache/qpid/tools/messagestore/commands/Dump.java
blob: 8bb5d02b01d4b8f63108c076b2bacf81ffc706a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 *
 *
 */
package org.apache.qpid.tools.messagestore.commands;

import org.apache.commons.codec.binary.Hex;
import org.apache.qpid.server.queue.QueueEntryImpl;
import org.apache.qpid.server.queue.QueueEntry;
import org.apache.qpid.server.message.ServerMessage;
import org.apache.qpid.tools.messagestore.MessageStoreTool;
import org.apache.qpid.tools.utils.Console;

import java.io.UnsupportedEncodingException;
import java.util.LinkedList;
import java.util.List;

public class Dump extends Show
{
    private static final int LINE_SIZE = 8;
    private static final String DEFAULT_ENCODING = "utf-8";
    private static final boolean SPACE_BYTES = true;
    private static final String BYTE_SPACER = " ";
    private static final String NON_PRINTING_ASCII_CHAR = "?";

    protected boolean _content = true;

    public Dump(MessageStoreTool tool)
    {
        super(tool);
    }

    public String help()
    {
        return "Dump selected message content. Default: show=content";
    }

    public String usage()
    {
        return getCommand() + " [show=[all],[msgheaders],[_amqHeaders],[routing],[content]] [id=<msgid e.g. 1,2,4-10>]";
    }

    public String getCommand()
    {
        return "dump";
    }

    public void execute(String... args)
    {
        assert args.length > 0;
        assert args[0].equals(getCommand());


        if (args.length >= 2)
        {
            for (String arg : args)
            {
                if (arg.startsWith("show="))
                {
                    _content = arg.contains("content") || arg.contains("all");
                }
            }

            parseArgs(args);
        }

        performShow();
    }


    protected List<List> createMessageData(java.util.List<Long> msgids, List<QueueEntry> messages, boolean showHeaders, boolean showRouting,
                                           boolean showMessageHeaders)
    {

        List<List> display = new LinkedList<List>();

        List<String> hex = new LinkedList<String>();
        List<String> ascii = new LinkedList<String>();
        display.add(hex);
        display.add(ascii);

        for (QueueEntry entry : messages)
        {
            ServerMessage msg = entry.getMessage();
            if (!includeMsg(msg, msgids))
            {
                continue;
            }

            //Add divider between messages
            hex.add(Console.ROW_DIVIDER);
            ascii.add(Console.ROW_DIVIDER);

            // Show general message information
            hex.add(Show.Columns.ID.name());
            ascii.add(msg.getMessageNumber().toString());

            hex.add(Console.ROW_DIVIDER);
            ascii.add(Console.ROW_DIVIDER);

            if (showRouting)
            {
                addShowInformation(hex, ascii, msg, "Routing Details", true, false, false);
            }
            if (showHeaders)
            {
                addShowInformation(hex, ascii, msg, "Headers", false, true, false);
            }
            if (showMessageHeaders)
            {
                addShowInformation(hex, ascii, msg, null, false, false, true);
            }

            // Add Content Body section
            hex.add("Content Body");
            ascii.add("");
            hex.add(Console.ROW_DIVIDER);
            ascii.add(Console.ROW_DIVIDER);


            final int messageSize = (int) msg.getSize();
            if (messageSize != 0)
            {
                hex.add("Hex");
                hex.add(Console.ROW_DIVIDER);


                ascii.add("ASCII");
                ascii.add(Console.ROW_DIVIDER);

                java.nio.ByteBuffer buf = java.nio.ByteBuffer.allocate(64 * 1024);

                int position = 0;

                while(position < messageSize)
                {

                    position += msg.getContent(buf, position);
                    buf.flip();
                    //Duplicate so we don't destroy original data :)
                    java.nio.ByteBuffer hexBuffer = buf;

                    java.nio.ByteBuffer charBuffer = hexBuffer.duplicate();

                    Hex hexencoder = new Hex();

                    while (hexBuffer.hasRemaining())
                    {
                        byte[] line = new byte[LINE_SIZE];

                        int bufsize = hexBuffer.remaining();
                        if (bufsize < LINE_SIZE)
                        {
                            hexBuffer.get(line, 0, bufsize);
                        }
                        else
                        {
                            bufsize = line.length;
                            hexBuffer.get(line);
                        }

                        byte[] encoded = hexencoder.encode(line);

                        try
                        {
                            String encStr = new String(encoded, 0, bufsize * 2, DEFAULT_ENCODING);
                            String hexLine = "";

                            int strLength = encStr.length();
                            for (int c = 0; c < strLength; c++)
                            {
                                hexLine += encStr.charAt(c);

                                if ((c & 1) == 1 && SPACE_BYTES)
                                {
                                    hexLine += BYTE_SPACER;
                                }
                            }

                            hex.add(hexLine);
                        }
                        catch (UnsupportedEncodingException e)
                        {
                            _console.println(e.getMessage());
                            return null;
                        }
                    }

                    while (charBuffer.hasRemaining())
                    {
                        String asciiLine = "";

                        for (int pos = 0; pos < LINE_SIZE; pos++)
                        {
                            if (charBuffer.hasRemaining())
                            {
                                byte ch = charBuffer.get();

                                if (isPrintable(ch))
                                {
                                    asciiLine += (char) ch;
                                }
                                else
                                {
                                    asciiLine += NON_PRINTING_ASCII_CHAR;
                                }

                                if (SPACE_BYTES)
                                {
                                    asciiLine += BYTE_SPACER;
                                }
                            }
                            else
                            {
                                break;
                            }
                        }

                        ascii.add(asciiLine);
                    }
                    buf.clear();
                }
            }
            else
            {
                List<String> result = new LinkedList<String>();

                display.add(result);
                result.add("No ContentBodies");
            }
        }

        // if hex is empty then we have no data to display
        if (hex.size() == 0)
        {
            return null;
        }

        return display;
    }

    private void addShowInformation(List<String> column1, List<String> column2, ServerMessage msg,
                                    String title, boolean routing, boolean headers, boolean messageHeaders)
    {
        List<QueueEntry> single = new LinkedList<QueueEntry>();
        single.add(new QueueEntryImpl(null,msg, Long.MIN_VALUE));

        List<List> routingData = super.createMessageData(null, single, headers, routing, messageHeaders);

        //Reformat data
        if (title != null)
        {
            column1.add(title);
            column2.add("");
            column1.add(Console.ROW_DIVIDER);
            column2.add(Console.ROW_DIVIDER);
        }

        // look at all columns in the routing Data
        for (List item : routingData)
        {
            // the item should be:
            //   Title
            //   *divider
            //   value
            // otherwise we can't reason about the correct value
            if (item.size() == 3)
            {
                //Filter out the columns we are not interested in.

                String columnName = item.get(0).toString();

                if (!(columnName.equals(Show.Columns.ID.name())
                      || columnName.equals(Show.Columns.Size.name())))
                {
                    column1.add(columnName);
                    column2.add(item.get(2).toString());
                }
            }
        }
        column1.add(Console.ROW_DIVIDER);
        column2.add(Console.ROW_DIVIDER);
    }

    private boolean isPrintable(byte c)
    {
        return c > 31 && c < 127;
    }
}