PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/stack/tools/src/main/java/org/apache/usergrid/tools/Cli.java

https://gitlab.com/indyaah/incubator-usergrid
Java | 270 lines | 219 code | 35 blank | 16 comment | 55 complexity | 83788e36984c311440b5637605a59fc1 MD5 | raw file
Possible License(s): JSON, BSD-3-Clause, Apache-2.0
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.usergrid.tools;
  18. import java.io.BufferedReader;
  19. import java.io.IOException;
  20. import java.io.InputStreamReader;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.UUID;
  25. import org.codehaus.jackson.JsonFactory;
  26. import org.slf4j.Logger;
  27. import org.slf4j.LoggerFactory;
  28. import org.apache.usergrid.persistence.Query;
  29. import org.apache.usergrid.persistence.exceptions.QueryParseException;
  30. import org.apache.usergrid.services.ServiceAction;
  31. import org.apache.usergrid.services.ServiceManager;
  32. import org.apache.usergrid.services.ServiceParameter;
  33. import org.apache.usergrid.services.ServicePayload;
  34. import org.apache.usergrid.services.ServiceRequest;
  35. import org.apache.usergrid.services.ServiceResults;
  36. import org.apache.usergrid.utils.HttpUtils;
  37. import org.apache.usergrid.utils.JsonUtils;
  38. import org.apache.usergrid.utils.UUIDUtils;
  39. import org.apache.commons.cli.CommandLine;
  40. import org.apache.commons.cli.Option;
  41. import org.apache.commons.cli.OptionBuilder;
  42. import org.apache.commons.cli.Options;
  43. import static org.apache.usergrid.persistence.cassandra.CassandraService.DEFAULT_APPLICATION_ID;
  44. public class Cli extends ToolBase {
  45. public static final int MAX_ENTITY_FETCH = 100;
  46. private static final Logger logger = LoggerFactory.getLogger( Cli.class );
  47. JsonFactory jsonFactory = new JsonFactory();
  48. @Override
  49. @SuppressWarnings("static-access")
  50. public Options createOptions() {
  51. Option hostOption =
  52. OptionBuilder.withArgName( "host" ).hasArg().withDescription( "Cassandra host" ).create( "host" );
  53. Option remoteOption = OptionBuilder.withDescription( "Use remote Cassandra instance" ).create( "remote" );
  54. Options options = new Options();
  55. options.addOption( hostOption );
  56. options.addOption( remoteOption );
  57. return options;
  58. }
  59. @Override
  60. public void runTool( CommandLine line ) throws Exception {
  61. startSpring();
  62. handleInput();
  63. }
  64. public void handleInput() throws QueryParseException {
  65. BufferedReader d = new BufferedReader( new InputStreamReader( System.in ) );
  66. UUID applicationId = DEFAULT_APPLICATION_ID;
  67. while ( true ) {
  68. System.out.println();
  69. System.out.print( ">" );
  70. String s = null;
  71. try {
  72. s = d.readLine();
  73. }
  74. catch ( IOException e ) {
  75. }
  76. if ( s == null ) {
  77. System.exit( 0 );
  78. }
  79. s = s.trim().toLowerCase();
  80. if ( s.startsWith( "use " ) ) {
  81. s = s.substring( 4 );
  82. applicationId = UUIDUtils.tryExtractUUID( s );
  83. if ( applicationId == null ) {
  84. try {
  85. applicationId = emf.lookupApplication( s.trim() );
  86. }
  87. catch ( Exception e ) {
  88. }
  89. }
  90. if ( applicationId == null ) {
  91. applicationId = DEFAULT_APPLICATION_ID;
  92. }
  93. System.out.println( "Using application " + applicationId );
  94. continue;
  95. }
  96. ServiceAction action = ServiceAction.GET;
  97. if ( s.startsWith( "get " ) ) {
  98. s = s.substring( "get ".length() ).trim();
  99. }
  100. else if ( s.startsWith( "post " ) ) {
  101. s = s.substring( "post ".length() ).trim();
  102. action = ServiceAction.POST;
  103. }
  104. else if ( s.startsWith( "put " ) ) {
  105. s = s.substring( "put".length() ).trim();
  106. action = ServiceAction.PUT;
  107. }
  108. else if ( s.startsWith( "delete " ) ) {
  109. s = s.substring( "delete ".length() ).trim();
  110. action = ServiceAction.DELETE;
  111. }
  112. else if ( s.startsWith( "quit" ) ) {
  113. return;
  114. }
  115. List<ServiceParameter> parameters = new ArrayList<ServiceParameter>();
  116. int i = 0;
  117. boolean next_is_query = false;
  118. boolean next_is_payload = false;
  119. boolean next_is_json = false;
  120. ServicePayload payload = null;
  121. while ( i < s.length() ) {
  122. boolean is_query = next_is_query;
  123. if ( next_is_payload ) {
  124. String str = s.substring( i );
  125. payload = ServicePayload.stringPayload( str );
  126. break;
  127. }
  128. else if ( next_is_json ) {
  129. next_is_json = false;
  130. int start = i - 1;
  131. int bracket_count = 1;
  132. while ( i < s.length() ) {
  133. char c = s.charAt( i );
  134. if ( c == '{' ) {
  135. bracket_count++;
  136. }
  137. else if ( c == '}' ) {
  138. bracket_count--;
  139. }
  140. if ( bracket_count == 0 ) {
  141. i++;
  142. String json = s.substring( start, i );
  143. Query query = Query.fromJsonString( json );
  144. ServiceParameter.addParameter( parameters, query );
  145. if ( ( i < s.length() ) && ( s.charAt( i ) == '/' ) ) {
  146. i++;
  147. }
  148. break;
  149. }
  150. i++;
  151. }
  152. continue;
  153. }
  154. next_is_query = false;
  155. next_is_payload = false;
  156. next_is_json = false;
  157. int slash = s.indexOf( '/', i );
  158. int semicolon = s.indexOf( ';', i );
  159. int question = s.indexOf( '?', i );
  160. int space = s.indexOf( ' ', i );
  161. int bracket = s.indexOf( '{', i );
  162. int j = s.length();
  163. if ( ( slash >= 0 ) && ( slash < j ) ) {
  164. j = slash;
  165. }
  166. if ( ( space >= 0 ) && ( space < j ) ) {
  167. j = space;
  168. next_is_payload = true;
  169. }
  170. if ( ( semicolon >= 0 ) && ( semicolon < j ) ) {
  171. next_is_query = true;
  172. next_is_payload = false;
  173. j = semicolon;
  174. }
  175. if ( ( question >= 0 ) && ( question < j ) ) {
  176. next_is_query = true;
  177. next_is_payload = false;
  178. j = question;
  179. }
  180. if ( ( bracket >= 0 ) && ( bracket < j ) ) {
  181. next_is_query = false;
  182. next_is_payload = false;
  183. next_is_json = true;
  184. j = bracket;
  185. }
  186. String segment = s.substring( i, j );
  187. if ( segment.length() > 0 ) {
  188. if ( is_query ) {
  189. Map<String, List<String>> params = HttpUtils.parseQueryString( segment );
  190. Query query = Query.fromQueryParams( params );
  191. ServiceParameter.addParameter( parameters, query );
  192. }
  193. else {
  194. UUID uuid = UUIDUtils.tryGetUUID( segment );
  195. if ( uuid != null ) {
  196. ServiceParameter.addParameter( parameters, uuid );
  197. }
  198. else {
  199. ServiceParameter.addParameter( parameters, segment );
  200. }
  201. }
  202. }
  203. i = j + 1;
  204. }
  205. if ( parameters.size() == 0 ) {
  206. continue;
  207. }
  208. System.out.println( action + " " + parameters + " " + payload );
  209. ServiceManager services = smf.getServiceManager( applicationId );
  210. ServiceRequest r = null;
  211. try {
  212. r = services.newRequest( action, parameters, payload );
  213. }
  214. catch ( Exception e ) {
  215. logger.error( "Error", e );
  216. }
  217. ServiceResults results = null;
  218. try {
  219. results = r.execute();
  220. }
  221. catch ( Exception e ) {
  222. logger.error( "Error", e );
  223. }
  224. if ( results != null ) {
  225. if ( results.hasData() ) {
  226. System.out.println( JsonUtils.mapToFormattedJsonString( results.getData() ) );
  227. }
  228. if ( results.getServiceMetadata() != null ) {
  229. System.out.println( JsonUtils.mapToFormattedJsonString( results.getServiceMetadata() ) );
  230. }
  231. System.out.println( JsonUtils.mapToFormattedJsonString( results.getEntities() ) );
  232. }
  233. }
  234. }
  235. }