diff options
Diffstat (limited to 'src/main/java/com/att/nsa/mr/tools')
-rw-r--r-- | src/main/java/com/att/nsa/mr/tools/ApiKeyCommand.java | 134 | ||||
-rw-r--r-- | src/main/java/com/att/nsa/mr/tools/AuthCommand.java | 69 | ||||
-rw-r--r-- | src/main/java/com/att/nsa/mr/tools/ClusterCommand.java | 80 | ||||
-rw-r--r-- | src/main/java/com/att/nsa/mr/tools/MRCommandContext.java | 100 | ||||
-rw-r--r-- | src/main/java/com/att/nsa/mr/tools/MRTool.java | 49 | ||||
-rw-r--r-- | src/main/java/com/att/nsa/mr/tools/MessageCommand.java | 128 | ||||
-rw-r--r-- | src/main/java/com/att/nsa/mr/tools/TopicCommand.java | 209 | ||||
-rw-r--r-- | src/main/java/com/att/nsa/mr/tools/TraceCommand.java | 118 |
8 files changed, 887 insertions, 0 deletions
diff --git a/src/main/java/com/att/nsa/mr/tools/ApiKeyCommand.java b/src/main/java/com/att/nsa/mr/tools/ApiKeyCommand.java new file mode 100644 index 0000000..0faefe7 --- /dev/null +++ b/src/main/java/com/att/nsa/mr/tools/ApiKeyCommand.java @@ -0,0 +1,134 @@ +/******************************************************************************* + * ============LICENSE_START======================================================= + * org.onap.dmaap + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed 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. + * ============LICENSE_END========================================================= + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * + *******************************************************************************/ +package com.att.nsa.mr.tools; + +import java.io.IOException; +import java.io.PrintStream; + +import com.att.nsa.apiClient.credentials.ApiCredential; +import com.att.nsa.apiClient.http.HttpException; +import com.att.nsa.apiClient.http.HttpObjectNotFoundException; +import com.att.nsa.cmdtool.Command; +import com.att.nsa.cmdtool.CommandNotReadyException; +import com.att.nsa.mr.client.MRClientFactory; +import com.att.nsa.mr.client.MRIdentityManager; +import com.att.nsa.mr.client.MRClient.MRApiException; +import com.att.nsa.mr.client.MRIdentityManager.ApiKey; + +public class ApiKeyCommand implements Command<MRCommandContext> +{ + + @Override + public String[] getMatches () + { + return new String[]{ + "key (create|update) (\\S*) (\\S*)", + "key (list) (\\S*)", + "key (revoke)", + }; + } + + @Override + public void checkReady ( MRCommandContext context ) throws CommandNotReadyException + { + if ( !context.checkClusterReady () ) + { + throw new CommandNotReadyException ( "Use 'cluster' to specify a cluster to use." ); + } + } + + @Override + public void execute ( String[] parts, MRCommandContext context, PrintStream out ) throws CommandNotReadyException + { + final MRIdentityManager tm = MRClientFactory.createIdentityManager ( context.getCluster(), context.getApiKey(), context.getApiPwd() ); + context.applyTracer ( tm ); + + try + { + if ( parts[0].equals ( "list" ) ) + { + final ApiKey key = tm.getApiKey ( parts[1] ); + if ( key != null ) + { + out.println ( "email: " + key.getEmail () ); + out.println ( "description: " + key.getDescription () ); + } + else + { + out.println ( "No key returned" ); + } + } + else if ( parts[0].equals ( "create" ) ) + { + final ApiCredential ac = tm.createApiKey ( parts[1], parts[2] ); + if ( ac != null ) + { + out.println ( " key: " + ac.getApiKey () ); + out.println ( "secret: " + ac.getApiSecret () ); + } + else + { + out.println ( "No credential returned?" ); + } + } + else if ( parts[0].equals ( "update" ) ) + { + tm.updateCurrentApiKey ( parts[1], parts[2] ); + out.println ( "Updated" ); + } + else if ( parts[0].equals ( "revoke" ) ) + { + tm.deleteCurrentApiKey (); + out.println ( "Updated" ); + } + } + catch ( HttpObjectNotFoundException e ) + { + out.println ( "Object not found: " + e.getMessage () ); + } + catch ( HttpException e ) + { + out.println ( "HTTP exception: " + e.getMessage () ); + } + catch ( MRApiException e ) + { + out.println ( "API exception: " + e.getMessage () ); + } + catch ( IOException e ) + { + out.println ( "IO exception: " + e.getMessage () ); + } + finally + { + tm.close (); + } + } + + @Override + public void displayHelp ( PrintStream out ) + { + out.println ( "key create <email> <description>" ); + out.println ( "key update <email> <description>" ); + out.println ( "key list <key>" ); + out.println ( "key revoke" ); + } +} diff --git a/src/main/java/com/att/nsa/mr/tools/AuthCommand.java b/src/main/java/com/att/nsa/mr/tools/AuthCommand.java new file mode 100644 index 0000000..72f226b --- /dev/null +++ b/src/main/java/com/att/nsa/mr/tools/AuthCommand.java @@ -0,0 +1,69 @@ +/******************************************************************************* + * ============LICENSE_START======================================================= + * org.onap.dmaap + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed 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. + * ============LICENSE_END========================================================= + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * + *******************************************************************************/ +package com.att.nsa.mr.tools; + +import java.io.PrintStream; + +import com.att.nsa.cmdtool.Command; +import com.att.nsa.cmdtool.CommandNotReadyException; + +public class AuthCommand implements Command<MRCommandContext> +{ + @Override + public void checkReady ( MRCommandContext context ) throws CommandNotReadyException + { + } + + @Override + public void execute ( String[] parts, MRCommandContext context, PrintStream out ) throws CommandNotReadyException + { + if ( parts.length > 0 ) + { + context.setAuth ( parts[0], parts[1] ); + out.println ( "Now authenticating with " + parts[0] ); + } + else + { + context.clearAuth (); + out.println ( "No longer authenticating." ); + } + } + + @Override + public void displayHelp ( PrintStream out ) + { + out.println ( "auth <apiKey> <apiSecret>" ); + out.println ( "\tuse these credentials on subsequent transactions" ); + out.println ( "noauth" ); + out.println ( "\tdo not use credentials on subsequent transactions" ); + } + + @Override + public String[] getMatches () + { + return new String[] + { + "auth (\\S*) (\\S*)", + "noauth" + }; + } +} diff --git a/src/main/java/com/att/nsa/mr/tools/ClusterCommand.java b/src/main/java/com/att/nsa/mr/tools/ClusterCommand.java new file mode 100644 index 0000000..5ecaf7c --- /dev/null +++ b/src/main/java/com/att/nsa/mr/tools/ClusterCommand.java @@ -0,0 +1,80 @@ +/******************************************************************************* + * ============LICENSE_START======================================================= + * org.onap.dmaap + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed 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. + * ============LICENSE_END========================================================= + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * + *******************************************************************************/ +package com.att.nsa.mr.tools; + +import java.io.PrintStream; + +import com.att.nsa.cmdtool.Command; +import com.att.nsa.cmdtool.CommandNotReadyException; +import com.att.nsa.mr.client.impl.MRConsumerImpl; + +public class ClusterCommand implements Command<MRCommandContext> +{ + + @Override + public String[] getMatches () + { + return new String[]{ + "cluster", + "cluster (\\S*)?", + }; + } + + @Override + public void checkReady ( MRCommandContext context ) throws CommandNotReadyException + { + } + + @Override + public void execute ( String[] parts, MRCommandContext context, PrintStream out ) throws CommandNotReadyException + { + if ( parts.length == 0 ) + { + for ( String host : context.getCluster () ) + { + out.println ( host ); + } + } + else + { + context.clearCluster (); + for ( String part : parts ) + { + String[] hosts = part.trim().split ( "\\s+" ); + for ( String host : hosts ) + { + for ( String splitHost : MRConsumerImpl.stringToList(host) ) + { + context.addClusterHost ( splitHost ); + } + } + } + } + } + + @Override + public void displayHelp ( PrintStream out ) + { + out.println ( "cluster host1 host2 ..." ); + } + +} diff --git a/src/main/java/com/att/nsa/mr/tools/MRCommandContext.java b/src/main/java/com/att/nsa/mr/tools/MRCommandContext.java new file mode 100644 index 0000000..3430d9c --- /dev/null +++ b/src/main/java/com/att/nsa/mr/tools/MRCommandContext.java @@ -0,0 +1,100 @@ +/******************************************************************************* + * ============LICENSE_START======================================================= + * org.onap.dmaap + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed 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. + * ============LICENSE_END========================================================= + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * + *******************************************************************************/ +package com.att.nsa.mr.tools; + +import java.util.Collection; +import java.util.LinkedList; + +import com.att.nsa.apiClient.http.HttpClient; +import com.att.nsa.apiClient.http.HttpTracer; +import com.att.nsa.cmdtool.CommandContext; +import com.att.nsa.mr.client.MRClient; + +public class MRCommandContext implements CommandContext +{ + public MRCommandContext () + { + fApiKey = null; + fApiPwd = null; + + fCluster = new LinkedList<String> (); + fCluster.add ( "localhost" ); + } + + @Override + public void requestShutdown () + { + fShutdown = true; + } + + @Override + public boolean shouldContinue () + { + return !fShutdown; + } + + public void setAuth ( String key, String pwd ) { fApiKey = key; fApiPwd = pwd; } + public void clearAuth () { setAuth(null,null); } + + public boolean checkClusterReady () + { + return ( fCluster.size () != 0 ); + } + + public Collection<String> getCluster () + { + return new LinkedList<String> ( fCluster ); + } + + public void clearCluster () + { + fCluster.clear (); + } + + public void addClusterHost ( String host ) + { + fCluster.add ( host ); + } + + public String getApiKey () { return fApiKey; } + public String getApiPwd () { return fApiPwd; } + + public void useTracer ( HttpTracer t ) + { + fTracer = t; + } + public void noTracer () { fTracer = null; } + + public void applyTracer ( MRClient cc ) + { + if ( cc instanceof HttpClient && fTracer != null ) + { + ((HttpClient)cc).installTracer ( fTracer ); + } + } + + private boolean fShutdown; + private String fApiKey; + private String fApiPwd; + private final LinkedList<String> fCluster; + private HttpTracer fTracer = null; +} diff --git a/src/main/java/com/att/nsa/mr/tools/MRTool.java b/src/main/java/com/att/nsa/mr/tools/MRTool.java new file mode 100644 index 0000000..7f1effd --- /dev/null +++ b/src/main/java/com/att/nsa/mr/tools/MRTool.java @@ -0,0 +1,49 @@ +/******************************************************************************* + * ============LICENSE_START======================================================= + * org.onap.dmaap + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed 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. + * ============LICENSE_END========================================================= + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * + *******************************************************************************/ +package com.att.nsa.mr.tools; + +import java.io.IOException; + +import com.att.nsa.cmdtool.CommandLineTool; +import com.att.nsa.mr.client.impl.MRClientVersionInfo; + +public class MRTool extends CommandLineTool<MRCommandContext> +{ + protected MRTool () + { + super ( "MR Tool (" + MRClientVersionInfo.getVersion () + ")", "MR> " ); + + registerCommand ( new ApiKeyCommand () ); + registerCommand ( new AuthCommand () ); + registerCommand ( new ClusterCommand () ); + registerCommand ( new MessageCommand () ); + registerCommand ( new TopicCommand () ); + registerCommand ( new TraceCommand () ); + } + + public static void main ( String[] args ) throws IOException + { + final MRTool ct = new MRTool (); + final MRCommandContext ccc = new MRCommandContext (); + ct.runFromMain ( args, ccc ); + } +} diff --git a/src/main/java/com/att/nsa/mr/tools/MessageCommand.java b/src/main/java/com/att/nsa/mr/tools/MessageCommand.java new file mode 100644 index 0000000..2c646bf --- /dev/null +++ b/src/main/java/com/att/nsa/mr/tools/MessageCommand.java @@ -0,0 +1,128 @@ +/******************************************************************************* + * ============LICENSE_START======================================================= + * org.onap.dmaap + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed 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. + * ============LICENSE_END========================================================= + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * + *******************************************************************************/ +package com.att.nsa.mr.tools; + +import java.io.IOException; +import java.io.PrintStream; +import java.util.List; +import java.util.concurrent.TimeUnit; + +import com.att.nsa.cmdtool.Command; +import com.att.nsa.cmdtool.CommandNotReadyException; +import com.att.nsa.mr.client.MRBatchingPublisher; +import com.att.nsa.mr.client.MRClientFactory; +import com.att.nsa.mr.client.MRConsumer; +import com.att.nsa.mr.client.MRClientBuilders.PublisherBuilder; +import com.att.nsa.mr.client.MRPublisher.message; + +public class MessageCommand implements Command<MRCommandContext> +{ + + @Override + public String[] getMatches () + { + return new String[]{ + "(post) (\\S*) (\\S*) (.*)", + "(read) (\\S*) (\\S*) (\\S*)", + }; + } + + @Override + public void checkReady ( MRCommandContext context ) throws CommandNotReadyException + { + if ( !context.checkClusterReady () ) + { + throw new CommandNotReadyException ( "Use 'cluster' to specify a cluster to use." ); + } + } + + @Override + public void execute ( String[] parts, MRCommandContext context, PrintStream out ) throws CommandNotReadyException + { + if ( parts[0].equalsIgnoreCase ( "read" )) + { + final MRConsumer cc = MRClientFactory.createConsumer ( context.getCluster (), parts[1], parts[2], parts[3], + -1, -1, null, context.getApiKey(), context.getApiPwd() ); + context.applyTracer ( cc ); + try + { + for ( String msg : cc.fetch () ) + { + out.println ( msg ); + } + } + catch ( Exception e ) + { + out.println ( "Problem fetching messages: " + e.getMessage() ); + } + finally + { + cc.close (); + } + } + else + { + final MRBatchingPublisher pub = new PublisherBuilder (). + usingHosts ( context.getCluster () ). + onTopic ( parts[1] ). + authenticatedBy ( context.getApiKey(), context.getApiPwd() ). + build () + ; + try + { + pub.send ( parts[2], parts[3] ); + } + catch ( IOException e ) + { + out.println ( "Problem sending message: " + e.getMessage() ); + } + finally + { + List<message> left = null; + try + { + left = pub.close ( 500, TimeUnit.MILLISECONDS ); + } + catch ( IOException e ) + { + out.println ( "Problem sending message: " + e.getMessage() ); + } + catch ( InterruptedException e ) + { + out.println ( "Problem sending message: " + e.getMessage() ); + } + if ( left != null && left.size () > 0 ) + { + out.println ( left.size() + " messages not sent." ); + } + } + } + } + + @Override + public void displayHelp ( PrintStream out ) + { + out.println ( "post <topicName> <partition> <message>" ); + out.println ( "read <topicName> <consumerGroup> <consumerId>" ); + } + +} diff --git a/src/main/java/com/att/nsa/mr/tools/TopicCommand.java b/src/main/java/com/att/nsa/mr/tools/TopicCommand.java new file mode 100644 index 0000000..8010a58 --- /dev/null +++ b/src/main/java/com/att/nsa/mr/tools/TopicCommand.java @@ -0,0 +1,209 @@ +/******************************************************************************* + * ============LICENSE_START======================================================= + * org.onap.dmaap + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed 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. + * ============LICENSE_END========================================================= + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * + *******************************************************************************/ +package com.att.nsa.mr.tools; + +import java.io.IOException; +import java.io.PrintStream; +import java.util.Set; + +import com.att.nsa.apiClient.http.HttpException; +import com.att.nsa.apiClient.http.HttpObjectNotFoundException; +import com.att.nsa.cmdtool.Command; +import com.att.nsa.cmdtool.CommandNotReadyException; +import com.att.nsa.mr.client.MRClientFactory; +import com.att.nsa.mr.client.MRTopicManager; +import com.att.nsa.mr.client.MRTopicManager.TopicInfo; + +public class TopicCommand implements Command<MRCommandContext> +{ + + @Override + public String[] getMatches () + { + return new String[]{ + "topic (list)", + "topic (list) (\\S*)", + "topic (create) (\\S*) (\\S*) (\\S*)", + "topic (grant|revoke) (read|write) (\\S*) (\\S*)", + }; + } + + @Override + public void checkReady ( MRCommandContext context ) throws CommandNotReadyException + { + if ( !context.checkClusterReady () ) + { + throw new CommandNotReadyException ( "Use 'cluster' to specify a cluster to use." ); + } + } + + @Override + public void execute ( String[] parts, MRCommandContext context, PrintStream out ) throws CommandNotReadyException + { + final MRTopicManager tm = MRClientFactory.createTopicManager ( context.getCluster(), context.getApiKey(), context.getApiPwd() ); + context.applyTracer ( tm ); + + try + { + if ( parts[0].equals ( "list" ) ) + { + try + { + if ( parts.length == 1 ) + { + for ( String topic : tm.getTopics () ) + { + out.println ( topic ); + } + } + else + { + final TopicInfo ti = tm.getTopicMetadata ( parts[1] ); + + final String owner = ti.getOwner (); + out.println ( " owner: " + ( owner == null ? "<none>" : owner ) ); + + final String desc = ti.getDescription (); + out.println ( "description: " + ( desc == null ? "<none>" : desc ) ); + + final Set<String> prods = ti.getAllowedProducers (); + if ( prods != null ) + { + out.println ( " write ACL: " ); + for ( String key : prods ) + { + out.println ( "\t" + key ); + } + } + else + { + out.println ( " write ACL: <not active>" ); + } + + final Set<String> cons = ti.getAllowedConsumers (); + if ( cons != null ) + { + out.println ( " read ACL: " ); + for ( String key : cons ) + { + out.println ( "\t" + key ); + } + } + else + { + out.println ( " read ACL: <not active>" ); + } + } + } + catch ( IOException x ) + { + out.println ( "Problem with request: " + x.getMessage () ); + } + catch ( HttpObjectNotFoundException e ) + { + out.println ( "Not found: " + e.getMessage () ); + } + } + else if ( parts[0].equals ( "create" ) ) + { + try + { + final int partitions = Integer.parseInt ( parts[2] ); + final int replicas = Integer.parseInt ( parts[3] ); + + tm.createTopic ( parts[1], "", partitions, replicas ); + } + catch ( HttpException e ) + { + out.println ( "Problem with request: " + e.getMessage () ); + } + catch ( IOException e ) + { + out.println ( "Problem with request: " + e.getMessage () ); + } + catch ( NumberFormatException e ) + { + out.println ( "Problem with request: " + e.getMessage () ); + } + } + else if ( parts[0].equals ( "grant" ) ) + { + try + { + if ( parts[1].equals ( "write" ) ) + { + tm.allowProducer ( parts[2], parts[3] ); + } + else if ( parts[1].equals ( "read" ) ) + { + tm.allowConsumer ( parts[2], parts[3] ); + } + } + catch ( HttpException e ) + { + out.println ( "Problem with request: " + e.getMessage () ); + } + catch ( IOException e ) + { + out.println ( "Problem with request: " + e.getMessage () ); + } + } + else if ( parts[0].equals ( "revoke" ) ) + { + try + { + if ( parts[1].equals ( "write" ) ) + { + tm.revokeProducer ( parts[2], parts[3] ); + } + else if ( parts[1].equals ( "read" ) ) + { + tm.revokeConsumer ( parts[2], parts[3] ); + } + } + catch ( HttpException e ) + { + out.println ( "Problem with request: " + e.getMessage () ); + } + catch ( IOException e ) + { + out.println ( "Problem with request: " + e.getMessage () ); + } + } + } + finally + { + tm.close (); + } + } + + @Override + public void displayHelp ( PrintStream out ) + { + out.println ( "topic list" ); + out.println ( "topic list <topicName>" ); + out.println ( "topic create <topicName> <partitions> <replicas>" ); + out.println ( "topic grant write|read <topicName> <apiKey>" ); + out.println ( "topic revoke write|read <topicName> <apiKey>" ); + } + +} diff --git a/src/main/java/com/att/nsa/mr/tools/TraceCommand.java b/src/main/java/com/att/nsa/mr/tools/TraceCommand.java new file mode 100644 index 0000000..0489172 --- /dev/null +++ b/src/main/java/com/att/nsa/mr/tools/TraceCommand.java @@ -0,0 +1,118 @@ +/******************************************************************************* + * ============LICENSE_START======================================================= + * org.onap.dmaap + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed 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. + * ============LICENSE_END========================================================= + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * + *******************************************************************************/ +package com.att.nsa.mr.tools; + +import java.io.PrintStream; +import java.net.URI; +import java.util.List; +import java.util.Map; + +import com.att.nsa.apiClient.http.HttpTracer; +import com.att.nsa.cmdtool.Command; +import com.att.nsa.cmdtool.CommandNotReadyException; + +public class TraceCommand implements Command<MRCommandContext> +{ + @Override + public void checkReady ( MRCommandContext context ) throws CommandNotReadyException + { + } + + @Override + public void execute ( String[] parts, MRCommandContext context, final PrintStream out ) throws CommandNotReadyException + { + if ( parts[0].equalsIgnoreCase ( "on" )) + { + context.useTracer ( new HttpTracer () + { + @Override + public void outbound ( URI uri, Map<String, List<String>> headers, String method, byte[] entity ) + { + out.println ( kLineBreak ); + out.println ( ">>> " + method + " " + uri.toString() ); + for ( Map.Entry<String,List<String>> e : headers.entrySet () ) + { + final StringBuffer vals = new StringBuffer (); + for ( String val : e.getValue () ) + { + if ( vals.length () > 0 ) vals.append ( ", " ); + vals.append ( val ); + } + out.println ( ">>> " + e.getKey () + ": " + vals.toString() ); + } + if ( entity != null ) + { + out.println (); + out.println ( new String ( entity ) ); + } + out.println ( kLineBreak ); + } + + @Override + public void inbound ( Map<String, List<String>> headers, int statusCode, String responseLine, byte[] entity ) + { + out.println ( kLineBreak ); + out.println ( "<<< " + responseLine ); + for ( Map.Entry<String,List<String>> e : headers.entrySet () ) + { + final StringBuffer vals = new StringBuffer (); + for ( String val : e.getValue () ) + { + if ( vals.length () > 0 ) vals.append ( ", " ); + vals.append ( val ); + } + out.println ( "<<< " + e.getKey () + ": " + vals.toString() ); + } + if ( entity != null ) + { + out.println (); + out.println ( new String ( entity ) ); + } + out.println ( kLineBreak ); + } + } ); + } + else + { + context.noTracer (); + } + } + + @Override + public void displayHelp ( PrintStream out ) + { + out.println ( "trace on|off" ); + out.println ( "\tWhen trace is on, HTTP interaction is printed to the console." ); + } + + @Override + public String[] getMatches () + { + return new String[] + { + "trace (on)", + "trace (off)" + }; + } + + private static final String kLineBreak = "======================================================================"; +} |