Created
June 6, 2020 13:40
-
-
Save afs/0fc46097c4f8930ac12da9ca69fc42a8 to your computer and use it in GitHub Desktop.
Example of installing a wrapper servlet filter in Apache jena Fuseki 3.15.0.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* | |
* Copyright [2020] Andy Seaborne | |
* | |
* Licensed 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 dev; | |
import java.io.IOException; | |
import java.security.Principal; | |
import javax.servlet.*; | |
import javax.servlet.http.HttpServletRequest; | |
import org.apache.jena.atlas.web.AuthScheme; | |
import org.apache.jena.fuseki.auth.Auth; | |
import org.apache.jena.fuseki.jetty.JettyLib; | |
import org.apache.jena.fuseki.main.FusekiServer; | |
import org.apache.jena.fuseki.servlets.FusekiFilter; | |
import org.apache.jena.fuseki.system.FusekiLogging; | |
import org.apache.jena.rdfconnection.RDFConnection; | |
import org.apache.jena.rdfconnection.RDFConnectionFactory; | |
import org.apache.jena.sparql.core.DatasetGraphFactory; | |
import org.eclipse.jetty.security.SecurityHandler; | |
import org.eclipse.jetty.security.UserStore; | |
import org.eclipse.jetty.server.Handler; | |
import org.eclipse.jetty.servlet.FilterHolder; | |
import org.eclipse.jetty.servlet.ServletContextHandler; | |
import org.eclipse.jetty.servlet.ServletHandler; | |
/** | |
* Example of intercepting Fuseki dispatch. | |
* Apache Jena Fuseki 3.15.0 | |
*/ | |
public class GetPrincipal | |
{ | |
public static void main(String ... a) { | |
try { | |
FusekiLogging.setLogging(); | |
UserStore userStore = JettyLib.makeUserStore("u", "p"); | |
SecurityHandler sh = JettyLib.makeSecurityHandler("TripleStore", userStore, AuthScheme.BASIC); | |
FusekiServer server = FusekiServer.create() | |
.add("/ds", DatasetGraphFactory.createTxnMem()) | |
//.verbose(true) | |
.serverAuthPolicy(Auth.ANY_USER) | |
.securityHandler(sh) | |
.build(); | |
addExtraFilter(server); | |
server.start(); | |
// And use it. | |
String URL = "http://localhost:3330/ds"; | |
try ( RDFConnection conn = RDFConnectionFactory.connectPW(URL, "u", "p") ) { | |
boolean b = conn.queryAsk("ASK{}"); | |
System.out.println("ASK="+b); | |
} | |
//server.join(); | |
} catch (Exception ex) { | |
ex.printStackTrace(); | |
} finally { System.exit(0); } | |
} | |
// Find the FusekiFilter and replace it with an indirection filter. | |
private static void addExtraFilter(FusekiServer server) { | |
Handler handler = server.getJettyServer().getHandler(); | |
ServletContextHandler sch = (ServletContextHandler)handler; | |
ServletHandler servletHander = sch.getServletHandler() ; | |
FilterHolder[] fHolders = servletHander.getFilters(); | |
for ( int i = 0 ; i <fHolders.length ; i++ ) { | |
FilterHolder fh = fHolders [i]; | |
if ( fh.getClassName().equals(FusekiFilter.class.getName()) ) { | |
FilterHolder fh2 = replacement(fh); | |
// ** Replacement. | |
fHolders [i] = fh2; | |
} | |
} | |
} | |
/** Build a replacement Filter */ | |
private static FilterHolder replacement(FilterHolder fh) { | |
final Filter fx = (fh.getFilter()==null) ? new FusekiFilter() : fh.getFilter(); | |
Filter filter2 = new Filter() { | |
@Override | |
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { | |
// Example: print the Principal | |
Principal user = ((HttpServletRequest)request).getUserPrincipal(); | |
System.out.println("Request User: "+user); | |
fx.doFilter(request, response, chain); | |
} | |
@Override public void init(FilterConfig filterConfig) throws ServletException { fx.init(filterConfig); } | |
@Override public void destroy() { fx.destroy(); } | |
} ; | |
FilterHolder fh2 = new FilterHolder(filter2); | |
fh2.setClassName(filter2.getClass().getName()); | |
fh2.setName(fh.getName()); | |
// Not needed if before server.start(); | |
//fh2.start(); | |
//fh2.initialize(); | |
return fh2; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment