Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you 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.
*/
package org.apache.calcite.adapter.splunk.search;
import org.apache.calcite.adapter.splunk.util.StringUtils;
import org.apache.calcite.linq4j.Enumerator;
import org.apache.calcite.linq4j.Linq4j;
import org.apache.calcite.util.Unsafe;
import org.apache.calcite.util.Util;
import au.com.bytecode.opencsv.CSVReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static org.apache.calcite.runtime.HttpUtils.appendURLEncodedArgs;
import static org.apache.calcite.runtime.HttpUtils.post;
/**
* Implementation of {@link SplunkConnection} based on Splunk's REST API.
*/
public class SplunkConnectionImpl implements SplunkConnection {
private static final Logger LOGGER =
LoggerFactory.getLogger(SplunkConnectionImpl.class);
private static final Pattern SESSION_KEY =
Pattern.compile(
"\\s*([0-9a-f]+)\\s*");
final URL url;
final String username;
final String password;
String sessionKey;
final Map requestHeaders = new HashMap();
public SplunkConnectionImpl(String url, String username, String password)
throws MalformedURLException {
this(new URL(url), username, password);
}
public SplunkConnectionImpl(URL url, String username, String password) {
this.url = url;
this.username = username;
this.password = password;
connect();
}
private static void close(Closeable c) {
try {
c.close();
} catch (Exception ignore) {
// ignore
}
}
private void connect() {
BufferedReader rd = null;
try {
String loginUrl =
String.format(Locale.ROOT,
"%s://%s:%d/services/auth/login",
url.getProtocol(),
url.getHost(),
url.getPort());
StringBuilder data = new StringBuilder();
appendURLEncodedArgs(
data, "username", username, "password", password);
rd = Util.reader(post(loginUrl, data, requestHeaders));
String line;
StringBuilder reply = new StringBuilder();
while ((line = rd.readLine()) != null) {
reply.append(line);
reply.append("\n");
}
Matcher m = SESSION_KEY.matcher(reply);
if (m.find()) {
sessionKey = m.group(1);
requestHeaders.put("Authorization", "Splunk " + sessionKey);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
close(rd);
}
}
public void getSearchResults(String search, Map otherArgs,
List fieldList, SearchResultListener srl) {
assert srl != null;
Enumerator