Skip to content

Instantly share code, notes, and snippets.

@Wavewash
Last active June 19, 2016 15:19
Show Gist options
  • Save Wavewash/70532f2cc945a830627e to your computer and use it in GitHub Desktop.
Save Wavewash/70532f2cc945a830627e to your computer and use it in GitHub Desktop.
Enabling CORS (Cross-Origin Resource Sharing) on the example solr Jetty server
In the example solr project when you start it up by calling the following line:
java -jar start
You are starting a Jetty server on your local machine that's going to serve the solr results. This server is not able to do CORS (Cross Origin Resource Sharing). Which means that if you tried to do an AJAX call from a webpage of a different origin than the server itself you would be denied a response.
To fix this you first need to get the apropriate jars to allow for cross-domain resource sharing.
I used the following jar:
http://repo1.maven.org/maven2/org/eclipse/jetty/jetty-servlets/8.1.10.v20130312/
But you may need to get the version that's right for your version of jetty:
http://repo1.maven.org/maven2/org/eclipse/jetty/jetty-servlets/
You can find out what version of Jetty you're running with your solr example by going:
java -jar start --version
and you'll see a dump as follows:
C:\Users\username\Desktop\solr-4.8.0\example>java -jar start.jar --version
Active Options: [default, *]
Version Information on 18 entries in the classpath.
Note: order presented here is how they would appear on the classpath.
changes to the OPTIONS=[option,option,...] command line option will be reflected he
re.
0: (dir) | ${jetty.home}\resources
1: 8.1.10.v20130312 | ${jetty.home}\lib\jetty-xml-8.1.10.v20130312.jar
2: 3.0.0.v201112011016 | ${jetty.home}\lib\servlet-api-3.0.jar
3: 8.1.10.v20130312 | ${jetty.home}\lib\jetty-http-8.1.10.v20130312.jar
4: 8.1.10.v20130312 | ${jetty.home}\lib\jetty-continuation-8.1.10.v20130312.jar
5: 8.1.10.v20130312 | ${jetty.home}\lib\jetty-server-8.1.10.v20130312.jar
6: 8.1.10.v20130312 | ${jetty.home}\lib\jetty-security-8.1.10.v20130312.jar
7: 8.1.10.v20130312 | ${jetty.home}\lib\jetty-servlet-8.1.10.v20130312.jar
8: 8.1.10.v20130312 | ${jetty.home}\lib\jetty-webapp-8.1.10.v20130312.jar
9: 8.1.10.v20130312 | ${jetty.home}\lib\jetty-deploy-8.1.10.v20130312.jar
10: 8.1.10.v20130312 | ${jetty.home}\lib\jetty-servlets-8.1.10.v20130312.jar
11: 1.7.6 | ${jetty.home}\lib\ext\jcl-over-slf4j-1.7.6.jar
12: 1.7.6 | ${jetty.home}\lib\ext\jul-to-slf4j-1.7.6.jar
13: 1.2.16 | ${jetty.home}\lib\ext\log4j-1.2.16.jar
14: 1.7.6 | ${jetty.home}\lib\ext\slf4j-api-1.7.6.jar
15: 1.7.6 | ${jetty.home}\lib\ext\slf4j-log4j12-1.7.6.jar
16: 8.1.10.v20130312 | ${jetty.home}\lib\jetty-util-8.1.10.v20130312.jar
17: 8.1.10.v20130312 | ${jetty.home}\lib\jetty-io-8.1.10.v20130312.jar
Look for the line that says ${jetty.home}\lib\jetty-server (in the dump above it's line 5) and you should be able to see your version.
You'll also want to get the "jetty-util" for your jetty version too:
http://mvnrepository.com/artifact/org.mortbay.jetty/jetty-util
You should be able to find your version there. I used jetty-util-8.1.10.v20130312.jar for mine.
Now take both the servlet.jar and util.jar files that you downloaded and place it into the following folder:
solr-4.8.0\example\lib
For your version it may be different but you want it in the lib folder under the example directory.
Finally, to allow these changes to take affect, you want to open up solr-4.8.0\example\etc\webdefault.xml
and add the following lines before </web-app>:
<filter>
<filter-name>cross-origin</filter-name>
<filter-class>org.eclipse.jetty.servlets.CrossOriginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>cross-origin</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Now restart your server and you should have CORS enabled.
Notes:
If you start to get fancy and have multiple webapps running on the solr example jetty server then this will effect all those web apps. Be aware that you have set the url patter to recognize any domain which is dangerous for a production set up. This is for local testing only.
Also I did try to change the web.xml file in the webapps folder so that these changes would remain local but after hours of trying to get it to go, I gave up and found that putting it in the global webdefault worked.
@mvassilev
Copy link

Many thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment