/** * Extracts the namespace components from the specified argument list * (modifying the original list in place) and returns the namespace * components in its own namespace Args. * * @param args An argument list. Will be modified in place. * @return The namespace arguments from the original argument list. */ private static Args extractNamespaceFrom(Args args) { Args namespace = new Args(); for (String componentName : NAMESPACE_COMPONENT_NAMES) { if (args.containsKey(componentName)) { namespace.put(componentName, args.get(componentName).toString()); args.remove(componentName); } } return namespace; }
/** * Parses a search query with additional arguments and returns a semantic * map for the search in JSON format. * * @param query The search query. * @param args Additional parse arguments. * @return The parse response message. */ public ResponseMessage parse(String query, Map args) { args = Args.create(args).add("q", query); return get("search/parser", args); }
/** * Encodes an {@code Args} instance into a UTF8-encoded string. * * @return The UTF8-encoded string. */ public String encode() { StringBuilder builder = new StringBuilder(); for (Entry<String, Object> entry : entrySet()) { if (builder.length() > 0) builder.append('&'); String key = entry.getKey(); Object value = entry.getValue(); if (value instanceof String[]) { encodeValues(builder, key, (String[])value); } else { builder.append(encode(key)); builder.append('='); builder.append(encode(value.toString())); } } return builder.toString(); }
/** * Class constructor. * * @param service The connected {@code Service} instance. * @param path The target endpoint. * @param args Arguments to use when you instantiate the entity. */ Resource(Service service, String path, Args args) { // Clone the original argument list, since it will be modified args = Args.create(args); Args namespace = extractNamespaceFrom(args); if (!args.containsKey("count")) { args.put("count", "-1"); } this.service = service; this.path = service.fullpath( path, namespace.size() == 0 ? null : namespace); this.refreshArgs = args; }
/** * Creates a new {@code Args} instance and initializes it with a single * key-value pair. * * @param key The key name. * @param value The value, as a {@code String:String} or * {@code String:String[]}. * @return The {@code Args} instance. */ public static Args create(String key, Object value) { return new Args(key, value); }
/** * Logs an event to this index through HTTP POST. * * @param indexName The index to write to. * @param data A string containing event data. * @param args Optional arguments for this stream. Valid parameters are: * "host", "host_regex", "source", and "sourcetype". */ public void submit(String indexName, Args args, String data) { String sendString = ""; RequestMessage request = new RequestMessage("POST"); request.setContent(data); if (indexName !=null) { sendString = String.format("?index=%s", indexName); } if (args != null && args.size() > 0) { sendString = sendString + ((indexName == null) ? "?" : "&"); sendString = sendString + args.encode(); } ResponseMessage response = service.send(service.simpleReceiverEndPoint + sendString, request); try { response.getContent().close(); } catch (IOException e) { // noop } }
if (namespace.containsKey("app")) { try { localApp = URLEncoder.encode((String)namespace.get("app"), "UTF-8"); } catch (UnsupportedEncodingException e) { if (namespace.containsKey("owner")) { try { localOwner = URLEncoder.encode((String)namespace.get("owner"), "UTF-8"); } catch (UnsupportedEncodingException e) { if (namespace.containsKey("sharing")) { localSharing = (String)namespace.get("sharing");
/** * Uploads a file to this index as an event stream. * <p> * <b>Note:</b> This file must be directly accessible by the Splunk server. * * @param filename The path and filename. * * @param args Optional arguments for this request. Valid parameters are: * "host", "sourcetype", "rename-source". More found at: * http://docs.splunk.com/Documentation/Splunk/latest/RESTAPI/RESTinput#data.2Finputs.2Foneshot * */ public void upload(String filename, Args args) { EntityCollection<Upload> uploads = service.getUploads(); if(args.containsKey("index")){ throw new IllegalArgumentException("The 'index' parameter cannot be passed to an index's oneshot upload."); } args.add("index", getName()); uploads.create(filename, args); } }
/** * Class constructor. * * @param service The connected {@code Service} instance. */ JobCollection(Service service) { super(service, REST_PATH, Job.class); this.refreshArgs.put("count", "0"); }
/** * Creates a license pool. * * @param name The name of the new license pool. * @param quota The indexing quota of this license pool, specified as: * <ul><li><i>number</i></li> * <li><i>number</i> followed by "MB" or "GB" (for example, "10GB")</li> * <li>"MAX" (Only one license pool can have "MAX" size in a stack.)</li> * </ul> * @param stackId The stack ID corresponding to this license pool. * @param args Optional arguments ("description" and "slaves"). * @return The new license pool. */ public LicensePool create(String name, String quota, String stackId, Map args) { args = Args.create(args); args.put("quota", quota); args.put("stack_id", stackId); return create(name, args); } }
/** * Throws an IllegalArgumentException if the specified namespace * is a wildcarded namespace. */ public static void ensureNamespaceIsExact(Args namespace) { String app = (String)namespace.get("app"); String owner = (String)namespace.get("owner"); boolean wildcardedApp = (app == null || app.equals("-")); boolean wildcardedOwner = (owner == null || owner.equals("-")); boolean isExact = !wildcardedApp && !wildcardedOwner; if (!isExact) { throw new IllegalArgumentException( "An exact namespace must be provided."); } } }
/** * Creates a credential with a username, password, and realm. * * @param name The username. * @param password The password. * @param realm The credential realm. * @return The new credential. */ public Password create(String name, String password, String realm) { Args args = new Args(); args.put("password", password); args.put("realm", realm); return create(name, args); }
/** * Creates a new empty instance of {@code Args}. * * @return The {@code Args} instance. */ public static Args create() { return new Args(); }
/** * Adds an argument to an {@code Args} object. * * @param key The key name. * @param value The value, as a {@code String:String} or * {@code String:String[]}. * @return This {@code Args} set. */ public Args add(String key, Object value) { put(key, value); return this; }
/** * Creates a new Splunk user from a username, password, role, and * additional arguments. * Usernames must be unique on the system, and are used by the user to log * in to Splunk. * * @param name The username for the new user. * @param password The password for the new user. * @param role The role to assign to the new user. * @param args A map of additional arguments. For a list of available * parameters, see * <a href="http://dev.splunk.com/view/SP-CAAAEJ7#userauthparams" * target="_blank">User authentication parameters</a> on * <a href="http://dev.splunk.com/view/SP-CAAAEJ7" * target="_blank">dev.splunk.com</a>. * @return The new user. */ public User create(String name, String password, String role, Map args) { args = Args.create(args); args.put("password", password); args.put("roles", role); return create(name.toLowerCase(), args); }
/** * Issues a DELETE request against the service using a given path * and query arguments. * * @param path The request path. * @param args The query arguments. * @return The HTTP response. */ public ResponseMessage delete(String path, Map<String, Object> args) { if (count(args) > 0) path = path + "?" + Args.encode(args); RequestMessage request = new RequestMessage("DELETE"); return send(path, request); }