a167c0f9fb97dccbb0458ee969225c619a9e5385
[dmaap/messagerouter/dmaapclient.git] / src / main / java / org / onap / dmaap / mr / tools / TraceCommand.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  org.onap.dmaap
4  *  ================================================================================
5  *  Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  *  ================================================================================
7  *  Modifications Copyright © 2021 Orange.
8  *  ================================================================================
9  *  Licensed under the Apache License, Version 2.0 (the "License");
10  *  you may not use this file except in compliance with the License.
11  *  You may obtain a copy of the License at
12  *        http://www.apache.org/licenses/LICENSE-2.0
13  *
14  *  Unless required by applicable law or agreed to in writing, software
15  *  distributed under the License is distributed on an "AS IS" BASIS,
16  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  *  See the License for the specific language governing permissions and
18  *  limitations under the License.
19  *  ============LICENSE_END=========================================================
20  *
21  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  *
23  *******************************************************************************/
24
25 package org.onap.dmaap.mr.tools;
26
27 import com.att.nsa.apiClient.http.HttpTracer;
28 import com.att.nsa.cmdtool.Command;
29 import com.att.nsa.cmdtool.CommandNotReadyException;
30
31 import java.io.PrintStream;
32 import java.net.URI;
33 import java.util.List;
34 import java.util.Map;
35
36 public class TraceCommand implements Command<MRCommandContext> {
37     @Override
38     public void checkReady(MRCommandContext context) throws CommandNotReadyException {
39     }
40
41     @Override
42     public void execute(String[] parts, MRCommandContext context, final PrintStream out) throws CommandNotReadyException {
43         if (parts[0].equalsIgnoreCase("on")) {
44             context.useTracer(new HttpTracer() {
45                 @Override
46                 public void outbound(URI uri, Map<String, List<String>> headers, String method, byte[] entity) {
47                     out.println(K_LINE_BREAK);
48                     out.println(">>> " + method + " " + uri.toString());
49                     for (Map.Entry<String, List<String>> e : headers.entrySet()) {
50                         final StringBuilder vals = new StringBuilder();
51                         for (String val : e.getValue()) {
52                             if (vals.length() > 0) vals.append(", ");
53                             vals.append(val);
54                         }
55                         out.println(">>> " + e.getKey() + ": " + vals);
56                     }
57                     if (entity != null) {
58                         out.println();
59                         out.println(new String(entity));
60                     }
61                     out.println(K_LINE_BREAK);
62                 }
63
64                 @Override
65                 public void inbound(Map<String, List<String>> headers, int statusCode, String responseLine, byte[] entity) {
66                     out.println(K_LINE_BREAK);
67                     out.println("<<< " + responseLine);
68                     for (Map.Entry<String, List<String>> e : headers.entrySet()) {
69                         final StringBuilder vals = new StringBuilder();
70                         for (String val : e.getValue()) {
71                             if (vals.length() > 0) {
72                                 vals.append(", ");
73                             }
74                             vals.append(val);
75                         }
76                         out.println("<<< " + e.getKey() + ": " + vals);
77                     }
78                     if (entity != null) {
79                         out.println();
80                         out.println(new String(entity));
81                     }
82                     out.println(K_LINE_BREAK);
83                 }
84             });
85         } else {
86             context.noTracer();
87         }
88     }
89
90     @Override
91     public void displayHelp(PrintStream out) {
92         out.println("trace on|off");
93         out.println("\tWhen trace is on, HTTP interaction is printed to the console.");
94     }
95
96     @Override
97     public String[] getMatches() {
98         return new String[] {
99             "trace (on)",
100             "trace (off)"
101         };
102     }
103
104     private static final String K_LINE_BREAK = "======================================================================";
105 }