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