SolrJ CloudSolrClient connection error - apache

I am struggling with indexing using Solrj.
I want to use SolrCloud and I set my connection like this :
SolrClient client = new CloudSolrClient.Builder().withSolrUrl("http://localhost:8983/solr/collectioname").build();
And I have this error. I checked evruwhere before posting here but I can't resolve it
Exception in thread "main" java.lang.RuntimeException: Couldn't initialize a HttpClusterStateProvider (is/are the Solr server(s), [http://localhost:8983/solr/collectioname], down?)
at org.apache.solr.client.solrj.impl.CloudSolrClient$Builder.build(CloudSolrClient.java:1496)
at indexsolr.<init>(indexsolr.java:29)
at LoadData.toIndex(LoadData.java:100)
at LoadData.loadDocuments(LoadData.java:72)
at IndexLaunch.main(IndexLaunch.java:12)
Caused by: org.apache.solr.client.solrj.impl.HttpSolrClient$RemoteSolrException: Error from server at http://localhost:8983/solr/collectioname: Expected mime type application/octet-stream but got text/html. <html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Error 404 Not Found</title>
</head>
<body><h2>HTTP ERROR 404</h2>
<p>Problem accessing /solr/collectioname/admin/collections. Reason:
<pre> Not Found</pre></p>
</body>
</html>
at org.apache.solr.client.solrj.impl.HttpSolrClient.executeMethod(HttpSolrClient.java:607)
at org.apache.solr.client.solrj.impl.HttpSolrClient.request(HttpSolrClient.java:255)
at org.apache.solr.client.solrj.impl.HttpSolrClient.request(HttpSolrClient.java:244)
at org.apache.solr.client.solrj.SolrClient.request(SolrClient.java:1219)
at org.apache.solr.client.solrj.impl.HttpClusterStateProvider.fetchLiveNodes(HttpClusterStateProvider.java:189)
at org.apache.solr.client.solrj.impl.HttpClusterStateProvider.<init>(HttpClusterStateProvider.java:64)
at org.apache.solr.client.solrj.impl.CloudSolrClient$Builder.build(CloudSolrClient.java:1494)
... 4 more

As mentioned in the previous answer, SolrJ uses the URL without the collection name in it (which I should have pointed out more directly):
final String solrUrl = "http://localhost:8983/solr";
return new HttpSolrClient.Builder(solrUrl)
.withConnectionTimeout(10000)
.withSocketTimeout(60000)
.build();

For SolrCloud you need to use CloudSolrClient. Depending on Solr version you are using you can create object of CloudSolrClient or you can use CloudSolrClient.Builder

This is how I did it.
Solr cloud just needs to take a list of urls as a parameter and not just a String url.
private static final String BASE_SOLR_URL = "http://localhost:8983/solr";
// or initialize a list of solr instances urls
#Bean(name = "solrCloudClient")
public static CloudSolrClient getCloudConnection() {
System.out.println("----Initializing cloud Solr Connection---");
List<String> solrCloudUrls = Arrays.asList(BASE_SOLR_URL); // or list of urls
CloudSolrClient client = new CloudSolrClient.Builder(solrCloudUrls).build();
client.setParser(new XMLResponseParser());
return client;
}

Related

Unable to verify the first certificate with postman on new .Net 5 core project

i need an help.
So i've created a project .NET core 5 with HTTPS. The project is new new.
So added the TestController where on get request i will have:
// GET: api/<TestController>
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
So, if i call the api https://localhost:44310/api/test on the browser:
["value1","value2"]
//that works correctly
But if i call the url on postman i receive:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<HEAD>
<TITLE>Bad Request</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii">
</HEAD>
<BODY>
<h2>Bad Request - Invalid Hostname</h2>
<hr>
<p>HTTP Error 400. The request hostname is invalid.</p>
</BODY>
</HTML>
My postman option:
and
and
and
where i'm wrong? I need to settings visual studio with other options? Maybe i need to use localhost certificate? Why i get this error only in postman and not by browser??

Safari losing hash params on http redirection

I am facing an issue wherein the url fragments are not getting
preserved on redirect in Safari as they should be according to the
http specifications.
Setting -
`/url1` redirects to `/url2#hash`
`/url2` redirects to `/url3`
Expected behaviour -
Hitting `/url1` should redirect to `/url3#hash`
Observed behaviour -
Chrome/FF - Hitting `/url1` redirects to `/url3#hash`
Safari(11+) - Hitting `/url1` redirects to `/url3`
I did read the issue reported for earlier versions of Safari. I also tried the solutions posted in other SO threads in vain.
Any help is appreciated.
We ran into the same issue today and made some additional observations. We were also able to reproduce the problem with an example Spring Boot app:
#Controller
class RedirectController {
#GetMapping("/url1")
String url1() {
return "redirect:/url2#hash";
}
#GetMapping("/url2")
String url2() {
return "redirect:/url3";
}
#GetMapping("/url3")
#ResponseBody
String url3() {
return "Hello World";
}
}
When running with Safari 14.1.2 on OSX 10.15.7 (Catalina) hitting /url1/ leads to /url3 without carrying over the fragment (#hash) from /url2.
But doing so with Safari 14.1.2 (same version) on OSX 11.5.2 (Big Sur) works. Hitting /url1 leads to /url3#hash.
In any case hitting /url1#this-is-fragment leads to /url3#this-is-fragment.
Our hypothesis is, that Safari (on Catalina) carries over fragments only if they are created in the browser and not on the server. Safari on Big Sur seems to behave like other browsers (e.g. Firefox and Chrome).
Based on that hypothesis one "solution" is a client side redirect, for example via meta refresh:
#Controller
class RedirectController {
#GetMapping("/url1")
#ResponseBody
String url1ClientSideRedirect() {
return """
<html>
<head>
<meta http-equiv="refresh" content="0;URL='/url2#hash'">
</head>
<body></body>
</html>
""";
}
#GetMapping("/url2")
String url2() {
return "redirect:/url3";
}
#GetMapping("/url3")
#ResponseBody
String url3() {
return "Hello World";
}
}
I suppose any client side JavaScript working with window.location would work as well.

ProxyServlet stop working after migration from jetty 8 to jetty 9

I have an eclipse plugin which uses jetty server with ProxyServlet. Basically, the implementation is the following:
ServletHolder proxyServletHolder = new ServletHolder(new SubClassOfProxyServlet());
proxyServletHolder.setAsyncSupported(true);
ServletHandler proxyServletHandler = new ServletHandler();
proxyServletHandler.addServletWithMapping(proxyServletHolder, "/mapping/url");
After that I add proxy handler to the handler list and set this list to the server:
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[] {
. // Other Handlers
.
proxyServletHandler,
.
.
.
new DefaultHandler()
});
server.setHandler(handlers);
Everything worked like a charm against jetty 8 but after migration to jetty 9 I get the following error:
Caused by: java.lang.IllegalStateException: No server executor for proxy
at org.eclipse.jetty.proxy.ProxyServlet.createHttpClient(ProxyServlet.java:279)
at org.eclipse.jetty.proxy.ProxyServlet.init(ProxyServlet.java:123)
... 24 more
Has the mechanism of working with ProxyServer changed? Am I missing something?
You need to update your SubClassOfProxyServlet class to include the various configurations that are now being passed from the Server to the Proxy which are then in turn used by the internal HttpClient
The particular error means you are not passing along the Executor properly.
You have 2 choices for the Executor specific piece (there might be more things for you to configure after this is addressed)
Set the init-parameter maxThreads to a valid integer value.
or Create an Executor, and set it in the servlet context attributes at ServletContext.setAttribute("org.eclipse.jetty.server.Executor", myExecutor) on application deployment / startup. - You could probably do this as well in your SubClassOfProxyServlet.init(ServletConfig config) method.
I was able to get it working via the maxThreads method mentioned above, setting it on creation. Applying this to the original example would result in this:
ServletHolder proxyServletHolder = new ServletHolder(new SubClassOfProxyServlet());
proxyServletHolder.setAsyncSupported(true);
proxyServletHolder.setInitParameter("maxThreads", "2");
ServletHandler proxyServletHandler = new ServletHandler();
proxyServletHandler.addServletWithMapping(proxyServletHolder, "/mapping/url");
Here is an example of how you can add a servlet to a list of handlers:
private void addWebApp(String contextPath, String resourceBase, Server server) {
WebAppContext webAppContext = new WebAppContext();
// webAppContext.setDescriptor(webapp + "/WEB-INF/web.xml");
webAppContext.setResourceBase(resourceBase);
webAppContext.setContextPath(contextPath);
webAppContext.setParentLoaderPriority(true);
webAppContext.setWelcomeFiles(new String[] {"index.html"});
webAppContext.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false");
webAppContext.setInitParameter("org.eclipse.jetty.servlet.Default.useFileMappedBuffer", "false");
final ServletHolder servletHolder =new ServletHolder();
servletHolder.setAsyncSupported(ContentBasedProxyServlet.class);
servletHolder.setAsyncSupported(true);
webAppContext.addServlet(servletHolder, "/*");
HandlerList handlers = (HandlerList) server.getHandler();
handlers.addHandler(webAppContext);
}
In addition you can put maxThreads to web.xml as well:
<servlet>
<servlet-name>proxy</servlet-name>
<servlet-class>example.MyProxyServlet</servlet-class>
<init-param>
<param-name>maxThreads</param-name>
<param-value>5</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>

signalR : /signalr/hubs is not generated

I can get this tutorial to work in a new project, but not in my existing project.
My project is an ASP.Net MVC 4 web application with the following attribute in the web.config file:
<appSettings>
<add key="webpages:Enabled" value="true"/>
</appSettings>
This is because my application is a Single-Page-Application, which uses AngularJS on the client side. The only page in my application is index.cshtml, to which I've added the relevant code for signalR:
<!-- signalR chat -->
<script src="~/Scripts/jquery.signalR-1.0.0.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="/signalr/hubs"></script>
<!--Add script to update the page and send messages.-->
<script type="text/javascript">
$(function () {
// Declare a proxy to reference the hub.
var chat = $.connection.chatHub;
// Create a function that the hub can call to broadcast messages.
chat.client.broadcastMessage = function (name, message) {
// Html encode display name and message.
var encodedName = $('<div />').text(name).html();
var encodedMsg = $('<div />').text(message).html();
// Add the message to the page.
$('#discussion').append('<li><strong>' + encodedName
+ '</strong>: ' + encodedMsg + '</li>');
};
// Get the user name and store it to prepend to messages.
$('#displayname').val(prompt('Enter your name:', ''));
// Set initial focus to message input box.
$('#message').focus();
// Start the connection.
$.connection.hub.start().done(function () {
$('#sendmessage').click(function () {
// Call the Send method on the hub.
chat.server.send($('#displayname').val(), $('#message').val());
// Clear text box and reset focus for next comment.
$('#message').val('').focus();
});
});
});
</script>
Then I've got the ChatHub.cs file:
public class ChatHub : Hub
{
public void Send(string name, string message)
{
// Call the broadcastMessage method to update clients.
Clients.All.broadcastMessage(name, message);
}
}
And finally in the global.asax:
protected void Application_Start()
{
RouteTable.Routes.MapHubs();
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
When I run the application, the /signalr/hubs file is not generated. I get a 404 when requesting the file, and it crashes on the line:
chat.client.broadcastMessage = function (name, message) { ....
because chat is null as the previous line did not find chatHub:
var chat = $.connection.chatHub;
Does anyone know what's wrong with my code ?
UPDATE
I have solved my problem by changing the line::
<script src="/signalr/hubs"></script>
to
<script src="~/signalr/hubs"></script>
I have solved my problem by changing the line::
<script src="/signalr/hubs"></script>
to
<script src="~/signalr/hubs"></script>
Also, the reason why /signalr/hubs are not generated is forget to Map SignalR in OWIN Startup Configuration.
public class Startup
{
public void Configuration(IAppBuilder appBuilder){
...
appBuilder.MapSignalR();
...
}
...
In my case, it was because my ChatHub class was not marked public.
I had a similar problem where the hubs file wasn't being generated. It looks like the OP was following the steps here. The way I fixed the problem had to do with the jquery includes. The tutorial I linked below was written with jquery 1.6.4 and jquery-signalr version 2.1.0. When Visual Studio generated the Scripts folder for me, it used jquery version 1.10.2 and jquery-signalr version 2.0.2.
The way I fixed this was simply to edit the index.html file. Note that you can use Chrome's javascript console window Ctrl+Shift+J to see errors.
For me the solution was to reinstall all the packages and restore all the dependecies.
Open nuget powershell console and use this command.
Update-Package -Reinstall
I'll like to add that the signalR Readme file have some note about this issue.
And also if your signalR page is in a PartialView some script should be place in the master page.
Please see http://go.microsoft.com/fwlink/?LinkId=272764 for more information on using SignalR.
Upgrading from 1.x to 2.0
-------------------------
Please see http://go.microsoft.com/fwlink/?LinkId=320578 for more information on how to
upgrade your SignalR 1.x application to 2.0.
Mapping the Hubs connection
----------------------------
To enable SignalR in your application, create a class called Startup with the following:
using Microsoft.Owin;
using Owin;
using MyWebApplication;
namespace MyWebApplication
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
}
}
}
Getting Started
---------------
See http://www.asp.net/signalr/overview/getting-started for more information on how to get started.
Why does ~/signalr/hubs return 404 or Why do I get a JavaScript error: 'myhub is undefined'?
--------------------------------------------------------------------------------------------
This issue is generally due to a missing or invalid script reference to the auto-generated Hub JavaScript proxy at '~/signalr/hubs'.
Please make sure that the Hub route is registered before any other routes in your application.
In ASP.NET MVC 4 you can do the following:
<script src="~/signalr/hubs"></script>
If you're writing an ASP.NET MVC 3 application, make sure that you are using Url.Content for your script references:
<script src="#Url.Content("~/signalr/hubs")"></script>
If you're writing a regular ASP.NET application use ResolveClientUrl for your script references or register them via the ScriptManager
using a app root relative path (starting with a '~/'):
<script src='<%: ResolveClientUrl("~/signalr/hubs") %>'></script>
If the above still doesn't work, you may have an issue with routing and extensionless URLs. To fix this, ensure you have the latest
patches installed for IIS and ASP.NET.
In my case i was missing :
app.MapSignalR();
in public void Configuration(IAppBuilder app) function located in startup.cs
Some advice for those that start scaling out from the get go. In my case I was working on getting a remote client to work and never realized that
A. the tutorial example lists the web app(server) startup in a using statement, after which the web app disposes properly and no long exists.
I got rid of the using statement and keep a reference to the web app
for later disposal
B. the client has a different url than the server. the example relies on them having the same url. the "/signalr/hubs" is an endpoint run by the signalr server, called by the signalr client to get a script of all the hubs the server implements.
You need to include "http://myServerURL/signalr/hubs", rather than making it
relative to the client.
No lies. This tripped me up for a solid 2 weeks, because by some magic the solution worked anyways on my coworker's setup. This caused me to keep looking for IIS settings and firewall settings and CORS settings that must have been blocking the connection on my computer. I scavenged every last stack overflow question I could and the ultimate answer was that I should have just implemented a heartbeat monitor on the web app from the start.
Good luck and hopefully this saves other people some time.
See if Microsoft.Owin.Host.Systemweb nuget package is not installed, and therefore not building the dynamic js lib.
OwinStartup not firing

Accessing XML via Web Handler results in 302 redirect

I'm trying to access an XML file via web handler. I am running into an infinite redirect (302) issue though. This is because cookies are not enabled. I'm not entirely certain what is causing it, but need some help figuring out what to do.
var url = context.Request.QueryString["xmlurl"];
HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(url);
hwr.Method = "GET";
hwr.MaximumAutomaticRedirections = 5;
//infinite redirect happens here
HttpWebResponse response = (HttpWebResponse)hwr.GetResponse();
I suspect that the following cookies (found via Fiddler) are required:
ASP.NET_SessionId= ...
.ASPXAUTH= ...
These are created when the page is accessed via browser, but not with my web handler. Does anyone know how I can work around this?
The exact response I get back via web handler is:
<html>
<head>
<title>Object moved</title>
</head>
<body>
<h2>Object moved to here.</h2>
</body>
</html>
The answer ended up being relatively simple. By adding
hwr.CookieContainer = new CookieContainer();
It allowed the server to attach cookies to the response, the redirect was then allowed and the final xml document was delivered.