Project

General

Profile

« Previous | Next » 

Revision 55643

merge uoa-search solr7 branch 50700:55641 into trunk

View differences:

modules/uoa-search/trunk/src/main/java/eu/dnetlib/data/search/solr/SolrResultSetFactory.java
5 5
import gr.uoa.di.driver.enabling.resultset.ResultSet;
6 6
import gr.uoa.di.driver.enabling.resultset.ResultSetFactory;
7 7
import org.apache.log4j.Logger;
8
import org.apache.solr.client.solrj.impl.CloudSolrServer;
8
import org.apache.solr.client.solrj.impl.CloudSolrClient;
9 9

  
10
import java.util.Arrays;
10 11
import java.util.HashMap;
11 12
import java.util.Map;
12 13

  
......
16 17
public class SolrResultSetFactory implements ResultSetFactory {
17 18

  
18 19
    private Logger logger = Logger.getLogger(getClass());
20
    private Map<String, CloudSolrClient> clients = new HashMap<String, CloudSolrClient>();
19 21

  
20
    private Map<String, CloudSolrServer> clients = new HashMap<String, CloudSolrServer>();
21

  
22
    //private static int Rsets = 0;
23

  
24 22
    @Override
25 23
    public ResultSet<String> createResultSet(EPR epr) {
26 24
        try {
27 25

  
28
            CloudSolrServer solrClient = null;
26
            String[] zkservers= epr.getAddress().split(",");
29 27

  
28
            CloudSolrClient solrClient;
29

  
30 30
            synchronized (clients) {
31 31
                solrClient =  clients.get(epr.getAddress());
32 32
            }
33 33
            if (solrClient == null) {
34
                solrClient = new CloudSolrServer(epr.getAddress());
34
                solrClient = new CloudSolrClient.Builder().withZkHost(Arrays.asList(zkservers)).build();
35 35

  
36 36
                clients.put(epr.getAddress(), solrClient);
37 37
            }
38 38

  
39
            logger.debug("Address " + epr.getAddress());
40
            //Rsets++;
41
            //System.out.println("Created Results sets " + Rsets);
42
            return new SolrResultSet(epr, solrClient);
39
            ResultSet<String> solrResultSets = new SolrResultSet(epr, solrClient);
43 40

  
41
            return solrResultSets;
42

  
44 43
        } catch (Exception e) {
45 44
            logger.error("Error creating solr client", e);
46 45
        }
47 46

  
47
        logger.debug("NOT HERE!");
48 48
        return null;
49 49
    }
50 50

  
modules/uoa-search/trunk/src/main/java/eu/dnetlib/data/search/solr/SolrResultSet.java
11 11
import gr.uoa.di.driver.enabling.resultset.ResultSet;
12 12
import org.apache.commons.lang.StringEscapeUtils;
13 13
import org.apache.log4j.Logger;
14
import org.apache.solr.client.solrj.SolrClient;
14 15
import org.apache.solr.client.solrj.SolrServerException;
15
import org.apache.solr.client.solrj.impl.CloudSolrServer;
16
import org.apache.solr.client.solrj.impl.CloudSolrClient;
16 17
import org.apache.solr.client.solrj.response.FacetField;
17 18
import org.apache.solr.client.solrj.response.QueryResponse;
18 19
import org.apache.solr.common.SolrDocumentList;
......
23 24
import javax.ws.rs.core.MediaType;
24 25
import java.io.IOException;
25 26
import java.io.OutputStream;
26
import java.util.ArrayList;
27
import java.util.HashMap;
28
import java.util.List;
29
import java.util.Map;
27
import java.util.*;
30 28

  
31 29
/**
32 30
 * Created by antleb on 2/4/14.
......
36 34
    private Logger logger = Logger.getLogger(getClass());
37 35

  
38 36
    private EPR epr = null;
39
    private CloudSolrServer solrClient = null;
37
    public SolrClient solrClient = null;
40 38

  
41 39
    private NamedList<String> queryOpts = new NamedList<String>();
42 40
    long size = -1;
43 41

  
44 42

  
45
    public SolrResultSet(EPR epr, CloudSolrServer solrClient) throws IOException, CQLParseException {
43
    public SolrResultSet(EPR epr, CloudSolrClient cloudSolrClient) throws IOException, CQLParseException {
44
        logger.debug("Setting solr client " + cloudSolrClient);
46 45
        this.epr = epr;
47
        this.solrClient = solrClient;
46
        this.solrClient = cloudSolrClient;
48 47
        this.queryOpts = SolrResultSetOptionsUtil.extractQueryOptions(epr.getParameter("query"));
49 48

  
50 49
        String layout = epr.getParameter("layout");
51 50
        String mdformat = epr.getParameter("mdformat");
52 51
        String interpretation = epr.getParameter("interpretation");
53 52

  
54
        solrClient.setDefaultCollection(mdformat + "-" + layout + "-" + interpretation);
53
        ((CloudSolrClient)solrClient).setDefaultCollection(mdformat + "-" + layout + "-" + interpretation);
55 54
    }
56 55

  
57 56
    @Override
......
66 65

  
67 66
    @Override
68 67
    public void close() {
69
        //solrClient.shutdown();
68
/*
69
        try {
70
            logger.debug("!!!!!!!!! !!!!!!! CLOSING !!!!!!!!! !!!!!!!!!! ");
71
            solrClient.close();
72

  
73
        } catch (IOException e) {
74
            logger.error("Error closing result set.", e);
75
        }
76
*/
70 77
    }
71 78

  
72 79
    @Override
......
133 140
            logger.debug("docs: " + docs.size());
134 141

  
135 142
            for (int i = 0; i < docs.size(); i++) {
136
                String result = ((ArrayList<String>) docs.get(i).get("__result")).get(0);
143
                String result = (String) docs.get(i).get("__result");
137 144
                res.add(result);
138 145
            }
139 146

  
......
141 148

  
142 149
        } catch (SolrServerException sse) {
143 150
            logger.error("Fail to get results from Solr. ", sse);
151

  
152
        } catch (IOException ioe) {
153
            logger.error("Fail to get results from Solr. ", ioe);
144 154
        }
145 155

  
146 156
        return null;
......
155 165
        List<String> refineSolrResults = new ArrayList<String>();
156 166
        List<String> searchSolrResults = new ArrayList<String>();
157 167

  
158
        logger.debug("format: " + format);
159

  
160 168
        QueryResponse rsp = null;
161 169
        HashMap<String, List<String>> map = new HashMap<String, List<String>>();
162 170

  
163
        //logger.info("from: " + from*to);
164
        //logger.info("to: " + to);
165

  
166 171
        queryOpts.add("start", from*to + "");
167 172
        queryOpts.add("rows", to +"");
168 173

  
169 174
        try {
170 175
            long startTime = System.nanoTime();
176

  
171 177
            rsp = solrClient.query(SolrParams.toSolrParams(queryOpts));
172 178
            long estimatedTime = System.nanoTime() - startTime;
173 179
            logger.info("Solrj time " + estimatedTime/1000000 +  " milliseconds for query:" + queryOpts.get("q") +
......
181 187
            this.size = docs.getNumFound();
182 188

  
183 189
            if (facetFields!=null && !facetFields.isEmpty()) {
184

  
185
                logger.debug("Checking " + (format != null && format.equals(MediaType.APPLICATION_JSON)));
186

  
187 190
                if (format != null && format.equals(MediaType.APPLICATION_JSON)) {
188 191
                    for (FacetField field : facetFields) {
189 192
                        map.put(field.getName(), new ArrayList<String>());
......
194 197
                            bf.setId(field.getValues().get(i).getName());
195 198
                            String[] facetedValues = field.getValues().get(i).getName().split("\\|\\|",2);
196 199

  
200

  
201
                            logger.debug("faceted values " + Arrays.toString(facetedValues));
202

  
197 203
                            if (facetedValues.length > 1) {
198 204
                                //bf.setName(org.apache.commons.lang3.StringEscapeUtils.escapeJson(facetedValues[1]));
199 205
                                bf.setName(facetedValues[1]);
206
                                logger.debug("faceted values [1] " + facetedValues[1]);
200 207

  
201 208
                            } else if (field.getValues().get(i).getName().split("_\\:\\:",2).length > 1) {
202 209
                                //bf.setName(org.apache.commons.lang3.StringEscapeUtils.escapeJson(field.getValues().get(i).getName().split("\\:\\:",2)[1]).replaceAll("\\:\\:", "\\|"));
......
222 229
                        refineSolrResults.add(builder.toString());
223 230
                    }
224 231

  
225
                } else { //the old implementation & xml as default //TODO check compatibility
232
                } else { //the old implementation & xml as default
226 233
                    logger.debug("Creating old browse results.");
227 234
                    createXmlRefineFields(refineSolrResults, oldRefineTransformer);
228 235
                }
229 236
            }
230 237

  
231 238
            for (int i = 0; i < docs.size(); i++) {
232
                String result = ((ArrayList<String>) docs.get(i).get("__result")).get(0);
239
                String result = (String) docs.get(i).get("__result");
240

  
241
                logger.debug("["+ i +"]: " + docs.get(i).get("__result"));
242

  
233 243
                try {
234 244
                    if (transformer != null) {
235
                        //logger.debug("1 >>>>>>" + result);
245
                        logger.debug("1 >>>>>>" + result);
236 246
                        String xml = result.replaceAll("<em>","").replaceAll("</em>","");
237 247
                        result = transformer.transform(xml);
238
                        //logger.debug("2 >>>>>>" + result);
248
                        logger.debug("2 >>>>>>" + result);
239 249
                    }
250

  
240 251
                } catch (TransformerException te) {
241 252
                    logger.warn("Error transforming " + result, te);
242 253
                    continue;
......
251 262

  
252 263
            Map<String,List<String>> response = new HashMap<String, List<String>>();
253 264

  
254
            logger.debug("refine results " + refineSolrResults);
265
            //logger.debug("refine results " + refineSolrResults);
255 266
            //logger.debug("search results " + searchSolrResults);
256 267

  
257 268
            response.put("refine",refineSolrResults);
......
261 272

  
262 273
        } catch (SolrServerException sse) {
263 274
            logger.error("Error calling Solr.", sse);
275

  
276
        } catch (IOException ioe) {
277
            logger.error("Error calling Solr.", ioe);
264 278
        }
265 279
        return null;
266 280
    }
......
279 293
        String nextCursorMark = "";
280 294

  
281 295
        int curs = 0;
282
        QueryResponse resp = solrClient.query(SolrParams.toSolrParams(queryOpts));
296
        try {
297
            QueryResponse resp = solrClient.query(SolrParams.toSolrParams(queryOpts));
283 298

  
284
        while (!cursorMark.equals(nextCursorMark)) {
285
            resp = solrClient.query(SolrParams.toSolrParams(queryOpts));
286
            cursorMark = nextCursorMark;
287
            nextCursorMark = resp.getNextCursorMark();
299
            while (!cursorMark.equals(nextCursorMark)) {
300
                resp = solrClient.query(SolrParams.toSolrParams(queryOpts));
301
                cursorMark = nextCursorMark;
302
                nextCursorMark = resp.getNextCursorMark();
288 303

  
289
            for (int i = 0; i < resp.getResults().size(); i++) {
290
                if (transformer != null) {
291
                    String result = null;
292
                    try {
293
                        result = transformer.transform(((ArrayList<String>) resp.getResults().get(i).get("__result")).get(0));
304
                for (int i = 0; i < resp.getResults().size(); i++) {
305
                    if (transformer != null) {
306
                        String result = null;
307
                        try {
308
                            result = transformer.transform((String) resp.getResults().get(i).get("__result"));
309
                            logger.debug("RESULT " + result);
294 310

  
295
                    } catch (TransformerException te) {
296
                        logger.warn("Error transforming " + result, te);
297
                        continue;
298
                    }
311
                        } catch (TransformerException te) {
312
                            logger.warn("Error transforming " + result, te);
313
                            continue;
314
                        }
299 315

  
300
                    try {
301
                        os.write(result.getBytes());
302
                        os.flush();
303
                    } catch (IOException e) {
304
                        logger.error("Cursor get... ", e);
305
                        continue;
316
                        try {
317
                            os.write(result.getBytes());
318
                            os.flush();
319
                        } catch (IOException e) {
320
                            logger.error("Cursor get... ", e);
321
                            continue;
322
                        }
306 323
                    }
307 324
                }
325

  
326
                queryOpts.remove("cursorMark");
327
                queryOpts.add("cursorMark", nextCursorMark);
328
                curs++;
308 329
            }
309 330

  
310
            queryOpts.remove("cursorMark");
311
            queryOpts.add("cursorMark", nextCursorMark);
312
            curs ++;
331
        } catch (IOException ioe) {
332
            logger.error("Error executing solr query. ", ioe);
313 333
        }
314 334

  
315 335
        logger.debug("CURS " + curs);
modules/uoa-search/trunk/src/main/java/eu/dnetlib/data/search/app/SearchServiceImpl.java
119 119
        } catch (Exception e) {
120 120
            logger.error("Fail to load search service profile with id " + serviceId + " from IS.", e);
121 121
        }
122

  
122 123
    }
123 124

  
124 125
    @Override
......
143 144
    public SearchResult searchNrefine(String text, String searchTransformer, String browseTransformer,
144 145
                                      String locale, int page, int size, Collection<String> fields) throws SearchServiceException {
145 146

  
146
        logger.info("deprecated searchNrefine > from: " + page + " to:" + size);
147
        //logger.info("deprecated searchNrefine > from: " + page + " to:" + size);
147 148
        //TODO check locale
148
        logger.debug("Search transformer " + searchTransformer);
149
        //logger.debug("Search transformer " + searchTransformer);
149 150
        Transformer sTransformer = transformerFactory.getTransformer(searchTransformer, Locale.getDefault());
150 151
        Transformer oldRefineTransformer = transformerFactory.getTransformer("results_openaire_browse", Locale.getDefault());
151 152

  
152
        logger.debug("Refine transformer " + browseTransformer);
153
        //logger.debug("Refine transformer " + browseTransformer);
153 154
        //Transformer rTranformer = transformerFactory.getTransformer(refineTransformer, Locale.getDefault());
154 155

  
155 156
        List<String> refineFields = null;
......
181 182
        //TODO check locale
182 183
        FormattedSearchResult formattedSearchResult = null;
183 184

  
184
        logger.debug("Search transformer " + searchTransformer);
185
        //logger.debug("Search transformer " + searchTransformer);
185 186
        Transformer sTransformer = transformerFactory.getTransformer(searchTransformer, Locale.getDefault());
186 187
        Transformer oldRefineTransformer = transformerFactory.getTransformer("results_openaire_browse", Locale.getDefault());
187 188

  
188
        logger.debug("Refine transformer " + refineTransformer);
189
        //logger.debug("Refine transformer " + refineTransformer);
189 190
        //Transformer rTranformer = transformerFactory.getTransformer(refineTransformer, Locale.getDefault());
190 191

  
191 192
        List<String> refineFields = null;
......
208 209
    public SearchResult newSearch (String text, String locale, List<String> refinefields, List<String> fieldQueries,
209 210
                                   int from, int to, String format, Transformer transformer, Transformer oldRefineTransformer,
210 211
                                   boolean oldPaging) throws SearchServiceException {
211
        //logger.info("newSearch > from: " + from + " to:" + to);
212
        logger.info("newSearch > from: " + from + " to:" + to);
212 213
        long startTime = System.nanoTime();
213 214

  
214 215
        IndexService index = getIndexLocator().getService();
......
299 300
            logger.error("Error getting cursor results.", ise);
300 301
            throw new SearchServiceException("Error getting cursor results.", ise);
301 302

  
302
        /*} catch (IOException ioe) {
303
            logger.error("Error getting cursor results.", ioe);
304
            throw new SearchServiceException("Error getting cursor results.", ioe);
305

  
306
        */} catch (SolrServerException sse) {
303
        } catch (SolrServerException sse) {
307 304
            logger.error("Error getting cursor results.", sse);
308 305
            throw new SearchServiceException("Error getting cursor results.", sse);
309 306
        }
......
384 381
        return queryBuffer.append(facetsBuffer.toString()).append(fqBuffer.toString()).toString();
385 382
    }
386 383

  
384

  
385
    //TODO: I wish to remove this. This was only made (quick and dirty - only the enhanceFieldQueries(fieldQueries) is missing
386
    //from newSearch() after a last time request for the portal to show all the publications and the deletedbyinference ones.
387
    //I did not want to pass a parameter since I do not know if we are going to keep it. This is for a tech meeting showcase.
388
    //If we want to keep this I need to redesign.
389

  
390
    public SearchResult newSearchWithoutFieldQueries (String text, String locale, List<String> refinefields, List<String> fieldQueries,
391
                                   int from, int to, String format, Transformer transformer, Transformer oldRefineTransformer,
392
                                   boolean oldPaging) throws SearchServiceException {
393
        logger.info("non filtered search for...  > from: " + from + " to:" + to);
394
        long startTime = System.nanoTime();
395

  
396
        IndexService index = getIndexLocator().getService();
397

  
398
        EPR epr = null;
399
        ResultSet<String> rs = null;
400

  
401
        List<String> browseResults = null;
402
        List<String> searchResults = null;
403

  
404
        String query = rewrite(text);
405
        logger.info("Performing query " + query + "' and fields " + fieldQueries + " and refine " + refinefields);
406

  
407
        try {
408
            //TODO see parser and maybe delete!
409
            //query = new CQLParser().parse(query).toCQL();
410
            String eprQuery = createEprQuery(query, refinefields, fieldQueries);
411

  
412
            epr = index.getBrowsingStatistics(eprQuery, "all", mdFormat, indexLayout);
413

  
414
            if (epr == null) {
415
                throw new SearchServiceException("Something really strange happened there! Index returned null result set id.");
416
            }
417

  
418
            //get the locale TODO do we need this?
419
            //String correctLocale = getCorrectLocale(locale);
420
            //StringTokenizer tokenizer = new StringTokenizer(correctLocale, "_");
421
            //Locale requestLocale = new Locale(tokenizer.nextToken(), tokenizer.nextToken());
422

  
423
            rs = rsFactory.createResultSet(epr);
424

  
425
            Map<String, List<String>> list = null;
426
            if (oldPaging) {
427
                list = ((SolrResultSet)rs).newGet(from-1, to, format, transformer, oldRefineTransformer);
428

  
429
            } else {
430
                list = ((SolrResultSet)rs).newGet(from, to, format, transformer, oldRefineTransformer);
431
            }
432

  
433

  
434
            searchResults = list.get("search");
435
            browseResults = list.get("refine");
436

  
437
        } catch (IndexServiceException ise) {
438
            logger.error("Error getting refine results.", ise);
439
            throw new SearchServiceException("Error getting refine results.", ise);
440

  
441
        }
442

  
443
        long estimatedTime = System.nanoTime() - startTime;
444
        logger.debug("Search time " + estimatedTime/1000000 +  " milliseconds for query " + query +
445
                " and fields " + fieldQueries + " and refine " + refinefields + " from: "+ from + " and size " + to);
446

  
447
        //logger.info("Returned results for NEW search query '" + query + "' and fields " + fieldQueries + " and refine " + refinefields);;
448
        rs.close();
449
        return new SearchResult(query, Locale.getDefault().toString(), rs.size(), from, to, searchResults, browseResults, refinefields);
450
    }
451

  
452

  
387 453
    public String getMdFormat() {
388 454
        return mdFormat;
389 455
    }
modules/uoa-search/trunk/src/main/java/eu/dnetlib/data/search/web/api/SearchApiService.java
979 979
    }
980 980

  
981 981

  
982
    //This follows the newSearchWithoutFieldQueries comment. I wish to remove it!!! This was only made
983
    // (quick and dirty - only the getResponseByEntity version with newSearchWithoutFieldQueries is used
984
    // after a last time request for the portal to show all the publications and the deletedbyinference ones.
985
    // I did not want to pass a parameter since I do not know if we are going to keep it. This is for a tech meeting showcase.
986
    // If we want to keep this I need to redesign.
987
    @GET
988
    @Path("/api/deletedByInferencePublications/{publicationid}")
989
    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
990
    public Response fetchDeletedByInferencePublicationsById(@PathParam("publicationid") String publicationid,
991
                                          @QueryParam("format") final String format,
992
                                          @QueryParam("fq") final List<String> fieldQueries,
993
                                          @Context final HttpServletRequest request) {
994
        return getDeletedByInferenceResponseByEntityId(request, RequestResponseHandler.Entity.PUBLICATION, publicationid, format, fieldQueries);
995
    }
996

  
997
    //Please see comment above. I wish to remove it.
998
    private Response getDeletedByInferenceResponseByEntityId(HttpServletRequest request, RequestResponseHandler.Entity entity, String entityId, String format, List<String> fieldQueries) {
999
        String responseType = extractResponseFormat(format);
1000

  
1001
        try {
1002
            StringBuilder queryBuilder = new StringBuilder();
1003
            builtEntityIdQuery(queryBuilder, entity, entityId, fieldQueries);
1004
            String fullQuery = queryBuilder.toString();
1005

  
1006
            if(fullQuery == null || fullQuery.isEmpty()) {
1007
                return Response.status(Response.Status.BAD_REQUEST).
1008
                        entity(APIResponseFormatter.compose400Message(format, "The 'query' parameter is required")).
1009
                        type(responseType).build();
1010
            }
1011

  
1012
            SearchResult searchResult = searchService.newSearchWithoutFieldQueries(fullQuery, Locale.getDefault().toString(), null, fieldQueries, 0, 1, responseType, null, null, false);
1013

  
1014
            if (searchResult.getSearchResults() == null || searchResult.getSearchResults().isEmpty() ||
1015
                    searchResult.getSearchResults().size() == 0 || searchResult.getSearchResults().get(0) == null) {
1016
                return Response.status(Response.Status.NOT_FOUND).entity(APIResponseFormatter.compose404Message(responseType, "404 - " + entity +" with id "
1017
                        + entityId + " not found.")).type(responseType).build();
1018
            }
1019

  
1020
            return Response.status(Response.Status.OK).entity(APIResponseFormatter.createEntityResponse(request, entity, searchResult.getSearchResults().get(0).toString(), responseType)).type(responseType).build();
1021

  
1022
        } catch (SearchServiceException sse) {
1023
            logger.error("Fail to fetch "+ entity + " with id " + entityId, sse);
1024
            return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(APIResponseFormatter.compose500Message(responseType, "Fail to fetch " + entity + " with id " + entityId, sse.getMessage())).build();
1025
        }
1026
    }
1027

  
982 1028
    /*
983 1029
    public static void main(String[] args) {
984 1030
        String json = " {\"result\":{\"xmlns:oaf\":\"http://namespace.openaire.eu/oaf\",\"xmlns:xsi\":\"http://www.w3.o" +
modules/uoa-search/trunk/src/main/java/eu/dnetlib/data/search/web/api/SearchRequestController.java
31 31
@Controller
32 32
public class SearchRequestController {
33 33

  
34
	@Autowired
35
	private SearchServiceImpl searchService = null;
36
	@Autowired
37
	private VocabularyManager vocabularyManager = null;
34
    @Autowired
35
    private SearchServiceImpl searchService = null;
36
    @Autowired
37
    private VocabularyManager vocabularyManager = null;
38 38

  
39
	@Resource
40
	private String maxResults = null;
39
    @Resource
40
    private String maxResults = null;
41 41

  
42
	@Resource
43
	private String maxSize = null;
42
    @Resource
43
    private String maxSize = null;
44 44

  
45
	private static Logger logger = Logger.getLogger(SearchRequestController.class);
45
    private static Logger logger = Logger.getLogger(SearchRequestController.class);
46 46

  
47
	private static final String XML_CONTENT_TYPE = "application/xml;charset=UTF-8";
48
	private static final String JSON_CONTENT_TYPE = "application/json;charset=UTF-8;";
47
    private static final String XML_CONTENT_TYPE = "application/xml;charset=UTF-8";
48
    private static final String JSON_CONTENT_TYPE = "application/json;charset=UTF-8;";
49 49
    private static final String CSV_CONTENT_TYPE = "text/csv;charset=UTF-8;";
50 50
    private static final String TSV_CONTENT_TYPE = "text/tab-separated-values;charset=UTF-8;";
51 51
    private static final String HTML_CONTENT_TYPE = "text/html;charset=UTF-8;";
......
76 76
    private static final List<String> PROJECT_FORMATS = Arrays.asList("xml", "json", "csv", "tsv", "html");
77 77

  
78 78
    //TODO this is for joomla - to be removed soon
79
	@RequestMapping(value = "/search", method = RequestMethod.GET)
80
	public void search(HttpServletRequest request, HttpServletResponse response) {
81
		PrintWriter writer = null;
82
		FormattedSearchResult formattedSearchResult = null;
79
    @RequestMapping(value = "/search", method = RequestMethod.GET)
80
    public void search(HttpServletRequest request, HttpServletResponse response) {
81
        PrintWriter writer = null;
82
        FormattedSearchResult formattedSearchResult = null;
83 83

  
84 84
        String action = readActionParameter(request);
85 85
        String query = readQueryParameter(request);
86
		String format = (request.getParameter("format") != null) ? request.getParameter("format") : "xml";
86
        String format = (request.getParameter("format") != null) ? request.getParameter("format") : "xml";
87 87
        createResponseMeta(response, format);
88 88

  
89
		String locale = request.getParameter("locale");
89
        String locale = request.getParameter("locale");
90 90

  
91 91
        int page= 0;
92 92
        int size =-1;
......
95 95
            page = readParameter(request, "page", 1);
96 96
            size = readParameter(request, "size", 10);
97 97
        }
98
        
99
		if (size > Integer.parseInt(maxSize)) {
100
			throw new IllegalArgumentException("Size argument have exceeded the maximum allowed number.");
101
		}
102 98

  
103
		String sTransformer = request.getParameter("sTransformer");
104
		String rTransformer = request.getParameter("rTransformer");
105
		Collection<String> fields = readParameter(request, "fields");
99
        if (size > Integer.parseInt(maxSize)) {
100
            throw new IllegalArgumentException("Size argument have exceeded the maximum allowed number.");
101
        }
106 102

  
103
        String sTransformer = request.getParameter("sTransformer");
104
        String rTransformer = request.getParameter("rTransformer");
105
        Collection<String> fields = readParameter(request, "fields");
106

  
107 107
        checkTransformerParameters(action, sTransformer, rTransformer, fields);
108 108

  
109 109
        try {
......
114 114
                    sTransformer, rTransformer, format, locale, page, size,
115 115
                    fields);
116 116

  
117
			writer.append(formattedSearchResult.getFormattedResult());
117
            writer.append(formattedSearchResult.getFormattedResult());
118 118

  
119
		} catch (Exception e) {
120
			logger.error("Fail to execute search.", e);
121
			createXmlErrorPage(writer, e);
119
        } catch (Exception e) {
120
            logger.error("Fail to execute search.", e);
121
            createXmlErrorPage(writer, e);
122 122

  
123
		} finally {
123
        } finally {
124 124
            if (writer != null) {
125 125
                writer.close();
126 126
            }
127
		}
128
	}
127
        }
128
    }
129 129

  
130 130
    private void checkTransformerParameters(String action, String sTransformer, String rTransformer, Collection<String> fields) {
131 131
        if (action.equals("search")) {
......
189 189
        return "openSearchDescriptor";
190 190
    }
191 191

  
192
	@RequestMapping(value = "/api/publications", method = RequestMethod.GET, produces = {MediaType.APPLICATION_XML_VALUE,MediaType.APPLICATION_JSON_VALUE})
193
	public void searchPublications(HttpServletRequest request, HttpServletResponse response) throws Exception {
192
    @RequestMapping(value = "/api/publications", method = RequestMethod.GET, produces = {MediaType.APPLICATION_XML_VALUE,MediaType.APPLICATION_JSON_VALUE})
193
    public void searchPublications(HttpServletRequest request, HttpServletResponse response) throws Exception {
194 194

  
195
	    long time = System.currentTimeMillis();
195
        long time = System.currentTimeMillis();
196 196
        checkParameters(ListUtils.union(PUB_N_DATA_COMMON_PARAMETERS, PUB_PARAMETERS),request.getParameterMap());
197 197

  
198 198
        int page = readParameter(request, "page", 1);
......
231 231
        try {
232 232
            formattedSearchResult = searchService.search(queryBuilder.toString(),sTransformer, (newFormat!=null)?newFormat:format, locale, page, size);
233 233

  
234
		} catch (Exception e) {
235
			logger.error("Fail to execute search.", e);
236
			throw new Exception("Fail to execute search", e);
234
        } catch (Exception e) {
235
            logger.error("Fail to execute search.", e);
236
            throw new Exception("Fail to execute search", e);
237 237

  
238
		}
238
        }
239 239

  
240 240
        PrintWriter writer = response.getWriter();
241 241
        writer.append(formattedSearchResult.getFormattedResult());
......
243 243

  
244 244
        time = System.currentTimeMillis() - time;
245 245
        logger.debug("Answer old time " + time);
246
	}
246
    }
247 247

  
248
	@RequestMapping(value = "/api/datasets", method = RequestMethod.GET, produces = {MediaType.APPLICATION_XML_VALUE,MediaType.APPLICATION_JSON_VALUE})
249
	public void searchData(HttpServletRequest request, HttpServletResponse response) throws Exception {
248
    @RequestMapping(value = "/api/datasets", method = RequestMethod.GET, produces = {MediaType.APPLICATION_XML_VALUE,MediaType.APPLICATION_JSON_VALUE})
249
    public void searchData(HttpServletRequest request, HttpServletResponse response) throws Exception {
250 250

  
251 251
        checkParameters(ListUtils.union(PUB_N_DATA_COMMON_PARAMETERS, DATA_PARAMETERS),request.getParameterMap());
252 252

  
......
286 286
        try {
287 287
            formattedSearchResult = searchService.search(queryBuilder.toString(),sTransformer, (newFormat!=null)?newFormat:format, locale, page, size);
288 288

  
289
		} catch (Exception e) {
289
        } catch (Exception e) {
290 290
            logger.error("Fail to execute search.", e);
291 291
            throw new Exception("Fail to execute search.", e);
292 292
        }
......
295 295
        writer.append(formattedSearchResult.getFormattedResult());
296 296
        writer.close();
297 297

  
298
	}
298
    }
299 299

  
300 300
    @RequestMapping(value = "/api/software", method = RequestMethod.GET, produces = {MediaType.APPLICATION_XML_VALUE,MediaType.APPLICATION_JSON_VALUE})
301 301
    public void searchSoftware(HttpServletRequest request, HttpServletResponse response) throws Exception {
......
403 403

  
404 404
    }
405 405

  
406
	@RequestMapping(value = "/api/projects", method = RequestMethod.GET, produces = {MediaType.APPLICATION_XML_VALUE,MediaType.APPLICATION_JSON_VALUE})
407
	public void searchProjects(HttpServletRequest request, HttpServletResponse response) throws Exception {
406
    @RequestMapping(value = "/api/projects", method = RequestMethod.GET, produces = {MediaType.APPLICATION_XML_VALUE,MediaType.APPLICATION_JSON_VALUE})
407
    public void searchProjects(HttpServletRequest request, HttpServletResponse response) throws Exception {
408 408

  
409
	    checkParameters(PROJECT_PARAMETERS, request.getParameterMap());
409
        checkParameters(PROJECT_PARAMETERS, request.getParameterMap());
410 410

  
411 411
        int page = readParameter(request, "page", 1);
412 412
        int size = readParameter(request, "size", 10);
......
445 445
        PrintWriter writer = response.getWriter();
446 446
        writer.append(formattedSearchResult.getFormattedResult());
447 447
        writer.close();
448
	}
448
    }
449 449

  
450 450

  
451 451
    @ExceptionHandler(IllegalArgumentException.class)
......
497 497
     * @return
498 498

  
499 499
    private String defineTransformer(String model) {
500
        if (model!=null && !model.trim().isEmpty()) {
501
            if (model.equals("openaire")) {
502
                return null;
500
    if (model!=null && !model.trim().isEmpty()) {
501
    if (model.equals("openaire")) {
502
    return null;
503 503

  
504
            } else if (model.equals("sygma")) {
505
                return "results_openaire";
506
            }
507
            else if (model.equals("dc")) {
508
                return "dc";
509
            }
504
    } else if (model.equals("sygma")) {
505
    return "results_openaire";
506
    }
507
    else if (model.equals("dc")) {
508
    return "dc";
509
    }
510 510

  
511
            throw new IllegalArgumentException("model '" + model + "' is not supported.");
511
    throw new IllegalArgumentException("model '" + model + "' is not supported.");
512 512

  
513
        } else {
514
            return null;
515
        }
513
    } else {
514
    return null;
515
    }
516 516
    }*/
517 517

  
518 518
    //TODO: check if needed
519 519
    private String defineTransformer(String model, String format) {
520
    if (model != null && !model.trim().isEmpty() && format != null && (format.equals("json")||format.equals("xml"))) {
520
        if (model != null && !model.trim().isEmpty() && format != null && (format.equals("json")||format.equals("xml"))) {
521 521
            if (model.equals("openaire")) {
522 522
                return null;
523 523

  
......
530 530

  
531 531
            throw new IllegalArgumentException("model '" + model + "' is not supported.");
532 532

  
533
    } else if (format != null && !format.trim().isEmpty()) {
534
        if (format.equals("rss") || format.equals("csv") || format.equals("tsv") || format.equals("html")) {
535
            return "results_openaire";
533
        } else if (format != null && !format.trim().isEmpty()) {
534
            if (format.equals("rss") || format.equals("csv") || format.equals("tsv") || format.equals("html")) {
535
                return "results_openaire";
536
            }
536 537
        }
537
    }
538 538

  
539
    return null;
540
	
539
        return null;
540

  
541 541
    }
542 542

  
543 543
    private String defineFormatter(String model, String format, boolean isPublications, Collection<String> referrers) {
......
562 562

  
563 563
    @Deprecated
564 564
    private void createXmlErrorPage(PrintWriter writer, Exception e) {
565
		writer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
566
		writer.append("<error>");
567
		writer.append("<exception>")
568
				.append(StringEscapeUtils.escapeXml(e.getClass().getName()))
569
				.append("</exception>");
570
		if (e.getMessage() != null) {
571
			writer.append("<message>")
572
					.append(StringEscapeUtils.escapeXml(e.getMessage()))
573
					.append("</message>");
574
		}
575
		if (e.getCause() != null) {
576
			writer.append("<cause>")
577
					.append(StringEscapeUtils.escapeXml(e.getCause()
578
							.toString())).append("</cause>");
579
		}
565
        writer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
566
        writer.append("<error>");
567
        writer.append("<exception>")
568
                .append(StringEscapeUtils.escapeXml(e.getClass().getName()))
569
                .append("</exception>");
570
        if (e.getMessage() != null) {
571
            writer.append("<message>")
572
                    .append(StringEscapeUtils.escapeXml(e.getMessage()))
573
                    .append("</message>");
574
        }
575
        if (e.getCause() != null) {
576
            writer.append("<cause>")
577
                    .append(StringEscapeUtils.escapeXml(e.getCause()
578
                            .toString())).append("</cause>");
579
        }
580 580

  
581
		StackTraceElement[] trace = e.getStackTrace();
582
		writer.append("<trace>");
583
		for (int i = 0; i < trace.length; i++) {
584
			writer.append(StringEscapeUtils.escapeXml(trace[i].toString()))
585
					.append("\n");
586
		}
587
		writer.append("</trace>");
581
        StackTraceElement[] trace = e.getStackTrace();
582
        writer.append("<trace>");
583
        for (int i = 0; i < trace.length; i++) {
584
            writer.append(StringEscapeUtils.escapeXml(trace[i].toString()))
585
                    .append("\n");
586
        }
587
        writer.append("</trace>");
588 588

  
589
		writer.append("</error>");
590
	}
589
        writer.append("</error>");
590
    }
591 591

  
592 592
    // Reading parameters from url and adding default values
593 593
    public int readParameter(HttpServletRequest request, String parameterName, int defaultValue) {
......
608 608
    }
609 609

  
610 610
    /**
611
	 * Checks if the requested result size exceeds the maximum allowed numbers of returned results
612
	 * @param page the page parameter
613
	 * @param size the size parameter
614
	 */
615
	private void checkRequestSize(int page, int size) {
616
		int total = page*size;
617
		if (total > Integer.parseInt(maxResults)) {
618
			throw new IllegalArgumentException("Size and page arguments have exceeded the maximum number of returned results.");
619
		}
620
	}
611
     * Checks if the requested result size exceeds the maximum allowed numbers of returned results
612
     * @param page the page parameter
613
     * @param size the size parameter
614
     */
615
    private void checkRequestSize(int page, int size) {
616
        int total = page*size;
617
        if (total > Integer.parseInt(maxResults)) {
618
            throw new IllegalArgumentException("Size and page arguments have exceeded the maximum number of returned results.");
619
        }
620
    }
621 621

  
622 622
    private String defineContentType(String format) {
623 623
        if (format != null) {
......
626 626
            } else if (format.equalsIgnoreCase("json")) {
627 627
                return JSON_CONTENT_TYPE;
628 628

  
629
			} else if (format.equalsIgnoreCase("csv")) {
629
            } else if (format.equalsIgnoreCase("csv")) {
630 630
                return CSV_CONTENT_TYPE;
631 631

  
632 632
            } else if (format.equalsIgnoreCase("tsv")) {
modules/uoa-search/trunk/pom.xml
9 9
	<groupId>eu.dnetlib</groupId>
10 10
	<artifactId>uoa-search</artifactId>
11 11
	<packaging>jar</packaging>
12
	<version>3.4.3-SNAPSHOT</version>
12
	<version>3.5.0-SNAPSHOT</version>
13 13
	<scm>
14 14
		<developerConnection>scm:svn:https://svn.driver.research-infrastructures.eu/driver/dnet45/modules/uoa-search/trunk</developerConnection> 
15 15
	</scm>
......
54 54
		<dependency>
55 55
			<groupId>org.apache.solr</groupId>
56 56
			<artifactId>solr-solrj</artifactId>
57
			<version>4.9.0</version>
57
			<version>7.2.1</version>
58 58
            <exclusions>
59 59
                <exclusion>
60 60
                    <groupId>org.apache.zookeeper</groupId>

Also available in: Unified diff