Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge remote-tracking branch 'eXist/develop' into develop
  • Loading branch information
shabanovd committed Feb 6, 2014
2 parents 5da9285 + 56fab01 commit 54b5b74
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 70 deletions.
Expand Up @@ -13,10 +13,12 @@
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.logging.Level;


import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;

import org.apache.log4j.Logger; import org.apache.log4j.Logger;

import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.util.CharArraySet; import org.apache.lucene.analysis.util.CharArraySet;
import org.apache.lucene.util.Version; import org.apache.lucene.util.Version;
Expand Down Expand Up @@ -132,88 +134,136 @@ protected static Analyzer configureAnalyzer(Element config) throws DatabaseConfi


// Get classname from attribute // Get classname from attribute
final String className = config.getAttribute(CLASS_ATTRIBUTE); final String className = config.getAttribute(CLASS_ATTRIBUTE);

Analyzer newAnalyzer=null;


if (StringUtils.isBlank(className)) { if (StringUtils.isBlank(className)) {
// No classname is defined. // No classname is defined.
LOG.error("Missing class attribute or attribute is empty."); LOG.error("Missing class attribute or attribute is empty.");

// DW: throw exception? // DW: throw exception?


} else { } else {
// Classname is defined. // Classname is defined.

// Probe class
Class<?> clazz = null;
try { try {
Class<?> clazz = Class.forName(className); clazz = Class.forName(className);
if (!Analyzer.class.isAssignableFrom(clazz)) {
LOG.warn(String.format("Lucene index: analyzer class has to be a subclass of %s", Analyzer.class.getName())); } catch (ClassNotFoundException e) {
return null; LOG.error(String.format("Lucene index: analyzer class %s not found. (%s)", className, e.getMessage()));
} return null;
}

// CHeck if class is an Analyzer
if (!Analyzer.class.isAssignableFrom(clazz)) {
LOG.error(String.format("Lucene index: analyzer class has to be a subclass of %s", Analyzer.class.getName()));
return null;
}

// Get list of parameters
List<KeyTypedValue> cParams;
try {
cParams = getAllConstructorParameters(config);

} catch (ParameterException pe) {
// Unable to parse parameters.
LOG.error(String.format("Unable to get parameters for %s: %s", className, pe.getMessage()), pe);
cParams = new ArrayList<KeyTypedValue>();
}

// Iterate over all parameters, convert data to two arrays
// that can be used in the reflection code
final Class<?> cParamClasses[] = new Class<?>[cParams.size()];
final Object cParamValues[] = new Object[cParams.size()];
for (int i = 0; i < cParams.size(); i++) {
KeyTypedValue ktv = cParams.get(i);
cParamClasses[i] = ktv.getValueClass();
cParamValues[i] = ktv.getValue();
}


// Get list of parameters // Create new analyzer
final List<KeyTypedValue> cParams = getAllConstructorParameters(config); if (cParamClasses.length > 0 && cParamClasses[0] == Version.class) {


// Iterate over all parameters, convert data to two arrays if (LOG.isDebugEnabled()) {
// that can be used in the reflection code Version version = (Version) cParamValues[0];
final Class<?> cParamClasses[] = new Class<?>[cParams.size()]; LOG.debug(String.format("An explicit Version %s of lucene has been specified.", version.toString()));
final Object cParamValues[] = new Object[cParams.size()];
for (int i = 0; i < cParams.size(); i++) {
KeyTypedValue ktv = cParams.get(i);
cParamClasses[i] = ktv.getValueClass();
cParamValues[i] = ktv.getValue();
} }


//try and get a matching constructor // A lucene Version object has been provided, so it shall be used
try { newAnalyzer = createInstance(clazz, cParamClasses, cParamValues);
final Constructor<?> cstr = clazz.getDeclaredConstructor(cParamClasses);
cstr.setAccessible(true); } else {
return (Analyzer) cstr.newInstance(cParamValues); // Either no parameters have been provided or more than one parameter


} catch (NoSuchMethodException nsme) { // Extend arrays with (default) Version object info, add to front.

Class<?>[] vcParamClasses = addVersionToClasses(cParamClasses);
// We could not find a constructor that had a complete match Object[] vcParamValues = addVersionValueToValues(cParamValues);
// This makes sense because because a lucene Version class is requires most of the time

// Finally create Analyzer

newAnalyzer = createInstance(clazz, vcParamClasses, vcParamValues);
//couldnt find a matching constructor,
//if a version parameter wasnt already specified // Fallback scenario: a special (not standard type of) Analyzer has been specified without
//see if there is one with a Version parameter // a 'Version' argument on purpose. For this (try) to create the Analyzer with
if (cParamClasses.length == 0 || (cParamClasses.length > 0 && cParamClasses[0] != Version.class)) { // the original parameters.

if (newAnalyzer == null) {
// extend arrays, add to front newAnalyzer = createInstance(clazz, cParamClasses, cParamValues);
Class<?>[] vcParamClasses = addVersionToClasses(cParamClasses);
Object[] vcParamValues = adVersionValueToValues(cParamValues);

// Finally invoke again
try {
final Constructor<?> cstr = clazz.getDeclaredConstructor(vcParamClasses);
cstr.setAccessible(true);
LOG.warn(String.format("Using analyzer %s", clazz.getName()));
return (Analyzer) cstr.newInstance(vcParamValues);

} catch (NoSuchMethodException vnsme) {
LOG.error(String.format("Could not find matching analyzer class constructor%s: %s", className, vnsme.getMessage()), vnsme);
}
}
} }


} catch (ClassNotFoundException e) {
LOG.error(String.format("Lucene index: analyzer class %s not found.", className));
} catch (IllegalAccessException e) {
LOG.error(String.format("Exception while instantiating analyzer class %s: %s", className, e.getMessage()), e);
} catch (InstantiationException e) {
LOG.error(String.format("Exception while instantiating analyzer class %s: %s", className, e.getMessage()), e);
} catch (InvocationTargetException ite) {
LOG.error(String.format("Exception while instantiating analyzer class %s: %s", className, ite.getMessage()), ite);
} catch (ParameterException pe) {
LOG.error(String.format("Exception while instantiating analyzer class %s: %s", className, pe.getMessage()), pe);
} }

} }

if (newAnalyzer == null) {
LOG.error(String.format("Unable to create analyzer '%s'", className));
}

return newAnalyzer;
}

/**
* Create instance of the lucene analyzer with provided arguments
*
* @param clazz The analyzer class
* @param vcParamClasses The parameter classes
* @param vcParamValues The parameter values
* @return The lucene analyzer
*/
private static Analyzer createInstance(Class<?> clazz, Class<?>[] vcParamClasses, Object[] vcParamValues) {

String className = clazz.getName();

try {
final Constructor<?> cstr = clazz.getDeclaredConstructor(vcParamClasses);
cstr.setAccessible(true);

if(LOG.isDebugEnabled()){
LOG.debug(String.format("Using analyzer %s", className));
}

return (Analyzer) cstr.newInstance(vcParamValues);

} catch (IllegalArgumentException e) {
LOG.error(String.format("Exception while instantiating analyzer class %s: %s", className, e.getMessage()), e);
} catch (IllegalAccessException e) {
LOG.error(String.format("Exception while instantiating analyzer class %s: %s", className, e.getMessage()), e);
} catch (InstantiationException e) {
LOG.error(String.format("Exception while instantiating analyzer class %s: %s", className, e.getMessage()), e);
} catch (InvocationTargetException ite) {
LOG.error(String.format("Exception while instantiating analyzer class %s: %s", className, ite.getMessage()), ite);
} catch (NoSuchMethodException ex) {
LOG.error(String.format("Could not find matching analyzer class constructor%s: %s", className, ex.getMessage()), ex);
} catch (SecurityException ex) {
LOG.error(String.format("Exception while instantiating analyzer class %s: %s", className, ex.getMessage()), ex);
}

return null; return null;
} }


/** /**
* Extend list of values, add version-value to front * Extend list of values, add version-value to front
*/ */
private static Object[] adVersionValueToValues(final Object[] cParamValues) { private static Object[] addVersionValueToValues(final Object[] cParamValues) {
final Object vcParamValues[] = new Object[cParamValues.length + 1]; final Object vcParamValues[] = new Object[cParamValues.length + 1];
vcParamValues[0] = LuceneIndex.LUCENE_VERSION_IN_USE; vcParamValues[0] = LuceneIndex.LUCENE_VERSION_IN_USE;
System.arraycopy(cParamValues, 0, vcParamValues, 1, cParamValues.length); System.arraycopy(cParamValues, 0, vcParamValues, 1, cParamValues.length);
Expand Down Expand Up @@ -333,23 +383,28 @@ private static KeyTypedValue getConstructorParameter(Element param) throws Param
final File f = new File(value); final File f = new File(value);
parameter = new KeyTypedValue(name, f, File.class); parameter = new KeyTypedValue(name, f, File.class);


} else if ("java.io.FileReader".equals(type)) { } else if ("java.io.FileReader".equals(type) || "file".equals(type)) {

// DW: Experimental // DW: Experimental
final File f = new File(value); File f = new File(value);
Reader fileReader = null;

try { try {
final Reader r = new FileReader(f); fileReader = new FileReader(f);
parameter = new KeyTypedValue(name, r, Reader.class); parameter = new KeyTypedValue(name, fileReader, Reader.class);

} catch (FileNotFoundException ex) { } catch (FileNotFoundException ex) {
LOG.error(String.format("File %s could not be found.", f.getAbsolutePath()), ex); LOG.error(String.format("File %s could not be found.", f.getAbsolutePath()), ex);
IOUtils.closeQuietly(fileReader);
} }


} else if ("java.util.Set".equals(type)) { } else if ("java.util.Set".equals(type)) {
// This is actually deprecated now, Lucene4 requires CharArraySet // This is actually deprecated now, Lucene4 requires CharArraySet
final Set s = getConstructorParameterSetValues(param); final Set s = getConstructorParameterSetValues(param);
parameter = new KeyTypedValue(name, s, Set.class); parameter = new KeyTypedValue(name, s, Set.class);


} else if ("org.apache.lucene.analysis.util.CharArraySet".equals(type)) { } else if ("org.apache.lucene.analysis.util.CharArraySet".equals(type) || "set".equals(type)) {
// This is mandatory since Lucene4 // This is mandatory to use iso a normal Set since Lucene 4
final CharArraySet s = getConstructorParameterCharArraySetValues(param); final CharArraySet s = getConstructorParameterCharArraySetValues(param);
parameter = new KeyTypedValue(name, s, CharArraySet.class); parameter = new KeyTypedValue(name, s, CharArraySet.class);


Expand Down
21 changes: 18 additions & 3 deletions src/org/exist/http/urlrewrite/XQueryURLRewrite.java
Expand Up @@ -33,6 +33,8 @@


import org.apache.log4j.Logger; import org.apache.log4j.Logger;


import org.exist.http.servlets.Authenticator;
import org.exist.http.servlets.BasicAuthenticator;
import org.exist.security.internal.web.HttpAccount; import org.exist.security.internal.web.HttpAccount;
import org.exist.source.Source; import org.exist.source.Source;
import org.exist.source.DBSource; import org.exist.source.DBSource;
Expand Down Expand Up @@ -139,7 +141,9 @@ public class XQueryURLRewrite extends HttpServlet {
private boolean compiledCache = true; private boolean compiledCache = true;


private RewriteConfig rewriteConfig; private RewriteConfig rewriteConfig;


private Authenticator authenticator;

@Override @Override
public void init(ServletConfig filterConfig) throws ServletException { public void init(ServletConfig filterConfig) throws ServletException {
// save FilterConfig for later use // save FilterConfig for later use
Expand Down Expand Up @@ -187,8 +191,18 @@ protected void service(HttpServletRequest servletRequest, HttpServletResponse se
Subject user = defaultUser; Subject user = defaultUser;


Subject requestUser = HttpAccount.getUserFromServletRequest(request); Subject requestUser = HttpAccount.getUserFromServletRequest(request);
if (requestUser != null) if (requestUser != null) {
{user = requestUser;} user = requestUser;
} else {
// Secondly try basic authentication
final String auth = request.getHeader("Authorization");
if (auth != null) {
requestUser = authenticator.authenticate(request, response);
if (requestUser != null) {
user = requestUser;
}
}
}


try { try {
configure(); configure();
Expand Down Expand Up @@ -637,6 +651,7 @@ private void configure() throws ServletException {
LOG.error("User can not be authenticated ("+username+"), using default user."); LOG.error("User can not be authenticated ("+username+"), using default user.");
} }
} }
authenticator = new BasicAuthenticator(pool);
} }


private void logResult(DBBroker broker, Sequence result) throws IOException, SAXException { private void logResult(DBBroker broker, Sequence result) throws IOException, SAXException {
Expand Down

0 comments on commit 54b5b74

Please sign in to comment.