Project

General

Profile

« Previous | Next » 

Revision 61432

Improvements in parameters and author seaches

View differences:

SearchRequestController.java
25 25
import org.springframework.http.ResponseEntity;
26 26
import org.springframework.stereotype.Controller;
27 27
import org.springframework.ui.ModelMap;
28
import org.springframework.web.bind.annotation.ExceptionHandler;
29
import org.springframework.web.bind.annotation.RequestMapping;
30
import org.springframework.web.bind.annotation.RequestMethod;
31
import org.springframework.web.bind.annotation.ResponseBody;
28
import org.springframework.web.bind.annotation.*;
32 29
import org.z3950.zing.cql.CQLParseException;
33 30

  
31
import javax.annotation.PostConstruct;
34 32
import javax.annotation.Resource;
35 33
import javax.servlet.ServletOutputStream;
36 34
import javax.servlet.http.HttpServletRequest;
......
73 71
    private static final String OTHER_BASIC_QUERY = "(oaftype exact result) and (resulttypeid exact other)";
74 72
    private static final String PROJECT_BASIC_QUERY = "(oaftype exact project)";
75 73

  
76
    private static final List<String> GLOBAL_PARAMETERS = Arrays.asList("page", "size", "format", "sortBy");
74
    private static final HashSet<String> GLOBAL_PARAMETERS = new HashSet<>(Arrays.asList("page", "size", "format", "sortBy"));
77 75

  
78
    private static final List<String> PUB_N_DATA_COMMON_PARAMETERS = Arrays.asList("author", "doi", "originalId", "community", "FP7ProjectID", "FP7scientificArea",
79
            "fromDateAccepted", "funder", "fundingStream", "hasECFunding", "hasProject", "hasWTFunding", "keywords",
80
            "model", "OA", "openaireProjectID", "openaireProviderID", "projectID", "title", "toDateAccepted", "country");
76
    private static final HashSet<String> PUB_N_DATA_COMMON_PARAMETERS = new HashSet<>(Arrays.asList("author", "doi", "originalId",
77
            "community", "FP7ProjectID", "FP7scientificArea", "fromDateAccepted", "funder", "fundingStream", "hasECFunding", "hasProject",
78
            "hasWTFunding", "keywords", "model", "OA", "openaireProjectID", "openaireProviderID", "projectID", "title", "toDateAccepted",
79
            "country", "orcid"));
81 80

  
82
    private static final List<String> PUB_PARAMETERS = Arrays.asList("openairePublicationID");
83
    private static final List<String> DATA_PARAMETERS = Arrays.asList("openaireDatasetID");
84
    private static final List<String> SOFTWARE_PARAMETERS = Arrays.asList("openaireSoftwareID");
85
    private static final List<String> OTHER_PARAMETERS = Arrays.asList("openaireOtherID");
81
    private static final HashSet<String> PUB_PARAMETERS = new HashSet<>(Arrays.asList("openairePublicationID"));
82
    private static final HashSet<String> DATA_PARAMETERS = new HashSet<>(Arrays.asList("openaireDatasetID"));
83
    private static final HashSet<String> SOFTWARE_PARAMETERS = new HashSet<>(Arrays.asList("openaireSoftwareID"));
84
    private static final HashSet<String> OTHER_PARAMETERS = new HashSet<>(Arrays.asList("openaireOtherID"));
86 85

  
87
    private static final List<String> PUB_N_DATASET_MODELS = Arrays.asList("dc", "openaire", "sygma");
88
    private static final List<String> PUB_N_DATASET_FORMATS = Arrays.asList("json", "rss", "xml", "csv", "tsv", "html");
86
    private static final HashSet<String> PUB_N_DATASET_MODELS = new HashSet<>(Arrays.asList("dc", "openaire", "sygma"));
87
    private static final HashSet<String> PUB_N_DATASET_FORMATS = new HashSet<>(Arrays.asList("json", "rss", "xml", "csv", "tsv", "html"));
89 88

  
90
    private static final List<String> PROJECT_PARAMETERS = Arrays.asList("acronym", "callID", "endYear", "FP7scientificArea",
89
    private static final HashSet<String> PROJECT_PARAMETERS = new HashSet<>(Arrays.asList("acronym", "callID", "endYear", "FP7scientificArea",
91 90
            "funder", "fundingStream", "grantID", "hasECFunding", "hasWTFunding", "keywords", "name",
92
            "participantAcronyms", "participantCountries", "startYear", "sc39", "openaireParticipantID", "openaireProjectID");
93
    private static final List<String> PROJECT_FORMATS = Arrays.asList("xml", "json", "csv", "tsv", "html");
91
            "participantAcronyms", "participantCountries", "startYear", "sc39", "openaireParticipantID", "openaireProjectID"));
92
    private static final HashSet<String> PROJECT_FORMATS = new HashSet<>(Arrays.asList("xml", "json", "csv", "tsv", "html"));
94 93

  
94
    @PostConstruct
95
    public void init() {
96
        PUB_PARAMETERS.addAll(PUB_N_DATA_COMMON_PARAMETERS);
97
        DATA_PARAMETERS.addAll(PUB_N_DATA_COMMON_PARAMETERS);
98
        SOFTWARE_PARAMETERS.addAll(PUB_N_DATA_COMMON_PARAMETERS);
99
        OTHER_PARAMETERS.addAll(PUB_N_DATA_COMMON_PARAMETERS);
100
    }
101

  
95 102
    //TODO this is for joomla - to be removed soon
96 103
    @RequestMapping(value = "/search", method = RequestMethod.GET)
97 104
    @Timed(value = "search.joomla.requests", longTask = false)
......
210 217
    @RequestMapping(value = "/api/publications", method = RequestMethod.GET, produces = {MediaType.APPLICATION_XML_VALUE,MediaType.APPLICATION_JSON_VALUE})
211 218
    @Timed(value = "http.server.request.duration", extraTags = {"referer", "api", "uri", "/api/publications"}, longTask = false)
212 219
    public void searchPublications(HttpServletRequest request, HttpServletResponse response) throws Exception {
213

  
214 220
        long time = System.currentTimeMillis();
215
        checkParameters(ListUtils.union(PUB_N_DATA_COMMON_PARAMETERS, PUB_PARAMETERS),request.getParameterMap());
216

  
217 221
        int page = readParameter(request, "page", 1);
218 222
        int size = readParameter(request, "size", 10);
223
        String format = (request.getParameter("format") != null) ? request.getParameter("format") : "xml";
224
        String model = request.getParameter("model");
225

  
226
        checkParameters(PUB_PARAMETERS,request.getParameterMap());
219 227
        checkRequestSize(page, size);
220

  
221
        String format = (request.getParameter("format") != null) ? request.getParameter("format") : "xml";
222 228
        checkFormatParameter(PUB_N_DATASET_FORMATS, format);
223 229
        createResponseMeta(response, format);
230
        checkModelParameter(PUB_N_DATASET_MODELS, model);
224 231

  
225 232
        String locale = request.getParameter("locale");
226

  
227
        String model = request.getParameter("model");
228
        checkModelParameter(PUB_N_DATASET_MODELS, model);
229 233
        String sTransformer = defineTransformer(model,format);
230 234

  
231 235
        Collection<String> referrers = readParameter(request,"referrer");
......
243 247
        ParameterQueryEnhancer.enhanceQueryWithAccessRights(queryBuilder, request);
244 248
        ParameterQueryEnhancer.enhanceQueryWithDate(queryBuilder, request);
245 249
        ParameterQueryEnhancer.enhanceQueryWithDoi(queryBuilder, request);
250
        ParameterQueryEnhancer.enhanceQueryWithOrcid(queryBuilder, request);
246 251
        ParameterQueryEnhancer.enhanceQueryWithOriginalId(queryBuilder, request);
247 252

  
248

  
249 253
        ParameterQueryEnhancer.enhanceQueryWithResultsSortParameters(queryBuilder, request);
250 254
        FormattedSearchResult formattedSearchResult = null;
251 255

  
......
255 259
        } catch (Exception e) {
256 260
            logger.error("Fail to execute search.", e);
257 261
            throw new Exception("Fail to execute search", e);
258

  
259 262
        }
260 263

  
261 264
        PrintWriter writer = response.getWriter();
......
272 275
    @Timed(value = "http.server.request.duration", extraTags = {"referer", "api", "uri", "/api/datasets"}, longTask = false)
273 276
    public void searchData(HttpServletRequest request, HttpServletResponse response) throws Exception {
274 277

  
275
        checkParameters(ListUtils.union(PUB_N_DATA_COMMON_PARAMETERS, DATA_PARAMETERS),request.getParameterMap());
278
        checkParameters(DATA_PARAMETERS,request.getParameterMap());
276 279

  
277 280
        int page = readParameter(request, "page", 1);
278 281
        int size = readParameter(request, "size", 10);
......
331 334
    @Timed(value = "http.server.request.duration", extraTags = {"referer", "api", "uri", "/api/software"}, longTask = false)
332 335
    public void searchSoftware(HttpServletRequest request, HttpServletResponse response) throws Exception {
333 336

  
334
        checkParameters(ListUtils.union(PUB_N_DATA_COMMON_PARAMETERS, SOFTWARE_PARAMETERS),request.getParameterMap());
337
        checkParameters(SOFTWARE_PARAMETERS,request.getParameterMap());
335 338

  
336 339
        int page = readParameter(request, "page", 1);
337 340
        int size = readParameter(request, "size", 10);
......
386 389
    @Timed(value = "http.server.request.duration", extraTags = {"referer", "api", "uri", "/api/other"}, longTask = false)
387 390
    public void searchOther(HttpServletRequest request, HttpServletResponse response) throws Exception {
388 391

  
389
        checkParameters(ListUtils.union(PUB_N_DATA_COMMON_PARAMETERS, OTHER_PARAMETERS),request.getParameterMap());
392
        checkParameters(OTHER_PARAMETERS,request.getParameterMap());
390 393

  
391 394
        int page = readParameter(request, "page", 1);
392 395
        int size = readParameter(request, "size", 10);
......
682 685
     * @param allowedParameters the allowed parameters
683 686
     * @param currentParameters the given parameters
684 687
     */
685
    private void checkParameters(List<String> allowedParameters, Map<String, String[]> currentParameters){
688
    private void checkParameters(HashSet<String> allowedParameters, Map<String, String[]> currentParameters){
686 689
        if(currentParameters != null) {
687 690
            for (String parameter : currentParameters.keySet()) {
688 691
                if (!allowedParameters.contains(parameter) && !GLOBAL_PARAMETERS.contains(parameter) && !parameter.equals("referrer")) {
......
693 696
        }
694 697
    }
695 698

  
696
    private void checkFormatParameter(List<String> allowedFormats, String requestedFormat) {
699
    private void checkFormatParameter(HashSet<String> allowedFormats, String requestedFormat) {
697 700
        if (requestedFormat!= null && !allowedFormats.contains(requestedFormat)) {
698 701
            throw new IllegalArgumentException("The requested format \'"+ requestedFormat +"\' is not supported. The supported formats are: " + allowedFormats);
699 702
        }
700 703
    }
701 704

  
702
    private static void checkModelParameter(List<String> allowedModels, String requestedModel) {
705
    private static void checkModelParameter(HashSet<String> allowedModels, String requestedModel) {
703 706
        if (requestedModel!= null && !allowedModels.contains(requestedModel)) {
704 707
            throw new IllegalArgumentException("The requested model \'"+ allowedModels +"\' is not supported. The supported formats are: " + allowedModels);
705 708
        }

Also available in: Unified diff