Java client with Apache HttpClient to connect to Druid - java

I am working on ingesting and query data on Druid Server. But, when I query I just using the command line as below:
curl -X 'POST' -H 'Content-Type:application/json' -d #quickstart/ingest_statistic_hourly_generate.json localhost:8090/druid/indexer/v1/task
Can anyone tell me the way of utilizing Java client with Apache HttpClient to send that query to Druid server so as to get response. Thanks so much.

I have not tested this , but this should give you a fair idea of doing this
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
public class HTTPTestClient {
public static void main(String[] args) throws Exception {
String url = "http://localhost:8090/druid/indexer/v1/task";
String content = new String(Files.readAllBytes(Paths.get("quickstart/ingest_statistic_hourly_generate.json")));
HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
post.addHeader("Accept", "application/json");
post.addHeader("charset", "UTF-8");
post.addHeader("Content-Type", "application/json");
post.setEntity(new StringEntity(content));
HttpResponse response = client.execute(post);
System.out.println(response.getStatusLine());
System.out.println("Response Code : " + response);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println(result);
}
}

Related

Apache HttpClientBuilder produces ClassNotFoundException: org.apache.http.config.Lookup

I'm using HttpClient and and I use httpCore.jar and still I'm facing an exception
java.lang.ClassNotFoundException: org.apache.http.config.Lookup Error
around
HttpClient client = HttpClientBuilder.create().build();
My full code is following
package com.rest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
public class Test33 {
/**
* #param args
* #throws IOException
* #throws ClientProtocolException
*/
public static void main(String[] args) throws ClientProtocolException, IOException {
String url = "http://www.google.com/search?q=httpClient";
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);
System.out.println("Response Code : "
+ response.getStatusLine().getStatusCode());
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
}
}
The class is available in httpcore 4.4.4.jar, Please check the version of jar and I tried your code and it doesn't throw any class not found exception. If jar is there then please make sure that jar is added to application classpath.

Downloading a text from a URL that is password protected by a different URL (Java)

I have a URL with data I would like to access. The data is in text form and it is password protected...but here's the thing...it is password protected by a different website. I have spent weeks on this issue. When I use Apache HttpClient, I can log into the login URL just fine, but I cannot figure out how to gain access to the data URL. Every time I try to gain access to the data URL, I get an HTTP 500 error. Any suggestions for this issue? I don't think this is a very common problem considering I have not come across it in my many Stackoverflow and Google searches. THANK YOU SO MUCH IF YOU CAN HELP :)
Below is an example of one of the programs I have tried using to no avail...(Some of the information is private, so I changed the username, password, and url)
package Apache1;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.net.*;
import java.io.*;
/**
* A simple example that uses HttpClient to execute an HTTP request against
* a target site that requires user authentication.
*/
public class Apache2 {
public static void main(String[] args) throws Exception {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope("localhost", 443),
new UsernamePasswordCredentials("myUsername", "myPW"));
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider)
.build();
try {
HttpGet httpget = new HttpGet("LOGIN URL");
System.out.println("Executing request " + httpget.getRequestLine());
CloseableHttpResponse response = httpclient.execute(httpget);
try {
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
EntityUtils.consume(response.getEntity());
URL oracle = new URL("DATA URL");
URLConnection yc = oracle.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
} finally {
response.close();
}
} finally {
httpclient.close();
}
}
}
Here is my most recent code to handle the cookies. I keep getting a HTTP 500 error.
package Apache1;
//import org.apache.http.*;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.net.*;
import java.io.*;
/**
* A simple example that uses HttpClient to execute an HTTP request against
* a target site that requires user authentication.
*/
public class Apache2 {
public static void main(String[] args) throws Exception {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope("localhost", 443),
new UsernamePasswordCredentials("myUSERNAME", "myPASSWORD"));
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider)
.build();
try {
HttpGet httpget = new HttpGet("LOGIN_WEBSITE");
System.out.println("Executing request " + httpget.getRequestLine());
CloseableHttpResponse response = httpclient.execute(httpget);
try {
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
EntityUtils.consume(response.getEntity());
URL oracle = new URL("DATA_WEBSITE");
URLConnection yc = oracle.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
} finally {
response.close();
}
} finally {
httpclient.close();
}
}
}

Trying to HTTP-POST to GCM from Java

I'm trying to send a HTTP-POST to the Google Cloud Messaging service. I have setup the correct keys, and everything is working when I use a php script for sending push notifications to my cellphone.
But my Java httpPost only returns a 401 response. I have followed the instructions given at Android Developers but I'm still getting the annoying 401. Am I assigning the header fields wrong?
My Code :
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
public class Server {
public static void main(String[] args) throws IOException {
String url = "https://android.googleapis.com/gcm/send";
HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
HttpPost httppost = new HttpPost("https://android.googleapis.com/gcm/send");
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("registration_id=", "MY_DEVICE_GSM_REG_ID"));
urlParameters.add(new BasicNameValuePair("data=", "Type=value,Lat=58.365547,Long=8.613235,Comment=value"));
httppost.setHeader("Authorization",
"key=MY_API_AUTH_FROM_GOOGLE_API_CONSOLE_BROWSER_TOKEN");
httppost.setHeader("Content-Type",
"application/x-www-form-urlencoded;charset=UTF-8");
post.setEntity(new UrlEncodedFormEntity(urlParameters, "UTF-8"));
HttpResponse response = client.execute(post);
System.out.println("Response Code : "
+ response.getStatusLine().getStatusCode());
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
}
}
You are setting the Authorization header correctly. There is probably a problem with your API Key.
You do have a problem in your registration ID and payload (which is not related to the 401 error).
This is wrong :
urlParameters.add(new BasicNameValuePair("registration_id=", "MY_DEVICE_GSM_REG_ID"));
urlParameters.add(new BasicNameValuePair("data=", "Type=value,Lat=58.365547,Long=8.613235,Comment=value"));
You should remove the = from the key and each payload parameter should start with data.. Therefore you should have:
urlParameters.add(new BasicNameValuePair("registration_id", "MY_DEVICE_GSM_REG_ID"));
urlParameters.add(new BasicNameValuePair("data.Type", "value"));
urlParameters.add(new BasicNameValuePair("data.Lat", "58.365547"));
urlParameters.add(new BasicNameValuePair("data.Long", "8.613235"));
urlParameters.add(new BasicNameValuePair("data.Comment", "value"));

REST call using ApacheHttpClient with data and headers

I need to integrate Kii MbaaS services in one of my web application apart from the Mobile apps. I was using the Android SDK and was able to connect it. However for website using Java solution they don't have any SDK and asked me to do th operation using REST. Now I was planning to use ApacheHttpClient from a Servlet to connect to the REST services. The REST format from their docs is given below. In ApacheHttpClient I know I can pass the headers(-H) as HttpGet.addHeader("content-type", "application/json"). However I am not sure how to pass the data (-d). Can anyone help me here by pointing to any tutorial link or any sample code on how to pass data to a REST service along with headers?
The REST syntax is given below-
curl -v -X POST \
-H "content-type:application/json" \
-H "x-kii-appid:{APP_ID}" \
-H "x-kii-appkey:{APP_KEY}" \
"https://api.kii.com/api/oauth2/token" \
-d '{"username":"user_123456", "password":"123ABC"}'
Thanks in advance.
------------------------- Edit--------------------------------------------------
here is a sample java code I wrote to connect to using Apache HttpClient 4.3 library however I keep getting error as 400... can anyone pls advice?
error -
Exception in thread "main" java.lang.RuntimeException: Failed : HTTP
error code : 400 at
com.app.test.RestClientTest.main(RestClientTest.java:49)
package com.app.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.Consts;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
public class RestClientTest {
/**
* #param args
*/
public static void main(String[] args) {
CloseableHttpClient httpClient = null;
HttpPost httpost = null;
CloseableHttpResponse response = null;
try {
httpClient = HttpClients.createDefault();
httpost = new HttpPost("https://api.kii.com/api/oauth2/token");
httpost.addHeader("content-type", "application/json");
httpost.addHeader("x-kii-appid", "xxxxx");
httpost.addHeader("x-kii-appkey", "xxxxxxxx");
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("username", "xxxxx"));
nvps.add(new BasicNameValuePair("password", "xxxxx"));
// StringEntity input = new
// StringEntity("{\"qty\":100,\"name\":\"iPad 4\"}");
// input.setContentType("application/json");
httpost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
response = httpClient.execute(httpost);
if (response.getStatusLine().getStatusCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatusLine().getStatusCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(response.getEntity().getContent())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try{
response.close();
httpClient.close();
}catch(Exception ex) {
ex.printStackTrace();
}
}
}
}
Ok I got it solved. I need to wrap up the data in json format stringentity and post it and it will work.
Here I am posting the same for others who are planning to use the Kii MbaaS in their web apps apart from the Mobile app.
package com.app.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
public class RestClientTest {
/**
* #param args
*/
public static void main(String[] args) {
CloseableHttpClient httpClient = null;
HttpPost httpPost = null;
CloseableHttpResponse response = null;
try {
httpClient = HttpClients.createDefault();
httpPost = new HttpPost("https://api.kii.com/api/oauth2/token");
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("content-type", "application/json"));
nvps.add(new BasicNameValuePair("x-kii-appid", "xxxxx"));
nvps.add(new BasicNameValuePair("x-kii-appkey", "xxxxxxxxxxxxxx"));
StringEntity input = new StringEntity("{\"username\": \"dummyuser\",\"password\": \"dummypassword\"}");
input.setContentType("application/json");
httpPost.setEntity(input);
for (NameValuePair h : nvps)
{
httpPost.addHeader(h.getName(), h.getValue());
}
response = httpClient.execute(httpPost);
if (response.getStatusLine().getStatusCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatusLine().getStatusCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(response.getEntity().getContent())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try{
response.close();
httpClient.close();
}catch(Exception ex) {
ex.printStackTrace();
}
}
}
}

How to add,set and get Header in request of HttpClient?

In my application I need to set the header in the request and I need to print the header value in the console...
So please give an example to do this the HttpClient or edit this in my code...
My Code is ,
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
public class SimpleHttpPut {
public static void main(String[] args) {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://http://localhost:8089/CustomerChatSwing/JoinAction");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("userId",
"123456789"));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(post);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Thanks in advance...
You can use HttpPost, there are methods to add Header to the Request.
DefaultHttpClient httpclient = new DefaultHttpClient();
String url = "http://localhost";
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("header-name" , "header-value");
HttpResponse response = httpclient.execute(httpPost);
On apache page: http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html
You have something like this:
URIBuilder builder = new URIBuilder();
builder.setScheme("http").setHost("www.google.com").setPath("/search")
.setParameter("q", "httpclient")
.setParameter("btnG", "Google Search")
.setParameter("aq", "f")
.setParameter("oq", "");
URI uri = builder.build();
HttpGet httpget = new HttpGet(uri);
System.out.println(httpget.getURI());
You can test-drive this code exactly as is using the public GitHub API (don't go over the request limit):
public class App {
public static void main(String[] args) throws IOException {
CloseableHttpClient client = HttpClients.custom().build();
// (1) Use the new Builder API (from v4.3)
HttpUriRequest request = RequestBuilder.get()
.setUri("https://api.github.com")
// (2) Use the included enum
.setHeader(HttpHeaders.CONTENT_TYPE, "application/json")
// (3) Or your own
.setHeader("Your own very special header", "value")
.build();
CloseableHttpResponse response = client.execute(request);
// (4) How to read all headers with Java8
List<Header> httpHeaders = Arrays.asList(response.getAllHeaders());
httpHeaders.stream().forEach(System.out::println);
// close client and response
}
}

Categories

Resources