Solr Import via ModifiableSolrParams with security credentials - java

I'm trying to execute import via java using ModifiableSolrParams:
Can you please point me on the right direction or reference on how to add security credentials (username / password) to trigger the import.
Current code
SolrServer server = new HttpSolrServer(baseurl);
ModifiableSolrParams params = new ModifiableSolrParams();
params.set("command", "full-import");
params.set("clean", "true");
params.set("commit", "true");
QueryRequest request = new QueryRequest(params);
request.setPath("/dataimport");
server.request(request);

You need to add HttpRequestInterceptor to you HttpServer. This interceptor will be able to add authorization header to every your request.
For cloud Solr the util class that allow to do this is HttpClientUtil. You can start from this class, or check where in HttpSolrServer is actually HttpClient present.

I veered away from Solrj and went with this approach instead.
HttpClient Client = new DefaultHttpClient();
String userpass = usr + ":" + pwd;
HttpPost httpGet = new HttpPost(dataimport_cmd);
String encoding =
DatatypeConverter.printBase64Binary(userpass.getBytes("UTF-8"));
httpGet.setHeader("Authorization", "Basic " + encoding);
Client.execute(httpGet);

Related

Send file to bitbucket project using java and intellij

I am trying to send a file to a bitbucket master repository through my java selenium project.
I am currently able to send the file to another project on my local machine using
System.setProperty("webdriver.chrome.logfile",
"C:\\Users\\IdeaProjects\\project\\chromelogs1.txt");
However using the bitbucket location it doesnt work, any ideas?
I'm trying
System.setProperty("webdriver.chrome.logfile",
"https:\\bitbucket.org\\noting-automation\\src\\master\\Noting\\chromelogs1.txt");
But its not giving an error the driver doesnt initialize, not sure if the url is correct in that form
Update
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
//note that it the path contains browse after the repo-url
String url = "https://bitbucket.org/noting/src/master";
String user = "UN";
String pw = "PW";
File f= new
File("C:\\Users\\IdeaProjects\\filename\chromelogs1.txt");
HttpPost httpPost = new HttpPost(url);
String encoding = Base64.getEncoder().encodeToString((user
+ ":" + pw).getBytes(StandardCharsets.UTF_8));
System.out.println("URL Equals: "+ url);
httpPost.setHeader("Authorization", "Basic " + encoding);
HttpEntity reqEntity = MultipartEntityBuilder.create()
.addPart("branch", new StringBody("master",
ContentType.TEXT_PLAIN))
.addPart("content", new StringBody("file content,
can be different than stringbody, just as example",
ContentType.TEXT_PLAIN))
.addPart("message", new StringBody("commit
message", ContentType.TEXT_PLAIN))
.addPart("file", new FileBody(f))
.build();
System.out.println(reqEntity);
httpPost.setEntity(reqEntity);
CloseableHttpResponse response =
httpclient.execute(httpPost);
System.out.println(response.toString());
} finally {
httpclient.close();
}
Trying this now but its returning a 401, any ideas?
Your link does not seem right. It should start with org and work its way back. See here for an example under PropertySourceAnnotationTests.withResolvablePlaceholder()
Or go to BitBucket to find the right link. First click clone and then either use https:// or SSH key in the setProperty call.

Passing username to a HttpGet request

I need to access an API which works like this:
curl https://api.com/ratings/v1/ -u [your token here]:
The token is the username that should be passed to the HttpGet request. I am trying to do the same in the following way using java:
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("usrname", "passwrd"));
HttpHost proxy = new HttpHost("proxy.com", 8080, "http");
HttpClient httpClient = HttpClients.custom().setProxy(proxy).setDefaultCredentialsProvider(credentialsProvider).build();
HttpGet toesGet = new HttpGet("https://api.com/ratings/v1/");
toesGet.setHeader("Accept", "Application/Json");
toesGet.addHeader("Username", "[your token here]");
try {
HttpResponse toes = httpClient.execute(toesGet);
System.out.println(toes.getStatusLine());
System.out.println(toes.getEntity().toString());
} catch (Exception e) {
e.printStackTrace();
}
I am a behind proxy, so I am creating a HttpHost with the proxy details, setting the proxy for the HttpClient object and passing the credentials for proxy authentication using credentialsProvider in the following lines of code:
HttpHost proxy = new HttpHost("proxy.com", 8080, "http");
HttpClient httpClient = HttpClients.custom().setProxy(proxy).setDefaultCredentialsProvider(credentialsProvider).build();
I am passing the username to the HttpGet by adding the header like this:
toesGet.addHeader("Username", "[your token here]");
when I run the code, I get this response: HTTP/1.1 401 UNAUTHORIZED
This indicates that I am not passing the username to the HttpGet request in the right way(Or does this mean something else?). So what's the right way of passing the username to the get request?
Any help would be appreciated, Thank you!
Note: The usrname and passwrd I set in the credentialsProvider are for the proxy authentication. They have nothing to do with the HttpGet request itself. the token I need to pass is different from the usrname provided in the credentials.
I guess, your server uses Basic Authentication, then you need to add the "Authorization" header instead of "Username":
String user = "[your token here]";
String pwd = ""; // blank
toesGet.addHeader("Authorization", "Basic " + Base64.encodeToString((user + ":" + pwd).getBytes(), Base64.NO_WRAP));
or if your token contains user and pwd, then try it like that:
String token = "[your token here]";
toesGet.addHeader("Authorization", "Basic " + Base64.encodeToString(token.getBytes(), Base64.NO_WRAP));
I haven’t used Apache HttpComponents, but my understanding is that you have to set the credentials for specific hosts:
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
HttpHost proxy = new HttpHost("proxy.com", 8080, "http");
credentialsProvider.setCredentials(new AuthScope(proxy),
new UsernamePasswordCredentials("usrname", "passwrd"));
credentialsProvider.setCredentials(new AuthScope("api.com", AuthScope.ANY_PORT),
new UsernamePasswordCredentials("apiuser", "apipassword"));
Note: Do not actually type "apiuser" or "apipassword" in your code. I am showing those only as placeholders. Replace them with the correct user and password for accessing api.com. (I am pointing this out because, based on the code in your question, I’m not sure if you understood that you were not supposed to use the literal string "[your token here]".)

Example on using ldap authentication for apache http client to make rest api calls

Can any one give example on how to configure apache http client to use ldap authentication instead of Basic authentication.
For basic authentication,following is the code.
HttpClient client = HttpClientBuilder.create().build();
HttpGet get = new HttpGet(get_taskStatus_url);
String authData = userName + ":" + userName;
String encoded = new sun.misc.BASE64Encoder().encode(authData
.getBytes());
get.setHeader("Content-Type", "application/xml");
get.setHeader("Authorization", "Basic " + encoded);
get.setHeader("ACCEPT", "application/xml");
HttpResponse cgResponse = client.execute(get);
What changes do I need to perform in order to make ldap authentication instead of basic authentication.
Any help is greatly appreciated. Thanks.

Java httpClient 4.3.6 basic Authentication with complete URI and scheme

What I want:
Send a GET request with a preemtive bassic authentication.
The request looks about like this:
<startURL>/app/process?job=doSomething&param=value1,value2
whereas startURL is always a https link depends on the enviroment.
Looks something like this:
https://testABC.com
https://prodABC.com
startURL is also placed in a properties file as is for the diffrent enviroments.
What I looked into:
http://www.baeldung.com/httpclient-4-basic-authentication
http://www.java-tips.org/other-api-tips/httpclient/how-to-use-basic-authentication.html
http://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/client/ClientPreemptiveBasicAuthentication.java
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html
It all contains a
HttpHost targetHost = new HttpHost("hostname", portnumber, "scheme");
Which is what I am having trouble with. This method is also the only one that lets you specify the scheme as "https".
One issue is, hat I don't know the portnumber. I think (?) I probably could just specify -1 for the default port, to make it work, but even aside that I also don't have the hostname, only the above mentioned startURL. I don't really want to parse this extra each time, while I also don't really want to add another property, just for the hostname.
I digged around and found this snippet, which looks like just what I want:
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://foo.com/bar");
httpGet.addHeader(BasicScheme.authenticate(
new UsernamePasswordCredentials("user", "password"),
"UTF-8", false));
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity responseEntity = httpResponse.getEntity();
from HTTP requests with basic authentication
It gives the complete request URL and simply adds the basic header and does not need any port specified. Only that this is now deprecated since Version 4.2:
Deprecated. (4.2) Use ContextAwareAuthScheme.authenticate( Credentials, HttpRequest, org.apache.http.protocol.HttpContext)
I couldn't find a single example for this method to return the basic auth header. It also wants a context as a parameter, which above snipped doesn't have. I really have no real clue how this is supposed to be used.
So, what i want to know concretely:
I just want to set up a request with the complete link, that contains all that there is, like:
https://testABC.com/app/process?job=doSomething&param=value1,value2
and just give this as a parameter for a request that does preemptive basic authentication.
Is there any way to do this without digging up the deprecated methods and how does it look like?
I ran into the same problem as yours.
What worked for me is the following:
UsernamePasswordCredentials creds = new UsernamePasswordCredentials("user", "12345");
HttpGet get = new HttpGet("https://foo.bar.com/rest");
HttpHost targetHost = new HttpHost("foo.bar.com", 443, "https");
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(targetHost.getHostName(), targetHost.getPort()),
creds);
credsProvider.setCredentials(AuthScope.ANY,creds);
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local auth cache
BasicScheme basicAuth = new BasicScheme();
authCache.put(targetHost, basicAuth);
// Add AuthCache to the execution context
HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
context.setAuthCache(authCache);
HttpResponse response = client.execute(targetHost, get, context);
And I found this solution on: HttpClientBuilder basic auth
In the end I wound up writing the header manually on my own and sending things with that:
String header = "Basic ";
String headerValue = "username" + ":" + "password";
String encodedHeaderValue = Base64.encodeBase64String(headerValue.getBytes());
String headerBasic = header + encodedHeaderValue;
Header authHeader = new BasicHeader("Authorization", headerBasic);
ArrayList<Header> headers = new ArrayList<Header>();
headers.add(authHeader);
ArrayList<Header> headers = getHttpHeaders();
HttpClient client = HttpClients.custom().setDefaultHeaders(headers).build();
HttpUriRequest request = RequestBuilder.get().setUri(uri).build();
HttpResponse response = client.execute(request);
int responseCode = response.getStatusLine().getStatusCode();

Android HTTPGet to Rails REST Login

I have a login that needs an email and password.
If I hit it from Postman rest client with this:
www.site.com/login.json?session[email]=bob#gmail.com&session[password]=password
The server (Rails) will read it just fine, like this:
{"session"=>{"email"=>"bob#gmail.com", "password"=>"password"}, "action"=>"create", "controller"=>"sessions", "format"=>"json"}
But, if I send in the same thing with HTTPGet from android, like this:
HttpClient httpclient = new DefaultHttpClient();
//get the parameters
String email = params[0];
String password = params[1];
String url = "http://www.site.com/login.json?session[email]=" + email + "?session[password]=" + password;
HttpGet httpget = new HttpGet(url);
httpget.setHeader("Accept", "application/json");
httpget.setHeader("Content-type", "application/json");
response = httpclient.execute(httpget);
The server doesn't recognize the parameters, and I end up with an empty json object like this:
{"session"=>{}, "action"=>"create", "controller"=>"sessions", "format"=>"json"}
Anybody know how to form the parameters in this HTTPGet call in android to work like it will in a the rest client call? Thanks!
It seems you have a typo in URL and params get messed up. This line:
String url = "http://www.site.com/login.json?session[email]=" + email + "?session[password]=" + password;
should be
String url = "http://www.site.com/login.json?session[email]=" + email + "&session[password]=" + password;
Note that I've replaced ?session[password]= with &session[password]=.
Do you need to send USER-AGENT or other HTTP headers for it to be valid? Did you trace the HTTP connection with Fiddler2 or another HTTP trace? That ? instead of a & is probably it though.
For a type-safe REST client for Android try: http://square.github.io/retrofit/ It works very nice and you don't have to worry about manual parsing.
One thought I have is that you could try encoding the query string of your URL.
String query = URLEncoder.encode("session[email]=" + email + "?session[password]=" + password", "UTF-8");
String url = "http://www.site.com/login.json?" + query;
HttpGet httpget = new HttpGet(url);
....
* Replace the ?session in the URL with &session as its a mistake..
If the issue still persists, then try using a POST request as:
- in the first place exposing the email and password that way isn't safe practice
- and POST variables can be easily be handled in Rails controllers using the params tag eg. params[:tag] where tag is the key in the Json object.

Categories

Resources