/** * 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(); }
/** * Encodes a hash map of {@code String:String} or {@code String:String[]} * into a single UTF8-encoded string. * * @param args The hash map. * @return The string. */ public static String encode(Map<String, Object> args) { return Args.create(args).encode(); }
/** * 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); }
postUrl = postUrl + args.encode();
/** * Issues an HTTP GET 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 get(String path, Map<String, Object> args) { if (count(args) > 0) path = path + "?" + Args.encode(args); RequestMessage request = new RequestMessage("GET"); return send(path, request); }
/** * Issues a POST request against the service using a given path and * form arguments. * * @param path The request path. * @param args The form arguments. * @return The HTTP response. */ public ResponseMessage post(String path, Map<String, Object> args) { RequestMessage request = new RequestMessage("POST"); request.getHeader().put( "Content-Type", "application/x-www-form-urlencoded"); if (count(args) > 0) request.setContent(Args.encode(args)); return send(path, request); }
/** * 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 } }