Changeset 6787


Ignore:
Timestamp:
11/10/15 10:52:04 (9 years ago)
Author:
Oliver Schonefeld
Message:
  • re-work version and operation detection mechanism (still needs more re-factoring)
Location:
SRUServer/trunk/src/main/java/eu/clarin/sru/server
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • SRUServer/trunk/src/main/java/eu/clarin/sru/server/SRURequestImpl.java

    r6779 r6787  
    206206     */
    207207    boolean checkParameters() {
    208         /*
    209          * FIXME: those parameters must be inject from "outside" (probably
    210          *        by the appropriate Servlet instance)
    211          */
    212208        final SRUVersion minVersion = config.getMinVersion();
    213209        final SRUVersion maxVersion = config.getMaxVersion();
     210
     211        /*
     212         * generally assume, we will also allow processing of SRU 1.1 or 1.2
     213         */
     214        boolean processSruOld = true;
    214215
    215216        /*
     
    220221        if (maxVersion.compareTo(SRUVersion.VERSION_2_0) >= 0) {
    221222            if (getParameter(PARAM_VERSION, false, false) == null) {
     223                /*
     224                 * Ok, we're committed to SRU 2.0 now, so don't allow processing
     225                 * of SRU 1.1 and 1.2 ...
     226                 */
     227                processSruOld = false;
     228
    222229                logger.debug("handling request as SRU 2.0, because no '{}' " +
    223230                        "parameter was found in the request", PARAM_VERSION);
     
    246253                if (op != null) {
    247254                    /*
    248                      * XXX: if operation is searchRetrive an operation parameter,
    249                      * is also searchRetrieve, the server may ignore it
     255                     * XXX: if operation is searchRetrive and the 'operation'
     256                     * parameter is also searchRetrieve, should the server just
     257                     * ignore it?
    250258                     */
    251259                    if (!(operation == SRUOperation.SEARCH_RETRIEVE) &&
    252260                            op.equals(OP_SEARCH_RETRIEVE)) {
    253261                        addDiagnostic(SRUConstants.SRU_UNSUPPORTED_PARAMETER,
    254                                 PARAM_OPERATION, "Parameter '" +
     262                                PARAM_OPERATION, "Parameter \"" +
    255263                                        PARAM_OPERATION +
    256                                         "' is not valid for SRU version 2.0");
     264                                        "\" is not valid for SRU version 2.0");
    257265                    }
    258266                }
     
    260268                logger.debug("handling request as legacy SRU, because found " +
    261269                        "parameter '{}' in request", PARAM_VERSION);
    262 
    263                 // parse mandatory operation parameter
    264                 final String op = getParameter(PARAM_OPERATION, false, false);
    265                 if (op != null) {
    266                     if (!op.isEmpty()) {
    267                         if (op.equals(OP_EXPLAIN)) {
    268                             this.operation = SRUOperation.EXPLAIN;
    269                         } else if (op.equals(OP_SCAN)) {
    270                             this.operation = SRUOperation.SCAN;
    271                         } else if (op.equals(OP_SEARCH_RETRIEVE)) {
    272                             this.operation = SRUOperation.SEARCH_RETRIEVE;
    273                         } else {
    274                             addDiagnostic(SRUConstants.SRU_UNSUPPORTED_OPERATION,
    275                                     null, "Operation \"" + op + "\" is not supported.");
    276                         }
     270            }
     271        }
     272
     273        if (processSruOld) {
     274            // parse mandatory operation parameter
     275            final String op = getParameter(PARAM_OPERATION, false, false);
     276            if (op != null) {
     277                if (!op.isEmpty()) {
     278                    if (op.equals(OP_EXPLAIN)) {
     279                        operation = SRUOperation.EXPLAIN;
     280                    } else if (op.equals(OP_SCAN)) {
     281                        operation = SRUOperation.SCAN;
     282                    } else if (op.equals(OP_SEARCH_RETRIEVE)) {
     283                        operation = SRUOperation.SEARCH_RETRIEVE;
    277284                    } else {
    278285                        addDiagnostic(SRUConstants.SRU_UNSUPPORTED_OPERATION,
    279                                 null, "An empty parameter \"" +
    280                                         PARAM_OPERATION +
    281                                       "\" is not supported.");
     286                                null, "Operation \"" + op + "\" is not supported.");
    282287                    }
    283 
    284                     // parse and check version
    285                     version = parseAndCheckVersionParameter(operation);
    286288                } else {
    287                     /*
    288                      * absent parameter should be interpreted as "explain"
    289                      */
    290                     operation = SRUOperation.EXPLAIN;
    291 
    292                     // parse and check version
    293                     version = parseAndCheckVersionParameter(operation);
     289                    addDiagnostic(SRUConstants.SRU_UNSUPPORTED_OPERATION,
     290                            null, "An empty parameter \"" +
     291                                    PARAM_OPERATION +
     292                                  "\" is not supported.");
    294293                }
    295             }
    296         }
    297 
    298         if ((version == null) || (operation == null)) {
    299             logger.error("internal error!!!!1elf");
    300         }
    301 
    302         if (minVersion.compareTo(version) < 0) {
    303             addDiagnostic(SRUConstants.SRU_UNSUPPORTED_VERSION,
    304                     version.getVersionString(), "Version '{}' is not supported by this endpoint");
    305         }
    306         return checkParameters2();
    307     }
    308 
    309 
    310     boolean checkParameters2() {
    311 
     294
     295                // parse and check version
     296                version = parseAndCheckVersionParameter(operation);
     297            } else {
     298                /*
     299                 * absent parameter should be interpreted as "explain"
     300                 */
     301                operation = SRUOperation.EXPLAIN;
     302
     303                // parse and check version
     304                version = parseAndCheckVersionParameter(operation);
     305            }
     306        }
     307
     308
     309        /*
     310         * sanity check
     311         */
     312        if ((version != null) && (operation != null)) {
     313            logger.debug("min = {}, min? = {}, max = {}, max? = {}, version = {}",
     314                    minVersion,
     315                    version.compareTo(minVersion),
     316                    maxVersion,
     317                    version.compareTo(maxVersion),
     318                    version);
     319            if ((version.compareTo(minVersion) >= 0) &&
     320                    (version.compareTo(maxVersion) <= 0)) {
     321                /*
     322                 * FIXME: re-factor to make this nicer ...
     323                 */
     324                this.version   = version;
     325                this.operation = operation;
     326                return checkParameters2();
     327            } else {
     328                addDiagnostic(SRUConstants.SRU_UNSUPPORTED_VERSION,
     329                        maxVersion.getVersionString(),
     330                        "Version \"" + version.getVersionString() +
     331                        "\" is not supported by this endpoint.");
     332            }
     333        }
     334        logger.debug("bailed");
     335        return false;
     336    }
     337
     338
     339    private boolean checkParameters2() {
    312340        if (diagnostics == null) {
    313341            // check mandatory/optional parameters for operation
     
    457485        }
    458486
    459         // diagnostics != null -> consider as success
     487        /*
     488         *  diagnostics == null -> consider as success
     489         *  FIXME: this should ne done nicer!
     490         */
    460491        return (diagnostics == null);
    461492    }
  • SRUServer/trunk/src/main/java/eu/clarin/sru/server/SRUServer.java

    r6779 r6787  
    11411141            /* FALL-THROUGH */
    11421142        case VERSION_1_2:
    1143             return NAMESPACES_LEGACY;
     1143            switch (config.getLegacyNamespaceMode()) {
     1144            case LOC:
     1145                return NAMESPACES_LEGACY_LOC;
     1146            case OASIS:
     1147                return NAMESPACES_1_2_OASIS;
     1148            default:
     1149                // FIXME: better exception?
     1150                throw new IllegalAccessError("invalid legacy mode: " + version);
     1151            } // switch
    11441152        case VERSION_2_0:
    11451153            return NAMESPACES_2_0;
     
    11531161
    11541162
    1155     private static final SRUNamespaces NAMESPACES_LEGACY = new SRUNamespaces() {
     1163    private static final SRUNamespaces NAMESPACES_LEGACY_LOC = new SRUNamespaces() {
    11561164        private static final String SRU_NS =
    11571165                "http://www.loc.gov/zing/srw/";
     
    11911199        public String getScanPrefix() {
    11921200            return SRU_PREFIX;
     1201        }
     1202
     1203
     1204        @Override
     1205        public String getDiagnosticNS() {
     1206            return SRU_DIAGNOSIC_NS;
     1207        }
     1208
     1209
     1210        @Override
     1211        public String getDiagnosticPrefix() {
     1212            return SRU_DIAGNOSTIC_PREFIX;
     1213        }
     1214
     1215
     1216        @Override
     1217        public String getExplainNS() {
     1218            return SRU_EXPLAIN_NS;
     1219        }
     1220
     1221
     1222        @Override
     1223        public String getExplainPrefix() {
     1224            return SRU_EXPLAIN_PREFIX;
     1225        }
     1226
     1227
     1228        @Override
     1229        public String getXcqlNS() {
     1230            return SRU_XCQL_NS;
     1231        }
     1232    };
     1233
     1234
     1235    private static final SRUNamespaces NAMESPACES_1_2_OASIS = new SRUNamespaces() {
     1236        private static final String SRU_RESPONSE_NS =
     1237                "http://docs.oasis-open.org/ns/search-ws/sruResponse";
     1238        private static final String SRU_RESPONSE_PREFIX =
     1239                "sruResponse";
     1240        private static final String SRU_SCAN_NS =
     1241                "http://docs.oasis-open.org/ns/search-ws/scan";
     1242        private static final String SRU_SCAN_PREFIX =
     1243                "scan";
     1244        private static final String SRU_DIAGNOSIC_NS =
     1245                "http://docs.oasis-open.org/ns/search-ws/diagnostic";
     1246        private static final String SRU_DIAGNOSTIC_PREFIX =
     1247                "diag";
     1248        private static final String SRU_EXPLAIN_NS =
     1249                "http://explain.z3950.org/dtd/2.0/";
     1250        private static final String SRU_EXPLAIN_PREFIX =
     1251                "zr";
     1252        private static final String SRU_XCQL_NS =
     1253                "http://docs.oasis-open.org/ns/search-ws/xcql";
     1254
     1255
     1256        @Override
     1257        public String getResponseNS() {
     1258            return SRU_RESPONSE_NS;
     1259        }
     1260
     1261
     1262        @Override
     1263        public String getResponsePrefix() {
     1264            return SRU_RESPONSE_PREFIX;
     1265        }
     1266
     1267
     1268        @Override
     1269        public String getScanNS() {
     1270            return SRU_SCAN_NS;
     1271        }
     1272
     1273
     1274        @Override
     1275        public String getScanPrefix() {
     1276            return SRU_SCAN_PREFIX;
    11931277        }
    11941278
  • SRUServer/trunk/src/main/java/eu/clarin/sru/server/SRUServerConfig.java

    r6780 r6787  
    8989     * Valid values: "<code>1.1</code>", "<code>1.2</code>" or "
    9090     * <code>2.0</code>" (without quotation marks)
    91      * <p>
     91     * </p>
    9292     */
    9393    public static final String SRU_SUPPORTED_VERSION_MIN =
     
    9999     * Valid values: "<code>1.1</code>", "<code>1.2</code>" or "
    100100     * <code>2.0</code>" (without quotation marks)
    101      * <p>
     101     * </p>
    102102     */
    103103    public static final String SRU_SUPPORTED_VERSION_MAX =
     
    112112     * Valid values: "<code>1.1</code>", "<code>1.2</code>" or "
    113113     * <code>2.0</code>" (without quotation marks)
    114      * <p>
     114     * </p>
    115115     */
    116116    public static final String SRU_SUPPORTED_VERSION_DEFAULT =
    117117            "eu.clarin.sru.server.sruSupportedVersionDefault";
     118    /**
     119     * Parameter constant for setting the namespace URIs for SRU 1.1 and SRU
     120     * 1.2.
     121     * <p>
     122     * Valid values: "<code>loc"</code> for Library Of Congress URI or "
     123     * <code>oasis</code> for OASIS URIs (without quotation marks).
     124     * </p>
     125     */
     126    public static final String SRU_LEGACY_NAMESPACE_MODE =
     127            "eu.clarin.sru.server.legacyNamespaceMode";
    118128    /**
    119129     * Parameter constant for configuring the transports for this SRU server.
     
    268278    private static final SRUVersion DEFAULT_SRU_VERSION_MAX =
    269279            SRUVersion.VERSION_1_2;
     280    private static final LegacyNamespaceMode DEFAULT_LEGACY_NAMESPACE_MODE =
     281            LegacyNamespaceMode.LOC;
    270282    private static final int DEFAULT_NUMBER_OF_RECORDS    = 100;
    271283    private static final int DEFAULT_MAXIMUM_RECORDS      = 250;
     
    277289    private static final String CONFIG_FILE_SCHEMA_URL =
    278290            "META-INF/sru-server-config.xsd";
     291    public static enum LegacyNamespaceMode {
     292        LOC, OASIS
     293    } // enum LegacyNamespaceMode
    279294
    280295    public static final class LocalizedString {
     
    603618    private final SRUVersion maxVersion;
    604619    private final SRUVersion defaultVersion;
     620    private final LegacyNamespaceMode legacyNamespaceMode;
    605621    private final String transport;
    606622    private final String host;
     
    623639
    624640
    625     private SRUServerConfig(SRUVersion minVersion, SRUVersion maxVersion,
    626             SRUVersion defaultVersion, String transport, String host, int port,
    627             String database, int numberOfRecords, int maximumRecords,
    628             int numberOfTerms, int maximumTerms, boolean echoRequests,
    629             int indentResponse, int responseBufferSize,
     641    private SRUServerConfig(SRUVersion minVersion,
     642            SRUVersion maxVersion,
     643            SRUVersion defaultVersion,
     644            LegacyNamespaceMode legacyNamespaceMode,
     645            String transport,
     646            String host,
     647            int port,
     648            String database,
     649            int numberOfRecords,
     650            int maximumRecords,
     651            int numberOfTerms,
     652            int maximumTerms,
     653            boolean echoRequests,
     654            int indentResponse,
     655            int responseBufferSize,
    630656            boolean allowOverrideMaximumRecords,
    631657            boolean allowOverrideMaximumTerms,
    632             boolean allowOverrideIndentResponse, DatabaseInfo databaseinfo,
    633             IndexInfo indexInfo, List<SchemaInfo> schemaInfo) {
     658            boolean allowOverrideIndentResponse,
     659            DatabaseInfo databaseinfo,
     660            IndexInfo indexInfo,
     661            List<SchemaInfo> schemaInfo) {
    634662        this.minVersion                  = minVersion;
    635663        this.maxVersion                  = maxVersion;
    636664        this.defaultVersion              = defaultVersion;
     665        this.legacyNamespaceMode         = legacyNamespaceMode;
    637666        this.transport                   = transport;
    638667        this.host                        = host;
     
    683712
    684713
     714    public LegacyNamespaceMode getLegacyNamespaceMode() {
     715        return legacyNamespaceMode;
     716    }
     717
     718
    685719    public SRURecordPacking getDefaultRecordPacking() {
    686720        return SRURecordPacking.XML;
     
    952986                                SRU_SUPPORTED_VERSION_MAX + "\" (" +
    953987                                maxVersion.getVersionString() + ")");
     988            }
     989
     990            LegacyNamespaceMode legacyNamespaceMode =
     991                    DEFAULT_LEGACY_NAMESPACE_MODE;
     992            String mode = params.get(SRU_LEGACY_NAMESPACE_MODE);
     993            if ((mode != null) && !mode.isEmpty()) {
     994                if ("loc".equals(mode)) {
     995                    legacyNamespaceMode = LegacyNamespaceMode.LOC;
     996                } else if ("oasis".equals(mode)) {
     997                    legacyNamespaceMode = LegacyNamespaceMode.OASIS;
     998                } else {
     999                    throw new SRUConfigException(
     1000                            "invalid value for parameter \"" +
     1001                                    SRU_LEGACY_NAMESPACE_MODE + "\": " + mode);
     1002                }
    9541003            }
    9551004
     
    10261075                    DEFAULT_RESPONSE_BUFFER_SIZE, 0, -1);
    10271076
    1028             return new SRUServerConfig(minVersion, maxVersion, defaultVersion,
    1029                     transport, host, port, database, numberOfRecords,
    1030                     maximumRecords, numberOfTerms, maximumTerms, echoRequests,
    1031                     indentResponse, responseBufferSize,
    1032                     allowOverrideMaximumRecords, allowOverrideMaximumTerms,
    1033                     allowOverrideIndentResponse, databaseInfo, indexInfo,
     1077            return new SRUServerConfig(minVersion,
     1078                    maxVersion,
     1079                    defaultVersion,
     1080                    legacyNamespaceMode,
     1081                    transport,
     1082                    host,
     1083                    port,
     1084                    database,
     1085                    numberOfRecords,
     1086                    maximumRecords,
     1087                    numberOfTerms,
     1088                    maximumTerms,
     1089                    echoRequests,
     1090                    indentResponse,
     1091                    responseBufferSize,
     1092                    allowOverrideMaximumRecords,
     1093                    allowOverrideMaximumTerms,
     1094                    allowOverrideIndentResponse,
     1095                    databaseInfo,
     1096                    indexInfo,
    10341097                    schemaInfo);
    10351098        } catch (IOException e) {
Note: See TracChangeset for help on using the changeset viewer.